C++的忠实粉丝-继承的热情(2)

15626fc2fa934b89943ff7ecbff7bd37.png

目录

前言

1. 继承和友元

2.继承与静态成员

 3.多继承及其菱形继承

3.1 继承模型

3.2 虚继承

3.3 IO库中的菱形虚拟继承

4. 继承和组合

扫描二维码关注公众号,回复: 17417276 查看本文章

结束语


前言

前面我们讲了C++继承的部分知识,本节博客我们将对继承的知识进行进一步的补充。

1. 继承和友元

友元关系不能继承,也就是说基类友元不能访问派生类私有和保护成员。

#include <iostream>
#include <string>
using namespace std;
//class Student;
class Person {
public:
	friend void display(const Person& p, const Student& s);
	//Person(const string& name)
		//:_name(name){}
protected:
	string _name;
};
class Student :public Person {
public:
	//Student(const string&name ,int id)
		//:Person(name),_id(id){}
protected:
	int _id;
};
void display(const Person& p,const Student&s){
	cout << p._name<< endl;
	cout << s._id << endl;

}
int main() {
	Person p;
	Student s;
	display(p, s);
	return 0;
}

运行上面代码我们会发现报错一堆,而且有奇怪的错误,像缺少符号的错误,但是代码却没有问题,实际上类型出了问题,因为编译器在编译运行时,会进行向上查找,然而在父类的友元声明用了Student类,所以没有找到,因为是继承Person,不可能把Student类写在上面。

因此我们可以加一个前置声明。

因为友元关系不能被继承,所以在派生类里面在加一个相同的友元函数就可以运行了。

f15491a5390f46659af58e18a0aba329.png

代码修改后:

#include <iostream>
#include <string>
using namespace std;
class Student;//前置声明
class Person {
public:
	friend void display(const Person& p, const Student& s);
	//Person(const string& name)
		//:_name(name){}
protected:
	string _name;
};
class Student :public Person {
public:
	friend void display(const Person& p, const Student& s);
	//Student(const string&name ,int id)
		//:Person(name),_id(id){}
protected:
	int _id;
};
void display(const Person& p,const Student&s){
	cout << p._name<< endl;
	cout << s._id << endl;

}
int main() {
	Person p;
	Student s;
	display(p, s);
	return 0;
}

2.继承与静态成员

基类定义了static静态成员,则整个继承体系里面只有一个这样的成员。无论派生出多少个派生类,都只有一个static成员实例。
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
	string _name;
	static int count;
};
int Person::count = 1;
class Student : public  Person {
protected:
	int _id;
};
int main() {
	Person p;
	Student s;
	cout << &p._name << endl;
	cout << &s._name << endl;
	cout << "--------" << endl;
	cout << &p.count << endl;
	cout << &s.count << endl;
	cout << "--------" << endl;
	cout << s.count << endl;
	cout << p.count << endl;
	cout << "--------" << endl;
	cout << Student::count << endl;
	cout << Person:: count << endl;
	return 0;
}
这里的运行结果可以看到非静态成员 _name 的地址是不一样的
说明派生类继承下来了,父派生类对象各有一份
静态成员 count 的地址是一样的
说明派生类和基类共用同一份静态成员
公有的情况下,父派生类指定类域都可以访问静态成员
00a3f451817c4efd8e820e2f95e411ad.png

 3.多继承及其菱形继承

3.1 继承模型

单继承:一个派生类只有一个直接基类时称这个继承关系为单继承
f0310f133f2b44b2b0be574014459ef6.png
多继承:一个派生类有两个或以上直接基类时称这个继承关系为多继承,多继承对象在内存中的模型是,先继承的基类在前面,后面继承的基类在后面,派生类成员在放到最后面。

0ca81c514db44a4c917dd92d4e81a6f5.png

菱形继承:菱形继承是多继承的一种特殊情况。菱形继承的问题,从下面的对象成员模型构造,可以看出菱形继承有数据冗余和二义性的问题,在Assistant的对象中Person成员会有两份。支持多继承就一定会有菱形继承,像Java就直接不支持多继承,规避掉了这里的问题,所以实践中不建议设计出菱形继承这样的模型的。
bf9b701dd12b443f94150ddfdbb570bd.png

#include <iostream>
#include <string>
using namespace std;
class Person {
public:
	string _name;
};
class student : public Person {
protected:
	int _num;
};
class teacher : public Person {
protected:
	int _id;
};
class assistant :public student, teacher {
protected:
	string course;
};
int main() {
	assistant a;
	a._name="peter";
	return 0;
}
d7978fcddbb449cf9149dfba730de941.png

这段代码运行就出现问题,_name产生了二义性。

需要显示指定访问哪个基类的成员可以解决二义性问题,但是数据冗余问题无法解决 

a.student::_name = "xxx";
a.teacher::_name = "yyy"; 

3.2 虚继承

有了多继承,就存在菱形继承,有了菱形继承就有
菱形虚拟继承,底层实现就很复杂,性能也会有一些损失, 所以最好不要设计出菱形继承

使用虚继承,可以解决数据冗余和二义性

在继承的格式中加个virtual

#include <iostream>
#include <string>
using namespace std;
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 course;
};
int main() {
	assistant a;
	a._name="peter";
	//a.student::_name = "xxx";
	//a.teacher::_name = "yyy";
	return 0;
}

 故可以设计出多继承,但是不建议设计出菱形继承,因为菱形虚拟继承以后,无论是使用还是底层都会复杂很多。当然有多继承语法支持,就一定存在会设计出菱形继承,像Java是不支持多继承的,就避开了菱形继承。

#include <iostream>
#include <string>
using namespace std;
class Person {
public:
	Person(const char*name)
		:_name(name){}

	string _name;
};
class student : virtual public Person {
public:
	student(const char* name, int num)
		:Person(name),
		_num(num)
	{}
protected:
	int _num;
};
class teacher : virtual public Person {
public:
	teacher(const char* name, int id)
		:Person(name),
		_id(id)
	{}
protected:
	int _id;
};
class assistant :public student, public teacher {
public:
	assistant(const char* name1, const char* name2, const char* name3,const char*c)
		:Person(name3)
		, student(name1, 1)
		, teacher(name2, 2)
		,course(c)

	{}
protected:
	string course;
};
int main() {
	assistant aa("张三", "李四", "王五","大物");
	cout << aa._name  << endl;
	//a._name="peter";
	//a:tudent::_name = "xxx";
	//a.teacher::_name = "yyy";
	return 0;
}
在虚拟继承链中继承首先设置的值。在调试过程中,只走了Person的构造,所以输出王五。

多继承中指针偏移问题?下面说法正确的是( C)
A:p1 == p2 == p3 B:p1 < p2 < p3 C:p1 == p3 != p2 D:p1 != p2 != p3
#include <iostream>
#include <string>
using namespace std;
class Base1 { public: int _b1; };
class Base2 { public: int _b2; };
class Derive : public Base1, public Base2 { public: int _d; };
int main(){
		Derive d;
		Base1* p1 = &d;
		Base2* p2 = &d;
		Derive* p3 = &d;
	return 0;
}

3.3 IO库中的菱形虚拟继承

75878544ec744045a17fb6d2aaf083b0.png

801e12eefb73435db481ebfb74baf04e.png

4. 继承和组合

public继承是一种is-a的关系。也就是说每个派生类对象都是一个基类对象。
组合是一种has-a的关系。假设B组合了A,每个B对象中都有一个A对象。
继承允许根据基类的实现来定义派生类的实现。这种通过生成派生类的复用通常被称为白箱复用
(white-box reuse)。术语“白箱”是相对可视性而言:在继承方式中,基类的内部细节对派生类可见 。继承一定程度破坏了基类的封装,基类的改变,对派生类有很大的影响。派生类和基类间的依赖关系很强,耦合度高。
对象组合是类继承之外的另一种复用选择。新的更复杂的功能可以通过组装或组合对象来获得。对象组合要求被组合的对象具有良好定义的接口。这种复用风格被称为黑箱复用(black-box reuse), 因为对象的内部细节是不可见的。对象只以“黑箱”的形式出现。 组合类之间没有很强的依赖关系,耦合度低。优先使用对象组合有助于你保持每个类被封装。
优先使用组合,而不是继承。实际尽量多去用组合,组合的耦合度低,代码维护性好。不过也不太
那么绝对,类之间的关系就适合继承(is-a)那就用继承,另外要实现多态,也必须要继承。类之间的关系既适合用继承(is-a)也适合组合(has-a),就用组合。

#include <iostream>
#include <string>
using namespace std;
class Tire {
protected:
	string _brand = "Michelin"; // 品牌
	size_t _size = 17; // 尺⼨
};
class car {
protected:
	string _colour = "white"; // 颜⾊
	string _num = "川ABIT00"; // ⻋牌号
	Tire _t1; // 轮胎
	Tire _t2; // 轮胎
	Tire _t3; // 轮胎
	Tire _t4; // 轮胎
};
class Benchi :public car {
public:
	void Drive() { cout << "好开-操控" << endl; }
};
// Car和BMW/Benz更符合is-a的关系
class Benz : public car {
public:
	void Drive() { cout << "好坐-舒适" << endl; }
};
int main() {
	Benchi b;
	b.Drive();
	return 0;
}

Tire(轮胎)Car(车)更符合has-a的关系 , CarBenchi/Benz更符合is-a的关系

#include <iostream>
#include <string>
using namespace std;
template<class T>
class vector {

};

template<class T>
class stack :public vector<T> {
};

//template<class T>
//class stack {
//protected:
//	vector <T> v;
//};
int main() {
	return 0;
}
stack和vector的关系,既符合is-a,也符合has-a

结束语

本节内容就到此结束了,对继承的学习也就告别一段时间,关于继承更加底层的内容大家感兴趣可以去了解,下节内容我们将学习C++的下一个特性,多态!!!

最后感谢各位友友的支持!!! 

猜你喜欢

转载自blog.csdn.net/2302_79376097/article/details/143237039