面向?qū)ο驝++習(xí)題參考解答.doc_第1頁
面向?qū)ο驝++習(xí)題參考解答.doc_第2頁
面向?qū)ο驝++習(xí)題參考解答.doc_第3頁
面向?qū)ο驝++習(xí)題參考解答.doc_第4頁
面向?qū)ο驝++習(xí)題參考解答.doc_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余8頁可下載查看

下載本文檔

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

文檔簡(jiǎn)介

4-8 定義一個(gè)Dog類,包含age,weight等屬性,以及對(duì)這些屬性操作的方法。實(shí)現(xiàn)并測(cè)試這個(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()歲,重d.getWeight()斤。endl;4-9 設(shè)計(jì)并測(cè)試一個(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()Rectangle rec(0,0,10,20);cout矩形面積:rec.getArea()endl;4-11 定義并實(shí)現(xiàn)一個(gè)矩形類,有長(zhǎng)、寬兩個(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-13 定義一個(gè)Circle類,有數(shù)據(jù)成員radius(半徑),成員函數(shù)getArea(),計(jì)算圓的面積,構(gòu)造一個(gè)Circle的對(duì)象進(jìn)行測(cè)試。#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,使得下面的代碼能夠工作。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()Complex 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ì)程序測(cè)試這個(gè)類,體會(huì)靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)的用法。#include using namespace std;class Catpublic:Cat()numOfCats+;Cat()numOfCats-;static int getNumOfCats()return numOfCats;private: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(float 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船和汽車共重getTotalWeight(boat,car)公斤。endl;7-5 定義一個(gè)基類Shape,在此基礎(chǔ)上派生出Rectangle和Circle,二者都有g(shù)etArea()函數(shù)計(jì)算對(duì)象的面積。使用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,float 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()endl;cout圓的面積:c.getArea()endl;cout正方形的面積:s.getArea()endl;7-6 定義一個(gè)哺乳動(dòng)物類Mammal,再由此派生出狗類Dog,定義一個(gè)Dog類的對(duì)象,觀察基類與派生類的構(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 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):Document(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對(duì)象,觀察構(gòu)造函數(shù)與析構(gòu)函數(shù)的調(diào)用順序。#include using namespace std;class Objectpublic:Object()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 height;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 print()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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論