题目:
1、事先编好程序,上机调试和运行程序,分析结果。
- 将教材第11章例11.1的程序片段补充和改写成一个完整、正确的程序,用公用继承方式。在程序中包括输入数据的函数,在程序运行时输入num,name,sex,age,addr的值,程序应输出以上5个数据的值。
- 将教材第11章例11.3的程序修改、补充和改写成一个完整、正确的程序,用保护继承的方式。在程序中应包括输入数据的函数。
- 修改上面第2题的程序,改为用公用继承方式。运行得到正确结果
- 分别声明Teacher (教师)类和Cadre(干部)类,采用多重继承方式由这个类派生出新类Teacher_Cadre(教师兼干部)。
要求:
1在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员
2在Teache类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务)。在Teacher_Cadre类中还包含数据成员wages(工资)。
3对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员是,指定作用域。
4在类体中声明成员函数,在类外定义成员函数
5在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话、然后再用cout语句输出职务与工资。
参考代码:
1、
#include<iostream>
#include<string>
using namespace std;
class Student{
public:
void get_value(){
cin>>num>>name>>sex;
}
void display(){
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
private:
int num;
string name;
string sex;
};
class Student1:public Student{
public:
void get_value_1(){
cin>>age>>addr;
}
void display_1(){
cout<<"age:"<<age<<endl;
cout<<"address:"<<addr<<endl;
}
private:
int age;
string addr;
};
int main(){
Student1 stud;
cout<<"请输入学生编号,姓名,性别,年龄,住址"<<endl;
stud.get_value();
stud.get_value_1();
stud.display();
stud.display_1();
return 0;
}
2、
#include<iostream>
#include<string>
using namespace std;
class Student{
public:
protected:
int num;
string name;
string sex;
};
class Student1:protected Student{
public:
void get_valuel();
void display();
private:
int age;
string addr;
};
void Student1::get_valuel(){
cin>>num>>name>>sex;
cin>>age>>addr;
}
void Student1::display(){
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"age:"<<age<<endl;
cout<<"address:"<<addr<<endl;
}
int main(){
Student1 stud1;
cout<<"请输入学生编号,姓名,性别,年龄,住址"<<endl;
stud1.get_valuel();
stud1.display();
return 0;
}
3、
#include<iostream>
#include<string>
using namespace std;
class Student{
public:
protected:
int num;
string name;
string sex;
};
class Student1:public Student{
public:
void get_valuel();
void display();
private:
int age;
string addr;
};
void Student1::get_valuel(){
cin>>num>>name>>sex;
cin>>age>>addr;
}
void Student1::display(){
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"age:"<<age<<endl;
cout<<"address:"<<addr<<endl;
}
int main(){
Student1 stud1;
cout<<"请输入学生编号,姓名,性别,年龄,住址"<<endl;
stud1.get_valuel();
stud1.display();
return 0;
}
4、
#include<iostream>
#include<string>
using namespace std;
class Teacher{//教师类
public:
Teacher (string man,char se,int ag,string add,string te,string til);
void display();
protected:
string name;
char sex;
int age;
string addr;
string tel;
string title;
};
class Cadre{//干部类
public:
Cadre(string man,char se,int ag,string add,string te,string po);
void display();
protected:
string name;
char sex;
int age;
string addr;
string tel;
string post;
};
class Teacher_Cadre:public Teacher,public Cadre{//教师干部类
public:
Teacher_Cadre::Teacher_Cadre(string man,char se,int ag,string add,string te,string til,string po,double wag);
void show();
private:
double wages;
};
Teacher::Teacher (string man,char se,int ag,string add,string te,string til){
name = man;
sex = se;
age = ag;
addr = add;
tel = te;
title = til;
}
void Teacher::display(){
cout<<name<<' '<<sex<<' '<<age<<' '<<addr<<' '<<tel<<' '<<title<<endl;
}
Cadre::Cadre(string man,char se,int ag,string add,string te,string po){
name = man;
sex = se;
age = ag;
addr = add;
tel = te;
post = po;
}
void Cadre::display(){
cout<<name<<' '<<sex<<' '<<age<<' '<<addr<<' '<<tel<<' '<<post<<endl;
}
Teacher_Cadre::Teacher_Cadre(string man,char se,int ag,string add,string te,string til,string po,double wag):Teacher(man,se,ag,add,te,til),Cadre(man,se,ag,add,te,po){
wages = wag;
}
void Teacher_Cadre::show(){
Teacher::display();
cout <<Cadre::post <<' '<<wages<<endl;
}
int main(){
Teacher_Cadre ca("xiaowang",'n',23,"xinhuadalu","1234565432","资深教师","书记",10000.0);
ca.show();
return 0;
}