当前位置:文档之家› 编程题2_数据及教师学生类

编程题2_数据及教师学生类

Name:Liuling
sal=800
id=Yz040318
dn=Society Department
参考源程序代码
#include <iostream>
#include <string>
using namespace std;
class Data
{
public:
Data(char *na)
{
name = new char[strlen(na)+1];
名称
备注
描述
题目描述:
有五个类,各类的主要数据成员见下方说明,请自己定义合理的成员函数,编程实现体现该继承关系的程序,并定义教师对象、学生对象、研究生对象、在职研究生对象,输出他们的信息。
关于数据成员:
数据类Data:成员name保存姓名;//该类是Teacher类和Student类的基类
教师类Teacher:增加成员sal保存工资;
测试用例,无输入部分:
输出部分:
the teacher:
Name:Zhuhong
sal=2000
the student:
Name:Wanghui
id=B05030417
the postgraduate:
Name:Lixuefeng
id=Yj040217
dn=Computer Department
the teacher and postgraduate:
{
public:
Tpost(char *na, char *pid, char *p, float psal): Data(na), Teacher(na, psal), Postgrad(na,pid,p)
{ };
void print( )
{
Teacher::print( );
cout << "id=" << id << endl;
cout << "dn=" << dn << endl;
}
};
int main( )
{
Teacher tobj("Zhuhong", 2000);
Student sobj("Wanghui", "B05030417");
Postgrad pobj("Lixuefeng", "Yj040217", "Computer Department");
主函数代码如下:
int main ( )
{
Teacher tobj("Zhuhong", 2000);
Student sobj("Wanghui", "B05030417");
PostGrad pobj("Lixuefeng", "Yj040217", "Computer Department");
strcpy(name, na);
}
~Data( )
{
delete [ ] name;
}
void print( )
{
cout << "Name:" << name << endl;
}
protected:
char *name;
};
class Student: virtual public Data
{
public:
Tpost tpobj("Liuling", "Yz040318", "Society Department", 800);
cout << "the teacher:\n";
tobj.print ( ) ;
cout << "\nthe student:\n";
sobj.print ( ) ;
cout << "\nthe postgraduate:\n";
Student(char *na, char *pid): Data(na)
{
id = new char[strlen(pid)+1];
strcpy(id, pid);
}
~Student( )
{
delete [ ] id;
}
void print( )
{
Data::nt( );
cout << "id=" << id << endl;
}
protected:
char *id;
};
class Teacher: virtual public Data
{
public:
Teacher(char *na, float psal): Data(na)
{
sal = psal;
}
void print( )
{
Data::print( );
cout << "sal=" << sal << endl;
strcpy(dn, p);
}
~Postgrad( )
{
delete [ ] dn;
}
void print( )
{
Student::print( );
cout << "dn=" << dn << endl;
}
};
class Tpost: public Teacher, public Postgrad
Tpost tpobj("Liuling", "Yz040318", "Society Department", 800);
cout << "the teacher:\n";
tobj.print( );
cout << "\nthe student:\n";
sobj.print( );
}
protected:
float sal;
};
class Postgrad: public Student
{
protected:
char *dn;
public:
Postgrad(char *na, char *pid, char *p): Data(na), Student(na, pid)
{
dn = new char[strlen(p)+1];
pobj.print ( ) ;
cout << "\nthe teacher and postgraduate:\n";
tpobj.print ( ) ;
return 0;
}
提示:(1)最前面需要作相应的文件包含
(2)请注意类之间的继承关系,可能存在虚基类
在保存并预览之后,点开:“测试用例”选项卡,再选择下方的“添加”,加入两组测试
学生类Student:增加成员id保存学号;//该类是PostGrad类的基类
研究生类PostGrad:增加成员dn保存系别;
教师中的在职研究生类Tpost:不另外定义数据成员。//该类有两个直接基类:Teacher类和研究生类PostGrad
关于成员函数:
每个类中均定义输出所有数据成员的函数void print ( )。具体代码可参考主函数及对应的输出结果样例来完成。
相关主题