实验5类和对象题目Word格式文档下载.docx

上传人:b****1 文档编号:5031654 上传时间:2023-05-04 格式:DOCX 页数:17 大小:18.09KB
下载 相关 举报
实验5类和对象题目Word格式文档下载.docx_第1页
第1页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第2页
第2页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第3页
第3页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第4页
第4页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第5页
第5页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第6页
第6页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第7页
第7页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第8页
第8页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第9页
第9页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第10页
第10页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第11页
第11页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第12页
第12页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第13页
第13页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第14页
第14页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第15页
第15页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第16页
第16页 / 共17页
实验5类和对象题目Word格式文档下载.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验5类和对象题目Word格式文档下载.docx

《实验5类和对象题目Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《实验5类和对象题目Word格式文档下载.docx(17页珍藏版)》请在冰点文库上搜索。

实验5类和对象题目Word格式文档下载.docx

设计一个矩阵类CRectangle,该类中的私有成员变量存放Rectangle的长和宽,并设置它们的默认值为1,通过成员函数set()来设定长和宽的值,并确保长宽都在(0,50)范围之内,求其周长Perimeter并显示输出。

以下是完成此项工作的程序,请将未完成的部分填入,使之完整。

classCRectangle

voidSet(floata,floatb)

if((a>

0)&

&

(a<

50))length=a;

elselength=1;

if((b>

(b<

50))width=b;

elsewidth=1;

floatperimeter()

return2*(length+width);

floatlength;

floatwidth;

CRectangleR;

floatl,w;

//定义矩形的长和宽做为输入变量;

//cout<

"

请输入矩形的长和宽:

cin>

>

l>

w;

R.Set(1,w);

//设置矩形的长和宽

矩形的周长为:

R.perimeter()<

3.题目描述:

设计一个类CRectangle,要求如下所述。

(1)定义两个点坐标x1,y1,x2,y2,两点所确定的一条直线构成了矩形的对角线。

(2)初始化矩形的两个点时,判断给定的两个点是否能够构成一个矩形,如果不能构成矩形,则矩形对角线的两点初始化为(0,0)和(1,1)。

如果可以构成,则用形参初始化对象的数据成员。

根据以上描述完成下列程序。

CRectangle(floatRx1=0,floatRy1=0,floatRx2=1,floatRy2=1);

boolIsSquare();

voidPrintRectangle();

//确定直线的两点的坐标

floatx1,y1,x2,y2;

CRectangle:

:

CRectangle(floatRx1,floatRy1,floatRx2,floatRy2)

if(Rx1==Rx2||Ry1==Ry2)//两点的横坐标或纵坐标的值相等,则不能构成矩形

x1=y1=0;

x2=y2=1;

不能构成矩形!

"

else

x1=Rx1,x2=Rx2,y1=Ry1,y2=Ry2//初始化数据成员x1,y1,x2,y2

可以构成矩形!

CRectangleR1(1,3,5,6);

CRectangleR2(1,3,1,6);

4.题目描述:

下列程序中声明了类girl,其中函数“display”是类girl的友元函数,请在

(1)、

(2)和(3)处各填入正确的内容,使程序能正常运行。

classgirl

charname;

intage;

girl(charn,intd)//构造函数

name=n;

age=d;

Friendvoiddisplay(girl&

x);

//声明友元函数

voiddisplay(girl&

x)//类外定义友元函数

Girl'

snameis:

x.name<

ageis:

x.age<

//girl类的友元函数能访问girl类对象的私有成员

intmain()

girle('

z'

18);

display(e);

//调用友元函数

5.题目描述:

,请完善下面程序,使程序的运行结果如下:

Thisisaconstructor!

Thevalueofchisa

Thevalueofchisb

Thisisadestructorofb

Thisisadestructorofa

classMyClass

charch;

MyClass()

Thisisaconstructor!

ch='

a'

;

MyClass(charcharacter)

ch=character;

voidPrint()

Thevalueofchis"

ch<

~MyClass()

Thisisadestructorof"

MyClassfirst,second(b);

first.Print();

second.Print();

程序设计

6.题目标题:

计算两点间的距离

题目描述:

仿照本次实验预习的程序填空题1,将以上Distance函数定义为类piont的友元函数,实现程序的功能。

并在主函数中增加输入两点作为友元函数的实参。

其主函数如下:

输入描述:

输入四个数,用空格隔开。

输出描述:

两个点的距离。

样例输入:

1356

样例输出:

5

friendfloatDistance(point&

p1,point&

p2);

floatDistance(point&

p2)

floatdx=p1.x-p2.x;

floatdy=p1.y-p2.y;

{

floatp1_x,p1_y,p2_x,p2_y;

//输入四个点

p1_x>

p1_y>

p2_x>

p2_y;

pointp1(p1_x,p1_y),p2(p2_x,p2_y);

Distance(p1,p2)<

7.题目标题:

日期类CDateInfo的设计。

根据以下主函数的功能来设计日期类CDateInfo,使其能正确运行。

类CDateInfo中应该具有描述年、月、日的三个数据成员和相应的成员函数。

classCDateibfo

intday,month,year;

CDateibfo();

CDateibfo(intyy,intmm,intdd);

voidsetdate(intyy,intmm,intdd);

voidgetdate();

CDateibfo:

CDateibfo()

day=10;

month=10;

year=2011;

CDateibfo(intyy,intmm,intdd)

year=yy;

month=mm;

day=dd;

voidCDateibfo:

setdate(intyy,intmm,intdd)

getdate()

year<

-"

month<

day<

CDateibfodate1,date2(2011,10,10);

inty,m,d;

y>

m>

d;

date1.setdate(y,m,d);

date1.getdate();

date2.getdate();

三个整数,分别用来设置对象data1的年、月、日

两行:

第1行为对象data1的年月日;

第2行为data2的年月日。

2011125

2012-12-5

2011-10-10

8.题目标题:

学生类Student的设计

根据以下主函数的功能来设计日期类Student,使其能正确运行。

类Student中应该具有描述学生姓名、性别、年龄的三个数据成员和相应的成员函数。

3行,第一行为一个长度不超过10的字符串表示姓名;

第二行为0和1中的一个整数;

第三行为一个整数,表示年龄。

按主函数要求输出。

cstring>

classStudent

charname[20];

intsex;

unsignedold;

voidSetName(char*chOne);

voidSetGender(intisex);

voidSetAge(unsignediold);

voidGetName();

voidGetGender();

voidGetAge();

voidStudent:

SetName(char*chOne)

strcpy(name,chOne);

SetGender(intisex)

sex=isex;

SetAge(unsignediold)

old=iold;

GetName()

Zhang_San'

snameis"

name<

GetGender()

sgenderis"

sex<

GetAge()

sageis"

old<

StudentZhang_San;

char*chOne;

intiSex;

unsignediOld;

chOne=newchar[11];

chOne;

iSex;

iOld;

Zhang_San.SetName(chOne);

Zhang_San.SetGender(iSex);

Zhang_San.SetAge(iOld);

Zhang_San.GetName();

Zhang_San.GetGender();

Zhang_San.GetAge();

ZhangSan

20

snameisZhangSan

sgenderis0

sageis20

9.题目标题:

计算器类Calculator的设计

根据以下主函数的功能来设计计算器类Calculator,使其能正确运行。

类Calculator中应该具有描述运算数的a和b及其表示a和b运算结果的三个数据成员和相应计算并显示结果的成员函数。

classCalculator

Calculator(inta,intb)

x=a;

y=b;

};

voidadd()

cout<

x<

+"

y<

="

x+y<

voidsubtract()

x-y<

voidmultiply()

*"

x*y<

voiddivide()

/"

x/y<

floata,b;

a>

b;

Calculatorcal(a,b);

cal.add();

cal.subtract();

cal.multiply();

cal.divide();

2个整数a和b,表示参与运算的两个数据。

4行,分别显示a+b、a-b、a*b和a/b的结果

12

1+2=3

1-2=-1

1*2=2

1/2=0.5

10.题目标题:

复数类Imaginary的设计

根据以下主函数的功能来设计复数类Imaginary,使其能正确运行。

类Imaginary中应该具有描述复数的实部和虚部的私有数据成员a和b,还有相应的构造函数和按照“a±

bi”格式显示复数的成员函数print()。

设计类Imaginary的2个友元函数分别进行复数的加、减运算,并在这些函数调用类Imaginary的成员函数print()显示结果。

classImaginary

Imaginary(inta,intb)

friendvoidAdd(Imaginary&

p,Imaginary&

b);

friendvoidSub(Imaginary&

q);

voidAdd(Imaginary&

q)

p.x+q.x<

p.y+q.y<

i"

voidSub(Imaginary&

p.x-q.x<

abs(p.y-q.y)<

x1>

y1>

x2>

y2;

Imaginaryimag1(x1,y1),imag2(x2,y2);

Add(imag1,imag2);

Sub(imag1,imag2);

输入4个数据,分别表示进行运算的两个复数的实部和虚部

4行,分别显示两个复数进行加、减、乘、除运算的结果

1325

3+8i

-1-2i

 

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

当前位置:首页 > 总结汇报 > 学习总结

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

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