C++例题及答案.docx

上传人:b****3 文档编号:2916275 上传时间:2022-11-16 格式:DOCX 页数:14 大小:19.19KB
下载 相关 举报
C++例题及答案.docx_第1页
第1页 / 共14页
C++例题及答案.docx_第2页
第2页 / 共14页
C++例题及答案.docx_第3页
第3页 / 共14页
C++例题及答案.docx_第4页
第4页 / 共14页
C++例题及答案.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

C++例题及答案.docx

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

C++例题及答案.docx

C++例题及答案

C++例题及答案

作业1:

输入两个变量,输出两个变量的最大值。

要求:

使用C++语言,使用max函数完成求最大值的操作。

#includeintmax(intx,inty)

{return(x>y?

x:

y);}

intmain(intargc,char*argv[])

{

inta,b;

coutcin>>a>>b;

coutcin>>a;

return0;

}

作业2:

(1)输入下列简单C++程序,完成编译、连接、运行,熟悉C++程序的开发过程。

#includeusingnamespacestd;

constfloatPI=3.1416;

voidmain()

{

intiType;

floatradius,a,b,area;

coutcin>>iType;

switch(iType)

{

case1:

coutcin>>radius;

area=PI*radius*radius;

coutbreak;

case2:

coutcin>>a;

coutcin>>b;

area=a*b;

coutbreak;

case3:

coutcin>>a;

area=a*a;

coutbreak;

default:

cout}

}

(2)改写以上程序,要求:

编写三个内联函数分别完成1-圆形2-长方形3-正方形的求面积操作。

(3)改写以上程序,要求:

编写三个函数分别完成1-圆形2-长方形3-正方形的求面积操作,以上三个函数分别写在三个文件中。

(4)思考:

可否使用重载函数完成三种形状的求面积操作?

答案:

(2)改写以上程序,要求:

编写三个内联函数分别完成1-圆形2-长方形3-正方形的求面积操作。

#includeusingnamespacestd;

constdoublePI=3.1416;

inlinefloatcirlearea(float);

inlinefloatrectarea(float);

inlinefloatzrectarea(float);

voidmain()

{

intiType;

floatradius,a,b,area;

coutcin>>iType;

switch(iType)

{

case1:

coutcin>>radius;

area=cirlearea(radius);

coutbreak;

case2:

coutcin>>a;

coutcin>>b;

area=a*b;

coutbreak;

case3:

coutcin>>a;

area=a*a;

coutbreak;

default:

cout}

}

floatcirlearea(floatr)

{

floatarea;

returnarea=PI*r*r;

}

floatrectarea(floata,floatb)

{

floatarea;

returnarea=a*b;

}

floatzrectarea(floata)

{

area=a*a;

}

(3)改写以上程序,要求:

编写三个函数分别完成1-圆形2-长方形3-正方形的求面积操作,以上三个函数分别写在三个文件中。

#includeusingnamespacestd;

constdoublePI=3.1416;

inlinefloatcirlearea(float,float);

inlinefloatrectarea(float,float);

inlinefloatzrectarea(float,float);

voidmain()

{

intiType;

floatradius,a,b,area;

float(*carea)(float,float);

coutcin>>iType;

switch(iType)

{

case1:

carea=cirlearea;

coutcin>>radius;

area=(*carea)(radius,a=0);

coutbreak;

case2:

carea=rectarea;

coutcin>>a;

coutcin>>b;

area=(*carea)(a,b);

coutbreak;

case3:

carea=zrectarea;

coutcin>>a;

area=(*carea)(a,a);

coutbreak;

default:

cout}

}

floatcirlearea(floatr,floatdef)

{

floatarea;

returnarea=PI*r*r;

}

floatrectarea(floata,floatb)

{

returnarea=a*b;

}

floatzrectarea(floata,floatdef)

{

floatarea;

returnarea=a*a;

}

作业3:

(1)设计一个类CRectangle,要求如下所述:

a.该类中的私有成员变量存放Rectangle的左上角x,y和它的长、宽,并且它们的默认值都是10。

b.通过函数设置其长和宽,并确保长和宽在(0,50)范围之内。

c.定义求它的周长的函数Perimeter。

#includeusingnamespacestd;

classRect

{

private:

intlength;

intwidth;

intperim;

public:

Rect()

{

length=10;

width=10;

}

Rect(intlength,intwidth)

{

this->length=length;

this->width=width;

}

voidsetLength(intlength)

{

if(length>0&&length{

this->length=length;

}

else

{

cout}

}

voidsetWidth(intwidth)

{

if(width>0&&width{

this->width=width;

}

else

{

cout}

}

floatpermi()

{

returnlength*width;

}

};

intmain()

{

Recta;

a.setLength(20);

a.setWidth(20);

coutcin.get();

return0;

}

(2)在实验任务一的基础上,要求有如下成员函数。

a.Move:

从一个位置移动到另一个位置。

b.Size:

改变矩形的大小。

c.Where:

返回矩形左上角的坐标值。

d.Area:

计算面积。

claseCRectangle

{

public:

voidMove(intdx,intdy){left+=dx;top+=dy;};

voidSize(intnewW,intnewH){width=newW;height=newH;};

voidWhere(int&x,int&y){x=left;y=top;};

intArea(){return(width*height);};

private:

intleft,top;//矩形的左上角横坐标和纵坐标

intwidth,height;//矩形的宽度和高度

};

作业4:

声明一个CPU类,要求:

(1)包含主频(frequency),字长(wordlength),CPU倍频系数(coefficient)属性,其中字长为枚举型enumcpu_wordlen={W16,W32,W64,W128,W256},frequency是单位为GHz的实数,coefficient为浮点型数据。

两个公有成员函数run和stop分别表示CPU的运行与停止。

(2)请在构造函数(带参数和不带参数)、拷贝构造函数、析构函数、run和stop函数体给出相应的提示(输出提示字符串,例如“theCPUisrunning!

”)。

(3)说明并实现这个类,观察构造函数、拷贝构造函数和析构函数的调用顺序。

主函数内容如下所示:

intmain()

{

doublex=3.2,y=4.5;

CPUmycpu;

mycpu.run();

mycpu.stop();

CPUhiscpu(x,W64,y);

hiscpu.run();

hiscpu.stop();

CPUhercpu(hiscpu);

hercpu.run();

hercpu.stop();

return0;

}

答案:

#includeusingnamespacestd;

enumcpu_wordlen{W16,W32,W64,W128,W256};

classCPU

{

public:

CPU()

{

frequency=0;

wordlength=W16;

coefficient=0;

coutcout}

CPU(floatf,cpu_wordlenl,floatc)

{

frequency=f;

wordlength=l;

coefficient=c;

coutcout}

CPU(CPU&C)

{

frequency=C.frequency;

wordlength=C.wordlength;

coefficient=C.coefficient;

cout}

~CPU()

{

cout}

voidrun(){

cout}

voidstop(){

cout}

private:

doublefrequency;

enumcpu_wordlenwordlength;

doublecoefficient;

};

intmain()

{

doublex=3.2,y=4.5;

CPUmycpu;

mycpu.run();

mycpu.stop();

CPUhiscpu(x,W64,y);

hiscpu.run();

hiscpu.stop();

CPUhercpu(hiscpu);

hercpu.run();

hercpu.stop();

return0;

}

作业5:

声明一个学生信息类:

要求有学生的基本信息(自定),以及显示和设置这些信息的接口(公有成员函数),要求定义构造函数(带参数和不带参数)和析构函数以及复制构造函数。

并进行测试。

(2)执行下列程序,观察结果,并给出所有对象的构造、复制构造和析构函数的调用顺序。

#includeclasspoint

{intx,y;

public:

point(inta,intb)

{x=a;y=b;

cout}

point(point&p);

friendpointmove(pointq);

~point(){coutintgetx(){returnx;}

intgety(){returny;}

};

point:

:

point(point&p)

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

当前位置:首页 > 法律文书 > 调解书

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

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