c++中类的继承

【问题描述】编写一个基类person类,数据成员有编号(string id)和姓名(stirng name)。再编写派生类student类和teacher类。student类除了编号和姓名之外,还有数据成员班号(int classid)和成绩(int score)。teacher类student类除了编号和姓名之外,还有数据成员职称(string title)和部门(string department)。

为这些类编写输入相关信息的方法和 显示相关信息的方法。在主函数里声明一个student类的对象和一个teacher类的对象,调用其方法输入学生和老师的相关信息并显示。

【输入形式】

001

zhangsan

1

95

200

Chenxing

Lecture

Computer

【输出形式】

Please input student information

Please input id:

Please input name:

Please input classid:

Please input score:

The information of the student:

Id is 001

Name is zhangsan

The classid is: 1

The score is: 95

Please input teacher information

Please input id:

Please input name:

Please input title:

Please input department:

The information of the teacher:

Id is 200

Name is Chenxing

The title is: Lecture

The department is: Computer
代码如下:`//
// Created by 74706 on 2021/5/19.
//
#include
#include
using namespace std;
class person{ //基类person类
public:
person(string id1,string name1){
id=id1;
name=name1;
}
void show(){
cout << "Id is "<<id<<endl;
cout <<"Name is "<<name<<endl;
}
private:
string id;
string name;
};
class student:public person{ //继承的学生类
public:
student(string id1,string name1,int classid1,int score1):person(id1,name1){
classid=classid1;
score=score1;
}
void show1(){
person::show();
cout <<"The classid is: "<<classid<<endl;
cout <<"The score is: "<<score<<endl;
}
private:
int classid;
int score;
};

class teacher:public person{ //继承的教师类
public:
teacher(string id1,string name1,string title1,string department1):person(id1,name1){
title=title1;
department=department1;
}
void show2(){
person::show();
cout <<"The title is: "<<title<<endl;
cout <<"The department is: "<<department<<endl;
}
private:
string title;
string department;
};
int main()
{
string id,name,title,department;
int classid,score;
cout <<“Please input student information”<<endl;
cout <<“Please input id:”<<endl;
cin >>id;
cout <<“Please input name:”<<endl;
cin >>name;
cout <<“Please input classid:”<<endl;
cin >>classid;
cout <<“Please input score:”<<endl;
cin >>score;
cout <<“The information of the student:”<<endl;
student stu(id,name,classid,score);
stu.show1();
cout <<“Please input teacher information”<<endl;
cout <<“Please input id:”<<endl;
cin >>id;
cout <<“Please input name:”<<endl;
cin >>name;
cout <<“Please input title:”<<endl;
cin >>title;
cout <<“Please input department:”<<endl;
cin >>department;
cout <<“The information of the teacher:”<<endl;
teacher tea(id,name,title,department);
tea.show2();
return 0;
}

`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值