编写一个学生和教师的数据输入和显示程序。学生数据有编号、姓名、性别、年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。要求将编号、姓名、性别、年龄的输人和显示设计成一个类Person,

#include<iostream>

#include<string>

using namespace std;

class Person //声明基类

{

public:

    Person(string name1, int number1)//基类构造函数

    {

        name = name1;

        number = number1;

    }

    void display()//输出基类数据成员

    {

        cout << "姓名:" << name << endl;

        cout << "编号:" << number << endl;

    }

protected://保护部分

    string name;

    int number;

};

class Student :public Person //声明公用派生类student,即学生的相关数据

{

public:

    Student(string name1, int number1, int class_number1, int score1) ://派生类构造函数

        Person(name1, number1)

    {

        class_number = class_number1;//对派生类新增的数据成员初始化

        score = score1;

    }

    void show()//输出学生的所有相关数据

    {

        display();//输出姓名和编号

        cout << "x:" << class_number << "\n" << "成绩:" << score << endl;

    }

protected://保护成分

    int class_number;

    int score;

};

class Teacher :public Person //声明公用派生类teacher,即教师的相关数据

{

public:

    Teacher(string name1, int number1, string profession1, string dept1) :

        Person(name1, number1)

    {

        profession = profession1;//对派生类新增的数据成员初始化

        dept = dept1;

    }

    void show_all()//输出教师的所有相关数据

    {

        display();

        cout << "职称:" << profession << "\n" << "部门:" << dept << endl;

    }

protected:

    string profession;

    string dept;

};

int main()

{

    Student my_Student("wsq", 1, 1, 1);

    cout << "学生:" << endl;

    my_Student.show();

    Teacher my_Teacher("无", 1, "无", "无");

    cout << "教师:" << endl;

    my_Teacher.show_all();

    system("pause");

    return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值