




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、4-8 定義一個(gè)Dog類,包含age,weight等屬性,以及對這些屬性操作的方法。實(shí)現(xiàn)并測試這個(gè)類。#include using namespace std;class Dogpublic:void setAge(int a)age=a;int getAge()return age;void setWeight(float w)weight=w;float getWeight()return weight;private:int age;float weight;void main()Dog d;d.setAge(3);d.setWeight(30);cout小狗:d.getAge()歲,重
2、d.getWeight()斤。endl;4-9 設(shè)計(jì)并測試一個(gè)名為Rectangle的矩形類,其屬性為矩形的左下角與右上角兩個(gè)點(diǎn)的坐標(biāo),根據(jù)坐標(biāo)能計(jì)算矩形的面積。#include #include using namespace std;class Rectanglepublic:Rectangle(int xx1,int yy1,int xx2,int yy2)x1=xx1;y1=yy1;x2=xx2;y2=yy2;float getArea()return fabs(x2-x1)*fabs(y2-y1);private:int x1,y1;int x2,y2;void main()Rect
3、angle rec(0,0,10,20);cout矩形面積:rec.getArea()endl;4-11 定義并實(shí)現(xiàn)一個(gè)矩形類,有長、寬兩個(gè)屬性,由成員函數(shù)計(jì)算矩形的面積。#include using namespace std;class Rectanglepublic:Rectangle(int l,int w)length=l;width=w;float getArea()return length*width;private:int length;int width;void main()Rectangle rec(10,20);cout矩形面積:rec.getArea()endl;4
4、-13 定義一個(gè)Circle類,有數(shù)據(jù)成員radius(半徑),成員函數(shù)getArea(),計(jì)算圓的面積,構(gòu)造一個(gè)Circle的對象進(jìn)行測試。#include using namespace std;const float PI=3.1415;class Circlepublic:Circle(float r)radius=r;float getArea()return radius*PI*PI;private:float radius;void main()Circle c(5.5);cout圓的面積:c.getArea()endl;4-20 定義一個(gè)復(fù)數(shù)類Complex,使得下面的代碼能夠
5、工作。Complex c1(3,5);Complex c2=4.5;c1.add(c2);c1.show();/源程序如下:#include using namespace std;class Complexpublic:Complex(float r=0.0,float i=0.0)real=r;image=i;void add(Complex b)real=real+b.real;image=image+b.image;void show()coutreal+imageiendl;private:float real; /實(shí)部float image; /虛部;void main()Comp
6、lex c1(3,5);Complex c2=4.5; /相當(dāng)于Complex c2(4.5);c1.add(c2);c1.show();5-7 定義一個(gè)Cat類,擁有靜態(tài)數(shù)據(jù)成員numOfCats,記錄Cat的個(gè)體數(shù)目;靜態(tài)成員函數(shù)getNumOfCats(),讀取numOfCats。設(shè)計(jì)程序測試這個(gè)類,體會靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)的用法。#include using namespace std;class Catpublic:Cat()numOfCats+;Cat()numOfCats-;static int getNumOfCats()return numOfCats;private:
7、static int numOfCats;int Cat:numOfCats=0;void main()cout現(xiàn)在的Cat數(shù)量:Cat:getNumOfCats()endl;Cat a;cout現(xiàn)在的Cat數(shù)量:a.getNumOfCats()endl;Cat b;cout現(xiàn)在的Cat數(shù)量:b.getNumOfCats()endl;5-14 定義Boat與Car兩個(gè)類,二者都有weight屬性,定義二者的一個(gè)友元函數(shù)getTotalWeight(),計(jì)算二者的重量和。#include using namespace std;class Car;class Boatpublic:Boat(fl
8、oat w)weight=w;friend float getTotalWeight(Boat b,Car c);private:float weight;class Carpublic:Car(float w)weight=w;friend float getTotalWeight(Boat b,Car c);private:float weight;float getTotalWeight(Boat b,Car c)return b.weight+c.weight;void main()Boat boat(3500);Car car(1000);cout船和汽車共重getTotalWeig
9、ht(boat,car)公斤。endl;7-5 定義一個(gè)基類Shape,在此基礎(chǔ)上派生出Rectangle和Circle,二者都有g(shù)etArea()函數(shù)計(jì)算對象的面積。使用Rectangle類創(chuàng)建一個(gè)派生類Square。#include using namespace std;const float PI=3.14;class Shapepublic:Shape(float a,float b=0.0)this-a=a;this-b=b;protected:float a,b;class Rectangle : public Shapepublic:Rectangle(float l,floa
10、t w):Shape(l,w)float getArea()return a*b;class Circle : public Shapepublic:Circle(float r):Shape(r)float getArea()return a*PI*PI;class Square : public Rectanglepublic:Square(float l):Rectangle(l,l)float getArea()return a*a;void main()Rectangle r(10,20);Circle c(5);Square s(10);cout矩形的面積:r.getArea()e
11、ndl;cout圓的面積:c.getArea()endl;cout正方形的面積:s.getArea()endl;7-6 定義一個(gè)哺乳動物類Mammal,再由此派生出狗類Dog,定義一個(gè)Dog類的對象,觀察基類與派生類的構(gòu)造函數(shù)和析構(gòu)函數(shù)的調(diào)用順序。#include using namespace std;class Mammalpublic:Mammal()coutConstructing Mammal.endl;Mammal()coutDesstructing Mammal.endl;class Dog : public Mammalpublic:Dog()coutConstructing
12、Dog.endl;Dog()coutDesstructing Dog.endl;void main()Dog d;7-8 定義一個(gè)Document類,有數(shù)據(jù)成員name,從Document派生出Book類,增加數(shù)據(jù)成員pageCount。#include using namespace std;class Documentpublic:Document(char * n)strcpy(name,n);void show()coutname;private:char name50;class Book : public Documentpublic:Book(char *n,int p):Doc
13、ument(n),pageCount(p)void show()cout書名:;Document:show();coutendl頁數(shù):pageCountendl;private:int pageCount;void main()Book book(C+語言程序設(shè)計(jì),529);book.show();7-10 定義一個(gè)Object類,有數(shù)據(jù)成員weight及相應(yīng)的操作函數(shù),由此派生出Box類,增加數(shù)據(jù)成員height和width及相應(yīng)的操作函數(shù),聲明一個(gè)Box對象,觀察構(gòu)造函數(shù)與析構(gòu)函數(shù)的調(diào)用順序。#include using namespace std;class Objectpublic:O
14、bject()coutConstructing Object.endl;Object()coutDestructing Object.endl;void setWeight(int w)weight=w;int getWeight()return weight;private:int weight;class Box : public Objectpublic:Box()coutConstructing Box.endl;Box()coutDestructing Box.endl;void setHeight(int h)height=h;int getHeight()return heigh
15、t;void setWidth(int w)width=w;int getWidth()return width;private:int height;int width;void main()Box box;box.setHeight(5);box.setWidth(10);box.setWeight(8);cout盒子:高box.getHeight(),寬box.getWidth(),重box.getWeight()endl;8-4#include using namespace std;class Counterpublic:Counter(int ii=0)i=ii;void prin
16、t()couti=iendl;Counter operator +(int a)Counter temp;temp.i=i+a;return temp;private:int i;void main()Counter c;c=c+3;c.print();c=c+5;c.print();8-5#include using namespace std;class Mammalpublic:virtual void speak()coutMammal Speak!endl;class Dog:public Mammalpublic:virtual void speak()coutDog Speak!speak();8-7#include using
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 江西省新余第四中學(xué)、上高第二中學(xué)2025屆高一化學(xué)第二學(xué)期期末聯(lián)考試題含解析
- 發(fā)票搖獎資金管理辦法
- 園區(qū)企業(yè)梯隊(duì)管理辦法
- 江蘇工地進(jìn)度管理辦法
- 佛教用品規(guī)范管理辦法
- 農(nóng)業(yè)公司戰(zhàn)略管理辦法
- 新疆煤礦礦井管理辦法
- 村民身份認(rèn)定管理辦法
- 小學(xué)生經(jīng)典古詩文誦讀活動
- 數(shù)控車床主傳動系統(tǒng)設(shè)計(jì)與控制研究
- (完整word版)A3試卷模板
- GB/T 10095.2-2023圓柱齒輪ISO齒面公差分級制第2部分:徑向綜合偏差的定義和允許值
- 腰大池置管引流術(shù)的護(hù)理
- 軟件開發(fā)項(xiàng)目工作量及報(bào)價(jià)模板
- 機(jī)械式濕度計(jì)考試題
- 項(xiàng)目管理班子配備情況
- 精選常熟市化工企業(yè)名單
- GB/T 3723-1999工業(yè)用化學(xué)產(chǎn)品采樣安全通則
- FZ/T 73044-2012針織配飾品
- 長白綠葉冰泉人參飲料商業(yè)計(jì)劃書0714
- 船舶修理92黃本
評論
0/150
提交評論