C++ qt day5

代码实现

定义一个学生类:成员–>姓名、年龄、成绩以及相关函数
定义一个老师类:成员–>年龄、职称以及相关函数
由学生类和老师类共同派生出研究生类:成员–>性别
实例化就研究生类的对象,并输出该对象的所有信息

代码

#include
using namespace std;

class Teacher
{
public:
    Teacher(){}
    Teacher(int a,string j):age(a),job(j){}
protected:
    int age;
    string job;
};

class Student
{
public:
    Student(){}
    Student(string n,int a,float s):name(n),age(a),score(s){}
protected:
    string name;
    int age;
    float score;
};

class Graduate:virtual public Student,virtual public Teacher
{
public:
    Graduate(){}
    Graduate(string n,int a,float s,int a1,string j,string s1):Student(n,a,s),Teacher(a1,j),sex(s1){}
    void show()
    {
        cout<<"研究生  姓名:"<<name<<'\t'<<"年龄:"<<Student::age<<"\t成绩:"<<score<<"\t性别:"<<sex<<endl;
        cout<<"导师   年龄:"<<Teacher::age<<"\t职称:"<<job<<endl;
    }

private:
    string sex;
};
int main()
{
    Graduate g1("cjy",22,100,45,"math","male");
    g1.show();
    return 0;
}

你可能感兴趣的:(c++,开发语言)