北邮C++实验实验六.docx

上传人:b****2 文档编号:1953736 上传时间:2023-05-02 格式:DOCX 页数:22 大小:78.12KB
下载 相关 举报
北邮C++实验实验六.docx_第1页
第1页 / 共22页
北邮C++实验实验六.docx_第2页
第2页 / 共22页
北邮C++实验实验六.docx_第3页
第3页 / 共22页
北邮C++实验实验六.docx_第4页
第4页 / 共22页
北邮C++实验实验六.docx_第5页
第5页 / 共22页
北邮C++实验实验六.docx_第6页
第6页 / 共22页
北邮C++实验实验六.docx_第7页
第7页 / 共22页
北邮C++实验实验六.docx_第8页
第8页 / 共22页
北邮C++实验实验六.docx_第9页
第9页 / 共22页
北邮C++实验实验六.docx_第10页
第10页 / 共22页
北邮C++实验实验六.docx_第11页
第11页 / 共22页
北邮C++实验实验六.docx_第12页
第12页 / 共22页
北邮C++实验实验六.docx_第13页
第13页 / 共22页
北邮C++实验实验六.docx_第14页
第14页 / 共22页
北邮C++实验实验六.docx_第15页
第15页 / 共22页
北邮C++实验实验六.docx_第16页
第16页 / 共22页
北邮C++实验实验六.docx_第17页
第17页 / 共22页
北邮C++实验实验六.docx_第18页
第18页 / 共22页
北邮C++实验实验六.docx_第19页
第19页 / 共22页
北邮C++实验实验六.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

北邮C++实验实验六.docx

《北邮C++实验实验六.docx》由会员分享,可在线阅读,更多相关《北邮C++实验实验六.docx(22页珍藏版)》请在冰点文库上搜索。

北邮C++实验实验六.docx

北邮C++实验实验六

7.3实验题

一、基础题

1.标出下列程序中的错误

classCSample

{inta=2.5;(A)实际赋值与定义的类型不符,数据成员不能在类中作初始化

CSample();(B)

public:

CSample(intval);(C)

~CSample();};(D)

2.在下列程序的填空中填上适当的语句,完成立方体体积的计算,并按图7-1的样式输出结果.

要求:

(1)对有注释标记的语句进行功能注释。

(2)建立一个头文件和测试类文件,上机调试并输出下列结果。

程序如下:

#include

usingnamespacestd;

classcube

{

public:

cube();//定义一个结构函数

~cube();//定义一个析构函数

intvolume();//定义一个成员函数

private:

intheight,width,depth;//定义三个数据成员

};

cube:

:

cube()

{height=10;

width=8;

depth=10;

cout<<"Constructorcalled."<

}

cube:

:

~cube()

{

cout<<"Destructorcalled"<

}

intcube:

:

volume()

{

returnheight*width*depth;

}

voidmain()

{

cubecubeone;//定义一个对象

cout<<"立方体的体积为:

"<

}

图7-1第1题的输出示例

3.修改下列程序中的错误,并说明调用构造函数和析构函数的次序和次数

#include

usingnamespacestd;

classCCounter

{

intvalue;

public:

CCountr()

{

cout<<"CCounterConstructor1"<

value=0;

}

CCountr(intval)

{

cout<<"CCounterConstructor2"<

value=val;

}

~CCounter(){cout<<"CCounterDestructor"<

};

classCExample

{

intvalue;

public:

CCountercar;

CExample()

{

cout<<"CExampleConstructor1"<

value=0;

}

CExample(intval)

{

cout<<"CExampleConstructor1"<

value=val;

}

~CExample(){cout<<"CExampleDestructor"<

voidDisplay(){cout<<"value="<

};

voidmain()

{

CExampleobj(350);

obj.Display();

}

先调用CExample的构造函数CExample()

然后调用CExample的构造函数CExample(intval)

然后调用CExample的析构函数~CExample()

最后调用CCounter的析构函数~CCounter()

 

改为

#include

usingnamespacestd;

classCCounter

{

intvalue;

public:

voidCCountr()

{

cout<<"CCounterConstructor1"<

value=0;

}

voidCCountr(intval)

{

cout<<"CCounterConstructor2"<

value=val;

}

~CCounter(){cout<<"CCounterDestructor"<

};

classCExample

{

intvalue;

public:

CCountercar;

CExample()

{

cout<<"CExampleConstructor1"<

value=0;

}

CExample(intval)

{

cout<<"CExampleConstructor1"<

value=val;

}

~CExample(){cout<<"CExampleDestructor"<

voidDisplay(){cout<<"value="<

};

voidmain()

{

CExampleobj(350);

obj.Display();

}

4.上机调试下列程序,说明出错的原因,并提交正确的程序。

输出结果如图7-2所示。

#include

usingnamespacestd;

classPoint

{

protected:

intx;

public:

intGetx(){returnx;}

intY;

voidinit(){x=a;Y=b;}

intGety(){returnY;}

voidSetx(inta){x=a;}

voidSety(intb){Y=b;}

};

voidmain()

{

PointA,B;

A.init(25,27);

B.init(29,31);

cout<<"A.x="<

cout<<"B.x="<

cout<<"A.Y="<

cout<<"A.y="<

cout<<"B.y="<

cout<<"B.y="<

}

图7-2第4题的输出显示

出错的原因:

init定义的形参与实参个数不符

 

改为:

#include

usingnamespacestd;

classPoint

{

protected:

intx;

public:

intGetx(){returnx;}

intY;

voidinit(inta,intb){x=a,Y=b;}

intGety(){returnY;}

voidSetx(inta){x=a;}

voidSety(intb){Y=b;}

};

voidmain()

{

PointA,B;

A.init(25,27);

B.init(29,31);

cout<<"A.x="<

cout<<"B.x="<

cout<<"A.Y="<

cout<<"A.y="<

cout<<"B.y="<

cout<<"B.y="<

}

5.阅读以下程序,写出下列程序的运行结果,并对有注释带注释标记语句进行功能注释

#include

usingnamespacestd;

classbase

{

private:

inti;

staticintk;//定义一个静态成员

public:

base(){i=0;k++;}//定义一个结构函数

voidDisplay();

};

voidbase:

:

Display()

{

k=k+10;

i++;

cout<<"i="<

}

intbase:

:

k=0;//将静态成员赋值为0

voidmain()

{

basea,b;

a.Display();

b.Display();

}

运行结果为:

__i=1,k=12________________

___i=1,k=22_______________

二、编程题

1.设计一个类用于对整数进行判别,并根据判断的结果分别统计所判断过的正整数和负

整数的个数,并以格式显示其统计结果。

#include

#include

usingnamespacestd;

classA

{

private:

inta;

staticintb,c;

public:

A(intx);

voidprint();

};

A:

:

A(intx)

{

a=x;

if(a>0)

{cout<

b++;}

elseif(a<0)

{cout<

c++;}

else

cout<

}

voidA:

:

print()

{

cout<<"正整数的个数是:

"<

"<

}

intA:

:

b=0;

intA:

:

c=0;

voidmain()

{

Aa(3);

Ac(7);

Ad(0);

Ab(-1);

b.print();

}

2.编写一个程序,定义一个类,此类包括3个私有数据成员:

colorcast(彩电)、ridge(冰箱)、washer洗衣机以及total(当天总收入)。

彩电目前的价格是4200元/每台、冰箱目前的价格是2800元/每台、洗衣机的价格1500元/每台。

要求实现输入彩电、冰箱、洗衣机的价格和当天所卖出的数量,本程序将计算出百货商店家电部当天的总营业额。

#include

usingnamespacestd;

classshop

{

private:

intcolorcast;

intridge;

intwasher;

public:

voidtotal();

inta,b,c,totalnumber;

voidgetprice();

voidgetnumber();

};

voidshop:

:

getprice()

{

cout<<"请输入彩电,冰箱,洗衣机的价格:

"<

intx,y,z;

cin>>x>>y>>z;

colorcast=x;

ridge=y;

washer=z;

}

voidshop:

:

getnumber()

{

cout<<"请输入彩电,冰箱,洗衣机的个数:

"<

cin>>a>>b>>c;

}

voidshop:

:

total()

{

cout<<"家电的总营业额为a:

"<

totalnumber=colorcast*a+ridge*b+washer*c;

cout<

}

voidmain()

{

shopmyshop;

myshop.getprice();

myshop.getnumber();

myshop.total();

}

3.编写一个程序,定义一个汽车类(car),其中包括车型、颜色、车门数、座位数和轴距等属性,包括chage_parameters()和disp_parameters()等函数;disp_parameters()显示车型、颜色、车门数、座位数和轴距属性,函数change_parameters()改变对象的车型、颜色、车门数、座位数和轴距属性,实现并测试这个类。

#include

#include

usingnamespacestd;

classcar

{

private:

stringtype;

stringcolor;

intdoornumber;

intchairnumber;

intzhoucheng;

public:

voiddisp_parameters(char*,char*,int,int,int);

voidchange_parameters(char*,char*,int,int,int);

};

voidcar:

:

disp_parameters(char*a,char*b,intc,intd,inte)

{

type=a;

color=b;

doornumber=c;

chairnumber=d;

zhoucheng=e;

cout<<"更改前为:

"<<"车型í:

"<

"<

"<

"<

"<

}

voidcar:

:

change_parameters(char*a,char*b,intc,intd,inte)

{type=a;

color=b;

doornumber=c;

chairnumber=d;

zhoucheng=e;

cout<<"更改后为:

"<<"车型í:

"<

"<

"<

"<

"<

}

voidmain()

{

carmycar;

mycar.disp_parameters("奥拓","黑色?

",4,4,5);

mycar.change_parameters("奥迪","红色",2,2,3);

}

测试结果:

 

4.编写一个程序,使其可实现输入5个学生学号、姓名、性别和年龄,并按年龄从大到小进行排序输出。

#include

usingnamespacestd;

structstu{

longno;

charname[9];

charsex[5];

intage;};

 

voidmain()

{stum;

stustudent[5];

for(intb=0;b<5;b++)

{cin>>student[b].no>>student[b].name>>student[b].sex>>student[b].age;

}

for(inti=1;i<5;i++)

{

for(intj=0;j<=4-i;j++)

{

if(student[j].age

{

m=student[j];

student[j]=student[j+1];

student[j+1]=m;

}

}

}

for(intk=0;k<5;k++)

{cout<

}

}

5.设计一个模拟银行ATM柜台机的程序,包括存款、取款和查询余额三种功能。

提示:

在存款、取款时都需要检查帐号和密码,可以直接引用其成员函数来检查,如果是合法客户,返回true,然后执行增加存款或减少存款的程序。

#include

usingnamespacestd;

classband

{

private:

staticintmoney;

staticinta,b;

public:

intcheck()

{cout<<"请输入账号和密码:

"<

intx,y;

for(inti=1;i<=3;i++)

{cin>>x>>y;

if(a==x,b==y)

{return1;

break;}

else

{if(i!

=3){cout<<"输入错误,重新输入";}

else

{cout<<"拒绝服务"<

}}

}

voidincrease()

{

cout<<"请输入存款金额:

"<

inti;

cin>>i;

money+=i;

}

voidreduce()

{

cout<<"请输入取款金额:

"<

intj;

cin>>j;

money-=j;

}

voidcheckmoney()

{

cout<

}

};

intband:

:

a=444641;

intband:

:

b=12345;

intband:

:

money=2000;

voidmain()

{

inta;

intm=1;

bandmyband;

do

{

cout<<"请选择服务类型:

1.存款2.取款?

3.查询余额4.结束服务"<

cin>>a;

if(a==1)

{

if(myband.check())

{myband.increase();}

}

elseif(a==2)

{

if(myband.check())

{myband.reduce();}

}

elseif(a==3)

{

myband.checkmoney();

}

elseif(a==4)

break;

}while(m==1);

 

}

 

6.有一个学生类,包括学号、姓名和成绩,设计一个友员函数,比较学生成绩的高低,并求出最高分和最低分。

#include

usingnamespacestd;

classstudent

{

private:

intno;

stringname;

intscore;

public:

voidget(int,char*,int);

friendvoidcompare(student&a,student&b,student&c);

};

voidcompare(student&a,student&b,student&c)

{

intx,y,m,n;

x=a.score>b.score?

a.score:

b.score;

y=x>c.score?

x:

c.score;

cout<<"最高分为:

"<

m=a.score

a.score:

b.score;

n=m

m:

c.score;

cout<<"最低分为:

"<

}

voidstudent:

:

get(intx,char*y,intz)

{

no=x;

name=y;

score=z;

}

voidmain()

{

studenta,b,c;

a.get(01,"zhang",87);

b.get(02,"wang",97);

c.get(03,"li",65);

compare(a,b,c);

}

 

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

当前位置:首页 > 解决方案 > 学习计划

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

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