实验5-类和对象题目_第1页
实验5-类和对象题目_第2页
实验5-类和对象题目_第3页
实验5-类和对象题目_第4页
实验5-类和对象题目_第5页
已阅读5页,还剩20页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验5类和对象

程序填空

1、题目描述:仔细阅读下列求两个点之间距离得程序,程序得揄出结果就就

是50,根据程序得输出结果在划线处填入正确语句。

代码:

#incIude<iostream>

#incIude<cmath>

usingnamespacestd;

classpoint

(

pubIic:

point(fIoata,fIoatb){x=a;y=b;}

floatDistance(point&p)

{

fIoatdx=p、x-x;

fIoatdy=p、y-y;

return(fIoat)sqrt(dx*dx+dy*dy);

1

private:

fIoatx,y;

);

intmain()

(

pointp1(2,3),p2(32,43);

cout<<p1^Distance(p2)«endI;

return0;

}

2、题目描述:设计一个矩阵类CRectangIe,该类中得私有成员变量存

放Rectangle得长和宽,并设置她们得默认值为1,通过成员函数set()来

设定长和宽得值,并确保长宽都在(0,50)范围之内,求其周长Perimeter并显

示输出。以下就就是完成此项工作得程序,请将未完成得部分填入,使之完整。

代码:

#incIudeViostream>

usingnamespacestd;

cIassCRectangIe

publie:

voidSet(floata,fIoatb)

(

if((a>0)&&(a<50))length=a;

elseIength=1;

if((b>0)&&(b<50))width=b;

elsewidth=1;

}

floatperimeter()

(

return2*(Iength+width);

)

private:

floatIength;

fIoatwidth;

1;

#incIude<iostream>

#include<cmath>

usingnamespacestd;

classCRectangle

(

pubIic:

CRectangIe(fIoatRx1=0,floatRy1=0,fIoatRx2=1,

floatRy2=1);

boolIsSquare();

voidPrintRectangIe();

private:

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

floatx1,y1,x2,y2;

1;

CRectangIe::CRectangle(floatRx1,fIoatRy1,fIoatRx2,

fIoatRy2)

if(Rx1==Rx2||Ry1==Ry2)〃两点得横坐标或纵坐标得值相等,

则不能构成矩形

x1=y1=0;

x2=y2=1;

cout<V"不能构成矩形!"«endI;

eIse

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

y1,x2,y2

cout«"可以构成矩形!"«endl;

intmain()

CRectangleR1(1,3,5,6);

CRectangIeR2(1,3,1,6);

returnO;

)

4、题目描述:下列程序中声明了类girI,其中函数“dispIay”就就

是类girl得友元函数,请在⑴、⑵和(3)处各填入正确得内容,使程序能正常

运行。

代码:

#incIude<iostream>

usingnamespacestd;

classgirI

(

private:

charname;

intage;

pubIic:

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

name=n;

age=d;

)

FriendvoiddispIay(girI&x);//声明友元函数

1;

voiddisplay(girl&x)//类外定义友元函数

(

cout<<"Girl'snameis:"<<x、name<<",ageis:"<

Vx、age<<endI;

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

)

intmain()

(

girle('z',18);

dispIay(e);//调用友元函数

return0;

}

5、题目描述:,请完善下面程序,使程序得运行结果如下:

Thisisaconstruetor!

Thisisaconstructor!

Thevalueofchisa

Thevalueofchisb

Thisisadestructorofb

Thisisadestructorofa

代码:

#incIude<iostream>

usingnamespacestd;

classMyCIass

(

charch;

publie:

MyClass()

(

cout<<"Thisisaconstructor!"«endI;

ch='a

)

MyClass(charcharacter)

(

cout«"Thisisaconstruetor!"<<endI;

ch=character;

)

voidPrint()

I

cout«"ThevaIueofchis"«ch«endI;

}

~MyCIass()

(

cout«"Thisisadestructorof"«ch«endI;

)

1;

intmain()

MyCIassfirst,second(b);

first、Print();

second、Print();

returnO;

1

程序设计

6、题目标题:计算两点间得距离

题目描述:仿照本次实验预习得程序填空题1,将以上Distance函数定义为类

piont得友元函数,实现程序得功能。并在主函数中增加输入两点作为友元函数

得实参。

其主函数如下:

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

输出描述:两个点得距离。

样例输入:1356

样例输出:5

#incIude<iostresm>

#include<cmath>

usingnamespacestd;

cIasspoint

public:

point(floata,fIoatb){x=a;y=b;}

friendfIoatDistance(point&p1,point&p2);

private:

floatx,y;

};

fIoatDistance(point&p1,point&p2)

{

fIoatdx=p1、x—p2、x;

floatdy=p1、y-p2、y;

return(float)sqrt(dx*dx+dy*dy);

)

intmain0

floatp1_x,p1_y,p2_x,p2_y;

//输入四个点

cin»p1_x»p1_y»p2_x»p2_y;

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

cout<<Distance(p1,p2)«endI;

return0;

1

7、题目标题:日期类CDateInf。得设计。

题目描述:根据以下主函数得功能来设计日期类CDatelnfo,使其能正确运

行。类CDatelnfo中应该具有描述年、月、日得三个数据成员和相应得成

员函数。

#incIude<iostream>

usingnamespacestd;

classCDateibfo

(

intday,month,year;

pubIic:

CDateibfo();

CDateibfo(intyy,intmm,intdd);

voidsetdate(intyy,intmm,intdd);

voidgetdate();

);

CDateibfo::CDateibfo()

(

day=1O;

month=10;

year=2011;

}

CDateibfo::CDateibfo(intyy,intmm,intdd)

(

year=yy;

month=mm;

day=dd;

}

voidCDateibfo::setdate(intyy,intmm,intdd)

(

year=yy;

month=mm;

day=dd;

)

voidCDateibfo::getdate()

cout<<year<<"—"<<month<<"-"«day<<endI;

)

intmain()

(

CDateibfodate1,date2(2011,10,10);

inty,m,d;

cin»y»m»d;

datel>setdate(y,m,d);

date1、getdate();

date2、getdate();

return0;

)

输入描述:三个整数,分别用来设置对象data1得年、月、日

输出描述:两行:第1行为对象data1得年月日;第2行为data2得年月日。

样例输入:

2011125

样例输出:

2012-12-5

2011-10-10

8、题目标题:学生类Student得设计

题目描述:根据以下主函数得功能来设计日期类Student,使其能正确运

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

应得成员函数。

输入描述:3行,第一行为一个长度不超过10得字符串表示姓名;第二行为0

和1中得一个整数;第三行为一个整数,表示年龄。

输出描述:按主函数要求输出。

#incIude<iostream>

#include<cstring>

usingnamespacestd;

classStudent

(

private:

charname[2O];

intsex;

unsignedold;

pubIic:

voidSetName(char*ch0ne);

voidSetGender(intisex);

voidSetAge(unsignediold);

voidGetName();

voidGetGender();

voidGetAge();

};

voidStudent::SetName(char*chOne)

(

strcpy(name,ohOne);

)

voidStudent::SetGender(intisex)

(

sex=isex;

1

voidStudent::SetAge(unsignediold)

oId=ioId;

)

voidStudent::GetName()

(

cout«"Zhang_San'snameis"<<name«endI;

)

voidStudent;;GetGender()

(

cout<<"Zhang_San'sgenderisn<<sex«endI;

}

voidStudent::GetAge()

(

cout«"Zhang_Sansageis"<<old«endI;

1

intmain()

StudentZhang_San;

char*chOne;

intiSex;

unsignediOId;

chOne=newchar[11];

cin>>chOne;

cin»iSex;

cin»iOld;

Zhang_San、SetName(chOne);

Zhang_San^SetGender(iSex);

Zhang_SarisSetAge(iOld);

cout«endI;

Zhang_San、GetName0;

Zhang_San、GetGender();

ZhangSan、GetAge();

return0;

1

样例输入:

ZhangSan

0

20

样例输出:

Zhang_San'snameisZhangSan

Zhang_San'sgenderis0

Zhang_San'sageis20

9、题目标题:计算器类CaIcuIator得设计

题目描述:根据以下主函数得功能来设计计算器类CaIculator,使其能正

确运行。类CaIcuIator中应该具有描述运算数得a和b及其表示a和b运

算结果得三个数据成员和相应计算并显示结果得成员函数。

#incIude<iostream>

usingnamespacestd;

classCaIculator

floatx,y;

pubIic:

CaIculator(inta,intb)

(

x=a;

y=b;

);

voidadd()

(

cout«x«"+"<<y<<"="«x+y«endl;

};

voidsubtract()

(

cout«x«y«"="«x-y«endI;

);

voidmu11ipIy()

cout«x«"*"«y<<M="«x*y«endl;

1;

voiddivide()

(

cout«x«"/"«y<<"="«x/y<<endI;

1;

};

intmain()

(

floata,b;

cin»a»b;

CaleuIatorcaI(a,b);

caI、add();

cal、subtract();

cal、muItipiy();

cal、divide();

return0;

1

输入描述: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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论