面向?qū)ο驝++習(xí)題參考解答_第1頁
面向?qū)ο驝++習(xí)題參考解答_第2頁
面向?qū)ο驝++習(xí)題參考解答_第3頁
面向?qū)ο驝++習(xí)題參考解答_第4頁
面向?qū)ο驝++習(xí)題參考解答_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

4-8定義一個Dog類,包含age,weight等屬性,以及對這些屬性操作的方法。實現(xiàn)并測試這個類。#include<iostream>usingnamespacestd;classDog{public: voidsetAge(inta) { age=a; } intgetAge() { returnage; } voidsetWeight(floatw) { weight=w; } floatgetWeight() { returnweight; }private: intage; floatweight;};voidmain(){ Dogd; d.setAge(3); d.setWeight(30); cout<<"小狗:"<<d.getAge()<<"歲,重"<<d.getWeight()<<"斤。"<<endl;}4-9設(shè)計并測試一個名為Rectangle的矩形類,其屬性為矩形的左下角與右上角兩個點的坐標,根據(jù)坐標能計算矩形的面積。#include<iostream>#include<math.h>usingnamespacestd;classRectangle{public: Rectangle(intxx1,intyy1,intxx2,intyy2) { x1=xx1; y1=yy1; x2=xx2; y2=yy2; } floatgetArea() { returnfabs(x2-x1)*fabs(y2-y1); }private: intx1,y1; intx2,y2;};voidmain(){ Rectanglerec(0,0,10,20); cout<<"矩形面積:"<<rec.getArea()<<endl;}4-11定義并實現(xiàn)一個矩形類,有長、寬兩個屬性,由成員函數(shù)計算矩形的面積。#include<iostream>usingnamespacestd;classRectangle{public: Rectangle(intl,intw) { length=l; width=w; } floatgetArea() { returnlength*width; }private: intlength; intwidth;}; Cat() { numOfCats++; } ~Cat() { numOfCats--; } staticintgetNumOfCats() { returnnumOfCats; }private: staticintnumOfCats;};intCat::numOfCats=0;voidmain(){ cout<<"現(xiàn)在的Cat數(shù)量:"<<Cat::getNumOfCats()<<endl; Cata; cout<<"現(xiàn)在的Cat數(shù)量:"<<a.getNumOfCats()<<endl; Catb; cout<<"現(xiàn)在的Cat數(shù)量:"<<b.getNumOfCats()<<endl;}5-14定義Boat與Car兩個類,二者都有weight屬性,定義二者的一個友元函數(shù)getTotalWeight(),計算二者的重量和。#include<iostream>usingnamespacestd;classCar;classBoat{public: Boat(floatw) { weight=w; } friendfloatgetTotalWeight(Boatb,Carc);private: floatweight;};classCar{public: Car(floatw) { weight=w; } friendfloatgetTotalWeight(Boatb,Carc);private: floatweight;};floatgetTotalWeight(Boatb,Carc){ returnb.weight+c.weight;}voidmain(){ Boatboat(3500); Carcar(1000); cout<<"船和汽車共重"<<getTotalWeight(boat,car)<<"公斤。"<<endl;}7-5定義一個基類Shape,在此基礎(chǔ)上派生出Rectangle和Circle,二者都有g(shù)etArea()函數(shù)計算對象的面積。使用Rectangle類創(chuàng)建一個派生類Square。#include<iostream>usingnamespacestd;constfloatPI=3.14;classShape{public: Shape(floata,floatb=0.0) { this->a=a; this->b=b; }protected: floata,b;};classRectangle:publicShape{public: Rectangle(floatl,floatw):Shape(l,w) { } floatgetArea() { returna*b; }};classCircle:publicShape{public: Circle(floatr):Shape(r) { } floatgetArea() { returna*PI*PI; }};classSquare:publicRectangle{public: Square(floatl):Rectangle(l,l) { } floatgetArea() { returna*a; }};voidmain(){ Rectangler(10,20); Circlec(5); Squares(10); cout<<"矩形的面積:"<<r.getArea()<<endl; cout<<"圓的面積:"<<c.getArea()<<endl; cout<<"正方形的面積:"<<s.getArea()<<endl;}7-6定義一個哺乳動物類Mammal,再由此派生出狗類Dog,定義一個Dog類的對象,觀察基類與派生類的構(gòu)造函數(shù)和析構(gòu)函數(shù)的調(diào)用順序。#include<iostream>usingnamespacestd;classMammal{public: Mammal() { cout<<"ConstructingMammal."<<endl; } ~Mammal() { cout<<"DesstructingMammal."<<endl; }};classDog:publicMammal{public: Dog() { cout<<"ConstructingDog."<<endl; } ~Dog() { cout<<"DesstructingDog."<<endl; }};voidmain(){ Dogd;}7-8定義一個Document類,有數(shù)據(jù)成員name,從Document派生出Book類,增加數(shù)據(jù)成員pageCount。#include<iostream>usingnamespacestd;classDocument{public: Document(char*n) { strcpy(name,n); } voidshow() { cout<<name; }private: charname[50];};classBook:publicDocument{public: Book(char*n,intp):Document(n),pageCount(p) { } voidshow() { cout<<"書名:"; Document::show(); cout<<endl<<"頁數(shù):"<<pageCount<<endl; }private: intpageCount;};voidmain(){ Bookbook("C++語言程序設(shè)計",529); book.show();}7-10定義一個Object類,有數(shù)據(jù)成員weight及相應(yīng)的操作函數(shù),由此派生出Box類,增加數(shù)據(jù)成員height和width及相應(yīng)的操作函數(shù),聲明一個Box對象,觀察構(gòu)造函數(shù)與析構(gòu)函數(shù)的調(diào)用順序。#include<iostream>usingnamespacestd;classObject{public: Object() { cout<<"ConstructingObject."<<endl; } ~Object() { cout<<"DestructingObject."<<endl; } voidsetWeight(intw) { weight=w; } intgetWeight() { returnweight; }private: intweight;};classBox:publicObject{public: Box() { cout<<"ConstructingBox."<<endl; } ~Box() { cout<<"DestructingBox."<<endl; } voidsetHeight(inth) { height=h; } intgetHeight() { returnheight; } voidsetWidth(intw) { width=w; } intgetWidth() { returnwidth; }private: intheight; intwidth;};voidmain(){ Boxbox; box.setHeight(5); box.setWidth(10); box.setWeight(8); cout<<"盒子:高"<<box.getHeight()<<",寬"<<box.getWidth()<<",重"<<box.getWeight()<<endl;}8-4#include<iostream>usingnamespacestd;classCounter{public: Counter(intii=0) { i=ii; } voidprint() { cout<<"i="<<i<<endl; } Counteroperator+(inta) { Countertemp; temp.i=i+a; returntemp; }private: inti;};voidmain(){ Counterc; c=c+3; c.print(); c=c+5; c.print();}8-5#include<iostream>usingnamespacestd;classMammal{public: virtualvoidspeak() { cout<<"MammalSpeak!"<<endl; }};classDog:publicMammal{public: virtualvoidspeak() { cout<<"DogSpeak!"<<endl; }};voidmain(){ Dogd; d.speak(); Mammal*p=&d; p->speak();}8-7#include<iostream>usingnamespacestd;classPoint{public: Point(intx=0,inty=0) { X=x; Y=y; } void

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論