前言:
参考: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;
}