什么是继承
继承(inheritance)机制是面向对象程序设计使代码可以复用的最重要的手段,它允许程序员在保持原有类特
性的基础上进行扩展,增加功能,这样产生新的类,称派生类。继承呈现了面向对象程序设计的层次结构,
体现了由简单到复杂的认知过程。以前我们接触的复用都是函数复用,继承是类设计层次的复用。
class Person
{
public:
void Print()
{
cout << "name:" << _name << endl;
cout << "age:" << _age << endl;
}
protected:
string _name = "peter";
int _age = 18;
};
//这里子类student和teacher继承了父类person,拥有了父类的成员变量和成员函数
class Student : public Person
{
protected:
int _stuid;
};
class Teacher : public Person
{
protected:
int _jobid;
};
int main()
{
Student s;
Teacher t;
s.Print();
t.Print();
return 0;
}
继承关系和访问限定符
1,不可见是指在类外面和类里面都不能去访问。
2,权限问题 public > protected > private ,protected可以说专为继承而来
3,发现规律,除了基类的私有成员以外, 最终权限取决于:继承权限和成员权限中最小的一个 (权限只能缩小,不能扩大)。
4, 使用关键字class时默认的继承方式是private,使用struct时默认的继承方式是public,不建议这么使用
5, 而基类的私有成员是不可见(被派生类继承, 但是不允许访问)。
基类和派生类对象赋值转换
继承中的作用域
1,在继承体系中基类和派生类都有独立的作用域。(而重载的条件是要求在同一个作用域!!!!)
2,子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,这种情况叫隐藏,也叫重定
义。(在子类成员函数中,可以使用 基类::基类成员 显示访问)
3, 需要注意的是如果是成员函数的隐藏,只需要函数名相同就构成隐藏。
4. 注意在实际中在继承体系里面最好不要定义同名的成员
样例1
这里的_num就构成了隐藏
class Person
{
protected :
string _name = "张三"; // 姓名
int _num = 111;
};
class Student : public Person
{
public:
void Print()
{
cout<<" 姓名:"<<_name<< endl;
cout<<" 身份证号:"<<Person::_num<< endl;
cout<<" 学号:"<<_num<<endl;
}
protected:
int _num = 999;
};
void Test()
{
Student s1;
s1.Print();
}
样例2
这里的fun()函数不构成重载,因为子类和父类是两个不同的作用域
他们构成了隐藏
class A
{
public:
void fun()
{
cout << "class A func()" << endl;
}
};
class B : public A
{
public:
void fun()
{
A::fun();
cout << "class B func()" << endl;
}
};
void Test()
{
B b;
b.fun();
};
派生类的默认成员函数
1, 派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认的构造函
数,则必须在派生类构造函数的初始化列表阶段显示调用。
2,派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化。
3,派生类的operator=必须要调用基类的operator=完成基类的复制。
4,派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员。因为这样才能保证派生类
对象先清理派生类成员再清理基类成员的顺序。
5. 派生类对象初始化先调用基类构造再调派生类构造。
6. 派生类对象析构清理先调用派生类析构再自动(不用手动)调基类的析构
class Person
{
public:
Person(const char* name = "zhangsan")
: _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 = "xiaoming", int id = 0)
:Person(name) //先构造父类,再构造子类
,_id(id)
{
cout << "Student(const char* name,int id)" << endl;
}
//s1(s2)
Student(const Student& st)
:Person(st) //先拷贝构造父类,再拷贝构造子类
{
cout << "Student(const Student& st)" << endl;
_id = st._id;
}
//s1 = s2
Student& operator=(const Student& st)
{
cout << "Student& operator=(const Student& st)" << endl;
if (this != &st)
{
Person::operator=(st); //这里需要显示调用父类的赋值重载
_id = st._id;
}
return *this;
}
~Student()
{
cout << "~Student" << endl;
} //子类自动调用完以后,会自动去调用父类的析构函数
protected:
int _id;
};
顺序是从 上----->下
友元关系不能继承,也就是说基类友元不能访问子类私有和保护成员
基类定义了static静态成员,则整个继承体系里面只有一个这样的成员
复杂的菱形继承和菱形虚拟继承
单继承,多继承,菱形继承
class Person
{
public:
string _name;
};
//class student: virtual public Person 这就是虚拟继承
class Student : public Person
{
protected:
int _num; //学号
};
//class Teacher : virtual public Person
class Teacher : public Person
{
protected:
int _id; // 职工编号
};
class Assistant : public Student, public Teacher
{
protected:
string _majorCourse; // 主修课程
};
菱形继承有数据冗余和二义性的问题。在Assistant的对象中Person成员会有两份_name。
虚拟继承可以解决菱形继承的二义性和数据冗余的问题,但是虚拟继承是专门为菱形继承服务的,不能在别的地方去使用。
监视窗口被编译器处理过,可能不太准确,从内存窗口中去看会准确一些。
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;
}
不是虚拟继承,我们明显可以看到数据的冗余和二义性
虚拟继承后
其中B和C中的指针我们称之为虚基表指针, 他们指向的表称为虚基表, 表里面存放的是偏移量。
继承和组合的关系
继承是 is_a的关系,例如奔驰是辆车,奔驰和车就是继承关系。
组合是has_a的关系,例如奔驰有4个车轮,这里就是组合关系。
class Tire
{
protected:
string _brand = "benchi";
size_t _size = 17;
};
class Car {
protected:
string _colour = "白色";
string _num = "0001110";
Tire _t;
};
在继承方式中,基类的内部细节对子类可见 。继承一定程度破坏了基类的封装,基类的改变,对派生类有很
大的影响。派生类和基类间的依赖关系很强,耦合度高。
所以优先使用组合、