文章目录
一、继承的概念与定义
1.1 继承的概念
继承(inheritance)机制是面向对象程序设计使代码可以复用的最重要的手段,它允许程序员子在保持原有类特性的基础上进行扩展,增加功能,产生新的类,称为派生类(子类)。继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程。继承是类设计层次的复用
class Person //父类
{
public:
void printf()
{
cout << "name:" << _name << endl;
cout << "age:" << _age << endl;
}
protected:
string _name = "pater"; //姓名
int _age = 18; //年龄
};
class Student : public Person //子类继承父类
{
protected:
int _stuid = 0; //学号
};
class Teacher : public Person //子类继承父类
{
protected:
int _jobid = 0; //工号
};
int main()
{
Student s;
Teacher t;
s.printf();
t.printf();
return 0;
}
1.2 继承定义
1.2.1 定义格式
上面的代码可以看到Person类是父类(基类),Student类是子类(派生类)
1.2.2 继承关系和访问限定符
1.2.3 继承基类成员访问方式的变化
基类private成员在派生类中无论以什么方式继承都是不可见的。这里的不可见是指基类的私有成员还是被继承到了派生类对象中,但是语法上限制派生类对象不管在类里面还是类外面都不能去访问它
如果基类成员不想在类外直接被访问,但需要在派生类中能访问,就定义为protected。可以看出保护成员限定符是因继承才出现的
在实际运用中一般使用都是public继承,几乎很少使用protetced/private继承,也不提倡使用protetced/private继承,因为protetced/private继承下来的成员都只能在派生类的类里面使用,实际中扩展维护性不强
class Person
{
public :
void Print ()
{
cout<<_name <<endl;
}
protected :
string _name ; // 姓名
private :
int _age ; // 年龄
};
//class Student : protected Person
//class Student : private Person
class Student : public Person
{
protected :
int _stunum ; // 学号
};
二、基类和派生类对象赋值转换
- 派生类对象 可以赋值给 基类的对象 / 基类的指针 / 基类的引用。这里有个形象的说法叫切片或者切割。寓意把派生类中父类那部分切来赋值过去。
- 基类对象不能赋值给派生类对象
- 基类的指针可以通过强制类型转换赋值给派生类的指针
//子类对象向父类对象赋值会发生切片
class Person
{
protected:
string _name; // 姓名
string _sex; // 性别
int _age; // 年龄
};
class Student : public Person
{
public:
int _No; // 学号
};
int main()
{
Student s;
Person p = s; //子类对象可以赋值给父类对象/引用/指针
Person* pp = &s;
Person& rp = s;
// s = p; // 基类对象不能给子类对象赋值
pp = &s;
Student* ps1 = (Student*)pp; //强转类型之后可以赋值
}
三、继承中的作用域
- 在继承体系中基类和派生类都有独立的作用域
- 子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,这种情况叫隐藏,也叫重定义(在子类成员函数中,可以使用 基类::基类成员 显示访问)
- 需要注意的是如果是成员函数的隐藏,只需要函数名相同就构成隐藏
- 注意在实际中在继承体系里面最好不要定义同名的成员。
//子类中的成员与父类的成员名称相同,会构成父类成员隐藏也叫重定义
class Person
{
protected:
string _name = "小李子"; // 姓名
int _num = 111; // 身份证号
};
class Student : public Person
{
public:
void Print()
{
cout << " 姓名:" << _name << endl;
cout << " 身份证号:" << Person::_num << endl;
//这里要指明是父类作用域中的_num,不然会隐藏
cout << " 学号:" << _num << endl;
}
protected:
int _num = 999; // 学号
};
int main()
{
Student s1;
s1.Print();
return 0;
}
//子类中的成员函数名和父类函数名相同也会造成父类的成员函数隐藏
class A
{
public:
void fun()
{
cout << "func()" << endl;
}
};
class B : public A
{
public:
void fun(int i) //这里会对父类的fun造成隐藏
{
A::fun(); //用父类显示调用
cout << "func(int i)->" << i << endl;
}
};
int main()
{
B b;
b.fun(1);
return 0;
}
四、子类的默认成员函数
- 子类的构造函数: 调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认的构造函数,则必须在派生类构造函数的初始化列表阶段显示调用
- 派生类的拷贝构造函数: 调用基类的拷贝构造完成基类的拷贝初始化
- 派生类的operator=: 调用基类的operator=完成基类的复制
- 派生类的析构函数: 在被调用完成后自动调用基类的析构函数清理基类成员(虽然派生类和基类的析构函数构成隐藏,因为编译后析构函数名都为Destory,这是为了保证清理顺序)
- 派生类对象初始化先调用基类构造再调派生类构造
- 派生类对象析构清理先调用派生类析构再调基类的析构
//子类的默认成员函数
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)
{
}
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; //学号
};
int main()
{
Student s1("jack", 17);
Student s2(s1);
Student s3("rose", 18);
s1 = s3;
return 0;
}
五、继承与友元
友元关系不能继承,也就是说基类友元不能访问子类私有和保护成员
//基类的友元不能被子类继承
class Student;
class Person
{
public:
friend void Display(const Person& p, const Student& s);
protected:
string _name; // 姓名
};
class Student : public Person
{
protected:
int _stuNum = 0; // 学号
};
void Display(const Person& p, const Student& s)
{
cout << p._name << endl; //可以访问
//cout << s._stuNum << endl; //不能访问
}
int main()
{
Person p;
Student s;
Display(p, s);
return 0;
}
六、继承与静态成员
基类定义了static 静态成员,则整个继承体系里面只有一个这样的成员。无论派生出多少个子类,都只有一个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; // 研究科目
};
int main()
{
Student s1; //会调用父类的构造函数
Student s2;
Student s3;
Graduate g1;
cout << " 人数 :" << Person::_count << endl; // 4 因为调用了4次父类的构造函数
Student::_count = 0;
cout << " 人数 :" << Person::_count << endl; // 0
//因为静态成员是共享一份的,局部修改就全局改变
}
七、复杂的菱形继承及菱形虚拟继承、
7.1 菱形继承和虚继承
单继承:一个子类只有一个直接父类时称这个继承关系为单继承
多继承:一个子类有两个或以上直接父类时称这个继承关系为多继承
菱形继承:菱形继承是多继承的一种特殊情况
菱形继承的问题:菱形继承有数据冗余和二义性的问题。在Assistant的对象中Person成员会有两份。
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; // 主修课程
};
int main()
{
Assistant a;
//a._name = "peter"; //error 会发生二义性, Assistant::_name不明确
// 需要显示指定访问哪个父类的成员可以解决二义性问题,但是数据冗余问题无法解决
a.Student::_name = "xiaowang";
a.Teacher::_name = "xiaoli";
return 0;
}
虚拟继承可以解决菱形继承的二义性和数据冗余的问题。如上面的继承关系,在Student和Teacher的继承Person时使用虚拟继承,即可解决问题。需要注意的是,虚拟继承不要在其他地方去使用
//用虚继承来解决数据冗余的问题
class Person
{
public:
string _name; // 姓名
};
class Student : virtual public Person //在继承处用virtual关键字,完成虚继承
{
protected:
int _num; //学号
};
class Teacher : virtual public Person //在继承处用virtual关键字,完成虚继承
{
protected:
int _id; // 职工编号
};
class Assistant : public Student, public Teacher
{
protected:
string _majorCourse; // 主修课程
};
int main()
{
Assistant a;
a._name = "lisi"; //解决了数据冗余,自然就解决了二义性问题
}
7.2 虚继承的原理
class A
{
public:
int _a;
};
class B : virtual public A
//class B : public A
{
public:
int _b;
};
class C : virtual public A
//class C: public A
{
public:
int _c;
};
class D : public B, public C
{
public:
int _d;
};
int main()
{
D d;
d.B::_a = 1; //不是虚拟继承时,会单独改变作用域中的_a
d.C::_a = 2; // 虚拟继承之后,会修改所有的_a,可以证明所有子类的_a是同一个
d._b = 3;
d._c = 4;
d._d = 5;
return 0;
}
未进行虚继承前
进行虚继承后
这里是通过了B和C的两个指针,指向的一张表。这两个指针叫虚基表指针,这两个表叫虚基表。虚基表中存的偏移量。通过偏移量可以找到下面的A。
八、继承和组合
//继承和组合
class A
{
public:
void func()
{}
protected:
int _a;
};
//B继承A,可以复用A
class B : public A
{
protected:
int _b;
};
//C组合A,也可以复用A
class C
{
private:
int _c;
A _a;
};
-
优先使用对象组合,而不是类继承 。
-
在继承方式中,基类的内部细节对子类可见。继承一定程度破坏了基类的封装,基类的改变,对派生类有很大的影响。派生类和基类间的依赖关系很强,耦合度高
-
对象组合是类继承之外的另一种复用选择。新的更复杂的功能可以通过组装或组合对象来获得。对象组合要求被组合的对象具有良好定义的接口。组合类之间没有很强的依赖关系,耦合度低。优先使用对象组合有助于你保持每个类被封装
-
实际尽量多去用组合。组合的耦合度低,代码维护性好。不过继承也有用武之地的,有些关系就适合继承那就用继承,另外要实现多态,也必须要继承。类之间的关系可以用继承,可以用组合,就用组合。