实验六继承与派生之欧阳歌谷创编.docx
《实验六继承与派生之欧阳歌谷创编.docx》由会员分享,可在线阅读,更多相关《实验六继承与派生之欧阳歌谷创编.docx(20页珍藏版)》请在冰点文库上搜索。
实验六继承与派生之欧阳歌谷创编
继承与组合
欧阳歌谷(2021.02.01)
一、实验目的
1.了解继承在面向对象程序设计中的重要作用。
2.进一步理解继承与派生的概念。
3.掌握通过继承派生出一个新的类的方法。
4.了解虚基类的作用和用法。
5.掌握类的组合
二、实验内容
1.请先阅读下面的程序,写出程序运行的结果,然后再上机运行程序,验证自己分析的结果是否正确。
(1)
#include
usingnamespacestd;
classA
{public:
A(){cout<<"A:
:
A()called.\n";}
virtual~A(){cout<<"A:
:
~A()called.\n";}
};
classB:
publicA
{public:
B(inti)
{cout<<"B:
:
B()called.\n";
buf=newchar[i];
}
virtual~B()
{delete[]buf;
cout<<"B:
:
~B()called.\n";
}
private:
char*buf;
};
voidfun(A*a)
{cout<<"Mayyousucceed!
"<deletea;
}
intmain()
{
A*a=newB(15);
fun(a);
return0;
}
1、
(1)程序运行结果:
A:
:
A()called.
B:
:
B()called.
Mayyousucceed!
B:
:
~B()called.
A:
:
~A()called.
(2)
#include
usingnamespacestd;
classA{
public:
A(inta,intb):
x(a),y(b){cout<<"Aconstructor..."<voidAdd(inta,intb){x+=a;y+=b;}
voiddisplay(){cout<<"("<~A(){cout<<"destructorA..."<private:
intx,y;
};
classB:
privateA{
private:
inti,j;
AAobj;
public:
B(inta,intb,intc,intd):
A(a,b),i(c),j(d),Aobj(1,1){cout<<"Bconstructor..."<voidAdd(intx1,inty1,intx2,inty2)
{
A:
:
Add(x1,y1);
i+=x2;j+=y2;
}
voiddisplay(){
A:
:
display();
Aobj.display();
cout<<"("<
}
~B(){cout<<"destructorB..."<};
intmain()
{
Bb(1,2,3,4);
b.display();
b.Add(1,3,5,7);
b.display();
return0;
}
(2)程序运行结果:
Aconstructor...
Aconstructor...
Bconstructor...
(1,2)(1,1)(3,4)
(2,5)(1,1)(8,11)
destructorB...
destructorA...
destructorA...
(3)
#include
usingnamespacestd;
classA{
public:
A(inta):
x(a){cout<<"Aconstructor..."<intf(){return++x;}
~A(){cout<<"destructorA..."<private:
intx;
};
classB:
publicvirtualA{
private:
inty;
AAobj;
public:
B(inta,intb,intc):
A(a),y(c),Aobj(c){cout<<"Bconstructor..."<intf(){
A:
:
f();
Aobj.f();
return++y;
}
voiddisplay(){cout<:
f()<<"\t"<~B(){cout<<"destructorB..."<};
classC:
publicB{
public:
C(inta,intb,intc):
B(a,b,c),A(0){cout<<"Cconstructor..."<};
classD:
publicC,publicvirtualA{
public:
D(inta,intb,intc):
C(a,b,c),A(c){cout<<"Dconstructor..."<~D(){cout<<"destructorD..."<};
intmain()
{
Dd(7,8,9);
d.f();
d.display();
return0;
}
(3)程序运行结果:
Aconstructor...9
Aconstructor...9
Bconstructor...9
Cconstructor...
Dconstructor...
121211
destructorD...
destructorB...
destructorA...
destructorA...
(4)
#include
usingnamespacestd;
classBase1
{
public:
Base1()
{
cout<<"classBase1!
"<}
};
classBase2
{
public:
Base2()
{
cout<<"classBase2!
"<}
};
classLevel1:
publicBase2,virtualpublicBase1
{
public:
Level1()
{
cout<<"classLevel1!
"<}
};
classLevel2:
publicBase2,virtualpublicBase1
{
public:
Level2()
{
cout<<"classLevel2!
"<}
};
classTopLevel:
publicLevel1,virtualpublicLevel2
{
public:
TopLevel()
{
cout<<"classTopLevel!
"<}
};
intmain()
{
TopLevelobj;
return0;
}
(4)程序运行结果:
classBase1!
classBase12
classLeve12!
classBase2!
classLeve11!
classTopLeve1!
2.某出版系统发行图书和磁带,利用继承设计管理出版物的类。
要求如下:
建立一个基类Publication存储出版物的标题title、出版物名称name、单价price及出版日期date。
用Book类和Tape类分别管理图书和磁带,它们都从Publication类派生。
Book类具有保存图书页数的数据成员page,Tape类具有保存播放时间的数据成员playtime。
每个类都有构造函数、析构函数,且都有用于从键盘获取数据的成员函数inputData(),用于显示数据的成员函数display()。
2、
#include
usingnamespacestd;
#include
classDate
{public:
Date(){}
Date(inty,intm,intd);
Date(Date&d);
voidSetDate(inty,intm,intd);
voidShowDate();
private:
intyear,month,day;
};
Date:
:
Date(inty,intm,intd){year=y;month=m;day=d;}
Date:
:
Date(Date&d){year=d.year;month=d.month;day=d.day;}
voidDate:
:
SetDate(inty,intm,intd)
{year=y;month=m;day=d;}
voidDate:
:
ShowDate()
{cout<classTime
{public:
Time(){}
Time(inth,intm,ints);
Time(Time&t);
voidSetTime(inth,intm,ints);
voidShowTime();
private:
inthour,minute,second;
};
Time:
:
Time(inth,intm,ints){hour=h;minute=m;second=s;}
Time:
:
Time(Time&t){hour=t.hour;minute=t.minute;second=t.second;}
voidTime:
:
SetTime(inth,intm,ints){hour=h;minute=m;second=s;}
voidTime:
:
ShowTime(){cout<"<"<classPublication
{public:
Publication(){}
Publication(stringtitle,stringname,floatprice,inty,intm,intd);
voidinputData();
voiddisplay();
private:
stringtitle;
stringname;
floatprice;
Datedate;
};
Publication:
:
Publication(stringtitle,stringname,floatprice,inty,intm,intd)
:
title(title),name(name),price(price),date(y,m,d){}
voidPublication:
:
inputData()
{cout<<"Pleaseinputtitle,name,price,publicationdateofabook:
"<cin>>title>>name>>price;
intyear,month,day;
cin>>year>>month>>day;
date.SetDate(year,month,day);
}
voidPublication:
:
display()
{cout<<"title="<
cout<<"name="<cout<<"price="<cout<<"date=";
date.ShowDate();
}
classBook:
publicPublication
{public:
Book(){}
Book(stringtitle,stringname,floatprice,inty,intm,intd,intpage);
voidinputData();
voiddisplay();
private:
intpage;
};
Book:
:
Book(stringtitle,stringname,floatprice,inty,intm,intd,intpage)
:
Publication(title,name,price,y,m,d){this->page=page;}
voidBook:
:
inputData()
{Publication:
:
inputData();
cout<<"Pleasethepagesofthebook:
"<cin>>page;
}
voidBook:
:
display()
{Publication:
:
display();
cout<<"page="<}
classTape:
publicPublication
{public:
Tape(){}
Tape(stringtitle,stringname,floatprice,inty,intm,intd,Timeplaytime);
voidinputData();
voiddisplay();
private:
Timeplaytime;
};
Tape:
:
Tape(stringtitle,stringname,floatprice,inty,intm,intd,Timeplaytime)
:
Publication(title,name,price,y,m,d),playtime(playtime){}
voidTape:
:
inputData()
{Publication:
:
inputData();
cout<<"Pleasetheplaytimeofthetape:
"<inthour,minute,second;
cin>>hour>>minute>>second;
playtime.SetTime(hour,minute,second);
}
voidTape:
:
display()
{Publication:
:
display();
cout<<"playtime=";
playtime.ShowTime();
}
intmain()
{Bookbook1("教材","C++程序设计",30.00,2009,6,1,300);
book1.display();
cout<Timetime1(30,10,20);
Tapetape1("磁带","C++程序设计视频",10.00,2009,8,1,time1);
tape1.display();
Bookbook2;
book2.inputData();
book2.display();
cout<Tapetape2;
tape2.inputData();
tape2.display();
return0;
}
3.分别定义教师类Teacher和干部类Cadre,采用多重继承的方式由这两个类派生出新类Teacher_Cadre(教师兼干部类)。
要求:
(1)在两个基类中都包含姓名、年龄、性别、地址、电话数据成员。
(2)在Teacher类中还包含数据成员职称title,在Cadre类中还包含数据成员职务post,在Teacher_Cadre类中还包含数据成员工资wage。
(3)对两个基类中的姓名、年龄、性别、地址、电话数据成员用相同的名字,在访问这类数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、地址、电话,然后再用cout语句输出职务和工资。
3、
#include
#include
usingnamespacestd;
classTeacher
{public:
Teacher(stringnam,inta,chars,stringtit,stringad,stringt);
voiddisplay();
protected:
stringname;
intage;
charsex;
stringtitle;
stringaddr;
stringtel;
};
Teacher:
:
Teacher(stringnam,inta,chars,stringtit,stringad,stringt):
name(nam),age(a),sex(s),title(tit),addr(ad),tel(t){}
voidTeacher:
:
display()
{cout<<"name:
"<cout<<"age"<cout<<"sex:
"<cout<<"title:
"<
cout<<"address:
"<cout<<"tel:
"<}
classCadre
{public:
Cadre(stringnam,inta,chars,stringp,stringad,stringt);
voiddisplay();
protected:
stringname;
intage;
charsex;
stringpost;
stringaddr;
stringtel;
};
Cadre:
:
Cadre(stringnam,inta,chars,stringp,stringad,stringt):
name(nam),age(a),sex(s),post(p),addr(ad),tel(t){}
voidCadre:
:
display()
{cout<<"name:
"<cout<<"age:
"<cout<<"sex:
"<cout<<"post:
"<cout<<"address:
"<cout<<"tel:
"<}
classTeacher_Cadre:
publicTeacher,publicCadre
{public:
Teacher_Cadre(stringnam,inta,chars,stringtit,stringp,stringad,stringt,floatw);
voidshow();
private:
floatwage;
};
Teacher_Cadre:
:
Teacher_Cadre(stringnam,inta,chars,stringt,stringp,stringad,stringtel,floatw):
Teacher(nam,a,s,t,ad,tel),Cadre(nam,a,s,p,ad,tel),wage(w){}
voidTeacher_Cadre:
:
show()
{Teacher:
:
display();
cout<<"post:
"<:
post<cout<<"wages:
"<}
intmain()
{Teacher_Cadrete_ca("Wang-li",50,'f',"prof.","president","135BeijingRoad,Shanghai","(021)61234567",1534.5);
te_ca.show();
return0;
}
4.按下列要求编写程序。
(1)定义一个分数类Score。
它有
3个数据成员:
Chinese//语文课成绩
English//英语课成绩
Mathematics//数学课成绩
2个构造函数:
无参的和带参数的
3个成员函数:
是否带参数根据需要自定
sum()//计算三门课总成绩
print()//输出三门课成绩和总成绩
modify()//修改三门课成绩
(2)定义一个学生类Student。
它有
3个数据成员:
Num//学号
Name//姓名
MyScore//成绩
2个构造函数:
无参的和带参数的
3个成员函数:
是否带参数根据需要自定
sum()//计算某学生三门课总成绩
print()//输出某学生学号、姓名和成绩
modify()//修改某学生学号、姓名和成绩
(3)在主函数中,先定义一个学生类对象数组,再通过for循环给对象数组赋上实际值,最后输出对象数组个元素的值。
4、
#include
#include
usingnamespacestd;
classscore
{
public:
score()
{
chinese=0;
english=0;
mathematics=0;
}
score(floatch,floaten,floatma):
chinese(ch),english(en),mathematics(ma){}
floatsum();
voidprint();
voidmodify();
private:
floatchinese;
floatenglish;
floatmathematics;
};
classstudent
{
public:
student():
myscore(0,0,0)
{
num="000000000";
name="无";
}
student(stringn,stringna,floatm,floatc,floate):
num(n),name(na),myscore(m,c,e){}
floatsum();
voidprint();
voidmodify();
private:
stringnum;
stringname;
scoremyscore;
};