十六周 课后作业 虚函数和多态性.docx

上传人:b****2 文档编号:3143423 上传时间:2023-05-05 格式:DOCX 页数:22 大小:28.05KB
下载 相关 举报
十六周 课后作业 虚函数和多态性.docx_第1页
第1页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第2页
第2页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第3页
第3页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第4页
第4页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第5页
第5页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第6页
第6页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第7页
第7页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第8页
第8页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第9页
第9页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第10页
第10页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第11页
第11页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第12页
第12页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第13页
第13页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第14页
第14页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第15页
第15页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第16页
第16页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第17页
第17页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第18页
第18页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第19页
第19页 / 共22页
十六周 课后作业 虚函数和多态性.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

十六周 课后作业 虚函数和多态性.docx

《十六周 课后作业 虚函数和多态性.docx》由会员分享,可在线阅读,更多相关《十六周 课后作业 虚函数和多态性.docx(22页珍藏版)》请在冰点文库上搜索。

十六周 课后作业 虚函数和多态性.docx

十六周课后作业虚函数和多态性

十六周课后作业虚函数和多态性

【代码练习】

1、给名为Base的抽象基类编写头文件。

其中包含一个虚析构函数和一个虚print函数。

2、给以公有模式从Base类中继承而来的名为Derived的类编写头文件。

Derived类中具有一个整数作为其私有数据成员,同时应具有一个print函数。

3、给类Derived的被覆盖的print方法编写实现,将其私有数据成员打印出来。

4、对名为Base2的基类进行定义,在Base2中,包含一个名为function1的虚函数和一个名为function2的纯虚函数。

【程序输出练习】

设有如下的继承关系图:

其中Employees是抽象基类,包含数据成员:

firstName,lastName。

虚成员函数virtualvoidEmployee:

:

print(){cout<

纯虚函数virtualdoubleEmployee:

:

earnings()const=0;

SalariedEmployee公有继承Employee,新增数据成员:

weeklySalary

doubleSalariedEmployee:

:

earnings()const{returnweeklySalary;}

voidSalariedEmployee:

:

print()

{

cout<<”salariedemployee:

“;

Employee:

:

print();

cout<<”\nweeklysalary:

“<

}

HourlyEmployee公有继承Employee,新增数据成员:

wage(每小时工资),hours(每周工作多少小时)

doubleHourlyEmployee:

:

earnings()const

{

if(hours<=40)

returnwage*hours;

else

return40*wage+(hours-40)*wage*1.5;

}

voidHourlyEmployee:

:

print()const

{

cout<<”hourlyemployee:

“;

Employee:

:

print();

cout<<”\nhourlywage:

“<

“;hoursworked:

”hours;

}

CommissionEmployee公有继承Employee,新增数据成员:

grossSales(销售总额),commissionRate(提成比例)

doubleCommissionEmployee:

:

earnings()const

{

returngrossSales*commissionRate;

}

voidCommissionEmployee:

:

print()const

{

cout<<”commissionemployee:

“;

Employee:

:

print();

cout<<”\ngrosssales:

“<

<<”;commissionrate:

“<

}

BasePlusCommissionEmployee公有继承CommissionEmployy,新增数据成员:

baseSalary(基本工资)

doubleBasePlusCommissionEmployee:

:

earnings()const

{

returnbaseSalary+CommissionEmployee:

:

earnings();

}

voidBasePlusCommissionEmployee:

:

print()const

{

cout<<”base-salaried“;

CommissionEmployee:

:

print();

cout<<”;basesalary:

“<

}

1、写出下列程序的运行结果。

intmain()

{

BasePlusCommissionEmployeeb("john","Smith",5000,.04,300);

CommissionEmployeec("Sue","Jones",10000,.06);

SalariedEmployeep("Bob","Lewis",800);

HourlyEmployeeh("Karen","Price",16.75,40);

Employee*Container[4];

Container[0]=&b;

Container[1]=&c;

Container[2]=&p;

Container[3]=&h;

for(inti=0;i<4;i++)

{

Container[i]->print();

cout<<"earned$"<earnings()

<

}

return0;

}

2、以下程序段的输出结果是什么?

voidprintPtr(constEmployee*);

voidprintRef(constEmployee&);

intmain()

{

BasePlusCommissionEmployeeb(“John”,“Smith”,200.0,3.0,150);

printPtr(&b);

CommissionEmployeec(“Sue”,“Jones”,200,2.5);

printRef(c);

SalariedEmployeep(“Bob”,“Lewis”,800.00);

p.print();

cout<<“earned$“<

HourlyEmployeeh(“Karen”,“Price”,13.75,40);

printPtr(&h);

printRef(h);

cout<

return0;

}

voidprintPtr(constEmployee*basePtr)

{

basePtr->print();

cout<<”earned$”<earnings();

}

voidprintRef(constEmployee&baseRef)

{

baseRef.print();

cout<<”earned$”<

}

【程序调试】

实例输出:

错误的代码:

共7个程序文件(Animal.hAnimal.cpplion.hlion.cppdog.hdog.cppdebug.cpp)

*************************************************************************

//Animal.h

//Animal.h:

interfacefortheAnimalclass.

//

//////////////////////////////////////////////////////////////////////

#if!

defined(AFX_ANIMAL_H__23AFCE04_43D0_444D_858A_234D5FCF626D__INCLUDED_)

#defineAFX_ANIMAL_H__23AFCE04_43D0_444D_858A_234D5FCF626D__INCLUDED_

#if_MSC_VER>1000

#pragmaonce

#endif//_MSC_VER>1000

classAnimal

{

public:

//char*getName()const;

voidsetWeight(int);

voidsetHeight(int);

virtualintgetWeight()const=0;

virtualintgetHeight()const=0;

virtualvoidprint()const=0;

Animal(constint=0,constint=0);

private:

intweight;

intheight;

};

#endif//!

defined(AFX_ANIMAL_H__23AFCE04_43D0_444D_858A_234D5FCF626D__INCLUDED_)

************************************************************************

//Animal.cpp

//Animal.cpp:

implementationoftheAnimalclass.

//

//////////////////////////////////////////////////////////////////////

#include

usingnamespacestd;

#include"Animal.h"

//////////////////////////////////////////////////////////////////////

//Construction/Destruction

//////////////////////////////////////////////////////////////////////

Animal:

:

Animal(constinth,constintw)

{

height=h;

weight=w;

}

virtualvoidAnimal:

:

print()const

{

cout<<"Thisanimal'sheightandweightareasfollows\n"

<<"Height:

"<

"<

<

}

intAnimal:

:

getHeight()const

{

returnheight;

}

intAnimal:

:

getWeight()const

{

returnweight;

}

virtualvoidAnimal:

:

setHeight(constinth)

{

height=h;

}

virtualvoidAnimal:

:

setWeight(constintw)

{

weight=w;

}

**********************************************************************

//lion.h

//Lion.h:

interfacefortheLionclass.

//

//////////////////////////////////////////////////////////////////////

#if!

defined(AFX_LION_H__BA30A7B0_7A22_4CBE_8C67_C6A2B7369253__INCLUDED_)

#defineAFX_LION_H__BA30A7B0_7A22_4CBE_8C67_C6A2B7369253__INCLUDED_

#if_MSC_VER>1000

#pragmaonce

#endif//_MSC_VER>1000

#include"animal.h"

classLion:

publicAnimal

{

public:

Lion(constint=0,constint=0);

virtualvoidprint()const;

};

#endif//!

defined(AFX_LION_H__BA30A7B0_7A22_4CBE_8C67_C6A2B7369253__INCLUDED_)

*********************************************************************

//lion.cpp

//Lion.cpp:

implementationoftheLionclass.

//

//////////////////////////////////////////////////////////////////////

#include

usingnamespacestd;

#include"Lion.h"

//////////////////////////////////////////////////////////////////////

//Construction/Destruction

//////////////////////////////////////////////////////////////////////

Lion:

:

Lion(constinth,constintw)

:

Animal(h,w){}

voidLion:

:

print()const

{

cout<<"Thisanimalisalion\n";

Animal:

:

print();

}

******************************************************************************

//dog.h

//Dog.h:

interfacefortheDogclass.

//

//////////////////////////////////////////////////////////////////////

#if!

defined(AFX_DOG_H__20FB4370_AAE5_42B3_A67C_B6E7394399AA__INCLUDED_)

#defineAFX_DOG_H__20FB4370_AAE5_42B3_A67C_B6E7394399AA__INCLUDED_

#if_MSC_VER>1000

#pragmaonce

#endif//_MSC_VER>1000

#include"Animal.h"

classDog:

publicAnimal

{

public:

voidsetName(constchar*);

virtualvoidprint()const;

virtualvoidgetHeight()const=0;

virtualvoidgetWeight()const=0;

constchar*getName()const;

Dog(constint=0,constint=0,constchar*="Toto");

private:

charname[30];

constbooluseMetric()const;

intmetricHeight;

intmetricWeight;

};

#endif//!

defined(AFX_DOG_H__20FB4370_AAE5_42B3_A67C_B6E7394399AA__INCLUDED_)

**********************************************************************

//dog.cpp

//Dog.cpp:

implementationoftheDogclass.

//

//////////////////////////////////////////////////////////////////////

#include

usingnamespacestd;

#include"Dog.h"

#include

//////////////////////////////////////////////////////////////////////

//Construction/Destruction

//////////////////////////////////////////////////////////////////////

Dog:

:

Dog(constinth,constintw,constchar*n)

:

Animal(h,w)

{

strcpy(name,n);

metricHeight=h*2.5;

metricWeight=w/2.2;

}

constchar*Dog:

:

getName()const{returnname;}

voidDog:

:

setName(constchar*n){strcpy(name,n);}

voidDog:

:

print()const

{

cout<<"Thisanimalisadog,itsnameis:

"

<

Animal:

:

print();

}

intDog:

:

getHeight()

{

if(useMetric())

returnmetricHeight;

else

returnAnimal:

:

getHeight();

}

intDog:

:

getWeight()

{

if(useMetric())

returnmetricWeight;

else

returnAnimal:

:

getWeight();

}

constboolDog:

:

useMetric()const

{

intchoice=0;

cout<<"Whichunitswouldyouliketoseetheweightin?

"

<<"(Enter1or2)\n"

<<"\t1.metric\n"

<<"\t2.standard\n";

cin>>choice;

if(choice==1)

returntrue;

else

returnfalse;

}

**********************************************************************

//debug.cpp

#include

usingnamespacestd;

#include"animal.h"

#include"lion.h"

#include"dog.h"

voidsetHeightWeight(Animal);

intmain()

{

Dogd1(60,120,"Fido");

Lionlion1(45,300);

setHeightWeight(lion1);

setHeightWeight(d1);

return0;

}

voidsetHeightWeight(Animala)

{

inth,w;

a->print();

cout<<"Enteranewheight(usingstandardunits):

";

cin>>h;

a->setHeight(h);

cout<<"Enteranewweight(usingstandardunits):

";

cin>>w;

a->setWeight(w);

cout<<"Herearethenewheightandweightvalues:

\n"

<getHeight()<

<getWeight()<

}

阅读如下程序,试写出程序运行结果。

1、

#include

usingnamespacestd;

classA

{

public:

A(){cout<<“constructorofA“;foo();}

~A(){}

virtualvoidfoo(){cout<<“A:

:

foo()iscalled”<

};

classB:

publicA

{

public:

virtualvoidfoo(){cout<<“B:

:

foo()iscalled”<

};

voidtestFun(A*a)

{a->foo();}

intmain()

{

Ab,*a=newB();

cout<<“beginninginmain:

”<

a->foo();

b.foo();

testFun(a);

deletea;

return0;

}

2、

#include

usingnamespacestd;

classA

{

intm;

public:

A(intnM):

m(nM){}

intF(){returnm;}

};

classB:

publicA

{

public:

B(intnM):

A(nM){}

virtualintF(){return0;}

};

intmain()

{

A*p=newB(5);

cout<<(*p).F()<

return0;

}

3、

#include

usingnamespacestd;

classA

{

public:

virtualvoidF(){cout<<“A:

:

F()”<

voidG(){cout<<“A:

:

G()\n”;}

voidH(){G();}

};

classB:

publicA

{

public:

voidF(){cout<<“B:

:

F()”<

voidG(){coug<<“B:

:

G()\n”;}

};

classC:

publicB

{

public:

voi

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

当前位置:首页 > 工程科技 > 能源化工

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

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