C++进阶_继承
1.继承的定义
继承是面向对象程序设计使代码可以复用的最重要的手段,它允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类称为派生类
#include<iostream>
using namespace std;
class Person
{
protected:
string _name;
int _age;
public:
Person(const string& name = "", const int& age = 0) :_name(name), _age(age)
{}
void Print()
{
cout << "name:" << _name << endl;
cout << "age:" << _age << endl;
}
};
class Student:public Person
{
protected:
int _sid;
public:
Student(const string& name = "", const int& age = 0,const int& sid = 0) :Person(name,age),_sid(sid)
{}
void Print()
{
Person::Print();
cout << "sid:" << _sid << endl;
}
};
class Teacher:public Person
{
protected:
int _tid;
public:
Teacher(const string& name = "", const int& age = 0, const int& tid = 0) :Person(name,age),_tid(tid)
{}
void Print()
{
Person::Print();
cout << "tid:" << _tid << endl;
}
};
int main()
{
Student s("jhh", 20, 1);
Teacher t("ghh", 22, 2);
s.Print();
t.Print();
return 0;
}
继承基类成员访问方式的变化
(1)基类private成员在派生类中无论以什么方式继承都是不可见的。不可见是指基类的私有成员还是被继承到了派生类对象中,但是语法上限制派生类对象不管是在类里面还是类外面都不能去访问它。
(2)基类private成员在派生类中是不能被访问的,如果基类成员不想在类外被直接访问,但需要在派生类中能被访问,就定义为protected
(3)使用关键字class时默认的继承方式是private,使用struct时默认的是public
(4)protected/private继承下来的成员都只能在派生类的类里面使用,实际中扩展维护性不强
2.基类和派生类对象赋值转换
(1)派生类对象可以赋值给基类的对象/基类的指针/基类的引用
(2)基类对象不能赋值给派生类对象
(3)基类的指针可以通过强制类型转换赋值给派生类的指针,但是必须是基类的指针是指向派生类对象时才是安全的
(4)基类如果是多态类型,可以使用RTTI(Run-Time Type Information)的dynamic_cast 来进行识别后进行安全转换。
#include<iostream>
using namespace std;
class Person
{
protected:
string _name;
string _sex;
int _age;
};
class Student :public Person
{
public:
int _no;
};
int main()
{
Student sobj;//派生类对象
Person pobj = sobj;//派生类对象可以给基类的对象赋值
Person* ppobj = &sobj;//派生类对象可以给基类的指针赋值
Person& rpobj = sobj;//派生类对象可以给基类的引用赋值
//基类的指针可以通过强制类型转换为派生类指针赋值
ppobj = &sobj;
Student* psobj1 = (Student*)ppobj;
psobj1->_no = 123;
return 0;
}
3.继承的作用域
(1)在继承体系中基类和派生类都有独立的作用域
(2)当子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,被称为隐藏也交重定义(在子类成员函数中,可以使用“基类::基类成员”显示访问)
(3)如果是成员函数的隐藏,只需要函数名相同就隐藏
(4)在实际的继承体系中最好不要定义同名的成员
4.派生类的默认成员函数
编译器会自己生成6个默认成员函数:构造函数、析构函数、拷贝构造、赋值重载、普通对象取地址、const对象取地址
(1)派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员,如果基类没有默认的构造函数,则必须在派生类构造函数的初始化列表阶段显示调用
(2)派生类的拷贝构造函数必须调用基类的拷贝构造函数完成基类的拷贝初始化
(3)派生类的operator=必须调用基类的operator=完成基类的复制
(4)派生类的析构函数在被调用完成后自动调用基类的析构函数清理基类成员。这样才能保证派生类对象先清理派生类成员,再清理基类成员的顺序
(5)派生类对象初始化先调基类的构造再调派生类的构造。派生类对象析构清理先调用派生类析构再调用基类的析构
#include<iostream>
using namespace std;
class Person
{
protected:
string _name;
public:
//构造函数
Person(const string& name = "bhh") :_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;
}
};
class Student :public Person
{
protected:
int _num;
public:
//构造函数
Student(const string& name, const 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;
}
};
int main()
{
Student s1("dsfs", 22);
Student s2(s1);
Student s3 = s2;
return 0;
}
5.继承和友元
友元关系不能继承(基类友元不能访问派生类私有和保护成员)
#include<iostream>
using namespace std;
class Student;
class Person
{
protected:
string _name;
public:
friend void display(const Person& p, const Student& s);
};
class Student :public Person
{
protected:
string _sno;
};
void display(const Person& p, const Student& s)
{
cout << p._name << endl;
//cout << s._sno<< endl;(友元关系不能继承)
}
int main()
{
Person p;
Student s;
display(p, s);
return 0;
}
6.继承和静态成员
基类定义了static静态成员,则整个继承体系里面只有一个这样的成员。无论派生出多少个子类,都只有一个static成员实例
#include<iostream>
using namespace std;
class Person
{
protected:
string _name;
public:
Person()
{
++_count;
}
public:
static int _count;
};
int Person::_count = 0;
class Student :public Person
{
protected:
string _sno;
};
class Teacher :public Person
{
protected:
string _tno;
};
int main()
{
Person p;
Student s1;
Student s2;
Teacher t1;
Teacher t2;
cout << "Person Number:" << Person::_count << endl;//5
Teacher::_count = 0;//置0
cout << "Person Number:" << Person::_count << endl;//0
return 0;
}
7.复杂的菱形继承和菱形虚拟继承
(1)单继承:一个子类只有一个直接父类
class Person{};
class Student:public Person{};
class Graduate:public Student{};
(2)多继承:一个子类有两个及以上直接父类
class Student{};
class Teacher{};
class Assistant:public Student,public Teacher{};
(3)菱形继承:多继承的特殊情况
class Person{};
class Student:public Person{};
class Teacher:public Person{};
class Assistant:public Student,public Teacher{};
菱形继承的问题:数据冗余和二义性问题(在Assistant的对象中Person成员有两份)
class Person
{
public:
string _name;
};
class Student :public Person
{
protected:
string _num;
};
class Teacher :public Person
{
protected:
string _id;
};
class Assistant :public Student, public Teacher
{
protected:
string _major;
};
int main()
{
Assistant ass;
//ass._name = "f";//二义性
ass.Student::_name = "dc";
ass.Teacher::_name = "gy";
return 0;
}
虚拟继承
虚拟继承可以解决菱形继承的二义性和数据冗余的问题。在Student和Teacher继承Person时使用虚拟继承
class Person
{
public:
string _name;
};
class Student : virtual public Person
{
protected:
string _num;
};
class Teacher : virtual public Person
{
protected:
string _id;
};
class Assistant :public Student, public Teacher
{
protected:
string _major;
};
int main()
{
Assistant ass;
ass._name = "f";
return 0;
}
虚拟继承原理
8.小结
继承与组合
(1)public继承是is-a的关系,即每个派生类对象都是一个基类对象
(2)组合是has-a的关系。即B组合了A,则每个B对象中都有一个A对象
(3)优先使用对象组合,而不是类继承
(4)继承允许根据基类的实现来定义派生类的实现。这种通过生成派生类的复用成为白箱复用。在继承方式中,基类的内部细节对派生类可见。继承一定程度破坏了基类的封装,基类的改变对派生类有很大的影响。派生类与基类之间的依赖关系很强,耦合度高
(5)对象组合是类继承之外的另一种复用。对象组合要求被组合的对象具有良好定义的接口,这种复用风格被称为黑箱复用。因为对象的内部细节是不可见的,对象只以“黑箱”的形式出现,组合类之间没有很强的依赖关系,耦合度低。优先使用对象组合有助于保持每个类被封装
(6)尽量多的使用对象组合。组合的耦合度低,代码维护性好。类之间的关系在既可以用对象组合又可以用继承的情况下,就用组合。但是要实现多态,就必须要继承
//A、B构成is-a的关系
class A
{
protected:
string _a = "_a";
};
class B :public A
{
protected:
string _b = "_b";
public:
void display()
{
cout << _a << _b << endl;
}
};
//C、D构成has-a的关系
class C
{
protected:
string _c = "_c";
public:
string get_c()
{
return _c;
}
};
class D
{
protected:
string _d = "_d";
C c;
public:
void display()
{
cout << c.get_c() << _d << endl;
}
};
int main()
{
B b;
b.display();
D d;
d.display();
return 0;
}