1.继承的概念和定义
继承(inheritance)机制是面向对象程序设计使代码可以复用的重要手段,它允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类,称为派生类。继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程。继承是类设计层次的复用。
class Animal
{
public:
void Print()
{
cout << _kind << endl;
cout << size << endl;
}
protected:
string _kind = "";
size_t size = 0;
};
class Dog :public Animal
{
protected:
string skin = "brown";
};
int main()
{
Dog d;
d.Print();
return 0;
}
Dog继承了 Animal类,Dog被称为子类,也作派生类;Animal是父类,也作基类。
1.2 继承的定义
1.2.1 定义的格式
1.2.2继承关系和访问限定符
1.2.3继承基类成员访问方式的变化
总结:
- private成员在派生类中无论以怎样方式继承都不可见。
- 如果基类成员不想在类外被直接访问,但是需要在派生类中访问,就定义为protected。保护限定符是因为继承才出现的。
- 基类的其他成员在子类的访问方式 == Min(成员的基类访问限定符,继承方式) ,public > protected > private
- 使用关键字class时默认继承方式为private,struct为public。
- 实际运用中一般都是public继承,几乎很少使用protected/private继承。
2.基类和派生类对象赋值转换
-
派生类对象可以赋值给 基类的对象/基类的指针/基类的引用。可以被称为切片或者切割。
-
基类对象不能赋值给派生类对象。
-
基类的指针或者引用可以通过强制类型转换赋值给派生类的指针或者引用。但是必须是基类的指针是指向派生类的对象时才是安全的。
class Person
{
protected:
string _name;
string _sex;
int _age;
};
class Student:public Person
{
public:
int _id;
};
void test1()
{
Student sobj;//子类对象
//1.子类对象可以赋值给父类对象/指针/引用
Person pobj = sobj;
Person* pp = &sobj;
Person& rp = sobj;
//2.基类对象不能赋值给派生类对象
// sobj = pobj; 报错
//3.基类的指针可以通过强制类型转换赋值给派生类的指针
Student* ps1 = (Student*)pp;
pp = &pobj;
Student* ps2 = (Student*)pp;//虽然也可以,但是会存在越界访问
}
3.继承中的作用域
- 在继承体系中基类和派生类都有独立的作用域
- 子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问, 这种情况被称为隐藏,或者重定义。(可以通过 基类::基类成员访问)
- 如果是成员函数的隐藏,只需要函数名相同就构成隐藏。
- 注意实际中**,在继承体系里面最好不要定义同名的成员。**
class Person
{
protected:
string _name = "张三";
int _age = 18;
};
class Student:public Person
{
public:
void Print()
{
cout << _name << endl;
cout << _age << endl;
cout << Person::_age << endl;
}
protected:
int _age = 20;
};
class A
{
public:
void fun()
{
cout << "fun()" << endl;
}
};
class B :public A
{
public:
void fun(int i)
{
A::fun();
cout << "fun(int i)" << endl;
}
};
void mtest()
{
B b;
//b.fun(); //会报错 需要参数
b.fun(1);
b.A::fun();//直接访问基类的fun()
}
A和B类中的fun函数并不是构成重载,因为不在同一作用域,而是构成隐藏,成员函数满足函数名相同就构成隐藏。
4.派生类的默认成员函数
- 派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。
- 派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化
- 派生类的operator= 必须调用基类的operator=完成基类的复制
- 派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员。因为这样才能保证派生类对象先清理派生类成员再清理基类成员的顺序。
- 派生类对象初始化先调用基类构造再调用派生类构造。
- 派生类对象析构清理先调用派生类析构再调用基类的析构
- 编译器会对析构函数名进行特殊处理,处理成destructor() ,所以父类析构函数不加 virtual的情况下,子类析构函数和父类析构函数会构成隐藏关系。
class Person
{
public:
Person(const char* name)
:_name(name)
{
cout << "Person的构造函数" << endl;
++_count;
}
Person(const Person& p)
:_name(p._name)
{
cout << "Person的拷贝构造" << endl;
}
~Person()
{
cout << "Person的析构函数" << endl;
}
protected:
string _name = "张三";
//int _num = 1737;
public:
static int _count;
};
int Person::_count = 0;
class Student :public Person
{
public :
Student(const char* name = " ",int num = 0)
:Person(name)
,_num(num)
{
cout << "Student 的构造函数" << endl;
}
Student(const Student& s)
:Person(s)
,_num(s._num)
{
cout << "Student的拷贝构造" << endl;
}
~Student()
{
cout << "Student的析构函数" << endl;
}
protected:
string _name;
int _num = 5201314;
};
5.继承和友元
友元不能继承,也就是说基类友元不能访问子类私有和保护成员
无法被继承的类:
class A
{
private:
A(){}
};
class B : public A
{
//无法继承,因为无法调用A的构造函数.
};
6.继承与静态成员
基类定义了static静态成员,则整个继承体系里面只有一个这样的成员。无论派生出多少个子类,都只有一个static成员实例。
复杂的菱形继承以及菱形虚拟继承
class Person
{
public :
string _name ; // 姓名
};
class Student : public Person
{
protected :
int _num ; //学号
};
class Teacher : public Person
{
protected :
int _id ; // 职工编号
};
class Assistant : public Student, public Teacher
{
protected :
string _majorCourse ; // 主修课程
};
void Test ()
{
// 这样会有二义性无法明确知道访问的是哪一个
Assistant a ;
a._name = "peter";
// 需要显示指定访问哪个父类的成员可以解决二义性问题,但是数据冗余问题无法解决
a.Student::_name = "xxx";
a.Teacher::_name = "yyy";
}
虚拟继承可以解决菱形继承的二义性和数据冗余的问题。如上面的继承关系,在Student和Teacher的继承Person时使用虚拟继承,即可解决问题。需要注意的是,虚拟继承不要在其他地方去使用。
class Person
{
public :
string _name ; // 姓名
};
class Student : virtual public Person
{
protected :
int _num ; //学号
};
class Teacher : virtual public Person
{
protected :
int _id ; // 职工编号
};
class Assistant : public Student, public Teacher
{
protected :
string _majorCourse ; // 主修课程
};
void Test ()
{
Assistant a ;
a._name = "peter";
}
虚拟继承解决数据冗余和二义性的原理
class A
{
public:
int _a;
};
// class B : public A
class B : virtual public A
{
public:
int _b;
};
// class C : public A
class C : virtual public A
{
public:
int _c;
};
class D : public B, public C
{
public:
int _d;
};
int main()
{
D d;
d.B::_a = 1;
d.C::_a = 2;
d._b = 3;
d._c = 4;
d._d = 5;
return 0;
}
8. 继承和组合
- public继承是一种 is-a的关系,也就是说每个派生类对象都是一个基类对象
- 组合是一种 has-a的关系,假设B组合了A,则每个B对象中都有一个A对象