实验5类和对象题目.docx
《实验5类和对象题目.docx》由会员分享,可在线阅读,更多相关《实验5类和对象题目.docx(17页珍藏版)》请在冰豆网上搜索。
实验5类和对象题目
实验5类和对象
程序填空
1.题目描述:
仔细阅读下列求两个点之间距离的程序,程序的输出结果是50,根据程序的输出结果在划线处填入正确语句。
代码:
#include
#include
usingnamespacestd;
classpoint
{
public:
point(floata,floatb){x=a;y=b;}
floatDistance(point&p)
{
floatdx=p.x-x;
floatdy=p.y-y;
return(float)sqrt(dx*dx+dy*dy);
}
private:
floatx,y;
};
intmain()
{
pointp1(2,3),p2(32,43);
cout<return0;
}
2.题目描述:
设计一个矩阵类CRectangle,该类中的私有成员变量存放Rectangle的长和宽,并设置它们的默认值为1,通过成员函数set()来设定长和宽的值,并确保长宽都在(0,50)范围之内,求其周长Perimeter并显示输出。
以下是完成此项工作的程序,请将未完成的部分填入,使之完整。
代码:
#include
usingnamespacestd;
classCRectangle
{
public:
voidSet(floata,floatb)
{
if((a>0)&&(a<50))length=a;
elselength=1;
if((b>0)&&(b<50))width=b;
elsewidth=1;
}
floatperimeter()
{
return2*(length+width);
}
private:
floatlength;
floatwidth;
};
intmain()
{
CRectangleR;
floatl,w;//定义矩形的长和宽做为输入变量;
//cout<<"请输入矩形的长和宽:
"<cin>>l>>w;
R.Set(1,w);//设置矩形的长和宽
cout<<"矩形的周长为:
"<return0;
}
3.题目描述:
设计一个类CRectangle,要求如下所述。
(1)定义两个点坐标x1,y1,x2,y2,两点所确定的一条直线构成了矩形的对角线。
(2)初始化矩形的两个点时,判断给定的两个点是否能够构成一个矩形,如果不能构成矩形,则矩形对角线的两点初始化为(0,0)和(1,1)。
如果可以构成,则用形参初始化对象的数据成员。
根据以上描述完成下列程序。
代码:
#include
#include
usingnamespacestd;
classCRectangle
{
public:
CRectangle(floatRx1=0,floatRy1=0,floatRx2=1,floatRy2=1);
boolIsSquare();
voidPrintRectangle();
private:
//确定直线的两点的坐标
floatx1,y1,x2,y2;
};
CRectangle:
:
CRectangle(floatRx1,floatRy1,floatRx2,floatRy2)
{
if(Rx1==Rx2||Ry1==Ry2)//两点的横坐标或纵坐标的值相等,则不能构成矩形
{
x1=y1=0;
x2=y2=1;
cout<<"不能构成矩形!
"<}
else
{
x1=Rx1,x2=Rx2,y1=Ry1,y2=Ry2//初始化数据成员x1,y1,x2,y2
cout<<"可以构成矩形!
"<}
}
intmain()
{
CRectangleR1(1,3,5,6);
CRectangleR2(1,3,1,6);
return0;
}
4.题目描述:
下列程序中声明了类girl,其中函数“display”是类girl的友元函数,请在
(1)、
(2)和(3)处各填入正确的内容,使程序能正常运行。
代码:
#include
usingnamespacestd;
classgirl
{
private:
charname;
intage;
public:
girl(charn,intd)//构造函数
{
name=n;
age=d;
}
Friendvoiddisplay(girl&x);//声明友元函数
};
voiddisplay(girl&x)//类外定义友元函数
{
cout<<"Girl'snameis:
"<"<//girl类的友元函数能访问girl类对象的私有成员
}
intmain()
{
girle('z',18);
display(e);//调用友元函数
return0;
}
5.题目描述:
,请完善下面程序,使程序的运行结果如下:
Thisisaconstructor!
Thisisaconstructor!
Thevalueofchisa
Thevalueofchisb
Thisisadestructorofb
Thisisadestructorofa
代码:
#include
usingnamespacestd;
classMyClass
{
charch;
public:
MyClass()
{
cout<<"Thisisaconstructor!
"<ch='a';
}
MyClass(charcharacter)
{
cout<<"Thisisaconstructor!
"<ch=character;
}
voidPrint()
{
cout<<"Thevalueofchis"<}
~MyClass()
{
cout<<"Thisisadestructorof"<}
};
intmain()
{
MyClassfirst,second(b);
first.Print();
second.Print();
return0;
}
程序设计
6.题目标题:
计算两点间的距离
题目描述:
仿照本次实验预习的程序填空题1,将以上Distance函数定义为类piont的友元函数,实现程序的功能。
并在主函数中增加输入两点作为友元函数的实参。
其主函数如下:
输入描述:
输入四个数,用空格隔开。
输出描述:
两个点的距离。
样例输入:
1356
样例输出:
5
#include
#include
usingnamespacestd;
classpoint
{
public:
point(floata,floatb){x=a;y=b;}
friendfloatDistance(point&p1,point&p2);
private:
floatx,y;
};
floatDistance(point&p1,point&p2)
{
floatdx=p1.x-p2.x;
floatdy=p1.y-p2.y;
return(float)sqrt(dx*dx+dy*dy);
}
intmain()
{
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<return0;
}
7.题目标题:
日期类CDateInfo的设计。
题目描述:
根据以下主函数的功能来设计日期类CDateInfo,使其能正确运行。
类CDateInfo中应该具有描述年、月、日的三个数据成员和相应的成员函数。
#include
usingnamespacestd;
classCDateibfo
{
intday,month,year;
public:
CDateibfo();
CDateibfo(intyy,intmm,intdd);
voidsetdate(intyy,intmm,intdd);
voidgetdate();
};
CDateibfo:
:
CDateibfo()
{
day=10;
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<}
intmain()
{
CDateibfodate1,date2(2011,10,10);
inty,m,d;
cin>>y>>m>>d;
date1.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中的一个整数;第三行为一个整数,表示年龄。
输出描述:
按主函数要求输出。
#include
#include
usingnamespacestd;
classStudent
{
private:
charname[20];
intsex;
unsignedold;
public:
voidSetName(char*chOne);
voidSetGender(intisex);
voidSetAge(unsignediold);
voidGetName();
voidGetGender();
voidGetAge();
};
voidStudent:
:
SetName(char*chOne)
{
strcpy(name,chOne);
}
voidStudent:
:
SetGender(intisex)
{
sex=isex;
}
voidStudent:
:
SetAge(unsignediold)
{
old=iold;
}
voidStudent:
:
GetName()
{
cout<<"Zhang_San'snameis"<}
voidStudent:
:
GetGender()
{
cout<<"Zhang_San'sgenderis"<}
voidStudent:
:
GetAge()
{
cout<<"Zhang_San'sageis"<}
intmain()
{
StudentZhang_San;
char*chOne;
intiSex;
unsignediOld;
chOne=newchar[11];
cin>>chOne;
cin>>iSex;
cin>>iOld;
Zhang_San.SetName(chOne);
Zhang_San.SetGender(iSex);
Zhang_San.SetAge(iOld);
cout<Zhang_San.GetName();
Zhang_San.GetGender();
Zhang_San.GetAge();
return0;
}
样例输入:
ZhangSan
0
20
样例输出:
Zhang_San'snameisZhangSan
Zhang_San'sgenderis0
Zhang_San'sageis20
9.题目标题:
计算器类Calculator的设计
题目描述:
根据以下主函数的功能来设计计算器类Calculator,使其能正确运行。
类Calculator中应该具有描述运算数的a和b及其表示a和b运算结果的三个数据成员和相应计算并显示结果的成员函数。
#include
usingnamespacestd;
classCalculator
{
floatx,y;
public:
Calculator(inta,intb)
{
x=a;
y=b;
};
voidadd()
{
cout<};
voidsubtract()
{
cout<};
voidmultiply()
{
cout<};
voiddivide()
{
cout<};
};
intmain()
{
floata,b;
cin>>a>>b;
Calculatorcal(a,b);
cal.add();
cal.subtract();
cal.multiply();
cal.divide();
return0;
}
输入描述:
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()显示结果。
#include
#include
usingnamespacestd;
classImaginary
{
floatx,y;
public:
Imaginary(inta,intb)
{
x=a;
y=b;
};
friendvoidAdd(Imaginary&p,Imaginary&b);
friendvoidSub(Imaginary&p,Imaginary&q);
};
voidAdd(Imaginary&p,Imaginary&q)
{
cout<
}
voidSub(Imaginary&p,Imaginary&q)
{
cout<
}
intmain()
{
floatx1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
Imaginaryimag1(x1,y1),imag2(x2,y2);
Add(imag1,imag2);
Sub(imag1,imag2);
return0;
}
输入描述:
输入4个数据,分别表示进行运算的两个复数的实部和虚部
输出描述:
4行,分别显示两个复数进行加、减、乘、除运算的结果
样例输入:
1325
样例输出:
3+8i
-1-2i