赞
踩
1、定义Person类,由Person分别派生出Teacher(教师)类和Cadre(干部)类,再由Teacher(教师)类和Cadre(干部)类采用多重继承方式派生出新类TeacherCadre(教师兼干部类)
要求:
① Person类中包含数据成员有姓名、年龄、性别。Teacher类中还包括数据成员职称,在Cadre类中还包含数据成员职务,TeacherCadre类中还包括数据成员工资。
②每个类都有构造函数与显示信息函数(show)。
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
class Person //Person类的定义
{
public:
void get_person() //输入函数(这里有语法错误吧???)
{
cout<<"Please input name:"<<endl;
cin>>name;
cout<<"Please input age:"<<endl;
cin>>age;
cout<<"Please input sex:"<<endl;
cin>>sex;
}
void show_person() //输出函数
{
cout<<"哥们,你的信息如下:"<<endl;
cout<<"name:";
puts(name);
cout<<"age:"<<age<<endl;
cout<<"sex:";
puts(sex);
}
~Person() //Person析构函数
{cout<<"Person析构函数"<<endl;}
private:
char name[20];
int age;
char sex[5];
};
class Teacher:public Person //Teacher类的定义
{
public:
void get_teacher() //输入Teacher的基本信息
{
get_person();
cout<<"input your jobwhat:"<<endl;
cin>>jobwhat;
}
void show_teacher() //输出Teacher的基本信息
{
show_person();
cout<<"jobwhat:"; puts(jobwhat);
cout<<endl;
}
~Teacher() //析构函数
{cout<<"Teacher析构函数"<<endl;}
private:
char jobwhat[10];
};
class Cadre:public Person //Cadre类的定义
{
public:
void get_cadre()
{
cout<<"input your jobname:"<<endl;
cin>>jobname;
}
void show_cadre()
{
show_person();
cout<<"jobname:"; puts(jobname);
cout<<endl;
}
void showshow()
{
cout<<"jobname:"; puts(jobname);
cout<<endl;
}
~Cadre()
{cout<<"Cadre析构函数"<<endl;}
private:
char jobname[10];
};
class TeacherCadre:public Teacher,public Cadre //TeacherCadre类 ,继承Teacher和Cadre
{
public:
void get_teachercadre()
{
cout<<"Please input your pay:"<<endl;
cin>>pay;
}
void show_teachercadre()
{
show_teacher();
showshow();
cout<<"pay:"<<pay<<endl;
}
~TeacherCadre()
{cout<<"TeacherCadre析构函数"<<endl;}
private:
long pay;
};
int main() //主函数
{
int flag;
Person st1; Teacher st2; Cadre st3; TeacherCadre st4; //定义四个对象,分别对应每个类一个
cout<<"大牛,请选择:"<<endl;
cout<<"1-Person 2-Teacher 3-Cadre 4-Teacher&&Cadre"<<endl; //人为选择,选择自己属于哪一类,待会就输出相应类的信息
cin>>flag;
if(flag==1) //第一类,Person
{
st1.get_person();
cout<<"*******--------------********"<<endl;
st1.show_person();
cout<<"*******--------------********"<<endl;
}
if(flag==2) //第二类,Teacher
{
st2.get_teacher();
cout<<"*******--------------********"<<endl;
st2.show_teacher();
cout<<"*******--------------********"<<endl;
}
if(flag==3) //第三类,Cadre
{
st3.get_person();
st3.get_cadre();
cout<<"*******--------------********"<<endl;
st3.show_cadre();
cout<<"*******--------------********"<<endl;
}
if(flag==4) //第四类,teachercadre
{
st4.get_teacher();
st4.get_cadre();
st4.get_teachercadre();
cout<<"*******--------------********"<<endl;
st4.show_teachercadre();
cout<<"*******--------------********"<<endl;
}
system("pause");
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。