肚子饿了233 发表于 2021-5-27 12:19:25

关于C++多继承题目,救救菜鸟吧

#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
        Person(int a, char s, string n, string add, int t) :age(a), sex(s), name(n), address(add), telephone(t)
        {}
        //protected:
        int age, telephone;
        char sex;
        string name, address;
};
class Teacher :virtual public Person
{
public:
        string title;
        void virtual display();
};
class Cadre :virtual public Person
{
public:
        string post;
        void virtual display();
};
class Teacher_Cadre :virtual public Teacher, virtual public Cadre
{
public:
        int wages;
        void show();
};
void Teacher::display()
{
        cout << "该教师的年龄,性别,名字,地址,电话号码分别为:" << endl;
        cout << age << " " << sex << " " << name << " " << address << " " << telephone << endl;
        cout << "该老师的职称为:" << title;
}
void Cadre::display()
{
        cout << "该干部的年龄,性别,名字,地址,电话号码分别为:" << endl;
        cout << age << " " << sex << " " << name << " " << address << " " << telephone << endl;
        cout << "该干部的职务为:" << post;
}
void Teacher_Cadre::show()
{
        cout << "该教师兼干部的工资为: " << wages << endl;
}
int main()
{
        Person p1(25, 'f', "张红", "湖北大学", 12345);
        Teacher t;                                     //t 报错
        t.title = "班主任";
        t.display();
        Cadre c;                                       //c 也报错
        c.post = "校长";
        c.display();
        Teacher_Cadre tc;                         //tc 报错
        tc.wages = 10000;
        tc.show();

}



有没有好心人能指点迷津?

tian1234 发表于 2021-5-27 14:13:47

父类创个空构造函数就不报错啦

肚子饿了233 发表于 2021-5-28 11:54:27

tian1234 发表于 2021-5-27 14:13
父类创个空构造函数就不报错啦

哪个父类?

tian1234 发表于 2021-5-28 13:30:23

Person加个空构造函数,或者下面的子类孙类的构造函数都改成带参构造函数,因为子类在创建是默认调用的是父类的无参构造函数
页: [1]
查看完整版本: 关于C++多继承题目,救救菜鸟吧