多继承练习(定义老师,学生,研究生类)

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

class teacher{
public:
    teacher(){}
    teacher(string name,int age,string pos):name(name),age(age),pos(pos){}
    teacher(const teacher& t):name(t.name),age(t.age),pos(t.pos){}

    teacher& operator=(const teacher& t){
        this->name=t.name;
        this->age=t.age;
        this->pos=t.pos;
        return *this;
    }

protected:
    string name;
    int age;
    string pos;


};


class student{
public:
    student(){}
    student(string name,int age,int score ):name(name),age(age),score(score){}
    student(const student& s):name(s.name),age(s.age),score(s.score){}

    student& operator=(const student& s){
        this->name=s.name;
        this->age=s.age;
        this->score=s.score;
        return *this;
    }
protected:
    string name;
    int age;
    int score;


};



class graduate:public student,public teacher{
public:

    graduate(){}
    graduate(string s_name,int s_age,int score,string t_name,int t_age,string pos,string major):student(s_name,s_age,score),teacher(t_name,t_age,pos),major(major){}
    graduate(const graduate& g):student(g),teacher(g),major(g.major){}

    graduate& operator=(const graduate& g){
        student::operator=(g);
        teacher::operator=(g);
        this->major=g.major;
        return *this;
    }

    void show();

private:
    string major;
};

void graduate::show(){
    cout<<"studnet_name="<<student::name<<endl;
    cout<<"studnet_age="<<student::age<<endl;
    cout<<"studnet_score="<<student::score<<endl;
    cout<<"studnet_major="<<major<<endl;
    cout<<"teacher_name="<<teacher::name<<endl;
    cout<<"teacher_age="<<teacher::age<<endl;
    cout<<"teacher_pos="<<teacher::pos<<endl;

}

主函数 

int main(){
    

    graduate gstu("lisi",24,80,"zhangsan",55,"math teacher","math");
    gstu.show();
    cout<<"========================="<<endl;
    graduate gstu2(gstu);
    gstu2.show();
    cout<<"========================="<<endl;
    graduate gstu3(gstu);
    gstu3.show();
    cout<<"========================="<<endl;

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值