实验4.docx
《实验4.docx》由会员分享,可在线阅读,更多相关《实验4.docx(10页珍藏版)》请在冰豆网上搜索。
实验4
面向对象程序设计C++实验报告书
班级:
姓名:
学号:
课程名称
面向对象程序设计C++
实验项目
实验四、类的继承与派生
实验项目类型
验证
演示
综合
设计
指导教师
项宝卫
成绩
√
一、实验目的
1.掌握继承与派生的关系及方法;
2.在派生类构造函数中初始化基类成员;
3.多重继承的方法。
二、实验步骤
1、定义1个CPerson类,其属性有:
姓名、性别和年龄。
2、从CPerson类派生出CStudent类,增加属性:
学号、入学时间和入学成绩;
3、从CPerson类派生出CTeacher类,添加属性:
职务、部门和工作时间;
4、由CStudent类派生出CGraduate类,添加属性:
研究方向和导师;
5、由CGraduate和CTeacher共同派生出在职研究生类CGradonWork
6、对上述类分别定义其中的构造函数和输出函数,并在主函数中定义对象进行测试,测试的代码大致如下:
CGradonWorkgradonWork("tzcer","男",33,"15","2003",335,"computer","tangmin","jsj","sxxy","2002-8");
gradonWork.Print();
其中CGradonWork的构造函数参数分别对应的属性是一个在职的研究生对象,其姓名为tzcer,性别为男,33岁,学号15,2003年读在职,入学成绩335,就读计算机方向,其导师tangmin,同时tzcer也是sxxy部门,jsj专业的老师,2002-8月入职。
要求记录输出信息并说明实验步骤和解决方法!
7、问题:
何为继承二义性,在本实验中哪个地方存在二义性,你是如何解决的?
三、上机过程原始记录(源程序等)
1、定义1个CPerson类,其属性有:
姓名、性别和年龄。
class person{
char Name[10];
char Sex;
int Age;
public:
person(char *name,int age,char sex)
{
strcpy(Name,name);
Age=age;
Sex=(sex=='m'?
'm':
'f');
}
virtual void set()
{
int age;
char name[10];
char sex;
cout<<"请输入姓名:
"<cin>>name;
strcpy(Name,name);
cout<<"请输入年龄:
"<cin>>age;
Age=age;
cout<<"请输入性别:
"<cin>>sex;
Sex=sex;
}
virtual void showinfo()
{
cout<cout<system("pause");
}
};
2、从CPerson类派生出CStudent类,增加属性:
学号、入学时间和入学成绩;
class student:
public person
{
int number;//定义学好
int time;//定义入学时间
float score1; //定义入学成绩
public:
student(int num,int time,float sco1,char *name,int age,char sex):
person(name,age,sex)
{
score1=sco1;
}
void set()
{
int sco[3];
person:
:
set();
cout<<"请输入学号:
";
cin>>number;
number=number;
cout<<"请输入入学时间:
";
cin>>time;
time=time;
cout<<"请输入入学成绩:
";
cin>>sco[0];
score1=sco[0];
}
void showinfo()
{
cout<}
};
3、从CPerson类派生出CTeacher类,添加属性:
职务、部门和工作时间;
class teacher:
public person
{
char title[30];//定义职称
char dept[30];//定义部门
int length;//定义工作时间
public:
teacher(char *tit,char *de,int len,char *name,char sex,int age):
person(name,age,sex)
{
strcpy(title,tit);
strcpy(dept,de);
length=len;
}
void set()
{
char tit[30];//定义职称
char de[30];//定义部门
int len;//定义工作时间
person:
:
set();
cout<<"职称:
"<cin>>tit;
strcpy(title,tit);
cout<<"部门:
"<cin>>de;
strcpy(dept,de);
cout<<"工作时间:
"<cin>>len;
length=len;
}
void showinfo()
{
cout<
}
};
4、由CStudent类派生出CGraduate类,添加属性:
研究方向和导师;
class graduate:
public student
{
char RFields[30];//定义研究方向
char tutor[30];//定义导师
public:
graduate(char *RF,char *tu,int *number,int time,float score1):
student(number,time,score1)
{
strcpy(RFields,RF);
strcpy(tutor,tu);
}
void set()
{
char RF[30];//定义研究方向
char tu[30];//定义导师
student:
:
set();
cout<<"研究方向:
"<cin>>RF;
strcpy(RFields,RF);
cout<<"导师:
"<cin>>tu;
strcpy(tutor,tu);
}
void showinfo()
{
cout<}
};
5、由CGraduate和CTeacher共同派生出在职研究生类CGradonWork
class GradonWork:
public Graduate,public Teacher
{
};
6、对上述类分别定义其中的构造函数和输出函数,并在主函数中定义对象进行测试,测试的代码大致如下:
CGradonWork gradonWork("tzcer","男",33,"15","2003",335,"computer","tangmin","jsj","sxxy","2002-8");
gradonWork.Print();
其中CGradonWork 的构造函数参数分别对应的属性是一个在职的研究生对象,其姓名为tzcer,性别为男,33岁,学号15,2003年读在职,入学成绩335,就读计算机方向,其导师tangmin,同时tzcer也是sxxy部门,jsj专业的老师,2002-8月入职。
要求记录输出信息并说明实验步骤和解决方法!
主要代码:
#include
#include
#include
#include
class person{
char Name[10];
char Sex;
int Age;
public:
person(char *name,int age,char sex)
{
strcpy(Name,name);
Age=age;
Sex=(sex=='m'?
'm':
'f');
}
virtual void set()
{
int age;
char name[10];
char sex;
cout<<"请输入姓名:
"<cin>>name;
strcpy(Name,name);
cout<<"请输入年龄:
"<cin>>age;
Age=age;
cout<<"请输入性别:
"<cin>>sex;
Sex=sex;
}
virtual void showinfo()
{
cout<cout<system("pause");
}
};
class student:
public person
{
int number;//定义学好
int time;//定义入学时间
float score1; //定义入学成绩
public:
student(int num,int time,float sco1,char *name,int age,char sex):
person(name,age,sex)
{
score1=sco1;
}
void set()
{
int sco[3];
person:
:
set();
cout<<"请输入学号:
";
cin>>number;
number=number;
cout<<"请输入入学时间:
";
cin>>time;
time=time;
cout<<"请输入入学成绩:
";
cin>>sco[0];
score1=sco[0];
}
void showinfo()
{
cout<}
};
class teacher:
public person
{
char title[30];//定义职称
char dept[30];//定义部门
int length;//定义工作时间
public:
teacher(char *tit,char *de,int len,char *name,char sex,int age):
person(name,age,sex)
{
strcpy(title,tit);
strcpy(dept,de);
length=len;
}
void set()
{
char tit[30];//定义职称
char de[30];//定义部门
int len;//定义工作时间
person:
:
set();
cout<<"职称:
"<cin>>tit;
strcpy(title,tit);
cout<<"部门:
"<cin>>de;
strcpy(dept,de);
cout<<"工作时间:
"<cin>>len;
length=len;
}
void showinfo()
{
cout<
}
};
void hhh()
{
cout<<'\t'<<'\t'<<'\t'<<'\t'<<"1.基类基本情况:
"<cout<<'\t'<<'\t'<<'\t'<<'\t'<<"2.老师类基本情况:
"<cout<<'\t'<<'\t'<<'\t'<<'\t'<<"3.学生类基本情况:
"<cout<<'\t'<<'\t'<<'\t'<<'\t'<<"0.退出"<cout<<'\t'<<'\t'<<'\t'<<'\t'<<"请选择种类(0~3)"<cout<";
}
int main()
{
person *d[3];
person per("wenpiao",20,'f');
student stu(11290033,2011,523,"wenpiao",20,'f');
teacher tea("jiaoshou","dianxin",20,"chenxiaochang",40,'f');
d[0]=&per;
d[1]=&stu;
d[2]=&tea;
int choice;
cout<<'\t'<<'\t'<<'\t'<<'\t'<<"1.基类基本情况:
"<cout<<'\t'<<'\t'<<'\t'<<'\t'<<"2.老师类基本情况:
"<cout<<'\t'<<'\t'<<'\t'<<'\t'<<"3.学生类基本情况:
"<cout<<'\t'<<'\t'<<'\t'<<'\t'<<"0.退出"<cout<<'\t'<<'\t'<<'\t'<<'\t'<<"请选择种类(0~3)"<cout<";
for(;;)
{
cin>>choice;
system("cls");
if (choice>=0&&choice<=3)
switch (choice)
{
case 1:
{
d[choice-1]->set();
d[choice-1]->showinfo();
}
break;
case 2:
{
d[choice]->set();
d[choice]->showinfo();
}
break;
case 3:
{
d[choice-2]->set();
d[choice-2]->showinfo();
}
break;
case 0:
exit(0);
break;
}
else
cout<<"错误输入!
请重新选择:
"<hhh();
}
}
运行记录:
7、问题:
何为继承二义性,在本实验中哪个地方存在二义性,你是如何解决的?
一般说来,在派生类中对基类成员的访问应该是唯一的,但是,由
多继承情况下,可能造成对基类中某成员的访问出现了不唯一的情况,则称为对基类成员访问的二义性问题。
使用基类
class A{
public:
void f();
};
class B{
public:
void f();
void g();
};
class C:
public A,public B;
{
public:
void g();
void h();
};
//这里派生类A和基类B中都有f(),基类B和基类C中都有g() ,所以访问出现二义
使用基类名为避免这种二义:
obj.A:
:
f(); //A中的f()
obj.B:
:
f(); //B中的f()
这种使用基类名来控制成员访问的规则称为支配原则
obj.g(); //隐含用C的g()
obj.B:
:
g(); //用B的g()
四、上机结果及分析
1、
2、
3
.
4.