C++面向对象程序设计编程题_第1页
C++面向对象程序设计编程题_第2页
C++面向对象程序设计编程题_第3页
C++面向对象程序设计编程题_第4页
C++面向对象程序设计编程题_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C+面向对象程序设计2010期末考试编程题1、编写一个程序根据输入的三角形的三条边判断是否能组成三角形,如果可以 则输出它的面积和三角形类型(等边、等腰、直角三角形)。#in clude#in cludeusing n amespace std;int main()double a,b,c;double v,p;coutvv请输入三角形三条边: abc;if(a+bc&a+cb&b+ca)p=(a+b+c)/2;v=sqrt(p*(p-a)*(p-b)*(p-c);cout该三角形面积是vendl;if(a=b&a=c)coutvv该三角形是等边三角形!endl;elseif(a=b&a!=c

2、|a=c&a!=b|b=c&b!=a)coutvv该三角形是等腰三角形!endl;if(a*a+b*b=c*c)|(a*a+c*c=b*b)|(c*c+b*b=a*a)coutvv该三角形是直角三角形!endl;elsecoutvv这三条边组不成三角形!endl;return 0;2、定义一个学生类,其中有3个数据成员:学号、姓名、年龄,以及若干成员 函数。同时编写ma in函数使用这个类,实现对学生数据的赋值和输出。#i nclude #in cludeusing n amespace std;class stude ntint num;stri ng n ame;int age;publi

3、c:stude nt() num=0;n ame=0;age=0;stude nt(i nt,stri ng,i nt);void show();stude nt:stude nt(i nt a,stri ng b,i nt c):nu m(a), name(b),age(c) void stude nt:show() coutstude nt nu mber:e nds num e ndl;coutv n ame:e nds n amee ndl;coutage:e ndsvvagevve ndl;int main()student s1(200803986, 梅寒芳,23);s1.show

4、();return 0;3、从键盘输入若干个学生成绩,统计并输出最高成绩和最低成绩,当输入负数 时结束输入。#in cludeusing n amespace std;int main()double a100;double max=0 ,min=100,t;int i;for(i=0;i ai;if(aimax)max=ai;if(aimi n)min=ai;coutvv最大值是:vvmaxvvendl;coutvv最小值是:minvvendl;return 0;4、编写一个程序,从键盘输入半径和高,输出圆柱体的底面积和体积#in cludeusing n amespace std;int

5、main()double a,h,s,v;coutvv半径为: a;coutvv高为: h;s=3.14159*a*a;v=s*h;cout底面积为:vvsvvendl;cout体积为:vendl;return 0;5、编写一个程序,输入年、月,打印出该年份该月的天数。#i ncludemai n()int y,m,d;prin tf(year mon th=);sca nf(%d%d, &y,&m);switch(m)case 1:case 3:case 5:case 7:case 8:case 10:case 12:d=31;break;case 4:case 6:case 9:case

6、11:d=30;break;case 2:if (y%4=0 & y%100!=0 | y%400=0) d=29; else d=28;prin tf(days=%dn,d);6编写函数将化氏温度转换为摄氏温度,公式为 C=(F-32) *5/9 ;并在主函数 中调用。#in cludeusing n amespace std;double fun( double a);int main()double f=37.5,c;c=fu n(f);coutvv华氏温度为:f T endl;cout摄氏温度为:vvcvv C endl;return 0;double fun( double a)do

7、uble b;b=(a-32)*5/9;return b;7、声明一个Tree (树)类,有成员ages(树龄),成员函数grow(int years) 用 以对ages加上years , showage()用以显示tree 对象的ages值。在主函数中 定义Tree类对象,并调用成员函数(学生自行指定实参数#in clude using n amespace std;class Treeprivate:int ages;public:int grow(i nt years)ages=ages+years;retur n ages;void getage()cout输入树的树龄: ages;v

8、oid showage()cout该树的年龄是:vvagesvvendl;int main()Tree ages,years;ages.getage();ages.grow(5);ages.showage();return 0;8、定义一个复数类,用友元函数实现对双目运算符“+”的运算符重载,使其适用于复数运算。#in cludeclass Complexprivate:double real;double imag;public:Complex()real=0;imag=0;Complex(double r,double i):real(r),imag(i)friend Complex op

9、erator+(Complex &c1,Complex &c2);void display();void Complex:display()coutvvrealvv+vvimagvvive ndl;Complex operator+(Complex &c1,Complex &c2) retur n Complex(c1.real+c2.real,c1.imag+c2.imag);int main()Complex c1(3,4);Complex c2(4,2.3);Complex c3;c3=c1+c2;c3.display();return 0;9、有一个函数如下:输入x的值,计算出相应的y

10、值#in cludeusing n amespace std;int main()int x,y;cin x;if(x=5& x=15) y=x-6;coutvvyvve ndl;return 0;10、14、17、使用函数重载的方法定义两个重名函数,分别求出整型数的两数之 和和浮点数的两数之和,并在主函数中调用。#in cludeusing n amespace std;templatevtype name TT add(T a,T b)T c;c=a+b;return c;int main()int a,b,c;float x,y,z;coutvv请输入两个整型数: ab;coutvv请输

11、入两个浮点数: xy;c=add(a,b);z=add(x,y);coutvv整型数之和是:vvcvvendl;coutvv浮点数之和是:vvzvvendl;return 0;11、定义一个抽象类shape用以计算面积,从中派生出计算长方形、梯形、圆形 面积的派生类。程序中通过基类指针来调用派生类中的虚函数,计算不同形状的 面积。#i nclude #define PI 3.1415926using n amespace std;class Shapepublic:void show();protected:double s;void Shape:show()cout面积:sendl;clas

12、s Circle :public Shapepublic:void GetArea();Circle(double);private: double r;Circle:Circle(double a)r=a;void Circle:GetArea()s=r*r*PI;int main()Circle C(6);C.GetArea();C.show();return 0;12、定义计数器类Counter。要求具有以下成员:计数器值;可进行增值和减值记数;可提供记数值。#i nclude using n amespace std;class Coun terpublic:Coun ter(i nt

13、 );Coun ter operator +();Coun ter operator -();void display。;private:int i;;Coun ter:Co un ter(i nt a)i=a;void Coun ter:display()couti;Coun ter Coun ter: operator +()return Coun ter (+i);Coun ter Coun ter: operator -()return Cou nter(-i);int main()Cou nter C1(5);coutC1=;C1.display();+C1;coute ndlvC

14、仁;C1.display();-C1;coute ndlvC 仁;C1.display();coute ndl;return 0;13、输入一个自然数,输出其各因子的连乘形式,如输入12,则输出12=1*2*2*3#in cludeusing n amespace std;int main()int i,n;cinn;cout n=1;for(i=2;i=n;)if(n %i=0)n=n/i;cout*i;con ti nue;i+; coute ndl;return 0;15、定义一个基类:点类,包括x坐标和y坐标,从它派生一个圆类,增加数据 成员r (半径),圆类成员函数包括构造函数、求面

15、积的函数和输出显示圆心坐标及圆半径的函数。#in cludeusing n amespace std;class Dotpublic:int x;int y;Dot();class Circle:public Dotprivate:int r;public:Circle():Dot()void get();void area();void show();;void Circle:get()cout输入圆心坐标、圆的半径: xyr;void Circle:area()double s=0;s=3.14159*r*r;cout圆的面积是:sendl;void Circle:show()coutvv

16、圆心坐标是:(vvxvv,vvyvv)vvendl;coutvv圆的半径是:vvrvvendl;int main()Circle d;d.get();d.area();d.show();return 0;N+1个整数仍然有序16、N个整数从小到大排列,输入一个新数插入其中,使#in cludeusing n amespace std;int main()int a11=10,20,30,40,50,60,70,80,90,100;int i,j, n;cout原顺序为:endl;for(i=0;i10;i+)coutai;coute ndl;cout请输入一个新数:n;if(a0 n)for(

17、i=9;i=0;i-)ai+1=ai;a0=n;if(a9 a0&n a9)for(i=1;i ai) j=i+1;for(i=9;i=j;i-)ai+1=ai;aj=n;for(i=0;i11;i+)coutaivv;coute ndl;return 0;18、编写一个矩形rectangle类,有数据成员长len和宽wid,构造函数 retan ge(i nt,i nt).和友元函数int area(rectangle T)和int fun (rectangle T)分别计算给定长方形的周长和面积。#i nclude using n amespace std;class recta ngle

18、public:recta ngle(i nt ,i nt );friend int area(recta ngle T);friend int fun (rectangle T);private: int len, wid;recta ngle:recta ngle(i nt a, int b)len=a;wid=b;int area(recta ngle T)return (Ten *T.wid);int fun (recta ngle T)return (2*(T.le n+T.wid);int main()rectangle R(20,30);cout面积:area(R)endl;cou

19、t周长:fun(R)endl;return 0;19、定义一个复数类,用友元函数实现对双目运算符+和*的运算符重载,使其适 用于复数运算。#in clude #i nclude class CComplex#defi ne err 0.00000001double _x,_y;public:CComplex(double x=0,double y=0):_x(x),_y(y)CComplex operator+(c onst CComplex&z);CComplex operator*(c onst CComplex&z);friend ostream&operator (istrea m&i

20、s,CComplex&z);int main()CComplex z1(0,1.2);CComplex z2(1,1.2);coutz1+z2e ndl;coutz1*z2 z1;coutz1e ndl;return 0;CComplex CComplex:operator+(c onst CComplex&z)CComplex c;C._X=_X+Z._X; c._y=_y+乙_y;return c;CComplex CComplex:operator*(c onst CComplex&z) CComplex c;c._x=_x*z._x-_y* z._y return c;c._y=_x*

21、z._y+_y*z._x;ostrea m&operator err)coutz._x;if(fabs(z._y-1)err)couterr?+i:i);else if(fabs(z._y+1)err)couterr)couterr?+:)vv z._y i;else if(z._y-err) coutz._y(istream&is,CComplex&z) isz._xz._y;return is;20、输入10个同学的成绩,统计80分以上和不及格的人数,并输出平均值。#in cludeusing n amespace std;int main()double a10,sum=0,var;in

22、t i,j=0,k=0;cout请输入10个学生成绩:endl;for(i=0;i ai;sum=sum+ai;if(ai=80)j+;if(ai60)k+; var=sum/10;cout80分以上的人数是:vvjvvendl;cout不及格的人数是:vvkvvendl;cout平均分是:vvvarvvendl;return 0;21、声明一个类 String1,其数据成员为char head100,构造函数String (char *Head)实现head的初始化,成员函数 void Reverse ()实现head内字符串的 逆序存放,成员函数void Print() 实现head内字符

23、串的输出。#in clude #i nclude using n amespace std;class Stringpublic:String (char *Head);void Reverse。;void Print ();private:char head100;Stri ng:Stri ng(char *Head)int i=0;char *p=Head;while(*p!=0) headi=*p;i+;p+;headi=O;void Strin g:Reverse()int i=0;char h100,*p=head; while(*p!=0) p+; while(*p!=head0)

24、p-;hi=*p;i+;hi=0;while(i=0)headi=hi;i-;void Stri ng:Pri nt()char *p=head;for(;(*p)!=0;p+) cout(*p);coute ndl;int main()char *Head=abcdefgh;Stri ng s(Head);cout正序:;s.Pri nt();s.Reverse();cout逆序:;s.Pri nt();return 0;22编写程序形成如下矩阵。2 14 13 A=5 IL4321 32112111 山111 1111 #in clude using n amespace std;int

25、main()int a55;int iijj;for (ii = 0; ii 5; ii+)for (jj = 0; jj 5; jj+)if (ii - jj 1)aiijj = 1;elseaiijj = ii+1 - jj;for(ii=0;ii5;ii+)for(jj=0;jj5;jj+)coutaiijj;coute ndl;return 0;23、定义盒子Box类,要求具有以下成员:可设置盒子形状;可计算盒子体积;可计算盒子的表面积。#i nclude using n amespace std;class Boxpublic:Box(double,double,double);do

26、uble area();double v();private:double x,y,z;Box:Box(double x1,double y1,double z1)x=x1;y=yi;z=z1;double Box:area() return (2*(x*y+y*z+x*z);double Box:v()return (x*y*z);int main()Box B(2,3,4);cout表面积:vB.area()vendl;cout体积:B.v()endl;return 0;24、33、声明一个哺乳动物 Mammal类,再由此派生出狗 Dog类,声明一个Dog 类的对象,观察基类与派生类的构造

27、函数与析构函数的调用顺序。#in cludeusing n amespace std;class mammalpublic:mammal()默认构造函数coutmammals con structore ndl;mammal()coutmammala destucore ndl;; class dog :public mammalpublic:dog()coutdogs con structore ndl;dog()coutdogs destructore ndl;;int main()dog a;return 0;25、30、定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类, 教

28、师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩#in elude #i nclude using n amespace std;class pers onpublic:pers on( stri ng ,stri ng,i nt);protected:stri ng n ame;stri ng sex;int age;pers on:pers on( stri ng n, stri ng s, int a)n ame=n;sex=s;age=a;class teacher :public pers onpublic:teacher(string ,string,int,stri

29、ng,string,double);void display();private: stri ng work_ num;string title;double wage;;teacher:teacher(stri ng n, stri ng s, int a, stri ng wo, stri ng t, double wa):pers on( n, s,a) work_ num=wo;title=t;wage=wa;void teacher:display()agee ndl;工资:cout姓名:nameendl性别:sexendl年龄:cout工号:work_numendlvv职称:tit

30、leendlvv vvwagevve ndl;class stude nt: public pers onpublic:stude nt(stri ng,stri ng,i nt,stri ng,stri ng,stri ng,double);void show();private:stri ng num;stri ng grade;string major;double score;;stude nt:stude nt(stri ng n, stri ng s, int a, stri ng nu, stri ng g, stri ngm, double sc):pers on(n, s,a

31、)/注意:只写n,不是string nnum=nu;grade=g;major=m;score=sc;void stude nt:show()coutvv姓名:nameendlvv性别:sexendlvv年龄:ageendl;coutvv学号:vvnumendlvv年级:gradeendlvv专业:vvmajorvvendl入学成绩:vvscorevve ndl;int main()teacher t( 侯传旺,男,20,200901001010, 教授,2100);t.display();student s(秦洪敏,女,21,200902002026,大二,应用心理学,540);s.show

32、();return 0;26、写一个Complex类,将运算符“ +”重载为适用于复数加法,重载函数不作 为成员函数,而放在类外,作为Complex类的友元函数。如例题10.3#i nclude class complexprivate:double a;double b;public:complex。;complex(double i,double j);friend complex operator +(complex&,complex);void setreal(double x);void setimag(double y);void getreal();void getimag();

33、friend ostream& operator (istream& in, complex& x);complex:complex():a(0),b(0) complex:complex(double i,double j):a(i),b(j) complex operator +(complex&x,complex y)y.a+=x.a;y.b+=x.b;return complex(y.a,y.b);ostream& operator(ostream& out,complex &x)outvvx.avv+vvx.bvvi;return out;istream& operator (ist

34、ream& in, complex& x)in x.ax.b;return in;void complex:setreal(double x)a=x;void complex:setimag(double y)b=y;void complex:getreal()cin a;void complex:getimag()cin b;int main()complex a;complex b;cin ab;/coutvvavve ndl;/coutvvbvve ndl;coutvva+bvve ndl;/coutvvavve ndl;/coutvvbvve ndl;return 0;27、实现一个名

35、为SimpleCircle的简单圆类,其数据成员int *itsRadius 为一 个指向其半径值的指针,设计对数据成员的各种操作,给出这个类的完整实现并 测试这个类。#i nclude #define PI 3.1415926using n amespace std;class SimpleCirclepublic:SimpleCircle(i nt *);double fun();double area();private:int *itsRadius;SimpleCircle:SimpleCircle( int * i) itsRadius=i;double SimpleCircle:f

36、u n()return (2 * PI * (*itsRadius);double SimpleCircle:area()return (PI*PI*(*itsRadius);int main()int a;coutvv请输入一个整数:;cin a;SimpleCircle S1(&a);cout周长:S1.fun()endl;cout面积:S1.area()endl;return 0;28、有一个Time类,包含数据成员minute(分)和sec(秒),模拟秒表,每次走 一秒,满60秒进一分钟,此时秒又从0开始算。要求输出分和秒的值。(提示: 重载单目运算符+)#in elude using

37、 n amespace std;class Timepublic:Time(i nt ,i nt);void operator +();void display。;private:int minu te,sec;Time:Time(i nt a,i nt b)minu te=a;sec=b;void Time:operator +()if (sec59)sec+;elseminu te+;sec=0;void Time:display()cout minu te:sece ndl;int main()Time T1(3,59);T1.display();T1+;T1.display();ret

38、urn 0;29、有一个学生类student,包括学生姓名、成绩,设计一个友元函数,输出成 绩对应的等级:大于等于90:优;8090:良;7079:中;6069:及格;小于60:不及格。#in clude #i nclude using n amespace std;class stude ntpublic: friend void grade(stude nt & ); stude nt (stri ng ,in t);private :stri ng n ame;int score;;stude nt:stude nt(stri ng n ,i nt s)n ame=n;score=s;void grade (stude nt & s)if(s.score =90)cout姓名:endl获得:优endl;if (s.score=80)cout姓名:endl获得:良=70&s.score80)cout姓名:endlvv获得:中=60&s.score70)cout姓名:endl获得:及格endl;if (s.score60)cout姓名:s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论