c++ 设计一个虚基类Person包含姓名和年龄私有数据成员以及相关的成员函数;由它派生出领导类Leader包含职务和部门私有数据成员以及相关的成员函数;再由Person派生出工程师类Engineer包含职务和专业私有数据成员以及相关的成员函数;再由Leader和Engineer类派生出主任工程师类Chairman。并采用相关数据进行测试。
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
int age;
public:
Person(string n, int a): name(n), age(a) {}
virtual void showInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
class Leader: virtual public Person {
protected:
string position;
string department;
public:
Leader(string n, int a, string p, string d): Person(n, a), position(p), department(d) {}
virtual void showInfo() {
Person::showInfo();
cout << "Position: " << position << endl;
cout << "Department: " << department << endl;
}
};
class Engineer: virtual public Person {
protected:
string job;
string major;
public:
Engineer(string n, int a, string j, string m): Person(n, a), job(j), major(m) {}
virtual void showInfo() {
Person::showInfo();
cout << "Job: " << job << endl;
cout << "Major: " << major << endl;
}
};
class Chairman: public Leader, public Engineer {
public:
Chairman(string n, int a, string p, string d, string j, string m): Person(n, a), Leader(n, a, p, d), Engineer(n, a, j, m) {}
virtual void showInfo() {
Person::showInfo();
Leader::showInfo();
Engineer::showInfo();
}
};
int main() {
Chairman c("Tom", 40, "Chairman", "Engineering Department", "Chief Engineer", "Mechanical Engineering");
c.showInfo();
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/d6Zq 著作权归作者所有。请勿转载和采集!