解析C例題代碼第3章例題_第1頁(yè)
解析C例題代碼第3章例題_第2頁(yè)
解析C例題代碼第3章例題_第3頁(yè)
解析C例題代碼第3章例題_第4頁(yè)
解析C例題代碼第3章例題_第5頁(yè)
已閱讀5頁(yè),還剩23頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、第3章例題【例3-1】/employee2.h#include #include using namespace std;class Employee /定義Employee類public:Employee(); /構(gòu)造函數(shù)的聲明 void Setname(string); /設(shè)置姓名函數(shù) void Setpay(float pay); /設(shè)置工資函數(shù) string Getname();/提取姓名函數(shù)void Display();/顯示信息函數(shù)protected:string name; /姓名int no; /編號(hào)float salary; /月工資 static int maxno; /靜

2、態(tài)數(shù)據(jù)成員,表示員工編號(hào)基數(shù);/employee2.cpp#include employee2.hint Employee:maxno=1000; /靜態(tài)數(shù)據(jù)成員初始化Employee:Employee() /構(gòu)造函數(shù)的實(shí)現(xiàn) no=+maxno;void Employee:Setname(string namep) /設(shè)置姓名函數(shù),指針作函數(shù)參數(shù) name=namep;void Employee:Display()/顯示信息函數(shù) cout編號(hào)為:no,本月工資為:salaryendl;string Employee:Getname()/提取姓名函數(shù)return name;void Employ

3、ee:Setpay(float pay)/設(shè)置工資函數(shù)salary=pay;/example3_34.cpp#include employee2.hint main() Employee employy4; /創(chuàng)建對(duì)象數(shù)組float pay;string nameptr;/字符數(shù)組int i;for(i=0;i4;i+)coutnameptr;employyi.Setname(nameptr);coutpay;employyi.Setpay(pay);for(i=0;i4;i+)coutemployyi.Getname()的;employyi.Display();return 0;【例3-2】

4、/example3_2.cpp#include using namespace std;void f1()int a=1,b=2; static int c; a+; b+; c+; couta b cendl;int main() int a=1,b=2,c=3; f1(); f1(); f1(); couta b cendl; return 0;【例3-3】/example3_3.cpp#include using namespace std; int main()int *iptr; /聲明 int型指針 iptrint i; /聲明int型數(shù) iiptr=&i; /取 i的地址賦給 i

5、ptri=100; /int型數(shù)賦初值coutOutput int i= i endl; /輸出int型數(shù)的值coutOutput int pointer i=*iptrendl;/輸出指針?biāo)傅刂返膬?nèi)容return 0;【例3-4】/example3_4.cpp#include using namespace std;int main()int *a=new int; /在堆中分配int型變量所需空間,并將起始地址賦給指針a*a=76; /將76存入指針a指向的內(nèi)存空間cout*aendl; /輸出指針a指向的內(nèi)存空間的值delete a;/釋放a指向的動(dòng)態(tài)存儲(chǔ)空間return 0;【例3-

6、5】/example3_5.cpp#include using namespace std;int main()int arraysize;int *array;coutarraysize;if(array=new intarraysize)=NULL) /申請(qǐng)一塊連續(xù)的存儲(chǔ)空間coutCant allocate memory, terminating.;/未分配到存儲(chǔ)空間exit(1); /發(fā)生錯(cuò)誤,退出程序for(int count=0;countarraysize;count+)arraycount=count*2; coutarraycount ;coutendl;delete arr

7、ay; /釋放array指向的連續(xù)存儲(chǔ)空間return 0;【例3-6】/example3_6.cpp#include using namespace std;struct list/結(jié)構(gòu)體int data;list *next;class Queue/隊(duì)列類public:Queue()/構(gòu)造函數(shù)ptrf=ptrb=NULL; /將兩個(gè)指針初始化為空void enqueue(int); /入隊(duì)函數(shù)int dequeue(); /出隊(duì)函數(shù)private:list *ptrf; /隊(duì)首指針list *ptrb;/隊(duì)尾指針;void Queue:enqueue(int x) /入隊(duì)函數(shù)的實(shí)現(xiàn)list

8、 *newnode=new list; /動(dòng)態(tài)分配內(nèi)存 newnode-data=x;newnode-next=NULL;if(ptrb=NULL) /隊(duì)空時(shí)入隊(duì)的情況,如圖3-4所示ptrf=ptrb=newnode;else /隊(duì)非空時(shí)入隊(duì)的情況,如圖3-5所示ptrb-next=newnode; ptrb=newnode;int Queue:dequeue() /出隊(duì)函數(shù)的實(shí)現(xiàn)list *tmp;int value;value=ptrf-data;tmp=ptrf;ptrf=ptrf-next; /出隊(duì)后的情況如圖3-6所示delete tmp;return value; /返回出隊(duì)的數(shù)

9、據(jù)項(xiàng)int main()Queue A; /隊(duì)列對(duì)象Aint arr=2,3,4,5,7; cout入隊(duì)順序:;for(int i=0;i5;i+)coutarri ;A.enqueue(arri); /將數(shù)組元素作為數(shù)據(jù)項(xiàng)依次入隊(duì)coutendl出隊(duì)順序:;for(i=0;i5;i+)coutA.dequeue() ; /將隊(duì)列中數(shù)據(jù)項(xiàng)依次出隊(duì)coutendl; return 0;【例3-7】/student.h#include using namespace std;class Student /學(xué)生類的聲明 int id; /私有數(shù)據(jù)成員,外部不可見char name10; float

10、score; /為了簡(jiǎn)化程序,只保留了一個(gè)學(xué)生成績(jī)public: /公有成員函數(shù),外部接口void input(int pid,char *pname,float s);void modify(float s);void display();/student.cpp#include student.hvoid Student:input(int pid,char *pname,float s) /成員函數(shù)的實(shí)現(xiàn)id=pid;strcpy(name,pname);score=s;void Student:modify(float s)score=s; void Student:display()

11、 cout id:idendl; /雖在類外,成員函數(shù)仍可訪問私有成員cout name:nameendl;coutscore:scoreendl;/example3_7.cpp#include student.hint main()Student *pstu=new Student; /動(dòng)態(tài)申請(qǐng)內(nèi)存空間(*pstu).input(,Wang Li,90); /通過堆對(duì)象訪問公有成員函數(shù)(*pstu).display(); /通過堆對(duì)象訪問公有成員函數(shù)(*pstu).modify(85); /通過堆對(duì)象訪問公有成員函數(shù)(*pstu).display(); /通過堆對(duì)象訪問公有成員函數(shù)delet

12、e pstu;return 0;【例3-8】/example3_8.cpp#include using namespace std; namespace myown1 /定義名字空間myown1char *user_name=myown1;/在名字空間myown1中定義指針名user_namenamespace myown2 /定義名字空間myown2char *user_name=myown2;/在名字空間myown2中定義指針名user_nameint main()coutHello, myown1:user_name /用名字空間限制符訪問變量user_name. and goodbye

13、!endl;cout Hello, myown2:user_name /用名字空間限制符訪問變量user_name. and goodbye!endl;return 0;【例3-9】/example3_9.cpp#include using namespace std;/void v; /錯(cuò)誤,不能聲明void類型的變量void *vp; /正確,可以聲明void類型的指針int main()int *ip;int i=10;vp=&i; /void類型指針指向整型變量/cout*vp=*vpendl;/錯(cuò)誤,void型指針在使用前必須確定類型ip=(int *)vp; /類型強(qiáng)制轉(zhuǎn)換, vo

14、id類型指針賦值給int類型指針cout*ip=*ipinput(,Wang Li,90); /通過指針訪問公有成員函數(shù)pstu-display(); /通過指針訪問公有成員函數(shù)(*pstu).modify(85); (*pstu).display();return 0;【例3-11】/example3_11.cpp#include using namespace std;class Boxpublic:Box(); Box(int h,int w ,int len):height(h),width(w),length(len)int volume();Box();private:int he

15、ight;int width;int length;Box:Box() height=10;width=10;length=10;Box:Box() coutDestructor is calledendl;int Box:volume()return(height*width*length);int main()Box *pbox1=new Box; /定義指向Box堆對(duì)象的指針變量pbox1,coutThe volume of box1 is volume()endl;delete pbox1; /釋放pbox1指向的對(duì)象空間Box *pbox2=new Box(15,30,25);/定義

16、指向Box堆對(duì)象的指針變量pbox2, /在pbox2中存放堆對(duì)象的起始地址并初始化堆對(duì)象 coutThe volume of box2 is volume()endl;/指針訪問成員delete pbox2; /釋放pbox2指向的對(duì)象空間return 0; 【例3-12】/example3_12.cpp#include using namespace std;void swap(int *x,int *y) /形參為指針int *temp; temp=*x;*x=*y;*y=temp;int main() int a=1,b=2; /定義變量coutBefore Swap a=a,b=be

17、ndl; swap(&a,&b); /函數(shù)調(diào)用 coutAfter Swap a=a,b=bendl; return 0;【例3-13】/example3_13.cpp#include using namespace std;class CStrtemppublic: CStrtemp(char *s); CStrtemp(const CStrtemp&); /復(fù)制構(gòu)造函數(shù),省略形參 CStrtemp(); /析構(gòu)函數(shù) void show(); /成員函數(shù) void set(char *s); /賦值新串函數(shù)private: char *str; /私有數(shù)據(jù)成員;CStrtemp:CStrte

18、mp(char *s)coutconstructor.endl;str=new charstrlen(s)+1; /動(dòng)態(tài)申請(qǐng)內(nèi)存單元,見3.2.3節(jié)if(!str)cerrAllocationg Errorendl;/cerr是標(biāo)準(zhǔn)流對(duì)象,參看第8章exit(1);/發(fā)生錯(cuò)誤,退出程序strcpy(str,s); /將字符串復(fù)制到str指向的存儲(chǔ)空間中CStrtemp:CStrtemp(const CStrtemp& temp) /復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn) coutcopy constructor.endl; str=new charstrlen(temp.str)+1; if(!str) cerr

19、error in apply new space.endl; exit(1); /發(fā)生錯(cuò)誤,退出程序 strcpy(str,temp.str);CStrtemp:CStrtemp() /析構(gòu)函數(shù)coutdestructor.endl;if(str!=NULL)delete str; /釋放str指向的存儲(chǔ)空間void CStrtemp:show() /成員函數(shù)coutstrendl;void CStrtemp:set(char *s) /成員函數(shù)的實(shí)現(xiàn) delete str; /釋放str指向的存儲(chǔ)空間 str=new charstrlen(s)+1; if(!str) cerrAllocat

20、ion Errorendl; exit(1); /發(fā)生錯(cuò)誤,退出程序 strcpy(str,s);CStrtemp Input(CStrtemp *temp) /對(duì)象指針用做函數(shù)參數(shù) char s20; couts; /輸入新字符串 temp-set(s); /指針訪問成員函數(shù) return *temp; /返回指針?biāo)赶虻膶?duì)象int main() CStrtemp A(hello); /用字符串hello初始化對(duì)象A A.show(); CStrtemp B=Input(&A); /用對(duì)象指針?biāo)赶虻膶?duì)象初始化對(duì)象B A.show(); B.show(); return 0;【例3-14】/

21、point.h #include #include using namespace std; class point /聲明類 public: point(double=0,double=0);/構(gòu)造函數(shù) double Getx(); /成員函數(shù)原型 double Gety(); void Setxy(double,double); point(); private: double x,y; ; const int num=10; /申請(qǐng)數(shù)量 void Set(point *); /普通函數(shù)原型 void Display(point *);double Lenth(point *); /poi

22、nt.cpp#includepoint.h /定義類的成員函數(shù) point:point(double a,double b) x=a; y=b; double point:Getx() return x; double point:Gety() return y; void point:Setxy(double a,double b) x=a; y=b; point:point() coutdelete it:x,yendl; /example3_14.cpp#includepoint.h int main() point *p=new point10;/申請(qǐng)10個(gè)對(duì)象的內(nèi)存 if(p=NUL

23、L) /如果沒有申請(qǐng)到,則退出程序 cout地址申請(qǐng)失敗,結(jié)束程序運(yùn)行。n; return 0; Set(p); /調(diào)數(shù)據(jù)輸入函數(shù) cout內(nèi)存塊的數(shù)據(jù)如下:endl; Display(p); /顯示數(shù)據(jù) cout組成的折線長(zhǎng)度為:; coutLenth(p)endl; /輸出折線長(zhǎng)度 delete p; /釋放內(nèi)存 return 0; /給內(nèi)存塊賦值的函數(shù) void Set(point *p) double a,b; for(int i=0;inum;i+) coutInput 第i+1ab; (p+i)-Setxy(a,b); /給申請(qǐng)的內(nèi)存塊賦值 /顯示內(nèi)存塊的值的函數(shù) void Dis

24、play(point *p) for(int i=0;inum;i+) coutGetx(),Gety()Getx(); /保存第一個(gè)點(diǎn)坐標(biāo)的x值 b1=p-Gety(); /保存第一個(gè)點(diǎn)坐標(biāo)的y值 for(int i=1;iGetx(); /取下一點(diǎn)的x坐標(biāo) b2=(p+i)-Gety(); /取下點(diǎn)的Y坐標(biāo) sum=sum+sqrt(a1-a2)*(a1-a2)+(b1-b2)*(b1-b2); /累計(jì)長(zhǎng)度 a1=a2; /依次平移 b1=b2; /依次平移 return sum; 【例3-15】/example3_15.cpp#include using namespace std;cl

25、ass Boxpublic: Box(int h=10,int w=12,int len=15):height(h),width(w),length(len) /帶默認(rèn)參數(shù)的構(gòu)造函數(shù),含成員初始化表int volume();private: int height; int width; int length;int Box:volume()return(height*width*length); int main() Box a,b(15,18,20),c(16,20,26); coutvolume of a is a.volume()endl; coutvolume of b is b.vo

26、lume()endl; coutvolume of c is c.volume()endl; return 0;【例3-16】/example3_16.cpp#include using namespace std;class Testpublic:Test(int=0);void print();private:int x;Test:Test(int a) x=a;void Test:print()coutx=xx=x n(*this).x=(*this).xendl;int main() Test t(12);t.print();return 0;【例3-17】/example3_17.c

27、pp#include using namespace std;class Apublic:A()i=0; void add()i+;A *sp()return this; /定義指針函數(shù)sp(),顯式使用thisint Is(A *s)return s-i; /通過對(duì)象指針訪問私有數(shù)據(jù)成員private: int i; ;A *p2; /定義全局對(duì)象指針數(shù)組p2int main()A a1,*a2=new A; /定義對(duì)象指針a2p0=a1.sp();p1=a2;p0-add(); /通過對(duì)象指針訪問公有成員函數(shù)a2-add();p1-add();couta1-this:p0 a2-this:

28、p1endl;couti-(a1):Is(p0) i-(a2):Is(p1)endl;return 0;【例3-18】/example3_18.cpp#include using namespace std;class Point public: Point(int xx=0,int yy=0)X=xx;Y=yy;/構(gòu)造函數(shù) int GetX()return X; int GetY()return Y;private: int X,Y;int main() Point A(4,5);/聲明對(duì)象A Point *p1=&A;/聲明對(duì)象指針并初始化 int (Point:*pGetX)()= Po

29、int:GetX;/聲明成員函數(shù)指針并初始化 cout(A.*pGetX)() t; /使用成員函數(shù)指針訪問成員函數(shù) coutGetX)() t; /使用對(duì)象指針訪問成員函數(shù) coutA.GetX()endl;/使用對(duì)象名訪問成員函數(shù) return 0;【例3-19】/example3_19.cpp#include using namespace std;class Point public: Point(int xx=0,int yy=0)X=xx;Y=yy;count+;/構(gòu)造函數(shù) Point(const Point &p);/復(fù)制構(gòu)造函數(shù)的聲明 int GetX()return X; i

30、nt GetY()return Y; static int count;/靜態(tài)數(shù)據(jù)成員private: int X,Y; ;int Point:count=0;/靜態(tài)數(shù)據(jù)成員的初始化Point:Point(const Point &p) /復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn) X=p.X; Y=p.Y; count+;int main()/主函數(shù) int *countp=&Point:count;/聲明一個(gè)int型指針,指向類的靜態(tài)成員 Point A(4,5);/聲明對(duì)象A cout Point A: A.GetX(),A.GetY();/通過對(duì)象訪問成員函數(shù) cout Object id= *countpe

31、ndl;/通過指針訪問靜態(tài)數(shù)據(jù)成員 Point B(A);/聲明對(duì)象B cout Point B: B.GetX(),B.GetY(); cout Object id= *countpendl;/通過指針訪問靜態(tài)數(shù)據(jù)成員 return 0;【例3-20】/example3_20.cpp#include using namespace std;class Point public: Point(int xx=0,int yy=0)X=xx;Y=yy;count+;/構(gòu)造函數(shù) Point(const Point &p);/復(fù)制構(gòu)造函數(shù) int GetX()return X; int GetY()r

32、eturn Y; static void GetC()cout Object id= countendl;/靜態(tài)成員函數(shù)private: int X,Y; static int count;/靜態(tài)數(shù)據(jù)成員的初始化;Point:Point(const Point &p) X=p.X; Y=p.Y; count+;int Point:count=0; /靜態(tài)數(shù)據(jù)成員的初始化int main()/主函數(shù) void (*gc)()=Point:GetC;/聲明一個(gè)指向函數(shù)的指針,指向類的靜態(tài)成員函數(shù) Point A(4,5);/聲明對(duì)象A coutPoint A: A.GetX(),A.GetY();

33、 gc();/通過指針訪問靜態(tài)成員函數(shù) Point B(A);/聲明對(duì)象B coutPoint B: B.GetX(),B.GetY(); gc();/通過指針訪問靜態(tài)成員函數(shù) return 0;【例3-21】/example3_21.cpp#include using namespace std;int main() int a=5,b=5; int *ptr = &b; /定義并初始化指針 int & ref = a; /定義并初始化引用 *ptr += 2; ref += 2; couta = aendl; coutb = bendl;【例3-22】/example3_22.cpp#in

34、clude using namespace std;int main() int *ptr,a=10,b = 20; /定義指針,但未初始化 int & ref ; /定義引用,但未初始化 int &ref1 = 100; /用常量初始化非常量引用 const int &ref2 = 100; /用常量初始化常量引用 int & ref3 = a; ref2 += 2; ref3+;ref3 = b;cout a = aendl; return 0;【例3-23】/example3_23.cpp#include using namespace std;int main() int a=1; i

35、nt *p = &a; int & r = a; cout取變量a的地址:&aendl; /取變量a的地址 cout取指針p的地址:&pendl; /取指針p的地址 cout取引用r的地址:&rendl; /取引用r的地址return 0;【例3-24】/example3_24.cpp#include using namespace std;void swap(int& x,int& y) /形參為引用 int temp; temp=x;x=y;y=temp;int main() int a=1,b=2; /定義變量coutBefore Swap a=a,b=bendl; swap(a,b);

36、 /函數(shù)調(diào)用 coutAfter Swap a=a,b=bendl; return 0;【例3-25】/example3_25.cpp#include using namespace std;class CStrtemppublic: CStrtemp(char *s); CStrtemp(const CStrtemp&); /復(fù)制構(gòu)造函數(shù),省略形參 CStrtemp(); /析構(gòu)函數(shù) void show(); /成員函數(shù) void set(char *s); /賦值新串函數(shù)private: char *str; /私有數(shù)據(jù)成員;CStrtemp:CStrtemp(char *s)coutco

37、nstructor.endl;str=new charstrlen(s)+1; /動(dòng)態(tài)申請(qǐng)內(nèi)存單元,見3.2.3節(jié)if(!str)cerrAllocationg Errorendl;/cerr是標(biāo)準(zhǔn)流對(duì)象,參看第8章exit(1);/發(fā)生錯(cuò)誤,退出程序strcpy(str,s); /將字符串復(fù)制到str指向的存儲(chǔ)空間中CStrtemp:CStrtemp(const CStrtemp& temp) /復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn) coutcopy constructor.endl; str=new charstrlen(temp.str)+1; if(!str) cerrerror in apply ne

38、w space.endl; exit(1); /發(fā)生錯(cuò)誤,退出程序 strcpy(str,temp.str);CStrtemp:CStrtemp() /析構(gòu)函數(shù)coutdestructor.endl;if(str!=NULL)delete str; /釋放str指向的存儲(chǔ)空間void CStrtemp:show() /成員函數(shù)coutstrendl;void CStrtemp:set(char *s) /成員函數(shù)的實(shí)現(xiàn) delete str; /釋放str指向的存儲(chǔ)空間 str=new charstrlen(s)+1; if(!str) cerrAllocation Errorendl; ex

39、it(1); /發(fā)生錯(cuò)誤,退出程序 strcpy(str,s);CStrtemp Input(CStrtemp &temp) /對(duì)象引用做函數(shù)參數(shù) char s20; couts; /輸入新字符串 temp.set(s); /對(duì)象引用訪問成員函數(shù) return temp; /返回對(duì)象引用int main() CStrtemp A(hello); /用字符串hello初始化對(duì)象A A.show(); CStrtemp B=Input(A); /用對(duì)象指針?biāo)赶虻膶?duì)象初始化對(duì)象B A.show(); B.show(); return 0;【例3-36】/example3_36.cpp#includ

40、e #include #include using namespace std;class CStrtemppublic: CStrtemp(char *s) str=s;/將字符串賦給string對(duì)象 CStrtemp(const CStrtemp&); /復(fù)制構(gòu)造函數(shù)的聲明,省略形參 void show() /成員函數(shù) coutstrendl; /輸出string對(duì)象 void set(char *s); /賦值新串private: string str; /string對(duì)象作類成員;CStrtemp:CStrtemp(const CStrtemp& temp) /復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn)str=temp.str; /string對(duì)象之間的賦值void CStrtemp:set(char *s) /成員函數(shù)的實(shí)現(xiàn) str=s;CStrtemp Input(CStrtemp temp) /對(duì)象用做函數(shù)參

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論