C++面向对象程序设计 期末考试 编程题.docx

上传人:b****6 文档编号:3033397 上传时间:2022-11-17 格式:DOCX 页数:34 大小:29.08KB
下载 相关 举报
C++面向对象程序设计 期末考试 编程题.docx_第1页
第1页 / 共34页
C++面向对象程序设计 期末考试 编程题.docx_第2页
第2页 / 共34页
C++面向对象程序设计 期末考试 编程题.docx_第3页
第3页 / 共34页
C++面向对象程序设计 期末考试 编程题.docx_第4页
第4页 / 共34页
C++面向对象程序设计 期末考试 编程题.docx_第5页
第5页 / 共34页
点击查看更多>>
下载资源
资源描述

C++面向对象程序设计 期末考试 编程题.docx

《C++面向对象程序设计 期末考试 编程题.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计 期末考试 编程题.docx(34页珍藏版)》请在冰豆网上搜索。

C++面向对象程序设计 期末考试 编程题.docx

C++面向对象程序设计期末考试编程题

1、编写一个程序根据输入的三角形的三条边判断是否能组成三角形,如果可以则输出它的面积和三角形类型(等边、等腰、直角三角形)。

#include

#include

usingnamespacestd;

intmain()

{

doublea,b,c;

doublev,p;

cout<<"请输入三角形三条边:

"<

cin>>a>>b>>c;

if(a+b>c&&a+c>b&&b+c>a)

{

p=(a+b+c)/2;

v=sqrt(p*(p-a)*(p-b)*(p-c));

cout<<"该三角形面积是"<

if(a==b&&a==c)

cout<<"该三角形是等边三角形!

"<

else

if(a==b&&a!

=c||a==c&&a!

=b||b==c&&b!

=a)

cout<<"该三角形是等腰三角形!

"<

if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(c*c+b*b==a*a))

cout<<"该三角形是直角三角形!

"<

}

else

cout<<"这三条边组不成三角形!

"<

return0;

}

2、定义一个学生类,其中有3个数据成员:

学号、姓名、年龄,以及若干成员函数。

同时编写main函数使用这个类,实现对学生数据的赋值和输出。

#include

#include

usingnamespacestd;

classstudent

{

intnum;

stringname;

intage;

public:

student(){num=0;name='\0';age=0;}

student(int,string,int);

voidshow();

};

student:

:

student(inta,stringb,intc):

num(a),name(b),age(c){}

voidstudent:

:

show()

{

cout<<"studentnumber:

"<

cout<<"name:

"<

cout<<"age:

"<

}

intmain()

{

students1(200803986,"梅寒芳",23);

s1.show();

return0;

}

3、从键盘输入若干个学生成绩,统计并输出最高成绩和最低成绩,当输入负数时结束输入。

#include

usingnamespacestd;

intmain()

{

doublea[100];

doublemax=0,min=100,t;

inti;

for(i=0;i<100;i++)

{

cin>>a[i];

if(a[i]<0)

break;

else

{

if(a[i]>max)

max=a[i];

if(a[i]

min=a[i];

}

}

cout<<"最大值是:

"<

cout<<"最小值是:

"<

return0;

}

4、编写一个程序,从键盘输入半径和高,输出圆柱体的底面积和体积。

#include

usingnamespacestd;

intmain()

{

doublea,h,s,v;

cout<<"半径为:

"<

cin>>a;

cout<<"高为:

"<

cin>>h;

s=3.14159*a*a;

v=s*h;

cout<<"底面积为:

"<

cout<<"体积为:

"<

return0;

}

5、编写一个程序,输入年、月,打印出该年份该月的天数。

#include

main()

{

inty,m,d;

printf("yearmonth=");scanf("%d%d",&y,&m);

switch(m){

case1:

case3:

case5:

case7:

case8:

case10:

case12:

d=31;break;

case4:

case6:

case9:

case11:

d=30;break;

case2:

if(y%4==0&&y%100!

=0||y%400==0)d=29;elsed=28;

}

printf("days=%d\n",d);

}

6、编写函数将化氏温度转换为摄氏温度,公式为C=(F-32)*5/9;并在主函数中调用。

#include

usingnamespacestd;

doublefun(doublea);

intmain()

{

doublef=37.5,c;

c=fun(f);

cout<<"华氏温度为:

"<

cout<<"摄氏温度为:

"<

return0;

}

doublefun(doublea)

{

doubleb;

b=(a-32)*5/9;

returnb;

}

7、声明一个Tree(树)类,有成员ages(树龄),成员函数grow(intyears)用以对ages加上years,showage()用以显示tree对象的ages值。

在主函数中定义Tree类对象,并调用成员函数(学生自行指定实参数

#include

usingnamespacestd;

classTree

{

private:

intages;

public:

intgrow(intyears)

{

ages=ages+years;

returnages;

}

voidgetage()

{

cout<<"输入树的树龄:

"<

cin>>ages;

}

voidshowage()

{cout<<"该树的年龄是:

"<

};

intmain()

{

Treeages,years;

ages.getage();

ages.grow(5);

ages.showage();

return0;

}

8、定义一个复数类,用友元函数实现对双目运算符“+”的运算符重载,使其适用于复数运算。

#include

classComplex

{

private:

doublereal;

doubleimag;

public:

Complex(){real=0;imag=0;}

Complex(doubler,doublei):

real(r),imag(i){}

friendComplexoperator+(Complex&c1,Complex&c2);

voiddisplay();

};

voidComplex:

:

display()

{

cout<

}

Complexoperator+(Complex&c1,Complex&c2)

{

returnComplex(c1.real+c2.real,c1.imag+c2.imag);

}

intmain()

{

Complexc1(3,4);

Complexc2(4,2.3);

Complexc3;

c3=c1+c2;

c3.display();

return0;

}

9、有一个函数如下:

x(x<5)

y=x+6(5<=x<15)

x-6(x>=15)

输入x的值,计算出相应的y值。

#include

usingnamespacestd;

intmain()

{

intx,y;

cin>>x;

if(x<5)y=5;

if(x>=5&&x<15)y=x+6;

if(x>=15)y=x-6;

cout<

return0;

}

10、14、17、使用函数重载的方法定义两个重名函数,分别求出整型数的两数之和和浮点数的两数之和,并在主函数中调用。

#include

usingnamespacestd;

template

Tadd(Ta,Tb)

{

Tc;

c=a+b;

returnc;

}

intmain()

{

inta,b,c;

floatx,y,z;

cout<<"请输入两个整型数:

"<

cin>>a>>b;

cout<<"请输入两个浮点数:

"<

cin>>x>>y;

c=add(a,b);

z=add(x,y);

cout<<"整型数之和是:

"<

cout<<"浮点数之和是:

"<

return0;

}

11、定义一个抽象类shape用以计算面积,从中派生出计算长方形、梯形、圆形面积的派生类。

程序中通过基类指针来调用派生类中的虚函数,计算不同形状的面积。

#include

#definePI3.1415926

usingnamespacestd;

classShape

{

public:

voidshow();

protected:

doubles;

};

voidShape:

:

show()

{

cout<<"面积:

"<

}

classCircle:

publicShape

{

public:

voidGetArea();

Circle(double);

private:

doubler;

};

Circle:

:

Circle(doublea)

{

r=a;

}

voidCircle:

:

GetArea()

{

s=r*r*PI;

}

intmain()

{

CircleC(6);

C.GetArea();

C.show();

return0;

}

12、定义计数器类Counter。

要求具有以下成员:

计数器值;可进行增值和减值记数;可提供记数值。

#include

usingnamespacestd;

classCounter

{

public:

Counter(int);

Counteroperator++();

Counteroperator--();

voiddisplay();

private:

inti;

};

Counter:

:

Counter(inta)

{

i=a;

}

voidCounter:

:

display()

{

cout<

}

CounterCoun

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

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

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

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