《C++程序设计编程实例大全.docx》由会员分享,可在线阅读,更多相关《C++程序设计编程实例大全.docx(94页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、C+程序编程实例大全#include int main()(std:couthello world!u;#include iostream”using namespace std;void main()(int *p;p=new int;*p=5;/*p=new int(5);*/cout*p;delete p;#include iostream using namespace std; void main() char name10;coutplease input your name:;cinname;coutthe name is nameendl;#include iostreamus
2、ing namespace std;void main() char name10;int age;coutplease input your name:;cinname;couthow old are you:;cinage;coutthe name is nameendl;coutthe age is ageendl;#include iostreamusing namespace std;void main()(struct student(int no;float math;1;int n;cinn;student wang;wang.no=n;cinwang.math;coutwan
3、g.no wang.mathendl;#include iostreamusing namespace std;void main()int *p;int n;coutplease input the length of the array:;cinn;if(p=new intn)=0)(coutcan,t allocate more memory, terminatingendl; exit(l);)for(int i=0;in;i+)p|i=i*2;coutNow output the array: endl;fbr(i=0;in;i+) coutpi endl; delete p;)#i
4、nclude iostreamusing namespace std;void main()(int *p;int n;coutplease input the length of the array:;cinn;if(p=new intn)=0)(coutcanft allocate more memory, terminatingendl; exit(l);)for(int i=0;in;i+)p|i=i*i+l;coutNow output the array: endl;ft)r(i=0;in;i+4-)coutpicoutendl;delete p;/* Note: Your cho
5、ice is C IDE */#include using namespace std;float a=2.4;void main()(int a=8;coutaendl;cout:aendl;/* Note:Your choice is C IDE */#include void main()(int num=50;int &ref=num;ref+=10;coutnum=numendl;coutrefi=refendl;num+=40;coutnum=numendl; cou tre f,refendl;/* Note:Your choice is C IDE */#include voi
6、d main()(int num;int &ref=num;num=5;coutnum=Mnu me ndl;cout,refi=,refendl;cout,&num=,&numendl;cout,&ref=&refendl;/* Note:Your choice is C IDE */#include using namespace std; void swap(int &x,int &y);void main()(int x=5,y=6;coutbefore swap, x:xn ,y:yendl;swap(x,y);coutafter swap, x:nx ,y:My;)void swa
7、p(int &rx,int &ry)(int t=rx;rx=ry;ry=t;#include/using namespace std;void main()/*double radius;coutplease input radius:1;cinradius;double result=radius*radius*3.14*4;coutThe result is (double)resultHn;)*/(double radius;std:coutnplease input radius:; std:cinradius;std:coutnThe result is ,radius*radiu
8、s*3.14*4,n,;/* Note:Your choice is C IDE */#include using namespace std;intarray64= 60,80,90,75,75,85,65,77,80,88,90,98,89,100,78,81,62,68,69,75,85,85,7 7,91);int &level(int grade,int size,int &tA,int &tB ); void main()(int typeA=0,typeB=0;int student=6;int gradesize=4;fbr(int i=O;istudent;i+)level(
9、arrayi ,gradesize,type A,typeB)-H-;coutnnumber of type A is MtypeAendl;coutnnumber of type B is ntypeBendl;)int &level(int grade,int size,int &tA,int &tB)(int sum=0;for(int i=0;i=80)return tA;elsereturn tB;/* Note:Your choice is C IDE */#include using namespace std;float &fn2( float r)(float t;t=3.1
10、4*r*r;return t;)void main()(fn2(5.0)=12.4;coutfn2( 5.0)e ndl;/* Note:Your choice is C IDE */#include #include using namespace std;void main()(string s,t;coutplease input a zifuchuan:Hendl;cins;t=I like programming!;coutzifuchuan output: endlsendltendl;couts.append( OK!n)endl;/* Note:Your choice is C
11、 IDE */#include using namespace std;inline double circumference(double radius);void main()(double r=3.0,s;s=circumference(r);coutthe circumference is s.endl;inline double circumference(double radius)(return 2*3.1415926*radius;/* Note:Your choice is C IDE */#include int add(int x,int y)(int sum;sum=x
12、+y;return sum;)int add(int x,int y,int z)(int sum;sum=x+y+z;return sum;)void main()int a,b;a=add(5,10);b=add(5,10,20);coutHa= Mae ndl;cout,b=*be ndl;/* Note:Your choice is C IDE */#include tempi ate T min(T a,int n)(inti;T minv=a0;fbr(i=l;ivn;i+) if(minvai) minv=ai;return minv;)void main()(int a= 1,
13、3,027,6,4,5,2;double b= 1.2,-3.4,6.8,9.8);int c=l,9,5,-6,7,8;coutna de shuzu zhong min zhi wei: nmin(a,9)endl;coutnb de shuzu zhong min zhi wei:Mmin(b,4)endl;coutnc de shuzu zhong min zhi wei:nmin(c,6)endl;/* Note:Your choice is C IDE */#include #define PI 3.1415926535#define CS CircIe_Squaretempi a
14、tedouble Circle_Square(T x)(return x*x*PI;)double Circle_Square(long x)(return x*x*PI;)void main()(int rl=l;double r2=2.0;long r3=3;coutThe first cs is MCS(rl)endl;coutThe second cs is HCS(r2)endl;coutnThe third cs is MCS(r3)endl;/* Note:Your choice is C IDE */#include void main()(int i=0,&l=i,&k=l;
15、i=+l-k;coutui=Miendl;/* Note: Your choice is C IDE */#include class Tdatepublic:void set(int,int,int);int isLeapYear();void print();private:int month;int day;int year;void Tdate:set(int m,int d,int y)(month=m;day=d;year=y;int Tdate: :isLeap Ye ar()(return(year%4=0&year%100!=0)ll(year%400=0);)void Td
16、ate :print()(coutmonth,/,dayV,yearendl;coutisLeapYear()endl;)void main()(Tdate x;x.set( 10,23,2000);x.isLeapYear();x.print();/* Note:Your choice is C IDE */#include class Tdate(public:void set(int,int,int);int isLeapYear();void print();private: int month;int day;int year;);void Tdate:set(int m,int d
17、,int y)(month二m;day=d;year=y;)int Tdate: :isLeap Ye ar() (return(year%4=0&year%100!=0)ll(year%400=0);)voidTdate:print()(coutyear/,month,/,dayendl;void someFunc(Tdate& refs)(refs.print();if(refs.isLe 叩 Year() coutnerrornendl; elsecout,right,endl;1void main()(Tdate s,*pTdate=&s;s.set(2,15,1998);pTdate
18、-print();if(*pTdate).isLeapYear()coutnerrornendl;elsecoutnri ght*e ndl;someFunc(s);#includeusing namespace std;void sphere();int main()(sphere();void sphere()(double radius;coutnplease input radius:M;cinradius;if(radius0) return;coutHthe result is ,radius*radius*3.14*4,n,;/* Note: Your choice is C I
19、DE */#includeconst int SIZE=10;class C stack (private:char stkSIZE;int position;public: void init()|position=0;)char push(char ch);char pop(););char Cstack: push(char ch) (if(position=SIZE)(coutcv栈满 vendl: return 0;)stkposition+=ch;return ch;char Cstack:pop()if(position=0) coutv、n 栈空”vendl;return 0;
20、)return stkposition;1void main()(Cstack s;s.init();char ch;cout,please input some characters:Mendl;cinch;while(ch!=T&s.push(ch)cinch;coutendl;coutMnow output the data: Mendl;while(ch=s.pop()coutch;/* Note:Your choice is C IDE */#include class Tdate(public:void set(int m=5,int d=16,int y=1991)month二m
21、;day 二d;yea匸 y;void print()(coutmonth7day/Myearendl;)private:int month;int day;int year;);void main()(Tdate a,b,c;a.set(4,12,1996);b.set(3);c.set(8,10);a.print();b.print();c.print();/* Note: Your choice is C IDE */#includeclass cube(public:int volume(int ht,int wd)return ht*wd;)int volume(int ht,int
22、 wd,int dp)return ht*wd*dp;)private:int ht,wd,dp;);void main()(cube c;cout v vc. vol u me( 10,20) vvendl;coutc.volume( 10,20,30)endl;/* Note:Your choice is C IDE */#include class queue/默认私有/int q100;int sloc,rloc;public:queue();void qput(int i);int qget(););queue:queue() (sloc=rloc=0;coutMqueue init
23、ialized“vendl;void queue:qput(int i)(if(sloc=100)|coutnqueue is fullnendl;return;)sloc+;qsloc=i;int queue:qget()(if(rloc=sloc)(coutqueue is emptyendl; return 0;)rloc+;return qrloc;void main()(queue a,b;a.qput(lO);b.qput(20);a.qput(20);b.qput( 19);couta.qget()n M;coutb.qget(),n,;couta.qget()H M;coutb
24、.qget()nn;/* Note: Your choice is C IDE */#include class test(private:int num;float fl;public:test();test(int n,float f);int getint()return num;float getfloat()return fl;);test:test()(coutuInitializing default,endl;num=0;fl =0.0;test:test(int n,float f)(cout,Initializing,n,Mfendl;num=n;fl=f;void mai
25、n()(test x;test y(10,21.5);test *px=new test;test *py=new test(10,21.5);/* Note:Your choice is C IDE */#include class Tdate(public:Tdate(int m=5,int d=16,int y=1990)(month=m;day=d;year=y;coutmonth,/,day7,yearendl;)private:int month;int day;int year;);void main()Tdate aday;Tdate bday(2);Tdate cday(3,
26、12);Tdate dday( 1,22,1998);/* Note:Your choice is C IDE */#include #include using namespace std;class Student(public:Student(char *pName) (coutcall one parameter constructorvvendl;stmcpy(name,pName,sizeof(name);namesizeof(name)-1 =*0*;coutnthe name is Mnameendl;Student()(coutcall no parameter constr
27、uctorMendl; protected: char name20;);void main()Student noName;Student ss( Jenny”); /* Note:Your choice is C IDE */#include using namespace std;class testprivate:int num;float fl;public:test();test(int n,float f);int getint()return num;)float getfloat() return fl;);test:test() (coutMInitializing def
28、aultendl;num=0;fl=0.0;)test:test(int n,float f)(cout,Initializing,n,/,fendl;num=n;fl=f;)void main()coututhe main function: vvendl;test array5 ;coutthe second element of array is ,arrayl.getint(),arrayl.getfloat()endl;/* Note: Your choice is C IDE */#includeusing namespace std;class test(private:int
29、num;float fl;public:test(int n);test(int n,float f););inline test:test(int n)(cout,Initializingt,nendl; num=n;)test:test(int n,float f)coutInitializingtn,fendl;num=n;fl=f;)void main()(test arrayl3= 1,2,3;test array2=test(2,3.5),test(4);test array3|=test(5.5,6.5),test(7,8.5);test array4=test(5.5,6.5)
30、,7.5,8.5;/* Note:Your choice is C IDE */#include using namespace std;class test(private:int num;float fl;public:test(int n);test(int n,float f););inline test:test(int n)(cout,Initializingtnendl; num=n;)test:test(int n,float f)(coutInitializingt,n,fendl;num=n;fl=f;void main()(test arrayl 3= 1,2,3;tes
31、t array2=test(2,3.5),test(4);test array3=test(5.5,6.5),test(7,8.5);test array4=test(5.5,6.5),7.5,8.5);#include using namespace std; int main()fbr(int i=l;i=10;i+)(for(intj=l;j=i;+j) cout,Mn;coutendl;)#includeclass TdateJpublic:Tdate(int m=5,int d=16,int y=1990) (month=m;day=d;year=y;coutmonth7nday7n
32、yearendl;private:int month;int day;int year;);void main()(Tdate aday;Tdate bday(2);Tdate cday(3,12);Tdate dday( 1,21,1998);#include#includeusing namespace std;void main()(tryexception theError;throw(theError);catch(const exception &theError)(couttheError.what()endI;)trylogic_error theLogicError(Logi
33、c Error!); throw(theLogicError);)catch(const exception & theLogicError)couttheLogicError.what()endl;#include#include using namespace std;class Person (public:Person(char *na) coutcall constructorendl; name=new charstrlen(na)+l;if(name!=0)strcpy(name,na);)Person(Person&p) (coutcall copy constructoren
34、dl;name=new charstrlen(p.name)+1 ;if(name!=0)strcpy(name,p.name);)void printname() coutnameendl;-Person()delete name;private:char *name;;void main() (Person wang(wang);Person li(wang);wang.printname();li.printname();#includeusing namespace std;class SillyClass (public:SillyClass(int&i):ten( 10),refl
35、(i)protected:const int ten;int&refl;);void main()(int i;SillyClass sc(i);coutiendl;#includeclass RMB(public:RMB(double value=0.0);operator double()return yuan+j 17100.0;void display()cout(yuan+jf7100.0)e ndl;) protected:unsigned int yuan;unsigned int jf;);RMB:RMB(double value)(yuan=value;jf=(value-y
36、uan)* 100+0.5;)void main()(RMBdl(2.0),d2(1.5),d3;d3=RMB(double)dl+(double)d2);d3=dl+d2;d3.display();)#include#include class Student ( public:Student(char *pName=no nameM,int ssId=O) stm cpy (n ame, pN ame ,40);name39=,0,;id=ssld;coutConstructing new studentpNameendl; )Student(Student&s) coutConstruc
37、ting copy of s.nameendl;strcpy( name, copy of);strcat(name,s.name);id=s.id;Student。coutDestructing nameendl;protected:char name40;int id;);void fn(Student s) (coutIn function fn()nendl;)void main。(Student randy(Randy, 1234);Student wang(wang,5678);coutCalling fn()nendl;fn( randy);coutRetumed from fn
38、()nendl;#include #include using namespace std; void main。try(exception theError;throw(theError);)catch(const exception &theError)(couttheError.what()endl;try(logic_error the LogicError( Logic Error!); throw(theLogicError);)catch(const exception & theLogicError)(couttheLogi cError. what()endl;#includ
39、e class Student public:Student() coutconstructing studentAn; semesHours=100;gpa=3.5;Student。coutdestructing student.Xn”; protected:int semesHours;float gpa;);class Teacher(public:Teacher。coutconstructing teacherAn;-Teacher。coutdestructing teacher An; ;class Tutorpairpublic:Tutorpair() (coutMconstructi ng tutorpai r.nM; nomeeting=0;)TutorpairQ (coutndest ructing tutorpairAn”; protected:Student student;Teacher teacher; int nomeeting;;void main() (Tutorpair tp;coutnback mainAn;#includeclass A ( static int i;public:A()( i+;)int list()return i;);int A:i=0;void main()(A ai,a2,a3;
限制150内