分别定义Teacher类和Cader类,采用 多重继承方式由这两个类派生出新类 Teacher-Cader类。要求: (1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。 (2)在Teacher类中还包含数据成员 title,在Cader
时间: 2024-02-18 13:05:01 浏览: 53
分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:①在两个基类中都包含
类中还包含数据成员 department。同时,两个类都包含成员函数show(),可以输出自己的基本信息。最后,Teacher-Cader类中应该包含所有基类的数据成员和成员函数,并且可以输出自己的基本信息。下面是示例代码:
```c++
class Teacher {
protected:
string name;
int age;
char gender;
string address;
string phone;
string title;
public:
void show() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
cout << "Address: " << address << endl;
cout << "Phone: " << phone << endl;
cout << "Title: " << title << endl;
}
};
class Cader {
protected:
string name;
int age;
char gender;
string address;
string phone;
string department;
public:
void show() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
cout << "Address: " << address << endl;
cout << "Phone: " << phone << endl;
cout << "Department: " << department << endl;
}
};
class Teacher_Cader : public Teacher, public Cader {
public:
void show() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
cout << "Address: " << address << endl;
cout << "Phone: " << phone << endl;
cout << "Title: " << title << endl;
cout << "Department: " << department << endl;
}
};
```
在这个示例中,Teacher类和Cader类都包含了姓名、年龄、性别、地址、电话等数据成员,以及show()成员函数。Teacher类还包含了一个title数据成员,而Cader类还包含了一个department数据成员。Teacher_Cader类继承了这两个类,并且包含了所有的数据成员和成员函数。它也有自己的show()成员函数来输出所有的信息。
阅读全文