实验5 继承与派生.docx

上传人:b****3 文档编号:10443661 上传时间:2023-05-25 格式:DOCX 页数:12 大小:17.24KB
下载 相关 举报
实验5 继承与派生.docx_第1页
第1页 / 共12页
实验5 继承与派生.docx_第2页
第2页 / 共12页
实验5 继承与派生.docx_第3页
第3页 / 共12页
实验5 继承与派生.docx_第4页
第4页 / 共12页
实验5 继承与派生.docx_第5页
第5页 / 共12页
实验5 继承与派生.docx_第6页
第6页 / 共12页
实验5 继承与派生.docx_第7页
第7页 / 共12页
实验5 继承与派生.docx_第8页
第8页 / 共12页
实验5 继承与派生.docx_第9页
第9页 / 共12页
实验5 继承与派生.docx_第10页
第10页 / 共12页
实验5 继承与派生.docx_第11页
第11页 / 共12页
实验5 继承与派生.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验5 继承与派生.docx

《实验5 继承与派生.docx》由会员分享,可在线阅读,更多相关《实验5 继承与派生.docx(12页珍藏版)》请在冰点文库上搜索。

实验5 继承与派生.docx

实验5继承与派生

序号:

实验5继承与派生

[实验目的]

1、了解继承在面向对象程序设计中的重要作用;

2、进一步理解继承与派生的概念;

3、学会通过继承派生出一个新类的方法;

[实验要求]

给出以下各实验内容的源程序代码,并把编译、运行过程中出现的问题以及解决方法填入实验报告中,按时上交。

[实验学时]2学时。

[实验内容]

1、声明一个人员类(Person),包括3个数据成员:

name(姓名)、age(年龄)、sex(性别);2个成员函数:

构造函数和输出相关信息的函数display()。

利用单继承的方式声明一个学生(Student)派生类,其中增加2个数据成员:

grade(年级)、score(总学分);3个成员函数:

构造函数、输出函数show()和增加学分的函数add()。

在定义派生类对象时给出初始化的数据,然后输出这些数据。

[源程序]

#include

#include

usingnamespacestd;

classPerson

{

public:

Person(stringna,intag,stringse)

{

name=na;age=ag;sex=se;

}

voiddisplay()

{

cout<<"name:

"<

cout<<"age:

"<

cout<<"sex:

"<

}

private:

stringname;

intage;

stringsex;

};

classStudent:

publicPerson

{

public:

Student(stringna,intag,stringse,stringgr,floatsc):

Person(na,ag,se)

{

grade=gr;score=sc;

}

voidshow()

{

display();

cout<<"grade:

"<

cout<<"score:

"<

}

voidadd();

private:

stringgrade;

floatscore;

};

intmain()

{

Studentstu1("小明",19,"男","广告11A-2",93);

stu1.show();

return0;

}

2、分别定义教师(Teacher)类和干部(Cadre)类,采用多重继承方式由这两个类派生出新类:

教师兼干部(Teacher_Cadre)。

要求:

①在两个基类中都包含姓名、性别、地址、电话等数据成员。

②在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。

③对两个基类中的姓名、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。

④在类体中声明成员函数,在类外定义成员函数。

⑤在派生类Teacher_Cadre的成员函数show()中调用Teacher类中的display()函数,输出姓名、性别、职称、地址、电话,然后再用cout语句输出职务与工资。

[源程序]

#include

#include

usingnamespacestd;

classTeacher

{

public:

Teacher(stringna,stringse,stringti,stringad,longph);

voiddisplay();

protected:

stringname;

stringsex;

stringtitle;

stringaddress;

longphone;

};

Teacher:

:

Teacher(stringna,stringse,stringti,stringad,longph)

{

name=na;

sex=se;

title=ti;

address=ad;

phone=ph;

}

voidTeacher:

:

display()

{

cout<<"姓名:

"<

cout<<"性别:

"<

cout<<"职称:

"<

cout<<"地址:

"<

cout<<"电话:

"<

}

classCadre

{

public:

Cadre(stringna,stringse,stringad,longph,stringpo);

voiddisplay();

protected:

stringname;

stringsex;

stringaddress;

longphone;

stringpost;

};

Cadre:

:

Cadre(stringna,stringse,stringad,longph,stringpo)

{

name=na;

sex=se;

address=ad;

phone=ph;

post=po;

}

voidCadre:

:

display()

{

cout<<"姓名:

"<

cout<<"性别:

"<

cout<<"地址:

"<

cout<<"电话:

"<

cout<<"职务:

"<

}

classTeacherCadre:

publicTeacher,publicCadre

{

public:

TeacherCadre(stringna,stringse,stringad,longph,stringti,stringpo,floatwa):

Teacher(na,se,ti,ad,ph),Cadre(na,se,ad,ph,po)

{

wages=wa;

}

voidshow();

private:

floatwages;

};

voidTeacherCadre:

:

show()

{

Teacher:

:

display();

cout<<"职务:

"<

cout<<"工资:

"<

}

intmain()

{

TeacherCadreTC1("小明","男","北京工商大学",66667748,"硕士","教学校长",8000);

TC1.show();

return0;

}

3、类的组合应用。

现有两个类:

教师(Teacher)类和日期(Date)类。

其中Teacher类中包含数据成员name(姓名)、(sex)性别、(birthday)出生日期(日期类Date的子对象)、title(职称);Date类包含year(年)、month(月)、day(日)等数据成员。

教授(Professor)类是Teacher类的派生类,增加教授级别(grade)数据成员。

各类中都有相应的构造函数、析构函数等成员函数。

请设计各类,并在定义Professor类对象时给出所有数据的初值,然后修改该对象的生日数据,最后输出该对象的全部最新数据。

[源程序]

#include

#include

usingnamespacestd;

classDate

{public:

Date(intye,intmo,intda)

{

year=ye;month=mo;day=da;

}

~Date(){};

voiddisplay()

{

cout<<"出生年、月、日:

"<

}

voidchange1(intyear1,intmonth1,intday1)

{

year=year1;

month=month1;

day=day1;

}

protected:

intyear;

intmonth;

intday;

};

classTeacher

{public:

Teacher(intye,intmo,intda,stringna,stringse,stringti):

birthday(ye,mo,da)

{

name=na;sex=se;title=ti;

}

~Teacher(){};

voiddisplay()

{

cout<<"姓名:

"<

cout<<"性别:

"<

cout<<"职称:

"<

}

protected:

stringname;

stringsex;

Datebirthday;

stringtitle;

};

classProfessor:

publicTeacher

{public:

Professor(intye,intmo,intda,stringna,stringse,stringti,stringgr):

Teacher(ye,mo,da,na,se,ti)

{

grade=gr;

}

~Professor(){};

voidshow()

{

Teacher:

:

display();

birthday.display();

cout<<"教授级别:

"<

}

voidchange(intyear1,intmonth1,intday1)

{

birthday.change1(year1,month1,day1);

}

private:

stringgrade;

};

intmain()

{intyear1,month1,day1;

Professorp1(1962,11,11,"小明","男","教授","中等");

cout<<"原始数据为:

"<

p1.show();

cout<

cout<<"请输入更改的年、月、日:

";

cin>>year1>>month1>>day1;

cout<

"<

p1.change(year1,month1,day1);

p1.show();

return0;

}

 

4、在屏幕上以某一特定点为中心,画弧、画圆、画椭圆、画矩形,这些综合图形可以定义为各种图形的一个多继承派生类,而每一个图形都是Point(点)类的派生类,为了使综合图形类与Point类的一个实例对应,用此实例来表达此图形的共同中心,则将Point类定义为虚基类。

试设计虚基类Point(点)类,以及派生类Circle(圆)类、Rectangle(矩形)类,并用程序验证它们的功能。

[源程序]

#include

#include

classPoint

{

protected:

floatx;

floaty;

public:

Point(floatix,floatiy):

x(ix),y(iy){}

~Point(){}

};

classCircle:

virtualpublicPoint

{

public:

Circle(floatrad,floatix,floatiy):

Point(ix,iy)

{

radius=rad;

}

~Circle(){}

voiddisplay()

{

circle(radius,x,y);

}

private:

floatradius;

}classRect

{

public:

Rect(){};

Rect(inta,intb,intc,intd)

{

left=a;

top=b;

right=c;

bottom=d;}

voiddisplay()

{

rectangle(left,top,right,bottom);

}

~Rect(){};

protected:

intleft;

inttop;

intright;

intbottom;

};

voidmain()

{

inti;

initgraph(640,480);

Circleccc(100,10,100);

ccc.display();

Rectrr(110,112,11,11);

rr.display();

cin>>i;

}

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2