C++实验题---填空(类与继承A)
时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte
总提交:113 测试通过:92
总提交:113 测试通过:92
描述
仔细阅读以下程序,理解程序的主要功能,正确补充空白处的语句,空白处可能是一条语句或多条语句,也可能是一个表达式,严禁改动给出程序代码的段落、修改、移动、注释掉给出的程序代码,若有改动、修改或注释掉给出程序的任何一处该题记0分。并编译运行测试通过后进行提交,否则提交错一次扣1分,扣完为止。
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(char *nam,char s,int a)
{strcpy(name,nam);sex=s;age=a;}
protected:
char name[20];
char sex;
int age;
};
__________________
{
public:
Teacher(char *nam,char s,int a,char *t):Person(nam,s,a)
{strcpy(title,t); }
protected:
char title[10];
};
class Student:__________________
{
public:
Student(char *nam,char s,int a,float sco): Person(nam,s,a),score(sco){}
protected:
float score;
};
class Graduate:public Teacher,public Student
{
public:
Graduate(char *nam,char s,int a,char *t,float sco,float w):
Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco),wage(w){}
__________________
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"score:"<<score<<endl;
cout<<"title:"<<title<<endl;
cout<<"wages:"<<wage<<endl;
}
private:
float wage;
};
int main( )
{
char name[20],title[10],sex;int age;float score,wage;
while(cin>>name>>sex>>age>>title>>score>>wage)
{
Graduate grad1(name,sex,age,title,score,wage);
grad1.show( );
}
return 0;
}
输入
输入有多组测试数据,每行为一组测试数据,且用空格隔开个数据。
输出
根据程序代码输出相应的信息。
样例输入
Wangli f 24 assistant 89.5 1234.5
样例输出
name:Wangli
age:24
sex:f
score:89.5
title:assistant
wages:1234.5
age:24
sex:f
score:89.5
title:assistant
wages:1234.5
代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
class Person
{
public:
Person(char *nam,char s,int a)
{
strcpy(name,nam);
sex=s;age=a;
}
protected:
char name[20];
char sex;
int age;
};
class Teacher:virtual public Person
{
public:
Teacher(char *nam,char s,int a,char *t):Person(nam,s,a)
{
strcpy(title,t);
}
protected:
char title[10];
};
class Student:virtual public Person
{
public:
Student(char *nam,char s,int a,float sco):Person(nam,s,a),score(sco){}
protected:
float score;
};
class Graduate:public Teacher,public Student
{
public:
Graduate(char *nam,char s,int a,char *t,float sco,float w):
Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco),wage(w){}
void show()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"score:"<<score<<endl;
cout<<"title:"<<title<<endl;
cout<<"wages:"<<wage<<endl;
}
private:
float wage;
};
int main( )
{
char name[20],title[10],sex;int age;float score,wage;
while(cin>>name>>sex>>age>>title>>score>>wage)
{
Graduate grad1(name,sex,age,title,score,wage);
grad1.show( );
}
return 0;
}