您的位置:首页 > 编程语言 > C语言/C++

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

2011-07-31 08:53 225 查看
编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: