C++实验4Word格式.docx

上传人:b****2 文档编号:14092495 上传时间:2022-10-18 格式:DOCX 页数:19 大小:67.85KB
下载 相关 举报
C++实验4Word格式.docx_第1页
第1页 / 共19页
C++实验4Word格式.docx_第2页
第2页 / 共19页
C++实验4Word格式.docx_第3页
第3页 / 共19页
C++实验4Word格式.docx_第4页
第4页 / 共19页
C++实验4Word格式.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

C++实验4Word格式.docx

《C++实验4Word格式.docx》由会员分享,可在线阅读,更多相关《C++实验4Word格式.docx(19页珍藏版)》请在冰豆网上搜索。

C++实验4Word格式.docx

1、分析并调试下列程序,写出程序的输出结果,并解释输出结果。

#include<

iostream>

usingnamespacestd;

classB{

public:

virtualvoidf1(doublex)

{cout<

<

"

B:

:

f1(double)"

x<

endl;

}

voidf2(doublex)

f2(double)"

2*x<

voidf3(doublex)

f3(double)"

3*x<

}

};

classD:

publicB{

D:

voidf2(intx)

f2(int)"

intmain()

{Dd;

B*pb=&

d;

D*pd=&

pb->

f1(1.23);

pd->

f2(1.23);

f3(1.23);

f3(3.14);

return0;

【运行结果截图】:

【运行结果分析】:

函数f1()被定义为虚函数,所以执行pb->

f1(1.23)语句时,程序根据指针pb所指向的的实际对象,调用该对象的成员函数。

而f2()和f3()只是普通的成员函数,不管pb指向哪个对象,程序pb->

f2(1.23)和pb->

f3(1.23)调用的都是基类中定义的的函数。

2、编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。

【要求】:

将小时范围限制在大于等于0,分钟范围限制在0~59分,秒钟范围限制在0~59秒。

【提示】:

时间类Time的参考框架如下:

classTime{

Time(inth=0,intm=0,ints=0);

Timeoperator+(Time&

);

voiddisptime(string);

private:

inthourse;

intminuters;

intseconds;

3、给出下面的抽象基类container。

classcontainer{

protected:

doubleradius;

container(doubleradius);

virtualdoublesurface_area()=0;

virtualdoublevolume()=0;

建立3个继承container的派生类cube、sphere与cylinder,让每一个派生类都包含虚函数surface_area()和volume(),分别用来计算正方体、球体和圆柱体的表面积及体积。

写出主程序,用于C++的多态性,分别计算边长为6.0的正方体、半径为5.0的球体,以及半径为5.0和高为6.0的圆柱体的表面积和体积。

4、编写程序,用于进行集合的并、差和交运算。

例如输入整数集合{954367}和{2469},计算出它们进行集合的并、差和交运算后的结果。

1)可以用以下表达式实现整数集合的运算:

s1+s2两个整数集合的并运算

s1-s2两个整数集合的差运算

s1*s2两个整数集合的交运算

【Set类的框架】:

 

classSet

{

voidinput(int);

intlength();

voiddisp();

Setoperator+(Sets1);

Setoperator-(Sets1);

Setoperator*(Sets1);

intlen;

ints[MAX];

intm[MAX];

voidSet:

input(intd)//输入数组长度的函数

inti;

len=d;

cout<

输入集合元素"

d<

个:

"

;

for(i=0;

i<

i++)

cin>

>

s[i];

s[i]='

\0'

intSet:

length()//求数组长度的函数

inti=0;

while(m[i]!

='

i++;

returni;

disp()//输出最后结果的函数

intk=length();

k;

cout<

m[i]<

SetSet:

operator+(Sets1)//并运算

inti,j;

intt=s1.len;

//防止s1.len的值在下面的运算中被改变

intk=len;

//防止len的值在下面的运算中被改变

len;

i++)//防止s[i]在下面的运算中的改变

m[i]=s[i];

s1.len;

i++)//防止s1.s[i]在下面的运算中的改变

s1.m[i]=s1.s[i];

for(j=0;

j<

t;

j++)

if(m[i]==s1.m[j])

{

for(;

s1.m[j]=s1.m[j+1];

t--;

}

for(j=0;

m[k]=s1.m[j];

k++;

m[k]='

//用于判断数组长度

return*this;

operator-(Sets1)//差运算

for(j=0;

if(s1.m[i]==m[j])

{

for(;

m[j]=m[j+1];

k--;

}

m[k]='

operator*(Sets1)//并运算

inta[MAX],k=0;

if(m[i]==s1.m[j])

a[k]=m[i];

k++;

}

m[i]=a[i];

m[k]='

四、分析与讨论(记录实验过程中出现的主要问题和心得体会)

五、教师评语

签名:

日期:

成绩

附:

程序源代码

1、

2、

#include<

string>

classTime

Timeoperator+(Time&

Time:

Time(inth,intm,ints)

hourse=h;

minuters=m;

seconds=s;

TimeTime:

operator+(Time&

a)

Timetemp;

temp.seconds=(seconds+a.seconds)%60;

temp.minuters=(minuters+a.minuters+(seconds+a.seconds)/60)%60;

temp.hourse=hourse+a.hourse+(minuters+a.minuters+(seconds+a.seconds)/60)/60;

returntemp;

voidTime:

disptime(stringstr)

str;

hourse<

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

当前位置:首页 > 初中教育 > 科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1