《c++题库编程题.doc》由会员分享,可在线阅读,更多相关《c++题库编程题.doc(21页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流c+题库编程题【精品文档】第 21 页c+题库编程题c+题库编程题/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.
2、Area() t1.Perimeter();/2-3#includeinline int max(int a,int 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;coutn=overload(n
3、)endl;cout输入as;couta=;coutoverload(s)endl;int overload(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;else return ch+32;/3-1#includeusing std:cout
4、;using std:endl;int Min(int a,int b)return (ab?a:b);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 nam
5、espace std;void Area(float r)coutthe circles area 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
6、a,int n)bool flag;for(int i=0;in-1;i+)flag=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
7、 m=0;mn;m+)coutbm ;coutendl;void main()int 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);/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 C
8、ir()return (2*(a+b);float Area()return (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.
9、14*r;float Area()return 3.14*r*r;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
10、no; char*student:getname()return name;int main()student s1;s1.getno();s1.getname();const student s2;s2.getno();return 0;/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,floa
11、t wi):object(w),height(h),widt(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()
12、couta.cendl;virtual baseclass()couta.dendl;class derivedclass:public baseclasspublic:derivedclass()coutb.cendl;derivedclass()coutb.dendl;int main()derivedclass obj;baseclass *p;p=&obj;return 0;/4-5#includeiostream#includeusing namespace std;class Doumentchar *name;public: Doument(char *na)name=new c
13、har strlen (na)+1;strcpy(name,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;void disp(Doument& i)i.print
14、();int main()Book b(23,小明);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; poi
15、nt:point(int a,int b,int c)x=a;y=b;z=c;num+; int point:print()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()coutinp
16、ut the students 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:Point(float a=0,float b=0,float c=0):x(
17、a),y(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.
18、z=z+;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+;coutc = a+n;coutc :;c.show();coutendl;d=+b;coutd = +bn;coutd :;d.show();coutNew a and b :n;a.show();b.show();coutend
19、l;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.rea
20、l&x.image=y.image);void main()Complex Cp1(1,2),Cp2(3,4),Cp3(1,2);cout(Cp1=Cp2)endl(Cp1=Cp3)endl;/6-1#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);Complex opera
21、tor -(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 compute
22、rfloat 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;computer computer:operator +()return computer(+x,+y);computer computer:operator -()return computer(-x,-y);co
23、mputer 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;coutd = +bn;cou
24、td :;d.show();coutNew a and b :n;a.show();b.show();coutendl;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();/6-3#include#includeclass stringchar *xstring;public: string(char *str); string(); string &operator=(const string &);void prin
25、t();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;string & string :operator=(const string &s)if(this=&s)retur
26、n *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:char *p; ;String:String(cha
27、r *str) p=str; void String:display() coutp; /输出P所指向的字符串bool operator=(String &string1,String &string2) if(strcmp(string1.p,string2.p)=0) return true; else return false; void compare(String &string1,String &string2) if(operator=(string1,string2)=0) string1.display(); cout=; string2.display(); else co
28、uterror!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 *temp=new char(strlen(p)+s
29、trlen(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=Str1+nishi;coutStr3endl(
30、Str1=Str2)endl;/7-1#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 Sshow()Cshow();cout载客量: s_cnum 人次
31、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_cweight Tendl;int main()Car car(4,8);SmallCar smallcar(8,16,8);BigCar bigcar(16,32,16,32);cout汽车: endl;car.Cshow();coutn小车
32、: 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 high,width;public:Rec(float a,float b,flo
33、at 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)void show()cout椭圆 : n;Shape:show();cout椭圆x轴,y轴: xx
34、 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 int m;public:mammal(int i=0):m(i)coutmammal is construc
35、tendl;mammal() coutmammal is destructendl;class cat:public mammalint c;public:cat(int j=0):c(j)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() coutdog is destructendl;int main()cat c;dog d; return 0;/7-4p39.
36、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课本179/8-3#includeusing namespace std;const int M=100;template class ArrayT arrayM;int len;public:Array();void sort();void search()c
37、outx;for(int i=0;ilen;i+)if(x=arrayi)coutnthe position of the num x is i+1endl;break;if(i=len)coutnthe num x is not exist ! n;T sum()float s=0;for(int i=0;ilen;i+)s+=arrayi;return s;void show()for(int i=0;ilen;i+)coutarrayi ;coutendl;template Array:Array()coutlen;coutinput the element of the array :n;for(int i=0;iar