小甲鱼C++ 29 虚继承笔记

写在前面:作为一只小白,感谢小甲鱼老师提供这么好的入门课程。因此在这里做个笔记,如有侵权请联系删除

​​​​​​​www.fishc.com

①虚继承概念

前一节(https://blog.csdn.net/caofengtao1314/article/details/111861707)的student示例程序看起来似乎已经解决了问题,但它存在着一些隐患.

首先,在TeachingStudent类的introduce()方法里,我们不得不明确地告诉编译器应该使用哪一个属性。

这对于classes属性来说是应该的,因为教一门课和上一门课有着本质的区别,而作为常识,助教生教的课程和他学的课程不可能一样!

②思考

但是我们再深入考虑下,既然在TeachingStudent对象里可以继承两个不同的classes属性,那它是不是应该有两个不同的name属性呢?

答案:是!事实上,TeachingStudent还真可以有两个不同的名字,这肯定不是我们在设计这个类继承模型时所预期的

 

③代码验证

3.1前一节代码https://blog.csdn.net/caofengtao1314/article/details/111861707 运行结果

#include <iostream>
#include <string>

class Person
{
	public:
		Person(std::string theName);
		void introduce();

	protected:
		std::string name;
};

class Teacher : public Person
{
	public:
		Teacher(std::string theName,std::string theClass);

		void teach();
		void introduce();

	protected:
		std::string classes;	
};

class Student : public Person
{

	public:
		Student(std::string theName,std::string theClass);

		void attendClass();
		void introduce();

	protected:
		std::string classes;
};

class TeachingStudent : public Student,public Teacher
{
	public:
		TeachingStudent(std::string theName,std::string ClassTeaching,std::string ClassAttending);

		void introduce();	
};

Person::Person(std :: string theName)
{
	name = theName;
}

void Person::introduce()
{
	std::cout<<"hello everyone my name is "<< name << "\n";
}

Teacher::Teacher(std::string theName,std::string theClass) : Person(theName)
{
	classes = theClass;
}

void Teacher::teach()
{
	std::cout<< name << " teach " << classes <<"\n";
}

void Teacher::introduce()
{
	std::cout<<"hello everyone my name is "<< name << "teach" << classes << "\n";
}

Student::Student(std::string theName,std::string theClass) : Person(theName)
{
	classes = theClass;
}

void Student::attendClass()
{
	std::cout<< name << " attend " << classes <<"\n";
}

void Student::introduce()
{
	std::cout<<"hello everyone my name is "<< name << " study " << classes << "\n";
}

TeachingStudent::TeachingStudent(std::string theName,std::string ClassTeaching,std::string ClassAttending):Teacher(theName,ClassTeaching),Student(theName,ClassAttending)
{
	
}

void TeachingStudent::introduce()
{
	std::cout<< Student::name << " teach " << Teacher::classes <<"\n";
	std::cout<<"at the same time I am study "<<Student:: classes << "\n";
}

int main()
{
	Teacher teacher("tom "," c++first");
	Student student("jery "," c++first");
	TeachingStudent teachingstudent("tom1 "," c++first"," c++second");

	teacher.introduce();
	teacher.teach();
	
	student.introduce();
	student.attendClass();

	teachingstudent.introduce();
	teachingstudent.teach();
	teachingstudent.attendClass();
	

	return 0;
}

运行结果

hello everyone my name is tom teach c++first
tom  teach  c++first
hello everyone my name is jery  study  c++first
jery  attend  c++first
tom1  teach  c++first
at the same time I am study  c++second
tom1  teach  c++first
tom1  attend  c++second

1.2,验证思考的问题

#include <iostream>
#include <string>

class Person
{
	public:
		Person(std::string theName);
		void introduce();

	protected:
		std::string name;
};

class Teacher : public Person
{
	public:
		Teacher(std::string theName,std::string theClass);

		void teach();
		void introduce();

	protected:
		std::string classes;	
};

class Student : public Person
{

	public:
		Student(std::string theName,std::string theClass);

		void attendClass();
		void introduce();

	protected:
		std::string classes;
};

class TeachingStudent : public Student,public Teacher
{
	public:
		TeachingStudent(std::string theName1,std::string theName2,std::string ClassTeaching,std::string ClassAttending);

		void introduce();	
};

Person::Person(std :: string theName)
{
	name = theName;
}

void Person::introduce()
{
	std::cout<<"hello everyone my name is "<< name << "\n";
}

Teacher::Teacher(std::string theName,std::string theClass) : Person(theName)
{
	classes = theClass;
}

void Teacher::teach()
{
	std::cout<< name << "teach" << classes <<"\n";
}

void Teacher::introduce()
{
	std::cout<<"hello everyone my name is "<< name << " teach " << classes << "\n";
}

Student::Student(std::string theName,std::string theClass) : Person(theName)
{
	classes = theClass;
}

void Student::attendClass()
{
	std::cout<< name << "attend " << classes <<"\n";
}

void Student::introduce()
{
	std::cout<<"hello everyone my name is "<< name << " study " << classes << "\n";
}

TeachingStudent::TeachingStudent(std::string theName1,std::string theName2,std::string ClassTeaching,std::string ClassAttending):Teacher(theName1,ClassTeaching),Student(theName2,ClassAttending)
{
	
}

void TeachingStudent::introduce()
{
	std::cout<< Student::name << " teach " << Teacher::classes <<"\n";
	std::cout<<"at the same time I am study "<<Student:: classes << "\n";
}

int main()
{
	Teacher teacher("tom "," c++first");
	Student student("jery "," c++first");
	TeachingStudent teachingstudent(" tom2"," tom1 "," c++first"," c++second");

	teacher.introduce();
	teacher.teach();
	
	student.introduce();
	student.attendClass();

	teachingstudent.introduce();
	teachingstudent.teach();
	teachingstudent.attendClass();
	

	return 0;
}

运行结果,产生脑裂

hello everyone my name is tomteach c++first
tomteach c++first
hello everyone my name is jerystudy c++first
jeryattend c++first
 tom1teach c++first
at the same time I am study  c++second
 tom2teach c++first
 tom1attend c++second

④虚继承的引出:

》》TeachingStudent类继承自Teacher和Student两个类,因而继承了两组Person类的属性,这在某些时候完全有道理,例如classes属性。但它也有可能引起麻烦,例如发生在name属性身上的情况。

》》C++发明者也想到了这部分的冲突,因此为此提供了一个功能可以解决这个问题:虚继承(virtual inheritance)

》》通过虚继承某个基类,就是在告诉编译器:从当前这个类再派生出来的子类只能拥有那个基类的一个实例。

虚继承的语法:

class Teacher:virtual public Person

{....}

》》这样做我们的问题就解决了:让Student和Teacher类都虚继承自Person类,编译器将确保从Student和Teacher类再派生出来的子类只能拥有一份Person类的属性。

⑤修改的例子 仅仅class Teacher:virtual public Person 和class Student:virtual public Person,

#include <iostream>
#include <string>

class Person
{
	public:
		Person(std::string theName);
		void introduce();

	protected:
		std::string name;
};

class Teacher : virtual public Person
{
	public:
		Teacher(std::string theName,std::string theClass);

		void teach();
		void introduce();

	protected:
		std::string classes;	
};

class Student : virtual public Person
{

	public:
		Student(std::string theName,std::string theClass);

		void attendClass();
		void introduce();

	protected:
		std::string classes;
};

class TeachingStudent : public Student,public Teacher
{
	public:
		TeachingStudent(std::string theName1,std::string theName2,std::string ClassTeaching,std::string ClassAttending);

		void introduce();	
};

Person::Person(std :: string theName)
{
	name = theName;
}

void Person::introduce()
{
	std::cout<<"hello everyone my name is "<< name << "\n";
}

Teacher::Teacher(std::string theName,std::string theClass) : Person(theName)
{
	classes = theClass;
}

void Teacher::teach()
{
	std::cout<< name << "teach" << classes <<"\n";
}

void Teacher::introduce()
{
	std::cout<<"hello everyone my name is "<< name << " teach " << classes << "\n";
}

Student::Student(std::string theName,std::string theClass) : Person(theName)
{
	classes = theClass;
}

void Student::attendClass()
{
	std::cout<< name << "attend " << classes <<"\n";
}

void Student::introduce()
{
	std::cout<<"hello everyone my name is "<< name << " study " << classes << "\n";
}

TeachingStudent::TeachingStudent(std::string theName1,std::string theName2,std::string ClassTeaching,std::string ClassAttending):Teacher(theName1,ClassTeaching),Student(theName2,ClassAttending)
{
	
}

void TeachingStudent::introduce()
{
	std::cout<< Student::name << " teach " << Teacher::classes <<"\n";
	std::cout<<"at the same time I am study "<<Student:: classes << "\n";
}

int main()
{
	Teacher teacher("tom "," c++first");
	Student student("jery "," c++first");
	TeachingStudent teachingstudent(" tom2"," tom1 "," c++first"," c++second");

	teacher.introduce();
	teacher.teach();
	
	student.introduce();
	student.attendClass();

	teachingstudent.introduce();
	teachingstudent.teach();
	teachingstudent.attendClass();
	

	return 0;
}

编译报错如下

g++ Multi_Inheritance_virtual.cpp 
Multi_Inheritance_virtual.cpp: In constructor ‘TeachingStudent::TeachingStudent(std::string, std::string, std::string, std::string)’:
Multi_Inheritance_virtual.cpp:87:193: error: no matching function for call to ‘Person::Person()’
   87 | ingStudent(std::string theName1,std::string theName2,std::string ClassTeaching,std::string ClassAttending):Teacher(theName1,ClassTeaching),Student(theName2,ClassAttending)
      |                                                                                                                                                                           ^

Multi_Inheritance_virtual.cpp:47:1: note: candidate: ‘Person::Person(std::string)’
   47 | Person::Person(std :: string theName)
      | ^~~~~~
Multi_Inheritance_virtual.cpp:47:1: note:   candidate expects 1 argument, 0 provided
Multi_Inheritance_virtual.cpp:4:7: note: candidate: ‘Person::Person(const Person&)’
    4 | class Person
      |       ^~~~~~
Multi_Inheritance_virtual.cpp:4:7: note:   candidate expects 1 argument, 0 provided
Multi_Inheritance_virtual.cpp:4:7: note: candidate: ‘Person::Person(Person&&)’
Multi_Inheritance_virtual.cpp:4:7: note:   candidate expects 1 argument, 0 provided

⑥虚继承正确的demo

TeachingStudent::TeachingStudent(std::string theName1,std::string theName2,std::string ClassTeaching,std::string ClassAttending):Teacher(theName1,ClassTeaching),Student(theName2,ClassAttending)
{
    
}

修改为

TeachingStudent::TeachingStudent(std::string theName,std::string ClassTeaching,std::string ClassAttending):Teacher(theName,ClassTeaching),Student(theName,ClassAttending),Person(theName)
{
    
}

这里有虚继承的重点解释,因为每一个TeachingStudent这个多继承的对象,只有一个Person类的属性,其实主要是因为Student和Person类的虚继承Person所以无法将Person的属性传给TeachingStudent,所以必须自己继承Person类。

#include <iostream>
#include <string>

class Person
{
	public:
		Person(std::string theName);
		void introduce();

	protected:
		std::string name;
};

class Teacher : virtual public Person
{
	public:
		Teacher(std::string theName,std::string theClass);

		void teach();
		void introduce();

	protected:
		std::string classes;	
};

class Student : virtual public Person
{

	public:
		Student(std::string theName,std::string theClass);

		void attendClass();
		void introduce();

	protected:
		std::string classes;
};

class TeachingStudent : public Student,public Teacher
{
	public:
		TeachingStudent(std::string theName,std::string ClassTeaching,std::string ClassAttending);

		void introduce();	
};

Person::Person(std :: string theName)
{
	name = theName;
}

void Person::introduce()
{
	std::cout<<"hello everyone my name is "<< name << "\n";
}

Teacher::Teacher(std::string theName,std::string theClass) : Person(theName)
{
	classes = theClass;
}

void Teacher::teach()
{
	std::cout<< name << "teach" << classes <<"\n";
}

void Teacher::introduce()
{
	std::cout<<"hello everyone my name is "<< name << " teach " << classes << "\n";
}

Student::Student(std::string theName,std::string theClass) : Person(theName)
{
	classes = theClass;
}

void Student::attendClass()
{
	std::cout<< name << "attend " << classes <<"\n";
}

void Student::introduce()
{
	std::cout<<"hello everyone my name is "<< name << " study " << classes << "\n";
}

TeachingStudent::TeachingStudent(std::string theName,std::string ClassTeaching,std::string ClassAttending):Teacher(theName,ClassTeaching),Student(theName,ClassAttending),Person(theName)
{
	
}

void TeachingStudent::introduce()
{
	std::cout<< Student::name << " teach " << Teacher::classes <<"\n";
	std::cout<<"at the same time I am study "<<Student:: classes << "\n";
}

int main()
{
	Teacher teacher("tom "," c++first");
	Student student("jery "," c++first");
	TeachingStudent teachingstudent(" tom2"," c++first"," c++second");

	teacher.introduce();
	teacher.teach();
	
	student.introduce();
	student.attendClass();

	teachingstudent.introduce();
	teachingstudent.teach();
	teachingstudent.attendClass();
	

	return 0;
}

运行结果:

hello everyone my name is tom  teach  c++first
tom teach c++first
hello everyone my name is jery  study  c++first
jery attend  c++first
 tom2 teach  c++first
at the same time I am study  c++second
 tom2teach c++first
 tom2attend  c++second

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值