C++之 多继承

在学校里有老师和学生,他们都是人,我么应该创建一个名为 Person 的基类和两个名为 Teacher 和Student 的子类,后两者是从前者继承来的

有一部分学生还教课挣钱(助教),也就是同时存在着两个"是一个"关系,我们需要写一个 TeschingStudent 类让它同时继承 Teacher 类和 Student 类,换句话说,就是需要使用多继承。

cpp 复制代码
// 基本语法:
class TeachingStudent : public Student, public Teacher
{ ... }
cpp 复制代码
#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,I`m"<<name<<"。\n\n"; 
}


Teacher::Teacher(std::string theName,std::string theClass):Person(theName)
{
	classes=theClass;
}
void Teacher::teach()
{
	std::cout<<name<<"教"<<classes<<"。\n\n"; 	
}
void Teacher::introduce()
{
	std::cout<<"大家好,我是"<<name<<",我教"<<classes<<"。\n\n"; 
}


Student::Student(std::string theName,std::string theClass):Person(theName)
{
	classes=theClass;
}
void Student::attendClass()
{
	std::cout<<name<<"加入"<<classes<<"学习。\n\n";
}
void Student::introduce()
{
	std::cout<<"大家好,我是"<<name<<",我在"<<classes<<"学习\n\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<<",我教"<<Teacher::classes<<",";
		std::cout<<"同时我在"<<Student::classes<<"学习。\n\n";
}


int main()
{
	Teacher  teacher("小红","入门班");
	Student student("兰兰","C++入门班");
	TeachingStudent teachingStudent("茗茗","C++入门班级","C++进阶班");
	
	teacher.introduce();
	teacher.teach();
	
	student.introduce();
	student.attendClass();
	
	teachingStudent.introduce();
	teachingStudent.teach();
	teachingStudent.attendClass();
	
	return 0;
}

注意:

  • 在使用多继承的时候,一定要特别注意继承了基类的多少个副本。
  • 在使用多继承的时候,最安全最简明的做法是从没有任何属性且只有抽象方法的类开始继承。
  • 按照上边这么做可以让你远离后代子类可能拥有好几个基类属性的问题。
  • 这样的类又叫做接口( interface )。

未完待续。。。

相关推荐
weixin_3077791313 分钟前
C++代码实现MATLAB中的ode23t函数功能
开发语言·c++·算法·matlab
wuminyu24 分钟前
Markword在紧凑对象头上的实现原理剖析
java·linux·c语言·jvm·c++
hansang_IR1 小时前
【代数与组合数学 | 那忘算 5】生成函数 & 例题 & 卷积
c++·算法·多项式·生成函数·母函数
无忧.芙桃1 小时前
数据结构之排序算法(上):从评价指标到插入、希尔、选择与堆排序
c语言·c++·排序算法
syagain_zsx1 小时前
算法基础篇 · 02 高精度(C++ 题解)
开发语言·c++·学习·高精度
6Hzlia1 小时前
【Classic 150 刷题计划】 LeetCode 242. 有效的字母异位词 | C++ 哈希计数与严密防线
c++·算法·leetcode
wabs6661 小时前
关于二叉树【力扣101.对称二叉树的思考】
数据结构·c++·算法·leetcode·二叉树
6Hzlia1 小时前
【Classic 150 刷题计划】 LeetCode 228. 汇总区间 | C++ 双游标区间扫描与 to_string 规范
c++·算法·leetcode
纪念 2292 小时前
C++类和对象(二)
开发语言·c++
钓鱼的肝2 小时前
csp-j-s总结(1)
c++·笔记·算法·青少年编程