注册 登录
编程论坛 C++教室

类的继承与派生

朝闻道 发布于 2012-10-28 20:33, 562 次点击
编写一个能输入、输出学生和教师数据的程序。学生数据有编号、姓名、班号和成绩;
教师数据有编号、姓名、职称和部门。声明一个person类,作为学生数据操作类student和教师数据操作类teacher的基类。person类中含有性别、年龄等公有数据成员和Enter( ),display( )等成员函数。
要求:使用对象数组保存输入的对象。在main函数中,调用类中的成员函数录入学生或教师的信息,显示输出学生或教师的信息。
3 回复
#2
zhuanjia02012-10-28 22:06
上学期有写过类似的题目,你稍加修改即可。

程序代码:
/************************************************************************/
/*2、编写一个学生和教师的数据输入和显示程序。学生数据有编号、姓名、性别、
年龄、系别和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。要求将编号、
姓名、性别、年龄的输入和显示设计成一个类Persona,并作为学生类Student和教师
类Teacher的基类。                                                      
*/
/************************************************************************/

#include <iostream>
#include <string>
using namespace std;

class Persona
{
public:
    int Num;
    string Name;
    string Sex;
    int Age;

    void InfoSet(int Num0,string Name0,string Sex0,int Age0)
    {
        Num=Num0;
        Name=Name0;
        Sex=Sex0;
        Age=Age0;
    }

    void InfoPrint()
    {
        cout<<"编号:"<<Num<<endl;
        cout<<"姓名:"<<Name<<endl;
        cout<<"性别:"<<Sex<<endl;
        cout<<"年龄:"<<Age<<endl;
    }
};

class Student:protected Persona
{
public:
    Student(int num,string name,string sex,int age,string m,float s)
    {
        InfoSet(num,name,sex,age);
        major=m;score=s;
    }
    void StuMajorSet(string mm)
    {
        major=mm;
    }
    void StuScoreSet(float ss)
    {
        score=ss;
    }
    void StudentPrint()
    {
        InfoPrint();
        cout<<"系别:"<<major<<endl;
        cout<<"分数:"<<score<<endl;
    }
private:
    string major;
    float score;
};

class Teacher:protected Persona
{
public:
    Teacher(int num,string name,string sex,int age,string j,string d)
    {
        InfoSet(num,name,sex,age);
        job=j;department=d;
    }
    void TeachJobSet(string jj)
    {
        job=jj;
    }
    void TeachDepartSet(float dd)
    {
        department=dd;
    }
    void TeacherPrint()
    {
        InfoPrint();
        cout<<"职称:"<<job<<endl;
        cout<<"部门:"<<department<<endl;
    }
private:
    string job;
    string department;
};

int main()
{
    Student stu(1,"张三","",20,"计算机系",90);
    Teacher teach(2,"无云","",20,"讲师","计算机系");
    cout<<"/////////////////////////////"<<endl;
    stu.StudentPrint();
    cout<<"/////////////////////////////"<<endl;
    teach.TeacherPrint();
    cout<<"/////////////////////////////"<<endl;
    return 0;
}
#3
小小小火柴2012-10-29 08:44
  顶下哦!
#4
fangxia07162012-10-29 13:44
学习中
1