C++面向对象程序设计 编程题Word文件下载.docx
- 文档编号:22679341
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:40
- 大小:22.30KB
C++面向对象程序设计 编程题Word文件下载.docx
《C++面向对象程序设计 编程题Word文件下载.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计 编程题Word文件下载.docx(40页珍藏版)》请在冰豆网上搜索。
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()
studentnumber:
ends<
num<
name:
name<
age:
age<
students1(200803986,"
梅寒芳"
23);
s1.show();
3、从键盘输入若干个学生成绩,统计并输出最高成绩和最低成绩,当输入负数时结束输入。
doublea[100];
doublemax=0,min=100,t;
inti;
for(i=0;
i<
100;
i++)
a[i];
if(a[i]<
0)
break;
if(a[i]>
max)
max=a[i];
min)
min=a[i];
最大值是:
max<
最小值是:
min<
4、编写一个程序,从键盘输入半径和高,输出圆柱体的底面积和体积。
#include<
doublea,h,s,v;
半径为:
a;
高为:
h;
s=3.14159*a*a;
v=s*h;
底面积为:
s<
体积为:
5、编写一个程序,输入年、月,打印出该年份该月的天数。
stdio.h>
main()
inty,m,d;
printf("
yearmonth="
);
scanf("
%d%d"
&
y,&
m);
switch(m){
case1:
case3:
case5:
case7:
case8:
case10:
case12:
d=31;
case4:
case6:
case9:
case11:
d=30;
case2:
if(y%4==0&
y%100!
=0||y%400==0)d=29;
elsed=28;
days=%d\n"
d);
6、编写函数将化氏温度转换为摄氏温度,公式为C=(F-32)*5/9;
并在主函数中调用。
doublefun(doublea);
doublef=37.5,c;
c=fun(f);
华氏温度为:
f<
℉"
摄氏温度为:
c<
℃"
doublefun(doublea)
doubleb;
b=(a-32)*5/9;
returnb;
7、声明一个Tree(树)类,有成员ages(树龄),成员函数grow(intyears)用以对ages加上years,showage()用以显示tree对象的ages值。
在主函数中定义Tree类对象,并调用成员函数(学生自行指定实参数
classTree
private:
intages;
intgrow(intyears)
ages=ages+years;
returnages;
voidgetage()
输入树的树龄:
ages;
voidshowage()
{cout<
该树的年龄是:
ages<
Treeages,years;
ages.getage();
ages.grow(5);
ages.showage();
8、定义一个复数类,用友元函数实现对双目运算符“+”的运算符重载,使其适用于复数运算。
iostream.h>
classComplex
doublereal;
doubleimag;
Complex(){real=0;
imag=0;
Complex(doubler,doublei):
real(r),imag(i){}
friendComplexoperator+(Complex&
c1,Complex&
c2);
voiddisplay();
voidComplex:
display()
real<
+"
imag<
i"
Complexoperator+(Complex&
c2)
returnComplex(c1.real+c2.real,c1.imag+c2.imag);
Complexc1(3,4);
Complexc2(4,2.3);
Complexc3;
c3=c1+c2;
c3.display();
9、有一个函数如下:
输入x的值,计算出相应的y值。
intx,y;
x;
if(x<
5)y=5;
if(x>
=5&
x<
15)y=x+6;
=15)y=x-6;
y<
10、14、17、使用函数重载的方法定义两个重名函数,分别求出整型数的两数之和和浮点数的两数之和,并在主函数中调用。
template<
typenameT>
Tadd(Ta,Tb)
Tc;
c=a+b;
returnc;
inta,b,c;
floatx,y,z;
请输入两个整型数:
b;
请输入两个浮点数:
x>
y;
c=add(a,b);
z=add(x,y);
整型数之和是:
浮点数之和是:
z<
11、定义一个抽象类shape用以计算面积,从中派生出计算长方形、梯形、圆形面积的派生类。
程序中通过基类指针来调用派生类中的虚函数,计算不同形状的面积。
#definePI3.1415926
classShape
protected:
doubles;
voidShape:
面积:
classCircle:
publicShape
voidGetArea();
Circle(double);
doubler;
Circle:
Circle(doublea)
r=a;
voidCircle:
GetArea()
s=r*r*PI;
CircleC(6);
C.GetArea();
C.show();
12、定义计数器类Counter。
要求具有以下成员:
计数器值;
可进行增值和减值记数;
可提供记数值。
#include<
classCounter
Counter(int);
Counteroperator++();
Counteroperator--();
Counter:
Counter(inta)
i=a;
voidCounter:
i;
CounterCounter:
operator++()
returnCounter(++i);
operator--()
returnCounter(--i);
CounterC1(5);
C1="
C1.display();
++C1;
endl<
--C1;
13、输入一个自然数,输出其各因子的连乘形式,如输入12,则输出12=1*2*2*3。
inti,n;
n;
n="
1"
for(i=2;
=n;
)
if(n%i==0)
n=n/i;
*"
continue;
i++;
15、定义一个基类:
点类,包括x坐标和y坐标,从它派生一个圆类,增加数据成员r(半径),圆类成员
函数包括构造函数、求面积的函数和输出显示圆心坐标及圆半径的函数。
classDot
intx;
inty;
Dot(){}
classCircle:
publicDot
intr;
Circle():
voidget();
voidarea();
get()
输入圆心坐标、圆的半径:
y>
r;
area()
doubles=0;
s=3.14159*r*r;
圆的面积是:
圆心坐标是:
("
"
)"
圆的半径是:
r<
Circled;
d.get();
d.area();
d.show();
16、N个整数从小到大排列,输入一个新数插入其中,使N+1个整数仍然有序。
inta[11]={10,20,30,40,50,60,70,80,90,100};
inti,j,n;
原顺序为:
10;
a[i]<
"
请输入一个新数:
if(a[0]>
n)
for(i=9;
i>
=0;
i--)
a[i+1]=a[i];
a[0]=n;
if(a[9]<
n)a[10]=n;
if(n>
a[0]&
n<
a[9])
for(i=1;
9;
a[i])j=i+1;
=j;
a[j]=n;
11;
18、编写一个矩形rectangle类,有数据成员长len和宽wid,构造函数retange(int,int).和友元函数
intarea(rectangleT)和intfun(rectangleT)分别计算给定长方形的周长和面积。
classrectangle
rectangle(int,int);
friendintarea(rectangleT);
friendintfun(rectangleT);
intlen,wid;
rectangle:
rectangle(inta,intb)
len=a;
wid=b;
intarea(rectangleT)
return(T.len*T.wid);
intfun(rectangleT)
return(2*(T.len+T.wid));
rectangleR(20,30);
area(R)<
周长:
fun(R)<
19、定义一个复数类,用友元函数实现对双目运算符+和*的运算符重载,使其适用于复数运算。
cmath>
classCComplex
#defineerr0.00000001
double_x,_y;
CComplex(doublex=0,doubley=0):
_x(x),_y(y){}
CComplexoperator+(constCComplex&
z);
CComplexoperator*(constCComplex&
friendostream&
operator<
(ostream&
os,constCComplex&
friendistream&
operator>
(istream&
is,CComplex&
CComplexz1(0,1.2);
CComplexz2(1,1.2);
z1+z2<
z1*z2<
z1;
z1<
CComplexCComplex:
operator+(constCComplex&
z)
CComplexc;
c._x=_x+z._x;
c._y=_y+z._y;
operator*(constCComplex&
c._x=_x*z._x-_y*z._y;
c._y=_x*z._y+_y*z._x;
ostream&
if(fabs(z._x)>
err)
z._x;
if(fabs(z._y-1)<
(fabs(z._x)>
err?
+i"
elseif(fabs(z._y+1)<
-i"
elseif(z._y>
)<
z._y<
i"
elseif(z._y<
-err)
returnos;
istream&
is>
z._x>
z._y;
returnis;
20、输入10个同学的成绩,统计80分以上和不及格的人数,并输出平均值。
doublea[10],sum=0,var;
inti,j=0,k=0;
请输入10个学生成绩:
sum=sum+a[i];
=80)
j++;
60)
k++;
var=sum/10;
80分以上的人数是:
j<
不及格的人数是:
k<
平均分是:
var<
21、声明一个类String1,其数据成员为charhead[100],构造函数String(char*Head)实现head的初始化,成员函数voidReverse()实现head内字符串的逆序存放,成员函数voidPrint()实现head内字符串的输出。
classString
String(char*Head);
voidReverse();
voidPrint();
charhead[100];
String:
String(char*Head)
inti=0;
char*p=Head;
while(*p!
='
head[i]=*p;
p++;
head[i]='
voidString:
Reverse()
charh[100],*p=head;
)p++;
=head[0])
p--;
h[i]=*p;
h[i]='
while(i>
=0)
head[i]=h[i];
i--;
Print()
char*p=head;
for(;
(*p)!
p++)cout<
(*p);
char*Head="
abcdefgh"
Strings(Head);
正序:
s.Print();
s.Reverse();
逆序:
22编写程序形成如下矩阵。
ê
2ê
1é
4ê
3ê
A=
5ë
û
ú
4321ú
3211ú
ú
2111ú
1111ù
1111
inta[5][5];
intii,jj;
for(ii=0;
ii<
5;
ii++)
for(jj=0;
jj<
jj++)
if(ii-jj<
1)
a[ii][jj]=1;
a[ii][jj]=ii+1-jj;
for(ii=0;
ii<
5;
ii++)
for(jj=0;
jj<
jj++)
a[ii][jj]<
23、定义盒子Box类,要求具有以下成员:
可设置盒子形状;
可计算盒子体积;
可计算盒子的表面积。
classBox
Box(double,double,double);
doublearea();
doublev();
doublex,y,z;
Box:
Box(doublex1,doubley1,doublez1)
x=x
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+面向对象程序设计 编程题 C+ 面向 对象 程序设计 编程