一、概念:
继承就是在保持原有类特性的基础上进行扩展,增加功能,是类设计层次的复用。
二、继承方式的特点![](https://i-blog.csdnimg.cn/blog_migrate/a570ae892227583a2b28aad7f099a758.png)
总结:
1.基类的protected成员,不能在类外访问,但能在派生类中访问;
2.使用class时默认是私有继承,struct默认是公有继承;
3.绝大多数使用的都是public继承,几乎不使用protected/private继承,
protected/private继承扩展维护性不强。
三、基类和派生类对象的赋值转换
切片
![](https://i-blog.csdnimg.cn/blog_migrate/e1d2cde0a6f4f3f04fc4313dc9010f75.png)
总结:
1.派生类对象/指针/引用 可以赋给 基类对象/指针/引用/ ,把派生类中基类的一部分切下来赋 值,这就叫切片;
2.基类对象不能赋给派生类对象;
3.基类指针可通过强转赋给派生类指针,但存在越界访问的问题。
class Person
{
protected:
string _name = "小李子"; // 姓名
int _num = 111; // 身份证号
};
class Student : public Person
{
//Student中的_num和Person中的_num构成隐藏关系
public:
void Print()
{
cout << " 姓名:" << _name << endl;
cout << " 身份证号:" << Person::_num << endl;
cout << " 学号:" << _num << endl; //就近原则
}
public:
int _num = 999; // 学号
};
void Test3()
{
Person p;
Student s;
Student* ps = (Student*)&p; //这种情况虽可以,但存在越界访问的问题
}
四、继承的作用域
1.基类和派生类有同名成员,派生类将屏蔽基类对同名成员的直接访问,这叫隐藏(重定义);
2.当基类和派生类成员函数函数名相同时,就构成函数的隐藏;
3.当基类和派生类成员变量名相同时,也构成隐藏。(和重载不一样)
// B中的fun和A中的fun不是构成重载,因为不是在同一作用域
// B中的fun和A中的fun构成隐藏,成员函数满足函数名相同就构成隐藏。
class A
{
public:
void fun()
{
cout << "func()" << endl;
}
};
class B : public A
{
public:
void fun(int i)
{
if (i == 0)
return;
fun(--i);
cout << "func(int i)->" << i << endl;
}
};
五、派生类的默认成员函数
1.创建对象时,派生类调用 构造函数/拷贝构造函数/operator= ,都要调用基类的构造函数/拷贝构造函数/operator= 来完成对基类部分的初始化和赋值;
2.销毁对象时,先调用派生类的析构函数,然后再调用基类的析构函数;
3.C++11给出了关键字 final 来禁止该类被继承 (最终类)
class NonInherit final
{};
4.友元关系不能继承,也就是说,基类友元不能访问派生类的私有和保护成员。
class Person
{
public :
Person(const char* name = "peter")
: _name(name )
{
cout<<"Person()" <<endl;
}
Person(const Person& p)
: _name(p._name)
{
cout<<"Person(const Person& p)" <<endl;
}
Person& operator=(const Person& p )
{
cout<<"Person operator=(const Person& p)"<< endl;
if (this != &p)
_name = p ._name;
return *this ;
}
~Person()
{
cout<<"~Person()" <<endl;
}
protected :
string _name ; // 姓名
};
class Student : public Person
{
public :
Student(const char* name, int num)
: Person(name )
, _num(num )
{
cout<<"Student()" <<endl;
}
Student(const Student& s)
: Person(s)
, _num(s ._num)
{
cout<<"Student(const Student& s)" <<endl ;
}
Student& operator = (const Student& s )
{
cout<<"Student& operator= (const Student& s)"<< endl;
if (this != &s)
{
Person::operator =(s);
_num = s ._num;
}
return *this ;
}
~Student()
{
cout<<"~Student()" <<endl;
}
protected :
int _num ; //学号
};
void Test ()
{
Student s1 ("jack", 18);
Student s2 (s1);
Student s3 ("rose", 17);
s1 = s3 ;
}
六、继承与静态成员
基类定义了 static 静态成员,则整个继承体系里面只有一个这样的成员,无论派生出多少个子类,都只有一个static成员实例。
class Person
{
public :
Person () {++ _count ;}
protected :
string _name ; // 姓名
public :
static int _count; // 统计人的个数。
};
int Person :: _count = 0;
class Student : public Person
{
protected :
int _stuNum ; // 学号
};
class Graduate : public Student
{
protected :
string _seminarCourse ; // 研究科目
};
void TestPerson()
{
Student s1 ;
Student s2 ;
Student s3 ;
Graduate s4 ;
cout <<" 人数 :"<< Person ::_count << endl;
Student ::_count = 0;
cout <<" 人数 :"<< Person ::_count << endl;
}
七、菱形继承
代码:
class A
{
public:
int _a;
};
class B: public A
{
public:
int _b;
};
class C : public A
{
public:
int _c;
};
class D :public B, public C
{
public:
int _d;
};
void Test1() //无virtual
{
D d;
d.B::_a = 1; //先继承B,再继承C,最后再存子类的变量
d.C::_a = 2;
d._b = 3;
d._c = 4;
d._d = 5; //它的地址最高
}
结果:
总结:
1.这就存在二义性,对象d中存在两个成员变量_a,有数据冗余;
2. 根据继承顺序,依次存成员变量。
解决办法:虚拟继承!
class A
{
public:
int _a;
};
class B: virtual public A
{
public:
int _b;
};
class C : virtual public A
{
public:
int _c;
};
class D :public C, public B
{
public:
int _d;
};
void Test2()
{
D d;
d._a = 1;
d._a = 2; //_a的地址最高
d._b = 3;
d._c = 4;
d._d = 5;
}
总结:对象d中存有B和C的两个指针,指向一张表,这个指针叫虚基表指针,这两个表叫虚基表。虚基表中存的是偏移量。通过偏移量可以找到公共的_a变量。
其原理框图:
八、继承的总结与反思
一般不要设计多继承,不能设计出菱形继承。
2.继承与组合的关系
(1)public继承是一种is-a的关系,也就是说每个派生类对象都是一个基类对象;
(2)组合是一种has-a的关系。假设B组合了A,每个B对象中都有一个A对象;
(3)组合耦合度低,代码可维护性高;继承耦合度高,不太好;
(4)但继承可以实现多态。
(5)当可以用继承和组合时,优先使用组合。