<小甲鱼>C++实例练习22—多继承

前言:

参考:B站UP主鱼C_小甲鱼<C++快速入门>
代码调试平台:VS2017,调试成功。


问题描述:

问题描述:多继承


# 代码实现:
#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
	Person(string theName);

	void introduce();
protected:
	string name;
};
class Teacher:public Person
{
public:
	Teacher(string theName, string theClass);

	void teach();
	void introduce();

protected:
	string classes;
};
class Student:public Person
{
public:
	Student(string theName, string theClass);

	void attendClass();
	void introduce();

protected:
	string classes;
};
class TeachingStudent :public Student, public Teacher  //多继承
{
public:
	TeachingStudent(string theName, string classTeaching, string classAttending);

	void introduce();
};

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

void Person::introduce()
{
	cout << "大家好,我是" << name << "!";
}

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

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

void Student::attendClass()
{
	cout << "我上" << classes << "课.\n";
}
void Student::introduce()
{
	cout << "大家好,我是" << name << ".";
}

TeachingStudent::TeachingStudent(string theName, string classTeaching,
	string classAttending) :Teacher(theName, classTeaching),
	Student(theName, classAttending)
{

}
void TeachingStudent::introduce()
{
	cout << "大家好,我是" << Student::name << ".我教"<<Teacher::classes<<".同时我在"
			<<Student::classes<<"学习。\n\n";
}
int main()
{
	Teacher teacher("小甲鱼", "c++入门班");
	Student student("小金鱼", "c++入门班");
	TeachingStudent teachingstudent("小黄鱼", "c++进阶班", "c++入门班");

	teacher.introduce();
	teacher.teach();

	student.introduce();
	student.attendClass();

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值