虚函数与多态性实验.docx

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

虚函数与多态性实验.docx

《虚函数与多态性实验.docx》由会员分享,可在线阅读,更多相关《虚函数与多态性实验.docx(14页珍藏版)》请在冰点文库上搜索。

虚函数与多态性实验.docx

虚函数与多态性实验

一.实验目的及要求

1.进一步熟悉类的设计、运用继承与派生机制设计派生类,合理设置数据成员和成员函数。

2.掌握通过继承、虚函数、基类的指针或引用实现动态多态性的方法。

3.理解并掌握具有纯虚函数的抽象类的作用,在各派生类中重新定义各纯虚函数的方法,以及此时实现的动态多态性。

二.实验内容

在自己的文件夹下建立一个名为exp5的工程,在该工程中做如下操作:

定义一个抽象类容器类,其中定义了若干纯虚函数,实现求表面积、体积、输出等功能。

由此抽象类派生出正方体、球体和圆柱体等多个派生类,根据需要定义自己的成员变量,在各个派生类中重新定义各纯虚函数,实现各自类中相应功能,各个类成员的初始化均由本类构造函数实现。

(1)在主函数中,定义容器类的指针和各个派生类的对象,使指针指向不同对象处调用相同的函数能执行不同的函数代码,从而实现动态多态性。

(2)定义一个顶层函数voidTopPrint(Container&r);使得主函数中调用该函数时,根据实在参数所有的类自动调用对应类的输出函数。

(3)主函数中定义一个Container类对象,观察编译时的错误信息,从而得出什么结论

三.实验程序及运行结果

#include

usingnamespacestd;

classBase

{

public:

virtualvoidf(){cout<<"调用Base:

:

f()"<

};

classDerived:

publicBase

{

public:

voidf(){cout<<"调用Derived:

:

f()"<

};

intmain(void)

{

Derivedobj;//定义派生类对象

Base*p=&obj;//基类指针

p->f();//调用函数f()

system("PAUSE");

return0;

}

2.

#include

usingnamespacestd;//

classBase

{

public:

virtualvoidShow()const{cout<<"调用Base:

:

Show()"<

};

classDerived:

publicBase

{

public:

voidShow()const{cout<<"调用Derived:

:

Show()"<

};

voidRefers(constBase&obj)//基类引用

{

obj.Show();//调用函数Show()

}

intmain(void)

{

Baseobj1;

Derivedobj2;//定义对象

Refers(obj1);//调用函数Refers()

Refers(obj2);

system("PAUSE");

return0;

}

3.

#include

usingnamespacestd;/

classBase

{

private:

intm;

public:

Base(inta):

m(a){}

virtualvoidShow()const{cout<

};

classDerived:

publicBase

{

private:

intn;

public:

Derived(inta,intb):

Base(a),n(a){}//构造函数

voidShow()const

{

cout<

Base:

:

Show();//调用基类的Show()

}

};

intmain(void)

{

Baseobj1(168);

Derivedobj2(158,98);

Base*p;

p=&obj1;

p->Show();

p=&obj2;

p->Show();

p->Base:

:

Show();

system("PAUSE");

return0;

}

4.

#include

usingnamespacestd;

classA

{

public:

virtualvoidShow()const{cout<<"基类A"<

};

classB:

publicA

{

public:

voidShow()const{cout<<"派生类B"<

};

intmain(void)

{

Bobj;

A*p=&obj;

p->Show();

system("PAUSE");

return0;

}

5.

#include

usingnamespacestd;

constdoublePI=3.1415926;

classShape

{

public:

virtualvoidShow()const=0;

staticdoublesum;

};

classCircle:

publicShape

{

private:

doubleradius;

public:

Circle(doubler):

radius(r)

{sum+=PI*radius*radius;}

voidShow()const

{

cout<<"圆形:

"<

cout<<"半径:

"<

cout<<"面积:

"<

}

};

classRectangle:

publicShape

{

private:

doubleheight;

doublewidth;

public:

Rectangle(doubleh,doublew):

height(h),width(w)

{sum+=height*width;}

voidShow()const

{

cout<<"矩形:

"<

cout<<"高:

"<

cout<<"宽:

"<

cout<<"面积:

"<

}

};

doubleShape:

:

sum=0;

intmain(void)

{

charflag='Y';'

Shape*p;

while(toupper(flag)=='Y')

{

cout<<"请选择输入类别(1.圆形2.矩形)";

intselect;

cin>>select;

switch(select)

{

case1:

doubler;

cout<<"输入半径:

";

cin>>r;

p=newCircle(r);

p->Show();

deletep;

break;

case2:

doubleh,w;

cout<<"输入高:

";

cin>>h;

cout<<"输入宽:

";

cin>>w;

p=newRectangle(h,w);

p->Show();//显示相关信息

deletep;//释放存储空间

break;

default:

//其它情况,表示选择有误

cout<<"选择有误!

"<

break;

}

cout<

(Y/N)";

cin>>flag;

}

cout<<"总面积:

"<

:

sum<

system("PAUSE");

return0;

}

6.

#include

usingnamespacestd;

constdoublePI=3.1415926;

constintNUM=10;

classShape

{

public:

virtualvoidShowArea()const=0;

staticdoublesum;

};

classCircle:

publicShape

{

private:

doubleradius;

public:

Circle(doubler):

radius(r)

{sum+=PI*radius*radius;}

voidShowArea()const

{cout<<"圆面积:

"<

};

classRectangle:

publicShape

{

private:

doubleheight;

doublewidth;

public:

Rectangle(doubleh,doublew):

height(h),width(w)

{sum+=height*width;}

voidShowArea()const

{cout<<"矩形面积:

"<

};

classSquare:

publicShape

{

private:

doublelength;

public:

Square(doublea):

length(a)

{sum+=length*length;}

voidShowArea()const

{cout<<"正方形面积:

"<

};

doubleShape:

:

sum=0;

intmain(void)

{

Shape*shape[NUM];

intcount=0;

while(count

{

cout<<"请选择(1.圆形2.矩形3.正方形4.退出):

";

intselect;

cin>>select;

if(select==4)break;

switch(select)

{

case1:

doubler;

cout<<"输入半径:

";

cin>>r;

shape[count]=newCircle(r);

shape[count]->ShowArea();

count++;

break;

case2:

doubleh,w;

cout<<"输入高:

";

cin>>h;

cout<<"输入宽:

";

cin>>w;

shape[count]=newRectangle(h,w);

shape[count]->ShowArea();

count++;

break;

case3:

doublea;

cout<<"输入边长:

";

cin>>a;

shape[count]=newSquare(a);

shape[count]->ShowArea();

count++;

break;

default:

cout<<"选择有误!

"<

break;

}

}

cout<<"总面积:

"<

:

sum<

for(inti=0;i

system("PAUSE");

return0;

}

5.实验总结

通过本次试验我更深刻的理解了某些语句如何使用及结构体的优点也能更加熟练的编写简单的程序了我深知实践要比书本更加重要今后还要多练习在实践中学习。

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

当前位置:首页 > 教学研究 > 教学案例设计

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

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