




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、作業(yè)4一、 選擇題1 下列關(guān)于動態(tài)聯(lián)編的描述中,錯誤的是_。D A)動態(tài)聯(lián)編是以虛函數(shù)為基礎(chǔ)的 B)動態(tài)聯(lián)編是在運行時確定所調(diào)用的函數(shù)代碼的 C)動態(tài)聯(lián)編調(diào)用函數(shù)操作是指向?qū)ο蟮闹羔樆驅(qū)ο笠肈)動態(tài)聯(lián)編是在編譯時確定操作函數(shù)的注:先期聯(lián)編也稱靜態(tài)聯(lián)編,遲后聯(lián)編也稱動態(tài)聯(lián)編。注釋:動態(tài)聯(lián)編一直要到程序運行時才能確定調(diào)用哪個函數(shù)。 虛函數(shù)是實現(xiàn)動態(tài)聯(lián)編的必要條件之一。沒有虛函數(shù)一定不能實現(xiàn)動態(tài)聯(lián)編,但 有虛函數(shù)存在時,必須同時滿足下列條件,才能夠?qū)崿F(xiàn)動態(tài)聯(lián)編: 類之間滿足子類型關(guān)系; 調(diào)用虛函數(shù)操作的是指向?qū)ο蟮闹羔樆蛘邔ο笠茫夯蛘呤怯沙蓡T函數(shù)調(diào)用 虛函數(shù)。2 關(guān)于虛函數(shù)的描述中,正確的是_。
2、D A)虛函數(shù)是一個靜態(tài)成員函數(shù) B)虛函數(shù)是一個非成員函數(shù) C)虛函數(shù)既可以在函數(shù)說明時定義,也可以在函數(shù)實現(xiàn)時定義D)派生類的虛函數(shù)與基類中對應(yīng)的虛函數(shù)具有相同的參數(shù)個數(shù)和類型注釋:虛函數(shù)是非靜態(tài)的成員函數(shù)。它不能是友元函數(shù),但可以在另一個類中被聲明為友元函數(shù)。 虛函數(shù)聲明只能出現(xiàn)在類定義的函數(shù)原型聲明中,而不能在成員函數(shù)的函數(shù)體實現(xiàn)的時候。 派生類的虛函數(shù)與基類中對應(yīng)的虛函數(shù)必須滿足下列條件,否則派生類中的虛函數(shù)將丟失其虛特性,在調(diào)用時進行靜態(tài)聯(lián)編: 派生類中的虛函數(shù)與基類中的虛函數(shù)具有相同的名稱: 派生類中的虛函數(shù)與基類中的虛函數(shù)具有相同的參數(shù)個數(shù)和相同的對應(yīng)參數(shù) 類型: 派生類中的虛
3、函數(shù)與基類中的虛函數(shù)的返回值或者相同,或者都返回指針或 引用,并且派生類虛函數(shù)所返回的指針或引用的基類型是基類中的虛函數(shù)所 返回的指針或引用的基類型的子類型。3 在下面四個選項中,_是用來聲明虛函數(shù)的。A A)virtual B)public C)using D)false注釋:說明虛函數(shù)的一般格式如下: virtua1<函數(shù)返回類型><函數(shù)名>(<參數(shù)表>)4 對虛函數(shù)的調(diào)用_。D A)一定使用動態(tài)聯(lián)編 B)必須使用動態(tài)聯(lián)編C)一定使用靜態(tài)聯(lián)編 D)不一定使用動態(tài)聯(lián)編注釋:參見第1題的注釋。5 實現(xiàn)運行時的多態(tài)性要使用_。D A)重載函數(shù) B)構(gòu)造函數(shù) C
4、)析構(gòu)函數(shù) D)虛函數(shù)注釋:參見第1題的注釋。6 要實現(xiàn)動態(tài)聯(lián)編,必須通過_調(diào)用虛函數(shù)。A A)對象指針 B)成員名限定 C)對象名 D)派生類名注釋:參見第1題的注釋。7 在派生類中重新定義虛函數(shù)時,除了_方面,其他方面都必須與基類中相應(yīng)的 虛函數(shù)保持一致。D A)參數(shù)個數(shù) B)參數(shù)類型 C)函數(shù)名稱 D)函數(shù)體注釋:參見第2題的注釋。8 下面關(guān)于構(gòu)造函數(shù)和析構(gòu)函數(shù)的描述,錯誤的是_。D A)析構(gòu)函數(shù)中調(diào)用虛函數(shù)采用靜態(tài)聯(lián)編B)對虛析構(gòu)函數(shù)的調(diào)用可以采用動態(tài)聯(lián)編C)當基類的析構(gòu)函數(shù)是虛函數(shù)時,其派生類的析構(gòu)函數(shù)也一定是虛函數(shù)D)構(gòu)造函數(shù)可以聲明為虛函數(shù)注釋:構(gòu)造函數(shù)不能聲明為虛函數(shù),但析構(gòu)函
5、數(shù)可以聲明為虛函數(shù)。當基類的析構(gòu)函數(shù)聲明為虛函數(shù)時,無論派生類是否使用virtual關(guān)鍵字說明,派生類的析構(gòu)函數(shù)一定是虛函數(shù),對缺省析構(gòu)函數(shù)亦然。而且,如果滿足動態(tài)聯(lián)編的其他條件,對虛析構(gòu)函數(shù)的調(diào)用將采用動態(tài)聯(lián)編。 構(gòu)造函數(shù)不能聲明為虛函數(shù),但在構(gòu)造函數(shù)中可以調(diào)用虛函數(shù)。在構(gòu)造函數(shù)或析構(gòu)函數(shù)中調(diào)用虛函數(shù),將采用靜態(tài)聯(lián)編。9 關(guān)于純虛函數(shù)和抽象類的描述中,錯誤的是_。C A)純虛函數(shù)是一種特殊的虛函數(shù),它沒有具體的實現(xiàn)B)抽象類是指具有純虛函數(shù)的類C)一個基類中說明有純虛函數(shù),該基類的派生類一定不再是抽象類D)抽象類只能作為基類來使用,其純虛函數(shù)的實現(xiàn)由派生類給出注釋:帶有純虛函數(shù)的類稱為抽象類
6、。抽象類中的純虛函數(shù)的實現(xiàn)由派生類給出: 但派生類仍可不給出純虛函數(shù)的定義,繼續(xù)作為抽象類存在。10 下列描述中,_是抽象類的特性。D A)可以說明虛函數(shù) B)可以進行構(gòu)造函數(shù)重載C)可以定義友元函數(shù) D)不能說明其對象注釋:抽象類區(qū)別于其他類的最根本的特征是不能定義對象。11 _是一個在基類中說明的虛函數(shù),它在該基類中沒有定義,但要求任何派生 類都必須定義自己的版本。 C A)虛析構(gòu)函數(shù) B)虛構(gòu)造函數(shù) C)純虛函數(shù) D)靜態(tài)成員函數(shù)12 如果一個類至少有一個純虛函數(shù),那么就稱該類為_。A A)抽象類 B)虛基類 C)派生類 D)以上都不對13 以下_成員函數(shù)表示純虛函數(shù)。C A)virtu
7、al int vf(int); B)void vf(int)=0;C)virtual void vf()=0; D)virtual void vf(int)()注釋:純虛函數(shù)的聲明格式如下: virtual<函數(shù)返回類型><函數(shù)名>(<參數(shù)表>)=0; 注意純虛函數(shù)與虛函數(shù)體為空的區(qū)別。純虛函數(shù)根本就沒有函數(shù)體,而空的虛 函數(shù)的函數(shù)體為空:前者所在的類是抽象類,不能直接進行實例化,而后者所在的 類是可以實例化的:14 下面的描述中,正確的是_。A A)virtual可以用來聲明虛函數(shù)B)含有純虛函數(shù)的類是不可以用來創(chuàng)建對象的,因為它是虛基類C)即使基類的構(gòu)造
8、函數(shù)沒有參數(shù),派生類也必須建立構(gòu)造函數(shù)D)靜態(tài)數(shù)據(jù)成員可以通過成員初始化列表來初始化注釋:virtual關(guān)鍵字既可以用來聲明虛基類,也可以用來聲明虛函數(shù)。 含有純虛函數(shù)的類是抽象類,它不能用來定義對象。 靜態(tài)數(shù)據(jù)成員的初始化必須在類體外進行。 如果所有的基類和子對象構(gòu)造函數(shù)都不需要參數(shù),派生類也不需要參數(shù)時,派生類構(gòu)造函數(shù)可以不定義。15 在下面程序中,A、B、C、D四句編譯時不會出現(xiàn)錯誤的是_。A #include<iostream.h>class Base public: Base() Base(int c): count(c) virtual void print() con
9、st=0;private: int count;class Derived: public Basepublic: Derived() :Base(0 ) Derived(int c): Base(c) void print()const cout<<"Derived"<<endl; ;void main() Derived d(10); Base *pb; pb=&d; /A Base & cb=d; Derived dd=*pb; Derived &cd=cb; /C Base bb=d; /D注釋:B和C處不符合賦值兼容
10、規(guī)則。D處出錯是因為Base是含有純虛函數(shù)的抽象類,不能建立抽象類的對象。 16 在下面程序中,A,B、C、D四句編譯時出現(xiàn)錯誤的是_。C class A /A public: /B A( ) func(); /C virtual void func()=0; /D;注釋:構(gòu)造函數(shù)中不能調(diào)用純虛函數(shù),但可以調(diào)用虛函數(shù)。17 分析下面的程序,正確的輸出結(jié)果是_B#include <iostream.h>#include <string.h>class Basepublic: virtual char *fun() const=0;char* Base:fun() cons
11、t return “Base”; class Derivedl1: virtual public Basepublic:char* fun() const return "Derived11" ;class Derivedl2: virtual public Basepublic: char* fun() const return "Derivedl2" ;class Derived2: public Derivedl1,public Derivedl2public: char* fun() const char *ptr; ptr=new charst
12、rlen(Derivedl1:fun()+strlen(Derivedl2:fun()+l; strcpy(ptr,Derived11:fun(), strcat(ptr,Derived12:fun(); return ptr; ;void main() Base *pb; pb=new Derived11; cout<<pb->fun()<<endl; pb=new Derivedl2; cout<<pb->fun()<<endl; pb=new Derived2; cout<<pb->fun()<<e
13、ndl;A) B)Base Derivedl1Base Derivedl2Base Derivedl1Derivedl2C) D)Derivedl1 Derivedl2Derivedl1 Derivedl2Derivedl1Derivedl2 Derivedl1Derivedl2注釋:參見第1題、第2題的注釋。派生類Derivedll、Derivedl2和Derived2中的函數(shù)fun與基類Base中的虛函數(shù)fun的函數(shù)名、參數(shù)表和返回類型都完全相同,因此這三個類中的fun函數(shù)也是虛函數(shù),對fun函數(shù)的調(diào)用采用動態(tài)聯(lián)編。二、填空題1 動態(tài)聯(lián)編中直到_程序運行_時才能確定調(diào)用哪個函數(shù);而靜態(tài)聯(lián)編
14、則是在_程序編譯時進行的。 注釋:動態(tài)聯(lián)編與靜態(tài)聯(lián)編的概念。2 靜態(tài)聯(lián)編所支持的多態(tài)性稱為_編譯時的_多態(tài)性,動態(tài)聯(lián)編所支持的多態(tài)性則稱為 _運行時的_多態(tài)性,動態(tài)多態(tài)性由_虛函數(shù)_來支持。 注釋:在C+中,多態(tài)性的實現(xiàn)和聯(lián)編有關(guān)。3 對虛函數(shù)使用對象指針或引用調(diào)用,系統(tǒng)使用_動態(tài)_聯(lián)編;使用對象調(diào)用時系統(tǒng)使用_靜態(tài)_聯(lián)編。注釋:參見選擇填空第1題、第5題的注釋。4 動態(tài)聯(lián)編是在_虛函數(shù)_的支持下實現(xiàn)的,它通過_指針或引用來調(diào)用該函數(shù)操作。5 在一個成員函數(shù)內(nèi)調(diào)用一個虛函數(shù)時,對該虛函數(shù)的調(diào)用進行_動態(tài)_聯(lián)編。6 在析構(gòu)函數(shù)中調(diào)用虛函數(shù)時,采用_靜態(tài)_聯(lián)編。7 C+中_不支持_虛構(gòu)造函數(shù),但_
15、支持_虛析構(gòu)函數(shù)。 注釋:參見選擇填空第8題的注釋。 多態(tài)是不同的對象對同一消息有不同的行為特征,虛函數(shù)作為運行過程中多態(tài)的基礎(chǔ),主要是針對對象的,而構(gòu)造函數(shù)的調(diào)用意味著要建立一個對象,這時必須確切地知道這個對象的類型,并且,我們也不會為一個已存在的對象調(diào)用構(gòu)造函數(shù)。因此,虛構(gòu)造函數(shù)沒有意義。 在C+中可以聲明虛析構(gòu)函數(shù)。析構(gòu)函數(shù)的功能是在該類對象消亡之前進行一些必要的清理工作,如果一個類的析構(gòu)函數(shù)是虛函數(shù),那么,由它派生而來的所有子類的析構(gòu)函數(shù)也是虛函數(shù)。析構(gòu)函數(shù)設(shè)置為虛函數(shù)后,在使用指針調(diào)用時可以進行動態(tài)聯(lián)編,實現(xiàn)運行時的多態(tài),從而保證使用基類的指針就能夠調(diào)用適當?shù)奈鰳?gòu)函數(shù)針對不同的對象進
16、行清理工作。8 在類定義中,將_=0_置于虛函數(shù)的函數(shù)原型的末尾可以將該函數(shù)聲明為純虛函數(shù)9 帶有_純虛函數(shù)_的類稱為抽象類,它只能作為_基類_來使用。 注釋:抽象類的作用主要有兩個:直接用作基類或用作指針或引用的基類型。10 抽象類不能_定義對象_,但可以_聲明抽象類的指針或引用_作為參數(shù)類型,函數(shù)返回類型或顯式轉(zhuǎn)換類型。 注釋:注意抽象類不能定義對象是指不能定義需要分配存儲空間的對象。因此可以聲明抽象類的指針或引用,它們在程序運行時可以指向并訪問派生類對象。11 下列程序的運行結(jié)果如下: Derivel's Print() called.Derive2's Print()
17、called.根據(jù)結(jié)果將程序補充完整。#include <iostream.h>class Base public: Base(int i) b=i; _virtual void Print()=0;_protected: int b;class Derivel :public Basepublic:_Derive1( int i ):Base( i ) _ void Print() cout<<”Derive1s Print() called.”<<endl; ;class Derive2:public Base _public:Derive1(int i
18、 ): Base( i ) void Print( ) cout<<”Derive2s Print( ) called. “<< endl; _;void fun( _Base *obj _) obj->Print();void main() _Derive1 *d1=new Derive1(1);_ Derive2 *d2=new Derive2(2); fun(dl); fun(d2); 注釋:派生類Derived1和Derived2從基類Base公有繼承,它們是Base的子類型。 主程序中兩次調(diào)用fun函數(shù),該函數(shù)通過指針對象obj調(diào)用了Print函數(shù),得到
19、 了不同的輸出結(jié)果。而同樣的消息被不同類型的對象接收時導致完全不同的行為, 恰好體現(xiàn)了面向?qū)ο蟮亩鄳B(tài)特性。根據(jù)運行時的多態(tài)必須滿足的條件,Print函數(shù)一 定是一個虛函數(shù),并且在所有類中都必須進行定義。由于Base類中的Print函數(shù)除 了提供一個公共的接口外,沒有其他的作用,所以最好定義為純虛函數(shù)。 12 將下列程序補充完整。#include <iostream.h>class convertpublic: convert(double i) vail=i; _virtual void compute( )=0;_protected: double val1; double va
20、l2;/liters to gallonsclass l_to_g:public convertpublic: _l_to_g(double i ) : convert(i) _ void compute() val2=val1/3.7854; cout<<val1 <<" liters is "<<val2<<" gallons."<<endl;/Fahrenheit to Celsiusclass f_to_c:public convert _public: f_to_c(double i
21、):convert( i ) void compute( ) val2=(val1-32)*5/9;cout<<val1<<”Fahrenheit is “<<val2<<”Celsius.”<<endl; ;void fun(_convert& f _) pute();void main() l_to_g lgobj(4); f_to_c fcobj(70); fun(lgobj); fun(fcobj);13 根據(jù)不同的輸出結(jié)果,在函數(shù)Tone中填入正確的語句。#include <iostream.h>clas
22、s Instrumentpublic: virtual void Print() const cout<<"Instrument:Print"<<endl; ;class Piano: public Instrumentpublic: void Print()const cout<<"Piano:Print"<<endl; ;class Guitar: public Instrumentpublic: void Print() const cout<<"Guitar:Print&quo
23、t;<<endl; ;void Tone(_) _void main()Guitar g; Tone(g); Piano p; Tone(p);(1)輸出結(jié)果為:Instrument: :PrintInstmment:Print(2)輸出結(jié)果為:Guitar: :PrintPiano: :Print(1) Instrument obj obj.Print( ) (2) Instrument& obj obj.Print( ) 參考第3題,第一次的輸出是由靜態(tài)聯(lián)編產(chǎn)生的,第二次的輸出是由動態(tài)態(tài)聯(lián)編產(chǎn)生的。14 下列程序的運行結(jié)果如下:Base's cons.Deriv
24、ed's cons.Derived's des.Base's des.根據(jù)結(jié)果將程序補充完整。#include <iostream.h>class Basepublic: Base() cout<<"Base's cons."<<endl; _virtual Base( )_ cout<<"Base's des."<<endl; ;class Derived:public Basepublic: Derived() cout<<"De
25、rived's cons."<<endl; Derived() cout<<"Derived's des."<<endl; ;void main() Base *ptr=_new Derived _ delete ptr;三、編程1 在作業(yè)1編程1的Point類中完成賦值運算符=、插入運算符<<、比較運算符=、!=和加法運算符+、-的重載。#include <math.h>#include <iostream.h>class Point public: Point(float
26、x=0, float y=0, float z=0): x_(x), y_(y), z_(z) Point(const Point& p) : x_(p.x_), y_(p.y_), z_(p.z_) /形參point為常引用,它所引用的對象不能被更新,即傳給它的實參不能被更新 。 void negate() x_ *= -1; y_ *= -1; z_ *= -1; double norm() return sqrt(x_*x_ + y_*y_ + z_*z_); void print() cout << '(' << x_ <<
27、"," << y_ << "," << z_ << ")" Point& operator=(const Point& point); bool Point:operator=(const Point& point) const /常成員函數(shù),只有它才有資格操作常量和常對象 return x_ = point.x_ && y_ = point.y_ && z_ = point.z_;bool Point:operator!=(con
28、st Point& point) const return x_ != point.x_ | y_ != point.y_ | z_ != point.z_;friend Point operator+(const Point& p1, const Point& p2);friend Point operator-(const Point& p1, const Point& p2);friend ostream& operator<<(ostream& ostr, const Point& point); privat
29、e: float x_, y_, z_;Point operator+(const Point& p1, const Point& p2) return Point(p1.x_+p2.x_,p1.y_+p2.y_,p1.z_+p2.z_);Point operator-(const Point& p1, const Point& p2) return Point(p1.x_-p2.x_,p1.y_-p2.y_,p1.z_-p2.z_);ostream& operator<<(ostream& ostr, const Point&
30、; point) return ostr << "(" << point.x_ << "," <<point.y_ << "," <<point.z_ << ")"Point& Point:operator=(const Point& point) x_ = point.x_; y_ = point.y_; z_ = point.z_; return *this;void main() Point p(12,-3,4
31、),q(14,5,12),r1,r2; r1=p+q; cout<<r1<<r1.norm()<<endl; r2=p-q+r1; cout <<r2<<r2.norm()<<endl; if(r1=r2)cout <<"r1=r2"<<endl; else cout <<"r1!=r2"<<endl; 2 假設(shè)Point類的坐標為整型,對它重載+(自增)、-(自減)運算符(包括前后綴)。#include <math.h>#
32、include <iostream.h>class Point public: Point(int x=0, int y=0, int z=0): x_(x), y_(y), z_(z) Point(const Point& p) : x_(p.x_), y_(p.y_), z_(p.z_) Point& operator +() x_+;y_+;z_+; return *this;/為什么要返回引用,下列代碼行嗎?/Point operator +() / x_+;y_+;z_+;/ return *this;/Point operator +(int) /為什么
33、不必返回引用 Point temp(*this);x_+;y_+;z_+; return temp;Point& operator -() x_-;y_-;z_-; return *this;Point operator -(int) Point temp(*this);x_-;y_-;z_-; return temp; void print() cout << '(' << x_ << "," << y_ << "," << z_ << &quo
34、t;)" private: int x_, y_, z_;void main() Point p(12,-3,4); cout << "p = " p.print(); Point q(p+); cout << "q = " q.print(); cout << "p = " p.print(); +(+p); cout << "p = " p.print(); p-; cout << "p = " p.print();3 定
35、義一個Shape基類,由它派生出Rectangle和Circle類,二者都有GetArea( )函數(shù)計算對象的面積。使用Rectangle 類創(chuàng)建一個派生類Square。#include <iostream.h>class Shape public:/Shape() /Shape() virtual float GetArea()return -1;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius)/Circle()float GetArea()return 3.14*itsRadius*it
36、sRadius;private:float itsRadius;class Rectangle : public Shapepublic:Rectangle(float len,float width): itsLength(len),itsWidth(width);/Rectangle();float GetArea()return itsLength*itsWidth;float GetLength()return itsLength;float GetWidth()return itsWidth;private:float itsWidth;float itsLength;class S
37、quare:public Rectanglepublic:Square(float len);/Square(); Square:Square(float len): Rectangle(len,len) void main()Shape *sp;sp=new Circle(5);cout << "The area of the Circle is "<<sp->GetArea()<<endl;delete sp;sp=new Rectangle(4,6); cout << "The area of the R
38、ectangle is "<<sp->GetArea()<<endl;delete sp;sp=new Square(5); cout << "The area of the Square is "<<sp->GetArea()<<endl;delete sp;4 定義一個Shape抽象類,由它派生出Rectanglr和Circle類,二者都有GetArea( )函數(shù)計算對象的面積,GetPerim( ) 函數(shù)計算對象的周長。#include <iostream.h>class S
39、hape public:/Shape() /Shape() virtual float GetArea()=0;virtual float GetPerim()=0;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius)/Circle()float GetArea()return 3.14*itsRadius*itsRadius;float GetPerim()return 6.28*itsRadius;private:float itsRadius;class Rectangle : public Shap
40、epublic:Rectangle(float len,float width): itsLength(len),itsWidth(width);/Rectangle();virtual float GetArea()return itsLength*itsWidth;float GetPerim()return 2*(itsLength+itsWidth);private:float GetLength()return itsLength;float GetWidth()return itsWidth;private:float itsWidth;float itsLength;void m
41、ain()Shape *sp;sp=new Circle(5);cout << "The area of the Circle is "<<sp->GetArea()<<endl;cout << "The Perimeter of the Circle is "<<sp->GetPerim()<<endl;delete sp;sp=new Rectangle(4,6); cout << "The area of the Rectangle is &q
42、uot;<<sp->GetArea()<<endl;cout << "The Perimeter of the Rectangle is "<<sp->GetPerim()<<endl;delete sp;作業(yè)6答案:一,選擇題:1 D 2 D 3 A 4 D 5 D 6 A 7 D 8 D 9 C 10 D11 C 12 A 13 C 14 A 15 A 16 C 17 B二,填空題:2 程序運行程序編譯2 編譯時的運行時的虛函數(shù)3 動態(tài)靜態(tài)4 虛函數(shù)指針或引用5 動態(tài)6 靜態(tài)7不支持支持8 =09
43、純虛函數(shù)基類10定義對象聲明抽象類的指針或引用11 virtual void Print()=0;Derive1( int i ):Base( i ) public:Derive1(int i ): Base( i ) void Print( ) cout<<”Derive2s Print( ) called. “<< endl; Base *objDerive1 *d1=new Derive1(1);_ Derive2 *d2=new Derive2(2);12 virtual void compute( )=0;l_to_g(double i ) : convert
44、(i) public: f_to_c(double i ):convert( i ) void compute( ) val2=(val1-32)*5/9;cout<<val1<<”Fahrenheit is “<<val2<<”Celsius.”<<endl; convert& f 13 (1) Instrument obj obj.Print( ) (2) Instrument& obj obj.Print( )14virtual Base( )new Derived 三、編程1,#include <math
45、.h>#include <iostream.h>class Point public: Point(float x=0, float y=0, float z=0): x_(x), y_(y), z_(z) Point(const Point& p) : x_(p.x_), y_(p.y_), z_(p.z_) void negate() x_ *= -1; y_ *= -1; z_ *= -1; double norm() return sqrt(x_*x_ + y_*y_ + z_*z_); void print() cout << '(
46、9; << x_ << "," << y_ << "," << z_ << ")" Point& operator=(const Point& point); bool Point:operator=(const Point& point) const return x_ = point.x_ && y_ = point.y_ && z_ = point.z_;bool Point:operator!=(co
47、nst Point& point) const return x_ != point.x_ | y_ != point.y_ | z_ != point.z_;friend Point operator+(const Point& p1, const Point& p2);friend Point operator-(const Point& p1, const Point& p2);friend ostream& operator<<(ostream& ostr, const Point& point); priva
48、te: float x_, y_, z_;Point operator+(const Point& p1, const Point& p2) return Point(p1.x_+p2.x_,p1.y_+p2.y_,p1.z_+p2.z_);Point operator-(const Point& p1, const Point& p2) return Point(p1.x_-p2.x_,p1.y_-p2.y_,p1.z_-p2.z_);ostream& operator<<(ostream& ostr, const Point&am
49、p; point) return ostr << "(" << point.x_ << "," <<point.y_ << "," <<point.z_ << ")"Point& Point:operator=(const Point& point) x_ = point.x_; y_ = point.y_; z_ = point.z_; return *this;void main() Point p(12,-3,
50、4),q(14,5,12),r1,r2; r1=p+q; cout<<r1<<r1.norm()<<endl; r2=p-q+r1; cout <<r2<<r2.norm()<<endl; if(r1=r2)cout <<"r1=r2"<<endl; else cout <<"r1!=r2"<<endl; 2,#include <math.h>#include <iostream.h>class Point pu
51、blic: Point(int x=0, int y=0, int z=0): x_(x), y_(y), z_(z) Point(const Point& p) : x_(p.x_), y_(p.y_), z_(p.z_) Point& operator +() x_+;y_+;z_+; return *this;Point operator +(int) Point temp(*this);x_+;y_+;z_+; return temp;Point& operator -() x_-;y_-;z_-; return *this;Point operator -(i
52、nt) Point temp(*this);x_-;y_-;z_-; return temp; void print() cout << '(' << x_ << "," << y_ << "," << z_ << ")" private: int x_, y_, z_;void main() Point p(12,-3,4); cout << "p = " p.print(); Point q(p+);
53、 cout << "q = " q.print(); cout << "p = " p.print(); +(+p); cout << "p = " p.print(); p-; cout << "p = " p.print();3,#include <iostream.h>class Shape public:/Shape() /Shape() virtual float GetArea()return -1;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius)/Circle()float GetArea()return 3.14*itsRadius*itsRadius;private:float itsRadius;class Rectangle : public Shapepublic:Rectangle(fl
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 軟件開發(fā)技術(shù)交流題目
- 工程經(jīng)濟實際應(yīng)用考題試題及答案
- 國際貿(mào)易實務(wù)案例分析測試卷及解析
- 經(jīng)濟學與統(tǒng)計方法試題及答案
- 水利水電工程投資風險識別試題及答案
- 鄉(xiāng)村旅游+農(nóng)業(yè)特色產(chǎn)業(yè)融合協(xié)議
- 2025年經(jīng)濟法概論新趨勢試題及答案
- 行政管理團隊精英試題及答案
- 2025年中級經(jīng)濟師學習資源試題及答案
- 文職基本知識考試試題及答案
- 2017版銀皮書中英文對照翻譯稿
- GB/T 33825-2017密封繼電器用鋼包銅復合棒線材
- GB/T 14846-2014鋁及鋁合金擠壓型材尺寸偏差
- GB 30531-2014商用燃氣灶具能效限定值及能效等級
- GA/T 594-2006保安服務(wù)操作規(guī)程與質(zhì)量控制
- GA 258-2009警服單褲
- 高中生物365個判斷題涵蓋高一高二高三所有知識點
- 社會科學研究方法博士生課程
- 人教版初中音樂七年級上冊《牧歌》說課稿課件
- 2021年春新青島版(五四制)科學四年級下冊全冊教案
- 畢業(yè)論文指導教師指導記錄6篇
評論
0/150
提交評論