1. 继承的本质
继承的本质就是一种复用,把公共部分抽取出来
2.继承的例子
class Person
{
public:
void print()
{
cout << _name << endl;
}
int _age = 18;
string _name = "xxx";
};
class Student : public Person
{
public:
int _stuid;//学号
};
class Teacher : public Person
{
public:
int _jobid;//工号
};
int main()
{
Student s;
Teacher t;
s.print();
t.print();
return 0;
}
Student 和 Teacher都是 Person,都有自己的_name,_age(公共部分),也有各自独有的(_stuid学号,_jobid工号)
3.继承定义
父子类是独立的:
4.父子类复制兼容规则(切割 / 切片)
5.隐藏 (也叫重定义)
class Person
{
public:
void print()
{
cout << _age << endl;
}
int _age = 20;
int _num = 5;
};
class Student : public Person
{
public:
void print()
{
cout << _stuid << endl;
}
int _stuid = 100;
int _num = 3;
};
int main()
{
Student s;
cout << s._num << endl;//父类被隐藏
//如何访问父类继承来的_num
cout << s.Person::_num << endl;
//分别打印 3 5
//成员函数同理
s.print();
s.Person::print();//访问父类
//打印 100 20
return 0;
}
(1)父类和子类可以有同名成员,默认直接访问子类的,子类同名成员隐藏父类同名成员
(2)另外这个同名成员,要求变量名相同或者函数名相同(不考虑返回类型,参数)即可
(3)函数重载是同一作用域,隐藏是父子两个独立的作用域产生
6.派生类的默认成员函数
相关代码:
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; //学号
};
解析:
7.如何实现一个不能被继承的类
将基类构造私有,此时基类不能被继承
c++11新增final,修饰父类,直接明显的不能调用父类
class A final //
{
private://构造私有
A()
{
}
protected:
int _a;
};
8.继承与友元,继承与静态变量
友元关系不能继承,也就是说基类友元不能访问子类私有和保护成员
基类定义了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 Test()
{
Student s1;
Student s2;
Student s3;
Graduate s4;
cout << " 人数 :" << Person::_count << endl;
Student::_count = 0;
cout << " 人数 :" << Person::_count << endl;
}//打印 4 0
int main()
{
Test();
return 0;
}
9.菱形继承,菱形虚拟继承
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";
}
int main()
{
Test();
return 0;
}
根据之前学的知识:assistant有一份student成员变量拷贝,一份teacher成员变量拷贝,student拷贝了person的成员变量,teacher也是如此,因此assistant有两份person里的_name,这就造成了a._name访问不明确,会出问题;并且我们只需要一份_name,会出现二义性问题,数据冗余问题
解决问题:虚拟继承
class Student : virtual public Person
class Teacher : virtual public 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 = "zzz";
cout << a._name << endl; //zzz
cout << a.Student::_name << endl; //zzz
cout << a.Teacher::_name << endl; //zzz
cout << endl;
a.Student::_name = "xxx";
cout << a._name << endl; //xxx
cout << a.Student::_name << endl; //xxx
cout << a.Teacher::_name << endl; //xxx
cout << endl;
a.Teacher::_name = "yyy";
cout << a._name << endl; //yyy
cout << a.Student::_name << endl; //yyy
cout << a.Teacher::_name << endl; //yyy
cout << endl;
}
int main()
{
Test();
return 0;
}
虚拟继承:assistant里只有一份Person的_name
虚拟继承的具体原理:
10.继承与组合
public继承是一种is-a的关系。也就是说每个派生类对象都是一个基类对象。
组合是一种has-a的关系。假设B组合了A,每个B对象中都有一个A对象。
class A
{
public:
int _a;
};
class B
{
public:
A aa; //组合
int _b;
};
继承允许你根据基类的实现来定义派生类的实现。这种通过生成派生类的复用通常被称
为白箱复用(white-box reuse)。术语“白箱”是相对可视性而言:在继承方式中,基类的
内部细节对子类可见。派生类和基类间的依赖关系很强,耦合度高。
对象组合是类继承之外的另一种复用选择。新的更复杂的功能可以通过组装或组合对象
来获得。对象组合要求被组合的对象具有良好定义的接口。这种复用风格被称为黑箱复
用(black-box reuse),因为对象的内部细节是不可见的。组合类之间没有很强的依赖关系,耦合度低。
一般我们追求低耦合。但也要看实际,你不能 学生 has_a 人,不能 轮胎 is_a 车。