C++实训7

人员类的继承

实训7首先设计一个人员类person作为基类,其数据成员为姓名和身份证号包括数据成员地址和年龄,成员函数有输入数据和显示数据;生成person的派生类学生student,student包括数据成员电话号码和C++成绩,成员函数有输入数据和显示数据;设计学生成绩类score,它是student类和add类的派生类,继承两个类的所有属性,score类本身有数据成员数学成绩和英语成绩,成员函数有输入数据和显示数据;职员类employee继承person类,类中没有任何成员。编写主程序观察运行结果。

#include
#include
#include
using namespace std;

class People{
public:
    People(){}
    People(char*,char* );
    void Input();
    void Display();
    ~People(){ cout << "执行People的析构函数" << endl;}
private:
    char _name[32];
    char _id[20];       

};
People::People(char*m,char* i){
    strcpy(_name,m);
    strcpy(_id,i);
}
void People::Input(){
    cout<<"请输入姓名:"; 
    cin.getline(_name,32);
    cout<<"请输入身份证号:";
    cin>>_id;
}
void People::Display(){
    cout<<"姓名:"<<_name<cout<<"身份证:"<class Add{
public:
    Add(){}
    Add(char*, int );
    void AddInput();
    void Display(); 
    ~Add()  {cout << "执行Add的析构函数"<< endl;}
private:
    char size[32];
    int age;
};
Add::Add(char*A_size,int A_age){
    strcpy(size,A_size);
    age=A_age;
}
void Add::AddInput(){
    cout<<"请输入地址:"; 
    scanf("%s",size);
    cout<<"请输入年龄:";
    cin>>age;
}
void Add::Display(){
    cout<<"地址:"<cout<<"年龄:"<class Student:virtual public People{
public:
    Student(){}
    Student(char *m,char* i,char* t,float cpp):People(m,i)  {strcpy(tell,t);cppgrate=cpp;}
    void StuImput();
    void Display(); 
public:
    char tell[15];
    int cppgrate;
};
void Student::StuImput(){
    cout<<"输入电话:";
    scanf("%s",tell);
    cout << "输入c++成绩:";
    cin >>cppgrate; 
}
void Student::Display(){
    cout<<"电话:"<cout<<"c++成绩:"<class Score :virtual public Student,public Add{
public:
    Score(){}
//  Score(char *m="\0",float i=0,float t=0,float cpp=0,char*A_size="\0",int A_age=0,float math=0,float english=0):Student(m,i,t,cpp),Add(A_size,A_age){mathgrate=math;enggrate=english;}
    Score(char *m,char* i,char* t,float cpp,char*A_size,int A_age,float math,float english):Student(m,i,t,cpp),Add(A_size,A_age)
    {
        mathgrate=math;
        enggrate=english;   
    }
//  Score();
    void Input();
    void Display(); 

private:
    int mathgrate;
    int enggrate;
};
void Score::Input(){
    cout<<"输入数学成绩:";
    cin >>mathgrate;
    cout<<"输入英语成绩:";
    cin >>enggrate;
}
void Score::Display(){
    cout<<"数学成绩:"<cout<<"英语成绩:"<class Employee :virtual public People{

};

int main()
{

    Score tangyh;
    tangyh.People::Input();
    tangyh.StuImput();
    tangyh.AddInput();
    tangyh.Input();

    tangyh.People::Display();
    tangyh.Add::Display();
    tangyh.Student::Display();
    tangyh.Display();

    Employee s;
    s.Input();
    s.Display();

    Add a;
    a.AddInput();
    a.Display();
    Student st;
    st.Input();
    st.StuImput();
    st.Display();
    st.People::Display();


    return 0;   
}
功能
People类 数据成员姓名,身份证,输入输出
Add类 数据成员地址,手机号,输入输出
Student类 继承people类新添加数据成员年龄,Cpp成绩,输入输出
Score类 继承student类和add类添加数学英语成绩 输入输出
employee类 继承people类空类

你可能感兴趣的:(C++面向对象程序设计实训)