C++实验 实验4 继承与派生类 1


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


#include<iostream>
#include<string.h>

using namespace std;

class Person
{
public:
	Person(int n, char *str)
	{
		num = n;
		name = new char[sizeof(str)+1];
		strcpy(name, str);
	}
	void show_name()
	{
		cout << "Name: "<< name << endl;
	}
	void show_num()
	{
		cout << "Number: " << num << endl;
	}
private:
	char *name;
	int num;
};

class Student : public Person
{
public:
	Student(int n, char *str, int c, float s) : Person(n, str)
	{
		Class = c;
		score = s;
	}
	void show()
	{
		cout << "\n";
		show_name();
		show_num();
		cout << "Class: " << Class << endl;
		cout << "Score: " << score << "\n" <<endl;
	}
private:
	int Class;
	float score;
};

class Teacher : public Person
{
public:
	Teacher(int n, char *str, char *pro, char *dep) : Person(n, str)
	{
		pro_post = new char[sizeof(pro)+1];
		strcpy(pro_post, pro);
		department = new char[sizeof(dep)+1];
		strcpy(department, dep);
	}
	void show()
	{
		cout << "\n";
		show_name();
		show_num();
		cout << "Professional post: " << pro_post << endl;
		cout << "Department: " << department << endl;
	}
private:
	char *pro_post;
	char *department;
};

int main()
{
	Student Stu(9051109, "Hugo", 1, 88);
	Stu.show();
	Teacher Teach(1111, "Ann", "Doctor", "English");
	Teach.show();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值