c++题库编程题.doc_第1页
c++题库编程题.doc_第2页
c++题库编程题.doc_第3页
c++题库编程题.doc_第4页
c++题库编程题.doc_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

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

文档简介

1、./2-5#include#include#includeusing namespace std;class Triangle int a,b,c;public: double Area() double s=(a+b+c)/2;return sqrt(s*(s-a)*(s-b)*(s-c); ;int Perimeter()return a+b+c;void Input()cinabc; ;void main() Triangle t1; t1.Input();coutt1.Area() t1.Perimeter();/2-3#includeinline int max(int a,int

2、b)if(ab)return a;else return b;inline int max(int a,int b,int c)return max(a,max(b,c);void main()int x=21,y=15,z=22,d;d=max(x,max(y,z);coutd;/2-4#includeint overload(int n);char overload(char n);void main()int n;char s;coutn;1/24.coutn=overload(n)endl;cout 输入 as;couta=;coutoverload(s)endl;int overlo

3、ad(int n)return n;char overload(char n)coutbendl;return 0;/2-2#includeiostreamusing namespace std;inline char trans(char ch);int main()char ch;while(ch=getchar()!=n)couttrans(ch);cout=a&ch=z)return ch-32;elsereturn ch+32;/3-1#includeusing std:cout;using std:endl;int Min(int a,int b)return (ab?a:b);2

4、/24.int Min(int a,int b,int c)return (Min(a,b)c?Min(a,b):c);int Min(int a,int b,int c,int d)return (Min(a,b,c)d?Min(a,b,c):d);void main(void)coutMIN(1,2)=Min(1,2)endl;coutMIN(3,4,5)=Min(3,4,5)endl;coutMIN(4,5,6,7)=Min(4,5,6,7)endl;/3-2#includeusing namespace std;void Area(float r)coutthe circles are

5、a is: 3.14*r*rendl;void Area(float a,float b)coutthe rectangles(squares) area is: a*bendl;void Area(float a,float b,float h)coutthe trapzoids area is: (a+b)*h/2endl;void main()Area(5);Area(3,3);Area(3,5,4);/3-3#includeusing namespace std;void Sort(int a,int n)bool flag;for(int i=0;in-1;i+)3/24.flag=

6、false;for(int j=1;jaj)int temp=aj-1;aj-1=aj;aj=temp;flag=true;if(!flag)break;for(int m=0;mn;m+)coutam ;coutendl;void Sort(float b,int n)bool flag;for(int i=0;in-1;i+)flag=false;for(int j=1;jbj)float temp=bj-1;bj-1=bj;bj=temp;flag=true;if(!flag)break;for(int m=0;mn;m+)coutbm ;coutendl;void main()int

7、a10=3,5,1,2,9,0,8,6,4,7;float b10=0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0;Sort(a,10);Sort(b,10);4/24./3-4#includeusing namespace std;class Rectanglefloat a,b;public:Rectangle()coutinput the two side of the rectangle:ab;Rectangle(float x,float y):a(x),b(y)float Cir()return (2*(a+b);float Area()return

8、 (a*b);void main()Rectangle Re;coutthe rectangles circumference is:Re.Cir()endlthe rectangles area is:Re.Area()endl;/3-5#includeusing namespace std;class Circlefloat r;public:Circle()coutinput the radius of the circle:r;Circle(float x):r(x)float Cir()return 2*3.14*r;float Area()5/24.return 3.14*r*r;

9、void main()Circle C(5);cout the Circles circumference is:C.Cir()endlthe Circles Area is:C.Area()endl;/4-1class studentint no;char name20;public:student();int getno()const;char*getname();#includestudent:student()no=1;strcpy(name,wang);int student:getno ()constreturn no;char*student:getname()return na

10、me;int main()student s1;s1.getno();s1.getname();const student s2;s2.getno();return 0;6/24./4-3#includeclass objectfloat a;float weight;public:object(float w);object()coutdestruct weightendl;class box:public objectfloat b;float height,widt;public:box(float w,float h,float wi):object(w),height(h),widt

11、(wi)coutconstruct heightendl;coutconstruct widtendl;box()coutdestruct heightendl;coutdestruct widtendl;object:object(float w)weight=w;coutconstruct weightendl;int main()box b(1.1,2.2,3.3);return 0;/4-4#includeiostreamusing namespace std;class baseclasspublic:baseclass()couta.cendl;virtual baseclass(

12、)couta.dendl;7/24.class derivedclass:public baseclasspublic:derivedclass()coutb.cendl;derivedclass()coutb.dendl;intmain()derivedclass obj;baseclass *p;p=&obj;return 0;/4-5#includeiostream#includeusing namespace std;class Doumentchar *name;public:Doument(char *na)name=new char strlen (na)+1;strcpy(na

13、me,na);virtual void print() const cout姓名: nameendl;class Book:public Doumentint pagecount;char *name;public:Book(int p,char *na):Doument(na)p=pagecount;name=new char strlen (na)+1;strcpy(name,na);void print() const cout页码 :pagecount 姓名 :nameendl;8/24.void disp(Doument& i)i.print();int main()Book b(2

14、3, 小明 );disp(b);return 0;/5-1#includeiostreamusing namespace std;class pointprivate:int x;int y;int z;static int num;public:point(int a,int b,int c);int print();int point:num=0;int main()point p1(1,2,3);p1.print();point p2(5,3,2);p1.print();point p3(3,6,4);p1.print();return 0;point:point(int a,int b

15、,int c)x=a;y=b;z=c;num+;int point:print()9/24.cout 第 num 个点 x,y,zendl;return 0;/5-2#include#includeusing namespace std;class Stuchar name15;int age;float score;public:static int count;static float sum;Stu(char *p,int a,float s):age(a),score(s)strcpy(name,p);count+;sum+=score;Stu()coutinput the stude

16、nts name,age,score:nameagescore;count+;sum+=score;int Stu:count=0;float Stu:sum=0;void main()Stu p( 子机 ,19,98),q;coutthe number of students is: Stu:countendlthe total score is: Stu:sumendl;/5-3#includeusing namespace std;class Pointfloat x,y,z;public:10/24.Point(float a=0,float b=0,float c=0):x(a),y

17、(b),z(c)Point operator +();Point operator -();Point operator +(int);Point operator -(int);void show()cout( x , y , z ) = ( x , y , z )endl;Point Point:operator +()return Point(+x,+y,+z);Point Point:operator -()return Point(-x,-y,-z);Point Point:operator +(int)Point temp;temp.x=x+;temp.y=y+;temp.z=z+

18、;return temp;Point Point:operator -(int)Point temp;temp.x=x-;temp.y=y-;temp.z=z-;return temp;void main()Point a(1,1,1),b(2,2,2),c,d;couta :;a.show();coutb :;b.show();coutendl;c=a+;11/24.coutc = a+n;coutc :;c.show();coutendl;d=+b;coutd = +bn;coutd :;d.show();coutNew a and b :n;a.show();b.show();coute

19、ndl;c=a-;coutc = a-n;coutc :;c.show();coutendl;d=-b;coutd = -bn;coutd :;d.show();coutNew a and b :n;a.show();b.show();/5-4#includeusing namespace std;class Complexfloat real,image;public:Complex(float a,float b):real(a),image(b)Complex()friend bool operator =(Complex &x,Complex &y)return (x.real=y.r

20、eal&x.image=y.image);void main()Complex Cp1(1,2),Cp2(3,4),Cp3(1,2);cout(Cp1=Cp2)endl(Cp1=Cp3)endl;/6-112/24.#includeusing namespace std;class Complexfloat real,image;public:Complex(float x,float y):real(x),image(y)Complex()Complex operator +(Complex &a)return Complex(real+a.real,image+a.image);Compl

21、ex operator -(Complex &b)return Complex(real-b.real,image-b.image);friend ostream & operator (ostream &output,Complex &c)output(c.real,c.image);return output;void main()Complex Cp1(1,-2),Cp2(2,5),Cp3,Cp4;Cp3=Cp1+Cp2;Cp4=Cp1-Cp2;coutCp1+Cp2=Cp3endlCp1-Cp2=Cp4endl;/6-2#includeusing namespace std;class

22、 computerfloat x,y;public:computer(float a=0,float b=0):x(a),y(b)computer operator +();computer operator -();computer operator +(int);computer operator -(int);void show()cout( x , y ) = ( x , y )endl;13/24.computer computer:operator +()return computer(+x,+y);computer computer:operator -()return comp

23、uter(-x,-y);computer computer:operator +(int)computer temp;temp.x=x+;temp.y=y+;return temp;computer computer:operator -(int)computer temp;temp.x=x-;temp.y=y-;return temp;void main()computer a(1,1),b(2,2),c,d;couta :;a.show();coutb :;b.show();coutendl;c=a+;coutc = a+n;coutc :;c.show();coutendl;d=+b;c

24、outd = +bn;coutd :;d.show();coutNew a and b :n;a.show();b.show();coutendl;c=a-;14/24.coutc = a-n;coutc :;c.show();coutendl;d=-b;coutd = -bn;coutd :;d.show();coutNew a and b :n;a.show();b.show();/6-3#include#includeclass stringchar *xstring;public:string(char *str);string();string &operator=(const st

25、ring &);void print();string:string(char *str)xstring=new char strlen(str)+1;strcpy(xstring,str);string:string()delete xstring;void string:print()coutxstringendl;int main()string s1(hello);string s2(yangchenhuan);s2.print();s2=s1;cout 修改后: ;s2.print();return 0;15/24.string& string :operator=(const st

26、ring &s)if(this=&s)return *this;delete xstring;xstring=new char strlen(s.xstring)+1;strcpy(xstring,s.xstring);return *this;/6-4#include #include using namespace std;class String public:String() p=NULL;String(char *str);friend bool operator=(String &string1,String &string2);void display();private:cha

27、r *p; ;String:String(char *str) p=str; void String:display() coutp;/ 输出 P 所指向的字符串bool operator=(String &string1,String &string2) if(strcmp(string1.p,string2.p)=0)return true;elsereturn false; void compare(String &string1,String &string2)if(operator=(string1,string2)=0) string1.display(); cout=; stri

28、ng2.display();16/24.else couterror!endl;int main() String string1(Hello),string2(china); compare(string1,string2);return 0;/6-5#include#includeusing namespace std;class Stringchar *p;public:String()p=NULL;String(const char *s)p=new char(strlen(s)+1);strcpy(p,s);char * operator +(const char *s)char *

29、temp=new char(strlen(p)+strlen(s)+1);strcpy(temp,p);strcat(temp,s);return temp;friend bool operator =(const String &a,const String &b)return (!strcmp(a.p,b.p);friend ostream &operator (ostream &output,const String &d)outputd.p;return output;void main()String Str1(Malagebi!),Str2(Ready,Go!),Str3;Str3

30、=Str1+nishi;coutStr3endl(Str1=Str2)endl;/7-117/24.#includeusing namespace std;class Carint wnum;float weight;public:Car(int a,float b):wnum(a),weight(b)void Cshow()cout 轮子个数:wnum个 endl 车重:weight Tendl;class SmallCar:private Carint s_cnum;public:SmallCar(int a,float b,int c):Car(a,b),s_cnum(c)void Ss

31、how()Cshow();cout 载客量:s_cnum人次 endl;class BigCar:private Carint b_cnum;float b_cweight;public:BigCar(int a,float b,int c,float d):Car(a,b),b_cnum(c),b_cweight(d)void Bshow()Cshow();cout 载 客 量 :b_cnum人 次 endl 载 重 量b_cweightTendl;int main()Car car(4,8);SmallCar smallcar(8,16,8);BigCar bigcar(16,32,16,

32、32);cout 汽车:endl;18/24.car.Cshow();coutn 小车:endl;smallcar.Sshow();coutn 卡车:endl;bigcar.Bshow();return 0;/7-2#include#includeusing namespace std;class Shapefloat x,y;public:Shape(float a,float b):x(a),y(b)void show()cout 左上角坐标(x,y)= (x,y)n;virtual float GetArea()=0;class Rec:virtual public Shapefloat

33、 high,width;public:Rec(float a,float b,float c,float d):Shape(a,b),high(c),width(d)void show()cout 矩形 : n;Shape:show();cout 矩形宽、高:width highendl;float GetArea()show();return high*width;class Ell:virtual public Shapefloat xx,yy;public:Ell(float a,float b,float c,float d):Shape(a,b),xx(c),yy(d)19/24.v

34、oid show()cout 椭圆 : n;Shape:show();cout 椭圆 x 轴, y 轴:xx yyendl;float GetArea()show();return 4*atan(1)*xx*yy;int main()Rec rec(5,5,5,5);Ell ell(8,8,12,10);Shape *s;s=&rec;cout 矩形的面积:GetArea()endlendl;s=ℓcout 椭圆的面积:GetArea()endlendl;return 0;/7-3#include /#include using namespace std;class mammal i

35、nt m; public:mammal(int i=0):m(i)coutmammal is constructendl;mammal()coutmammal is destructendl;class cat:public mammalint c;public:cat(int j=0):c(j)20/24.cout cat is constructendl;cat()coutcat is destructendl;class dog:public mammalint d;public:dog(int k=0):d(k)cout dog is constructendl;dog()coutdo

36、g is destructendl;int main()cat c;dog d;return 0;/7-4p39.7/8-1#includeusing namespace std;template double sum(T1 a,T2 b)return (a+b);void main()coutsum(1,3)endl;coutsum(1.2,3)endl;coutsum(1,3.1)endl;coutsum(1.2,3.34)endl;/8-2P 课本 17921/24./8-3#includeusing namespace std;const int M=100;template class ArrayT arra

温馨提示

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

评论

0/150

提交评论