C++实验题.docx

上传人:b****4 文档编号:11872493 上传时间:2023-04-06 格式:DOCX 页数:31 大小:22.57KB
下载 相关 举报
C++实验题.docx_第1页
第1页 / 共31页
C++实验题.docx_第2页
第2页 / 共31页
C++实验题.docx_第3页
第3页 / 共31页
C++实验题.docx_第4页
第4页 / 共31页
C++实验题.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

C++实验题.docx

《C++实验题.docx》由会员分享,可在线阅读,更多相关《C++实验题.docx(31页珍藏版)》请在冰豆网上搜索。

C++实验题.docx

C++实验题

试题查看

标题:

抽象类与操作符重载

时限:

3000ms

内存限制:

10000K

总时限:

3000ms

描述:

定义表示形状的抽象类及相应的派生类,并实现相关操作符重载。

(1)定义表示形状的抽象类Shape:

添加公有成员函数doubleArea(),用于计算形状面积;定义为纯虚函数;

添加公有成员函数voidShow(),用于显示形状信息,定义为纯虚函数;

定义虚的析构函数;

重载比较操作符:

==、>和<,用于比较两个形状面积的大小关系,返回值类型为bool,可以定义为成员函数或友元函数。

(2)从形状类Shape派生矩形类Rectangle:

添加double型的保护数据成员:

rectWidth和rectHeight,分别表示矩形的宽度和高度;

定义带参构造函数;

重定义公有成员函数Show,打印矩形的宽度和高度,输出格式为“W:

宽度;H:

高度;Area:

面积”;

重定义公有成员函数Area,计算矩形面积。

(3)从形状类Shape派生椭圆类Ellipse:

添加double型的保护数据成员:

rectWidth和rectHeight,分别表示椭圆外接矩形的宽度和高度;

定义带参构造函数;

重定义公有成员函数Show,打印椭圆外接矩形的宽度和高度,输出格式为“W:

宽度;H:

高度;Area:

面积”;

重定义公有成员函数Area,计算椭圆面积。

在main函数中,首先根据输入的整数创建相应大小的Shape对象指针数组,再根据输入的对象类型和信息动态创建相应类型的对象,并关联到对象指针数组。

输入的信息格式如下:

3//对象指针数组的元素个数

R2317//对象类型、形状宽度、形状高度,R表示矩形对象

R8925//对象类型、形状宽度、形状高度,R表示矩形对象

E1729//对象类型、形状宽度、形状高度,E表示椭圆对象

接着通过调用Show成员函数输出所有对象的信息。

然后输出面积相等的形状对象的信息(要求使用重载的运算符“==”来判断对象的面积是否相等),输出格式如下:

AreaofShape[i]isequaltoShape[j]

最后将所有形状对象按面积从大到小排序(要求使用重载的运算符“>”来判断对象的面积的大小关系),并输出排序后的对象信息。

输入:

对象数目

对象类型对象的外接矩形宽度对象的外接矩形高度

输出:

排序前的对象信息

面积相等的对象信息

排序后的对象信息

输入样例:

6

R2317

R8925

R1723

E2917

E8975

E1729

输出样例:

W:

23;H:

17;Area:

391

W:

89;H:

25;Area:

2225

W:

17;H:

23;Area:

391

W:

29;H:

17;Area:

387.201

W:

89;H:

75;Area:

5242.53

W:

17;H:

29;Area:

387.201

AreaofShape[0]isequaltoShape[2]

AreaofShape[3]isequaltoShape[5]

W:

89;H:

75;Area:

5242.53

W:

89;H:

25;Area:

2225

W:

17;H:

23;Area:

391

W:

23;H:

17;Area:

391

W:

29;H:

17;Area:

387.201

W:

17;H:

29;Area:

387.201

提示:

来源:

#include

usingnamespacestd;

classShape

{

public:

virtualdoubleArea()=0;

virtualvoidShow()=0;

friendbooloperator==(Shape&shop1,Shape&shop2)

{

return(shop1.Area()==shop2.Area());

}

friendbooloperator>(Shape&shop1,Shape&shop2)

{

return(shop1.Area()>shop2.Area());

}

friendbooloperator<(Shape&shop1,Shape&shop2)

{

return(shop1.Area()

}

virtual~Shape()

{

}

};

classRectangle:

publicShape

{

public:

Rectangle(doublerectwidth,doublerectheight)

{

rectWidth=rectwidth;

rectHeight=rectheight;

}

doubleArea()

{

doublearea=rectWidth*rectHeight;

returnarea;

}

voidShow()

{

cout<<"W:

"<

"<

"<

}

protected:

doublerectWidth;

doublerectHeight;

};

classEllipse:

publicShape

{

public:

Ellipse(doublerectwidth,doublerectheight)

{

rectWidth=rectwidth;

rectHeight=rectheight;

}

~Ellipse()

{

}

doubleArea()

{

doublearea=3.1415926*(rectWidth/2)*(rectHeight/2);

returnarea;

}

voidShow()

{

cout<<"W:

"<

"<

"<

}

protected:

doublerectWidth;

doublerectHeight;

};

intmain()

{

intnumber;

doublerectWidth;

doublerectHeight;

chartype;

cin>>number;

Shape*P[number];

for(inti=0;i

{

cin>>type>>rectWidth>>rectHeight;

if(type=='R')

{

P[i]=newRectangle(rectWidth,rectHeight);

}

elseif(type=='E')

{

P[i]=newEllipse(rectWidth,rectHeight);

}

else

{

cout<<"输入类型错误,请重新输入!

"<

i=i-1;

}

}

for(inti=0;i

{

P[i]->Show();

}

for(inti=0;i

{

for(intj=i+1;j

{

if(*P[i]==*P[j])

{

cout<<"AreaofShape["<

}

}

}

Shape*base;

for(inti=0;i

{

for(intj=0;j

{

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;

}

for(inti=0;i

{

P[i]->Show();

}

for(inti=0;i

{

deleteP[i];

}

return0;

}

试题查看

标题:

虚函数

时限:

3000ms

内存限制:

10000K

总时限:

3000ms

描述:

利用虚函数实现多态:

(1)设计Person类,要求具有用于表示姓名的保护数据成员:

stringszName;实现信息打印的公有成员函数:

voidPrint()。

其中,Print函数设计为虚函数,输出的信息格式为:

“Person姓名”。

(2)从Person类派生Student类,添加用于表示学号的保护数据成员:

intiNumber;重定义用于信息打印的公有成员函数:

voidPrint()。

其中,Print函数输出的信息格式为:

“Student姓名学号

”。

(3)从Person类派生Teacher类,添加用于表示教龄的保护数据成员:

intiYear;重定义用于信息打印的公有成员函数:

voidPrint()。

其中,Print函数输出的信息格式为:

“Teacher姓名教龄

”。

(4)从Student类派生Graduate类,添加用于表示研究方向的保护数据成员:

stringszResearch;重定义用于信息打印的公有成员函数:

voidPrint()。

其中,Print函数输出的信息格式为:

“Graduate姓名研究方向

”。

在main函数中根据用输入的整数动态创建一个Person类的对象指针数组。

用户依次输入对象信息(对象类别及其相应的数据成员值),根据对象类别动态创建相应的对象并赋给相应的对象指针数组元素。

全部录入后,根据用户输入要显示的对象信息在数组中的位置,调用Print函数在屏幕上打印出相应对象的信息。

如果用户输入“exit”,则退出。

输入:

对象指针数组的长度;

对象类型及对象信息(输入方式见输入样例);

要显示的对象在数组中的位置;

exit。

输出:

用户要求显示的对象信息。

输入样例:

4

PersonZhang

StudentZhao200905

GraduateLi200905DataMining

TeacherLuo10

0

2

exit

输出样例:

PersonZhang

GraduateLi200905DataMining

提示:

基类的成员函数Print()定义成虚函数。

来源:

#include

usingnamespacestd;

#include

#include

classPerson

{

public:

Person(stringname)

{

szName=name;

}

virtualvoidPrint()

{

cout<<"Person"<

}

virtual~Person()

{

}

protected:

stringszName;

};

classStudent:

publicPerson

{

public:

Student(stringname,intnumber):

Person(name)

{

iNumber=number;

}

voidPrint()

{

cout<<"Student"<

}

virtual~Student()

{

}

protected:

intiNumber;

};

classTeacher:

publicPerson

{

public:

Teacher(stringname,intIYear):

Person(name)

{

iYear=IYear;

}

voidPrint()

{

cout<<"Teacher"<

}

virtual~Teacher()

{

}

protected:

intiYear;

};

classGraduate:

publicStudent

{

public:

Graduate(stringname,intnumber,stringSzResearch):

Student(name,number)

{

szResearch=SzResearch;

}

voidPrint()

{

cout<<"Graduate"<

}

protected:

stringszResearch;

};

intmain()

{

intNumber;//记录个数

stringtype;

intiNumber;

stringname;

intiYear;

stringszResearch;

cin>>Number;

Person*P[Number];

for(inti=0;i

{

cin>>type;

if(type=="Person")

{

cin>>name;

P[i]=newPerson(name);

}

elseif(type=="Student")

{

cin>>name>>iNumber;

P[i]=newStudent(name,iNumber);

}

elseif(type=="Graduate")

{

cin>>name>>iNumber>>szResearch;

P[i]=newGraduate(name,iNumber,szResearch);

}

elseif(type=="Teacher")

{

cin>>name>>iYear;

P[i]=newTeacher(name,iYear);

}

else

{

cout<<"输入类型错误,请重新输入!

"<

i=i-1;

}

}

stringchoice;

cin>>choice;

while(choice!

="exit")

{

intChoice=atoi(choice.c_str());

if(Choice>=Number)

{

cout<<"输入越界,请重新输入!

"<

}

else

{

P[Choice]->Print();

}

cin>>choice;

}

for(inti=0;i

{

deleteP[i];

}

return0;

}

试题查看

标题:

操作符重载

时限:

3000ms

内存限制:

10000K

总时限:

3000ms

描述:

定义有理数类(分母不为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++:

4/3

a--:

7/3

a

true

a<=b:

true

a>b:

false

a>=b:

false

提示:

来源:

#include

usingnamespacestd;

classRational

{

public:

Rational()

{

Reduce(*this);

}

Rational(intup,intdown)

{

iUp=up;

iDown=down;

Reduce(*this);

}

Rational&operator-()

{

iUp=-iUp;

return*this;

}

Rational&operator=(Rational&p)

{

if(this==&p)

{

return*this;

}

iUp=p.iUp;

iDown=p.iDown;

return*this;

}

Rational&operator++()

{

iUp=iUp+iDown;

Reduce(*this);

return*this;

}

Rationaloperator++(int)

{

Rationalbefore(iUp,iDown);

iUp=iUp+iDown;

Reduce(*this);

returnbefore;

}

Rational&operator--()

{

iUp=iUp-iDown;

Reduce(*this);

return*this;

}

Rationaloperator--(int)

{

Rationalbefore(iUp,iDown);

iUp=iUp-iDown;

Reduce(*this);

returnbefore;

}

friendRationaloperator+(Rational&q,Rational&p);

friendRationaloperator-(Rational&q,Rational&p);

friendRationaloperator*(Rational&q,Rational&p);

friendRationaloperator/(Rational&q,Rational&p);

friendbooloperator<(Rational&q,Rational&p);

friendbooloperator>(Rational&q,Rational&p);

friendbooloperator>=(Rational&q,Rational&p);

friendbooloperator<=(Rational&q,Rational&p);

friendistream&operator>>(istream&in,Rational&p);

friendostream&operator<<(ostream&out,Rational&p);

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&q,Rational&p)

{

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&q,Rational&p)

{

intsmall=q.Gcd(p.iDown,q.iDown)*q.iDown*p.iDown;

Rationalback;

back.iUp=small/p.iDown*p.iUp;

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

当前位置:首页 > 高中教育 > 英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1