




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、第八章 多態(tài)1.單選題(1)下列關(guān)于運算符重載的描述中,( D )是正確的。(A) 可以改變參與運算的操作數(shù)個數(shù) (B) 可以改變運算符原來的優(yōu)先級(C) 可以改變運算符原來的結(jié)合性 (D) 不能改變原運算符的語義(2)下列函數(shù)中,不能重載運算符的函數(shù)是( b )。(A) 成員函數(shù) (B) 構(gòu)造函數(shù)(C) 普通函數(shù) (D) 友員函數(shù)(3)要求用成員函數(shù)重載的運算符是( A )。(A) = (B) = (C) <=(D) +(4)要求用友員函數(shù)重載的運算符是( C )。(A) = (B) (C) <<(D) ()(5)在C+中,要實現(xiàn)動態(tài)聯(lián)編,必須使用( D )調(diào)用虛函數(shù)。(A
2、) 類名(B) 派生類指針(C) 對象名(D) 基類指針(6)下列函數(shù)中,不能說明為虛函數(shù)的是( C )。(A) 私有成員函數(shù)(B) 公有成員函數(shù)(C) 構(gòu)造函數(shù)(D) 析構(gòu)函數(shù)(7)在派生類中,重載一個虛函數(shù)時,要求函數(shù)名、參數(shù)的個數(shù)、參數(shù)的類型、參數(shù)的順序和函數(shù)的返回值( A )。(A) 相同(B)不同(C) 相容(D) 部分相同(8)C+中,根據(jù)( D )識別類層次中不同類定義的虛函數(shù)版本。(A) 參數(shù)個數(shù)(B) 參數(shù)類型(C) 函數(shù)名(D) this指針類型(9)虛析構(gòu)函數(shù)的作用是( C )。(A) 虛基類必須定義虛析構(gòu)函數(shù)(B) 類對象作用域結(jié)束時釋放資源(C)delete動態(tài)對象時
3、釋放資源(D) 無意義(10)下面函數(shù)原型中,( B )聲明了fun為純虛函數(shù)。(A) void fun()=0; (B) virtual void fun()=0;(C) virtual void fun(); (D) virtual void fun() ;(11)若一個類中含有純虛函數(shù),則該類稱為( C )。(A) 基類(B)純基類(C) 抽象類(D) 派生類 (12)假設(shè) Aclass為抽象類,下列正確的說明語句是( B )。(A) Aclass fun( int ) ; (B) Aclass * p ;(C) int fun( Aclass ) ; (D) Aclass Obj ;2
4、在C+中,使用類體系依靠什么機制實現(xiàn)程序運行時的多態(tài)?【解答】在C+中,基類指針可以指向派生類對象,以及基類中擁有虛函數(shù),是支持多態(tài)性的前提。程序通過用同一個基類指針訪問不同派生類的虛函數(shù)重載版本實現(xiàn)程序運行時的多態(tài)。C+的虛特性負責(zé)自動地在程序運行時把基類指針的關(guān)聯(lián)類型轉(zhuǎn)換成當(dāng)前指向?qū)ο蟮呐缮愵愋?。另外,抽象類機制提供了軟件抽象和可擴展性的手段,實現(xiàn)運行時的多態(tài)性。3如果一個類的虛函數(shù)被聲明為私有成員函數(shù),會有語法錯誤嗎?當(dāng)它作為基類時,可以在應(yīng)用類體系時實現(xiàn)動態(tài)聯(lián)編嗎?請你驗證一下?!窘獯稹繘]有語法錯誤。但在應(yīng)用類體系時無法實現(xiàn)動態(tài)編聯(lián)和多態(tài)。因為私有成員函數(shù)只在類內(nèi)可見,在類外無法調(diào)用
5、,無法在類外通過基類指針實現(xiàn)多態(tài)。程序略。4虛函數(shù)和純虛函數(shù)的區(qū)別是什么?【解答】虛函數(shù)定義時冠以關(guān)鍵字virtual,本身有實現(xiàn)代碼,作用是引導(dǎo)基類指針根據(jù)指向?qū)ο笳{(diào)用類體系中不同重載版本函數(shù)。純虛函數(shù)是指在說明時代碼“為0”的虛函數(shù),即純虛函數(shù)本身并沒有實現(xiàn)代碼,必須通過它的派生類定義實現(xiàn)版本。5閱讀下列程序,寫出運行結(jié)果。#include <iostream>using namespace std;class T public : T() a = 0; b = 0; c = 0; T( int i, int j, int k ) a = i; b =j; c = k; voi
6、d get( int &i, int &j, int &k ) i = a; j = b; k = c; T operator* ( T obj ); private: int a , b , c;T T:operator* ( T obj ) T tempobj; tempobj.a = a * obj.a; tempobj.b = b * obj.b; tempobj.c = c * obj.c; return tempobj;int main() T obj1( 1,2,3 ), obj2( 5,5,5 ), obj3; int a , b , c; obj3 =
7、 obj1 * obj2; obj3.get( a, b, c ); cout<<"( obj1*obj2 ): " <<"a = "<<a<<'t'<<"b = "<<b<<'t'<<"c = "<<c<<'t'<<endl; (obj2*obj3).get( a, b, c ); cout<<"( obj2*ob
8、j3 ): " <<"a = "<<a<<'t'<<"b = "<<b<<'t'<<"c = "<<c<<'t'<<endl; 【解答】( obj1 * obj2 ): a = 5 b = 10 c = 15 ( obj2 * obj3 ): a = 25 b = 50 c = 756閱讀下列程序,寫出運行結(jié)果。#include <iostream>
9、;using namespace std;class Vector public: Vector() Vector(int i,int j) x = i ; y = j ; friend Vector operator+ ( Vector v1, Vector v2 ) Vector tempVector ; tempVector.x = v1.x + v2.x ; tempVector.y = v1.y + v2.y ; return tempVector ; void display() cout << "( " << x << &q
10、uot;, " << y << ") "<< endl ; private: int x , y ;int main() Vector v1( 1, 2 ), v2( 3, 4 ), v3 ; cout << "v1 = " ; v1.display() ; cout << "v2 = " ; v2.display() ; v3 = v1 + v2 ; cout << "v3 = v1 + v2 = " ; v3.display()
11、;【解答】v1 = ( 1, 2 ) v2 = ( 3, 4 ) v3 = v1 + v2 = ( 4, 6 )7閱讀下列程序,寫出運行結(jié)果。#include <iostream>using namespace std;class Bclass public: Bclass( int i, int j ) x = i; y = j; virtual int fun() return 0 ; protected: int x, y ;class Iclass:public Bclass public : Iclass(int i, int j, int k):Bclass(i, j)
12、 z = k; int fun() return ( x + y + z ) / 3; private : int z ;int main() Iclass obj( 2, 4, 10 ); Bclass p1 = obj; cout << p1.fun() << endl; Bclass &p2 = obj ; cout << p2.fun() << endl; cout << p2.Bclass : fun() << endl; Bclass *p3 = &obj; cout << p3 -
13、> fun() << endl;【解答】0 5 0 58閱讀下列程序,寫出運行結(jié)果。#include <iostream>using namespace std;class Base public: virtual void getxy( int i,int j = 0 ) x = i; y = j; virtual void fun() = 0 ; protected: int x , y; ;class A : public Base public: void fun() cout<<"x = "<<x<<
14、;'t'<<"y = x * x = "<<x*x<<endl; ;class B : public Base public: void fun() cout << "x = " << x << 't' << "y = " << y << endl; cout << "y = x / y = " << x / y << endl; ;int
15、main() Base * pb; A obj1; B obj2; pb = &obj1; pb -> getxy( 10 ); pb -> fun(); pb = &obj2; pb -> getxy( 100, 20 ); pb -> fun();【解答】x = 10 y = x*x = 100 x = 100 y = 20 y = x / y = 59分別使用成員函數(shù)和友員函數(shù)編寫程序重載運算符“+”,使該運算符能實現(xiàn)兩個字符串的連接。【解答】(1) 使用成員函數(shù)#include <iostream>#include<cstrin
16、g>using namespace std;class s public: s() *str = '0' s( char *pstr ) strcpy( str,pstr ); char *gets() return str; s operator+( s obj ); private: char str10; ;s s:operator+( s obj ) strcat( str,obj.str ); return str; /或return *this int main() s obj1( "Visual" ),obj2( " C+&qu
17、ot; ),obj3; obj3 = obj1 + obj2; cout << obj3.gets() << endl;(2)使用友員函數(shù)#include <iostream>#include<cstring>using namespace std;class s public: s() *str= '0' s( char *pstr ) strcpy( str,pstr ); char *gets() return str; friend s operator+( s obj1,s obj2 ); private: char s
18、tr100; ; s operator+( s obj1,s obj2 ) s tempobj; strcat( tempobj.str,obj1.str ); strcat( tempobj.str,obj2.str ); return tempobj; int main() s obj1( "Visual" ),obj2( " C+" ),obj3; obj3 = obj1 + obj2; cout << obj3.gets() << endl;10定義一個整數(shù)計算類Integer,實現(xiàn)短整數(shù) +,-,*,/ 基本算術(shù)運算。要
19、求可以進行數(shù)據(jù)范圍檢查(-3276832767,或自行設(shè)定),數(shù)據(jù)溢出時顯示錯誤信息并中斷程序運行?!窘獯稹?include <iostream>using namespace std;class Integer private: short a; public:Integer (short n=0) a=n; Integer operator +(Integer); Integer operator -(Integer); Integer operator *(Integer); Integer operator /(Integer); Integer operator =(In
20、teger);void display() cout<<a<<endl; ;Integer Integer:operator+(Integer x) Integer temp; if(a+x.a<-32768|a+x.a>32767) cout<<"Data overflow!"<<endl; abort(); temp.a=a+x.a; return temp; Integer Integer:operator-(Integer x) Integer temp;if(a-x.a<-32768|a-x.a&g
21、t;32767) cout<<"Data overflow!"<<endl; abort(); temp.a=a-x.a;return temp; Integer Integer:operator*(Integer x) Integer temp;if(a*x.a<-32768|a*x.a>32767) cout<<"Data overflow!"<<endl; abort();temp.a=a*x.a;return temp; Integer Integer:operator/(Integer
22、 x) Integer temp; if(a/x.a<-32768|a/x.a>32767) cout<<"Data overflow!"<<endl; abort(); temp.a=a/x.a; return temp; Integer Integer:operator=(Integer x) a=x.a; return *this; int main() Integer A(90),B(30),C;cout<<"A="A.display();cout<<"B="B.di
23、splay();C=A+B;cout<<"C=A+B=" C.display();C=A-B;cout<<"C=A-B=" C.display();C=A*B;cout<<"C=A*B=" C.display();C=A/B;cout<<"C=A/B=" C.display();11使用虛函數(shù)編寫程序求球體和圓柱體的體積及表面積。由于球體和圓柱體都可以看做由圓繼承而來,所以可以把圓類Circle作為基類。在Circle類中定義一個數(shù)據(jù)成員radius和兩個虛函數(shù)are
24、a和volume。由Circle類派生Sphere類和Column類。在派生類中對虛函數(shù)area和volume重新定義,分別求球體和圓柱體的體積及表面積?!窘獯稹?include <iostream>using namespace std;const double PI=3.14159265;class circle public: circle(double r) radius = r; virtual double area() return 0.0; virtual double volume() return 0.0; protected: double radius;cl
25、ass sphere:public circle public: sphere( double r ):circle( r ) double area() return 4.0 * PI * radius * radius; double volume() return 4.0 * PI * radius * radius * radius / 3.0; ;class column:public circle public: column( double r,double h ):circle( r ) height = h; double area() return 2.0 * PI * r
26、adius * ( height + radius ); double volume() return PI * radius * radius * height;private: double height;int main() circle *p; sphere sobj(2); p = &sobj; cout << "球體:" << endl; cout << "體積 = " << p->volume() << endl; cout << "表面積 =
27、" << p->area() << endl; column cobj( 3,5 ); p = &cobj; cout << "圓柱體:" << endl; cout << "體積 = " << p->volume() << endl; cout << "表面積 = " << p->area() << endl;12某學(xué)校對教師每月工資的計算規(guī)定如下:固定工資+課時補貼。教授的固定
28、工資為5000元,每個課時補貼50元。副教授的固定工資為3000元,每個課時補貼30元。講師的固定工資為2000元,每個課時補貼20元。定義教師抽象類,派生不同職稱的教師類,編寫程序求若干個教師的月工資?!窘獯稹?include <iostream>using namespace std;class teacher public: teacher( char tname,int time ) strcpy( name,tname ); coursetime = time; virtual int pay() = 0; virtual void print() = 0; char *getname() return name; int getcoursetime() return coursetime; protected: char name30;int coursetime; ;class professor:public teacher public: professor( char pname,int time ):teacher( pnam
溫馨提示
- 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)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 建筑工程項目委托管理合同
- 工作流程標(biāo)準(zhǔn)化操作指南說明
- 中介業(yè)務(wù)合作協(xié)議合同
- 夫妻離婚協(xié)議書年
- 醫(yī)院治療流程規(guī)范
- 混凝土運輸承包合同
- 2025年武漢貨運資格證考試答題20題
- 三農(nóng)品牌塑造與推廣策略手冊
- 2025年哈爾濱貨運從業(yè)資格證模擬考試
- 2025年自貢貨運從業(yè)資格證考試模擬考試題庫下載
- 山地光伏施工方案
- 六年級心理健康ppt名師優(yōu)質(zhì)課獲獎市賽課一等獎?wù)n件
- 四川輕化工大學(xué)
- 六西格瑪質(zhì)量管理在口腔科器械管理中的作用
- 高中心理健康教育-認識自我悅納自我教學(xué)課件設(shè)計
- 素材合集-扁平化圖標(biāo)素材(彩色)
- (全)電梯安全風(fēng)險管控清單
- 中國思想史 馬工程329P
- 《網(wǎng)店美工》教案-商品詳情頁設(shè)計
- 原始狩獵圖哀郢楚商
- 新版冀教版(冀人版)科學(xué)五年級下冊全冊教案
評論
0/150
提交評論