C++程序設(shè)計(jì)編程實(shí)例大全_第1頁
C++程序設(shè)計(jì)編程實(shí)例大全_第2頁
C++程序設(shè)計(jì)編程實(shí)例大全_第3頁
C++程序設(shè)計(jì)編程實(shí)例大全_第4頁
C++程序設(shè)計(jì)編程實(shí)例大全_第5頁
已閱讀5頁,還剩101頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、可編輯C+程序編程實(shí)例大全#includeint main() std:couthello world!;#include iostreamusing namespace std;void main() int *p; p=new int; *p=5; /*p=new int(5);*/ cout*p; delete p; #include iostreamusing namespace std;void main() char name10; coutname; coutthe name is nameendl; #include iostreamusing namespace std;vo

2、id main() char name10; int age; coutname; coutage; coutthe name is nameendl; coutthe age is agen; student wang; wang.no=n; cinwang.math; coutwang.no wang.mathendl; #include iostreamusing namespace std;void main() int *p; int n; coutn; if(p=new intn)=0) coutcant allocate more memory, terminatingendl;

3、 exit(1); for(int i=0;in;i+) pi=i*2; coutNow output the array: endl; for(i=0;in;i+) coutpi endl; delete p; #include iostreamusing namespace std;void main() int *p; int n; coutn; if(p=new intn)=0) coutcant allocate more memory, terminatingendl; exit(1); for(int i=0;in;i+) pi=i*i+1; coutNow output the

4、 array: endl; for(i=0;in;i+) coutpi ; coutendl; delete p; /* Note:Your choice 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; coutref=refendl; num+=

5、40; coutnum=numendl; coutrefrefendl; /* Note:Your choice is C IDE */#include void main() int num; int &ref=num; num=5; coutnum=numendl; coutref=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;

6、 coutbefore swap, x:x ,y:yendl; swap(x,y); coutafter swap, x:x ,y:y;void swap(int &rx,int &ry) int t=rx; rx=ry; ry=t;#include/using namespace std;void main()/* double radius; coutradius; double result=radius*radius*3.14*4; coutThe result is (double)resultn;*/double radius;std:coutradius;std:coutThe

7、result is radius*radius*3.14*4n;/* Note:Your choice is C IDE */#include using namespace std;int array64=60,80,90,75,75,85,65,77,80,88,90,98,89,100,78,81,62,68,69,75,85,85,77,91;int &level(int grade,int size,int &tA,int &tB );void main() int typeA=0,typeB=0; int student=6; int gradesize=4; for(int i=

8、0;istudent;i+) level(arrayi,gradesize,typeA,typeB)+; coutnumber of type A is typeAendl; coutnumber of type B is typeBendl; int &level(int grade,int size,int &tA,int &tB) int sum=0; for(int i=0;i=80) return tA; else return tB;/* Note:Your choice is C IDE */#include using namespace std;float &fn2(floa

9、t r) float t; t=3.14*r*r; return t;void main() fn2(5.0)=12.4; coutfn2(5.0)endl; /* Note:Your choice is C IDE */#include #include using namespace std;void main() string s,t; coutplease input a zifuchuan:s; t=I like programming!; coutzifuchuan output:endlsendltendl; couts.append( OK!)endl;/* Note:Your

10、 choice is C 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

11、) int sum; sum=x+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); couta=aendl; coutb=bendl; /* Note:Your choice is C IDE */#include templateT min(T a,int n) int i; T minv=a0; for(i=1;iai) minv=ai; return minv; void main() int a

12、=1,3,0,2,7,6,4,5,2; double b=1.2,-3.4,6.8,9.8; int c=1,9,5,-6,7,8; couta de shuzu zhong min zhi wei:min(a,9)endl; coutb de shuzu zhong min zhi wei:min(b,4)endl; coutc de shuzu zhong min zhi wei:min(c,6)endl; /* Note:Your choice is C IDE */#include #define PI 3.1415926535#define CS Circle_Squaretempl

13、atedouble Circle_Square(T x) return x*x*PI;double Circle_Square(long x) return x*x*PI;void main() int r1=1; double r2=2.0; long r3=3; coutThe first cs is CS(r1)endl; coutThe second cs is CS(r2)endl; coutThe third cs is CS(r3)endl; /* Note:Your choice is C IDE */#include void main() int i=0,&l=i,&k=l

14、; i=+l-k; couti=iendl; /* 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:isLeapYear() return(year%4=0&year%100!=0)|(year%400=0);void Tdate:p

15、rint() coutmonth/day/yearendl; coutisLeapYear()endl;void main() Tdate x; x.set(10,23,2000); x.isLeapYear(); x.print(); /* 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) mo

16、nth=m;day=d;year=y;int Tdate:isLeapYear() return(year%4=0&year%100!=0)|(year%400=0);void Tdate:print() coutyear/month/dayendl;void someFunc(Tdate& refs) refs.print(); if(refs.isLeapYear() couterrorendl; else coutrightprint(); if(*pTdate).isLeapYear() couterrorendl; else coutrightendl; someFunc(s); #

17、includeusing namespace std;void sphere();int main() sphere();void sphere() double radius; coutradius; if(radius0) return; coutthe result is radius*radius*3.14*4n;/* Note:Your choice is C IDE */#includeconst int SIZE=10;class Cstack private: char stkSIZE; int position; public: void init() position=0;

18、 char push(char ch); char pop();char Cstack:push(char ch) if(position=SIZE) cout棧滿endl; return 0; stkposition+=ch; return ch;char Cstack:pop() if(position=0) coutn??誩ndl; return 0; return stk-position;void main() Cstack s; s.init(); char ch; coutplease input some characters:ch; while(ch!=#&s.push(ch

19、) cinch; coutendl; coutnow output the data:endl; while(ch=s.pop() coutch;/* Note:Your choice is C IDE */#include class Tdatepublic:void set(int m=5,int d=16,int y=1991) month=m;day=d;year=y;void print() coutmonth/day/yearendl;private:int month;int day;int year;void main() Tdate a,b,c; a.set(4,12,199

20、6); 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 wd,int dp) return ht*wd*dp; private: int ht,wd,dp;void main() cube c; coutc.volume(10,20)endl; coutc.volume(10,20,30)end

21、l; /* Note:Your choice is C IDE */#include class queue/*默認(rèn)私有*/int q100;int sloc,rloc;public:queue();void qput(int i);int qget();queue:queue() sloc=rloc=0; coutqueue initializedendl;void queue:qput(int i) if(sloc=100) coutqueue is fullendl; return; sloc+; qsloc=i;int queue:qget() if(rloc=sloc) coutqu

22、eue is emptyendl; return 0; rloc+; return qrloc;void main() queue a,b; a.qput(10); b.qput(20); a.qput(20); b.qput(19); couta.qget() ; coutb.qget()n; couta.qget() ; coutb.qget()n;/* Note:Your choice is C IDE */#include class testprivate:int num;float f1;public:test();test(int n,float f);int getint()

23、return num; float getfloat() return f1; ;test:test() coutInitializing defaultendl; num=0; f1=0.0;test:test(int n,float f) coutInitializingn,fendl; num=n; f1=f;void main() 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 Tdatepublic:

24、 Tdate(int m=5,int d=16,int y=1990) month=m;day=d;year=y; coutmonth/day/yearendl; private: int month; int day; int year;void main() Tdate aday; Tdate bday(2); Tdate cday(3,12); Tdate dday(1,22,1998);/* Note:Your choice is C IDE */#include #include using namespace std;class Student public: Student(ch

25、ar *pName) coutcall one parameter constructorendl; strncpy(name,pName,sizeof(name); namesizeof(name)-1=0; coutthe name is nameendl; Student() coutcall no parameter constructorendl; protected: char name20;void main() Student noName; Student ss(Jenny); /* Note:Your choice is C IDE */#includeusing name

26、space std;class test private: int num; float f1; public: test(); test(int n,float f); int getint() return num; float getfloat() return f1; ;test:test() coutInitializing defaultendl; num=0; f1=0.0;test:test(int n,float f) coutInitializingn,fendl; num=n; f1=f;void main() coutthe main function:endl; te

27、st array5; coutthe second element of array is array1.getint(),array1.getfloat()endl; /* Note:Your choice is C IDE */#includeusing namespace std;class test private: int num; float f1; public: test(int n); test(int n,float f);inline test:test(int n) coutInitializingtnendl; num=n;test:test(int n,float

28、f) coutInitializingtn,fendl; num=n; f1=f;void main() test array13=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),7.5,8.5; /* Note:Your choice is C IDE */#includeusing namespace std;class test private: int num; float f1; public: test(int n); t

29、est(int n,float f);inline test:test(int n) coutInitializingtnendl; num=n;test:test(int n,float f) coutInitializingtn,fendl; num=n; f1=f;void main() test array13=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),7.5,8.5; #includeusing namespace s

30、td;int main() for(int i=1;i=10;i+) for(int j=1;j=i;+j) coutM; coutendl; #includeclass Tdatepublic:Tdate(int m=5,int d=16,int y=1990)month=m;day=d;year=y;coutmonth/day/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#in

31、cludeusing namespace std;void main() try exception theError; throw(theError); catch(const exception &theError) couttheError.what()endl; try logic_error theLogicError(Logic Error!); throw(theLogicError); catch(const exception & theLogicError) couttheLogicError.what()endl; #include#includeusing namesp

32、ace std;class Personpublic:Person(char *na) coutcall constructorendl; name=new charstrlen(na)+1; if(name!=0) strcpy(name,na);Person(Person&p)coutcall copy constructorendl;name=new charstrlen()+1; if(name!=0) strcpy(name,);void printname() coutnameendl;Person()delete name;private:char *na

33、me;void main()Person wang(wang);Person li(wang);wang.printname();li.printname();#includeusing namespace std;class SillyClasspublic:SillyClass(int&i):ten(10),refI(i)protected:const int ten;int&refI;void main() int i; SillyClass sc(i); coutiendl;#includeclass RMBpublic:RMB(double value=0.0);operator d

34、ouble()return yuan+jf/100.0;void display()cout(yuan+jf/100.0)endl;protected:unsigned int yuan;unsigned int jf;RMB:RMB(double value) yuan=value; jf=(value-yuan)*100+0.5;void main()RMB d1(2.0),d2(1.5),d3;d3=RMB(double)d1+(double)d2);d3=d1+d2;d3.display();#include#includeclass Studentpublic:Student(cha

35、r *pName=no name,int ssId=0)strncpy(name,pName,40);name39=0;id=ssId;coutConstructing new studentpNameendl;Student(Student&s) coutConstructing copy of endl; strcpy(name,copy of); strcat(name,); id=s.id;Student() coutDestructing nameendl;protected:char name40;int id;void fn(Student s) cout

36、In function fn()nendl;void main()Student randy(Randy,1234);Student wang(wang,5678);coutCalling fn()nendl;fn(randy);coutReturned from fn()nendl;#include#includeusing namespace std;void main() try exception theError; throw(theError); catch(const exception &theError) couttheError.what()endl; try logic_

37、error theLogicError(Logic Error!); throw(theLogicError); catch(const exception & theLogicError) couttheLogicError.what()endl; #includeclass Studentpublic:Student()coutconstructing student.n;semesHours=100;gpa=3.5;Student()coutdestructing student.n;protected:int semesHours;float gpa;class Teacherpubl

38、ic:Teacher()coutconstructing teacher.n;Teacher()coutdestructing teacher.n;class Tutorpairpublic:Tutorpair()coutconstructing tutorpair.n;nomeeting=0;Tutorpair()coutdestructing tutorpair.n;protected:Student student;Teacher teacher;int nomeeting;void main()Tutorpair tp;coutback main.n;#includeclass Ast

39、atic int i;public: A() i+; int list() return i;int A:i=0;void main()A a1,a2,a3;couta1.list(),a2.list(),a3.list();#include#includeclass Studentpublic:Student(char *pName=no name) coutcreate one studentn; strncpy(name,pName,40); name39=0; noOfStudents+; coutnoOfStudentsendl;Student() coutdestruct one studentn; noOfStudents-; coutnoOfStudentsendl;static int number() return noOfStudents;protected:static int noOfStudents;char name40;int Student:noOfStudents=0;void fn(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論