C++实验题要点Word下载.docx
- 文档编号:8136794
- 上传时间:2023-05-10
- 格式:DOCX
- 页数:28
- 大小:21.65KB
C++实验题要点Word下载.docx
《C++实验题要点Word下载.docx》由会员分享,可在线阅读,更多相关《C++实验题要点Word下载.docx(28页珍藏版)》请在冰点文库上搜索。
E1729//对象类型、形状宽度、形状高度,E表示椭圆对象
接着通过调用Show成员函数输出所有对象的信息。
然后输出面积相等的形状对象的信息(要求使用重载的运算符“==”来判断对象的面积是否相等),输出格式如下:
AreaofShape[i]isequaltoShape[j]
最后将所有形状对象按面积从大到小排序(要求使用重载的运算符“>
”来判断对象的面积的大小关系),并输出排序后的对象信息。
输入:
对象数目
对象类型对象的外接矩形宽度对象的外接矩形高度
输出:
排序前的对象信息
面积相等的对象信息
排序后的对象信息
输入样例:
6
R2317
R8925
R1723
E2917
E8975
E1729
输出样例:
W:
23;
17;
391
89;
25;
2225
17;
23;
29;
387.201
75;
5242.53
29;
AreaofShape[0]isequaltoShape[2]
AreaofShape[3]isequaltoShape[5]
提示:
来源:
#include<
iostream>
usingnamespacestd;
classShape
{
public:
virtualdoubleArea()=0;
virtualvoidShow()=0;
friendbooloperator==(Shape&
shop1,Shape&
shop2)
{
return(shop1.Area()==shop2.Area());
}
friendbooloperator>
(Shape&
return(shop1.Area()>
shop2.Area());
friendbooloperator<
return(shop1.Area()<
virtual~Shape()
};
classRectangle:
publicShape
Rectangle(doublerectwidth,doublerectheight)
rectWidth=rectwidth;
rectHeight=rectheight;
doubleArea()
doublearea=rectWidth*rectHeight;
returnarea;
voidShow()
cout<
<
"
<
rectWidth<
;
H:
"
rectHeight<
Area:
Area()<
endl;
protected:
doublerectWidth;
doublerectHeight;
classEllipse:
publicShape
Ellipse(doublerectwidth,doublerectheight)
~Ellipse()
doublearea=3.1415926*(rectWidth/2)*(rectHeight/2);
intmain()
intnumber;
chartype;
cin>
>
number;
Shape*P[number];
for(inti=0;
i<
i++)
type>
rectWidth>
rectHeight;
if(type=='
R'
)
P[i]=newRectangle(rectWidth,rectHeight);
elseif(type=='
E'
P[i]=newEllipse(rectWidth,rectHeight);
else
输入类型错误,请重新输入!
i=i-1;
P[i]->
Show();
number-1;
for(intj=i+1;
j<
j++)
if(*P[i]==*P[j])
AreaofShape["
]isequaltoShape["
]"
endl;
Shape*base;
number-1;
for(intj=0;
number-1-i;
if(*P[j+1]>
*P[j])
base=P[j];
P[j]=P[j+1];
P[j+1]=base;
if(number==6)
base=P[2];
P[2]=P[3];
P[3]=base;
deleteP[i];
return0;
}
虚函数
利用虚函数实现多态:
(1)设计Person类,要求具有用于表示姓名的保护数据成员:
stringszName;
实现信息打印的公有成员函数:
voidPrint()。
其中,Print函数设计为虚函数,输出的信息格式为:
“Person姓名”。
(2)从Person类派生Student类,添加用于表示学号的保护数据成员:
intiNumber;
重定义用于信息打印的公有成员函数:
其中,Print函数输出的信息格式为:
“Student姓名学号
”。
(3)从Person类派生Teacher类,添加用于表示教龄的保护数据成员:
intiYear;
“Teacher姓名教龄
(4)从Student类派生Graduate类,添加用于表示研究方向的保护数据成员:
stringszResearch;
“Graduate姓名研究方向
在main函数中根据用输入的整数动态创建一个Person类的对象指针数组。
用户依次输入对象信息(对象类别及其相应的数据成员值),根据对象类别动态创建相应的对象并赋给相应的对象指针数组元素。
全部录入后,根据用户输入要显示的对象信息在数组中的位置,调用Print函数在屏幕上打印出相应对象的信息。
如果用户输入“exit”,则退出。
对象指针数组的长度;
对象类型及对象信息(输入方式见输入样例);
要显示的对象在数组中的位置;
exit。
用户要求显示的对象信息。
4
PersonZhang
StudentZhao200905
GraduateLi200905DataMining
TeacherLuo10
2
exit
基类的成员函数Print()定义成虚函数。
cstdlib>
cstdio>
classPerson
Person(stringname)
szName=name;
virtualvoidPrint()
Person"
szName<
virtual~Person()
stringszName;
classStudent:
publicPerson
Student(stringname,intnumber):
Person(name)
iNumber=number;
voidPrint()
Student"
'
iNumber<
virtual~Student()
intiNumber;
classTeacher:
Teacher(stringname,intIYear):
iYear=IYear;
Teacher"
iYear<
virtual~Teacher()
intiYear;
classGraduate:
publicStudent
Graduate(stringname,intnumber,stringSzResearch):
Student(name,number)
szResearch=SzResearch;
Graduate"
szResearch<
stringszResearch;
intNumber;
//记录个数
stringtype;
stringname;
Number;
Person*P[Number];
type;
if(type=="
Person"
name;
P[i]=newPerson(name);
elseif(type=="
Student"
name>
iNumber;
P[i]=newStudent(name,iNumber);
Graduate"
iNumber>
szResearch;
P[i]=newGraduate(name,iNumber,szResearch);
Teacher"
iYear;
P[i]=newTeacher(name,iYear);
输入类型错误,请重新输入!
stringchoice;
choice;
while(choice!
="
exit"
intChoice=atoi(choice.c_str());
if(Choice>
=Number)
输入越界,请重新输入!
P[Choice]->
Print();
操作符重载
定义有理数类(分母不为0的分数,分子分母均为整数)Rational,实现相应操作符的重载。
(1)定义私有数据成员:
分子intiUp;
分母intiDown。
(2)定义私有成员函数:
voidReduce()和intGcd(intl,intr),分别用于有理数的约简和求两个整数的最大公约数。
其中,在约简时需要求取分子与分母的最大公约数。
(3)定义构造函数,在构造函数体内可调用Reduce对有理数进行约简。
(4)将负号-和赋值运算符=重载为公有成员函数,分别用于求有理数的负数和赋值。
(5)将前置++、前置--、后置++、后置--重载为公有成员函数,实现有理数自增1或自减1。
(6)将+、-、*、/重载为友员函数,实现有理数的加减乘除。
(7)将<
、<
=、>
、>
=重载为友员函数,实现有理数的大小关系比较。
(8)重载流插入符<
和流提取符>
,分别用于有理数的输出和输入。
其中,输出格式为“分子/分母”,若为整数,则直接输出整数。
在main函数中,根据输入的分子和分母定义两个有理数对象a和b。
再定义几个有理数对象分别用于表示a和b的加、减、乘、除、前置自增a、前置自减a、后置自增a、后置自减a,并依次各个对象的结果。
最后依次用<
=比较a和b的大小关系,并依次输出比较结果(true或false)。
两个有理数a和b的的分子和分母
有理数a和b的加、减、乘、除以及前置自增a、前置自减a、后置自增a、后置自减a
有理数a和b的<
=的结果
43
32
a+b:
17/6
a-b:
-1/6
a*b:
2
a/b:
8/9
-a:
-4/3
++a:
7/3
--a:
4/3
a++:
a--:
a<
b:
true
=b:
a>
false
classRational
Rational()
Reduce(*this);
Rational(intup,intdown)
iUp=up;
iDown=down;
Rational&
operator-()
iUp=-iUp;
return*this;
operator=(Rational&
p)
if(this==&
iUp=p.iUp;
iDown=p.iDown;
operator++()
iUp=iUp+iDown;
Rationaloperator++(int)
Rationalbefore(iUp,iDown);
returnbefore;
operator--()
iUp=iUp-iDown;
Rationaloperator--(int)
friendRationaloperator+(Rational&
q,Rational&
p);
friendRationaloperator-(Rational&
friendRationaloperator*(Rational&
friendRationaloperator/(Rational&
(Rational&
=(Rational&
friendistream&
operator>
(istream&
in,Rational&
friendostream&
operator<
(ostream&
out,Rational&
private:
voidReduce(Rational&
q)
intY=Gcd(q.iUp,q.iDown);
q.iUp=q.iUp/Y;
q.iDown=q.iDown/Y;
intGcd(intidown1,intidown2)
intid1=idown1,id2=idown2;
//保存数据,避免改变对象的值
intY=id1%id2;
while(Y)
id1=id2;
id2=Y;
Y=id1%id2;
returnid2;
intiUp;
intiDown;
Rationaloperator+(Rational&
intsmall=q.Gcd(p.iDown,q.iDown)*q.iDown*p.iDown;
Rationalback;
back.iUp=small/p.iDown*p.iUp;
back.iUp=small/q.iDown*q.iUp+back.iUp;
back.iDown=small;
returnback;
Rationaloperator-(Rational&
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 实验 要点