C++ 实验8 继承

编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生类student和教师类teacher的基类。

类图如下:

代码如下:


头文件(ss.h)代码如下:

#include<string>
class person {   //父类
private:
	char name[10];//姓名
	int numble; //编号
public:
	void print();//输出
    const char *getname();//得到姓名
	int getnumble();//得到编号
	person(const char* na, int num);//构造函数
};
class student :public person {  //person的子类
private:
	char cla[20];//班级
	double grade;//成绩
public:
	student(const char* na, int num, const char* c, double g);
	void print();//输出
};
class teacher :public person {  //person的子类
private:
	char pro[20];//职称
	char section[10];//部门
public:
	teacher(const char* na, int num, const char* pr, const char* sec);
	void print();//输出
};

main文件代码如下:

#include "ss.h"
#include<iostream>
#include<string>
using namespace std;

const char* person::getname() {
	return name;
}
int person::getnumble() {
	return numble;
}
person::person(const char* na, int num)
{
	strcpy_s(name, na);
	numble = num;
}
void person::print() {
	cout << endl;
	cout << "姓名:" << getname()<< endl;
	cout << "编号:" << getnumble()<< endl;
}
student::student(const char* na, int num, const char* c, double g) :person(na, num) {
	strcpy_s(cla, c);
	grade = g;
}

void student::print() {
	person::print();
	cout << "班级:" << cla << endl;
	cout << "成绩:" << grade << endl;
}

teacher::teacher(const char* na, int num, const char* pr, const char* sec) :person(na, num) {
	strcpy_s(pro, pr);
	strcpy_s(section, sec);
}

void teacher::print()
{
	person::print();
	cout << "职称:" << pro << endl;
	cout << "部门:" << section << endl;
}
void main()
{
	person p("张三",101);
	p.print();
	student st("李四", 102, "七年级一班", 88.0);
	st.print();
	teacher t("王五", 103, "高数老师", "基础部");
	t.print();
}

 实验运行截图如下:


代码仅供参考

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值