




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、習(xí)題及其解答第 5 章 類與對象5.1 選擇題7章1. 在下列結(jié)構(gòu)變量的定義中,不正確的是( d )第8章第9章(b) struct char name 20 ; long code ;(d) struct char name20;long code; employee;employee emp;第10章(a) struct employee char name 20 ;long code ; emp ; emp ;(c) struct employee char name20;long code; ;employee emp;2.已知有職工情況結(jié)構(gòu)變量em能義為:struct employe
2、e char name 20 ;long code ;struct int year ;int month ;int day ; birth ; emp ;下列對 emp 的 birth 正確賦值方法是( d )。year = 1980 ; month = 5 ; day = 1 ;birth.year = 1980 ; birth.month = 5 ; birth.day = 1 ;emp.year = 1980 ; emp.month = 5 ; emp.day = 1 ;emp.birth.year = 1980 ; emp.birth.month = 5 ; emp.birth.da
3、y = 1 ;3假定有以下聲明和定義,則下面引用形式錯誤的是( b )。struct student int num ;float score ; stu3 = 1001,80,1002,75,1003,91,*p = stu ;(a) p-num(b) (p+).num(c) (p+)-num(d) (*p).num4下列四個運算符中,優(yōu)先級最低的是( a )。(a) +(b) .(c) -(d) ()5若有以下聲明和定義,則下列錯誤的引用是( d )。struct worker int no ;char name 20 ; w, *p = &w ;(a) w.no(b) p-no(c) (
4、*p).no(d) *p.no6若有以下聲明和定義,則下列引用非法的是( d )。struct data int n;float score;data *q ;data a3 = 1001,87,&a1,1002,75,&a2,1003,90,&a0;data *p = a;+p-n(d) *p-n(b) 對象是類的實例一個對象必屬于某個類( c )。(b) private 或 protected 的類成員(d) public 或 private 的類成員(a) p-n+(b) (*p).n+7關(guān)于類和對象不正確的說法是( c )(a) 類是一種類型,它封裝了數(shù)據(jù)和操作(c) 一個類的對象只有
5、一個8在類定義的外部,可以被訪問的成員有(a) 所有類成員(c) public 的類成員9關(guān)于this 指針的說法錯誤的是( a,d )this 指針必須顯式說明當(dāng)創(chuàng)建一個對象后, this 指針就指向該對象成員函數(shù)擁有this 指針靜態(tài)成員函數(shù)擁有this 指針10聲明一個類的對象時,系統(tǒng)自動調(diào)用 ( b,d ) 函數(shù);撤消對象時,系統(tǒng)自動調(diào)用( c ) 函數(shù)。成員函數(shù)(b) 構(gòu)造函數(shù)(c) 析構(gòu)函數(shù)(d) 復(fù)制構(gòu)造函數(shù)11下面對構(gòu)造函數(shù)的 不正確 描述是 ( b )第11章 (a) 系統(tǒng)可以提供默認(rèn)的構(gòu)造函數(shù)構(gòu)造函數(shù)可以有參數(shù), 所以也可以有返回值(c) 構(gòu)造函數(shù)可以重載(d) 構(gòu)造函數(shù)可
6、以設(shè)置默認(rèn)參數(shù) TOC o 1-5 h z 12下面對析構(gòu)函數(shù)的正確描述是( a,c )。析構(gòu)函數(shù)必須由用戶定義析構(gòu)函數(shù)可以設(shè)置默認(rèn)參數(shù)所以靜態(tài)成員函數(shù)擁有this 指針友元函數(shù)(d) 復(fù)制構(gòu)造函數(shù)(a) 系統(tǒng)可以提供默認(rèn)的析構(gòu)函數(shù)(b)析構(gòu)函數(shù)沒有參數(shù)(d)13對靜態(tài)成員的不正確描述是( c,d )。靜態(tài)成員不屬于對象,是類的共享成員靜態(tài)數(shù)據(jù)成員要在類外定義和初始化調(diào)用靜態(tài)成員函數(shù)時要通過類或?qū)ο蠹せ睿挥徐o態(tài)成員函數(shù)可以操作靜態(tài)數(shù)據(jù)成員14下面選項中,不是類的成員函數(shù)為( c )。(a) 構(gòu)造函數(shù)(b) 析構(gòu)函數(shù) (c)15下面對友員的錯誤描述是( d )。關(guān)鍵字 friend 用于聲明友
7、員一個類中的成員函數(shù)可以是另一個類的友員友員函數(shù)訪問對象的成員不受訪問特性影響友員函數(shù)通過this 指針訪問對象成員2 閱讀下列程序,寫出執(zhí)行結(jié)果#include class A public :int f1();int f2();void setx( int m ) x = m ; cout x endl; void sety( int n ) y = n ; cout y endl; int getx() return x ; int gety() return y ; private :int x, y ;int A:f1() return x + y ; int A:f2() retu
8、rn x - y ; void main() A a ;a.setx( 10 ) ;a.sety( 5 ) ;cout a.getx() t a.gety() endl ;cout a.f1() t a.f2() endl ;答案:105105155#include class T public :T( int x, int y ) a = x ; b = y ;cout 調(diào)用構(gòu)造函數(shù)1. endl ;cout a t b endl ;T( T &d ) cout 調(diào)用構(gòu)造函數(shù)2. endl ;cout d.a t d.b endl ;T() cout 調(diào)用析構(gòu)函數(shù) .endl; int ad
9、d( int x, int y = 10 ) return x + y ; private :int a, b ;void main() T d1( 4, 8 ) ;T d2( d1 ) ;cout d2.add( 10 ) endl ;答案: TOC o 1-5 h z 調(diào)用構(gòu)造函數(shù)1.8調(diào)用構(gòu)造函數(shù)2.820調(diào)用析構(gòu)函數(shù).調(diào)用析構(gòu)函數(shù).#include class T public:T(int x) a=x; b+=x;static void display(T c) couta=c.atb=c.bendl; private:int a;static int b; ;int T:b=5;v
10、oid main() T A(3),B(5);T:display(A);T:display(B);答案:a=3b=13a=5b=13#include #include class point public :point( float x, float y ) a = x; b = y; cout 點( a , b ) ; friend double d( point &A , point &B ) return sqrt(A.a-B.a) * (A.a-B.a) + (A.b-B.b) * (A.b-B.b) ; private:double a, b ;void main() point p
11、1( 2, 3 ) ;cout 到 ;point p2( 4, 5 ) ;cout 的距離是: d( p1,p2 ) endl ;答案:點( 3 , 4)到點( 4 , 5)的距離是:2.82843#include class A public :A() a = 5 ; void printa() cout A:a = a endl ; private :int a ;friend class B ; ;class B public:void display1( A t )t.a + ; cout display1:a = t.a endl ; ;void display2( A t )t.a
12、 - ; cout display2:a = t.a endl ; ;void main() A obj1 ;B obj2 ;obj1.printa() ;obj2.display1( obj1 ) ;obj2.display2( obj1 ) ;obj1.printa() ;答案:A:a = 5display1:a = 6display2:a = 4A:a = 55.3 編程題6.定義一個Book (圖書)類,在該類定義中包括數(shù)據(jù)成員:bookname (書名)、price (價格)和number (存書數(shù)量);成員函數(shù): display() 顯示圖書的情況; borrow() 將存書數(shù)量減
13、1 ,并顯示當(dāng)前存書數(shù)量; restore() 將存書數(shù)量加1 ,并顯示當(dāng)前存書數(shù)量。在 main 函數(shù)中, 要求創(chuàng)建某一種圖書對象, 并對該圖書進(jìn)行簡單的顯示、 借閱和歸還 管理。解 答:#include #include #include class Book public:void setBook(char*,float,int);void borrow();void restore();void display();private:char bookname40;float price;int number;/ 在類外定義Book 類的成員函數(shù)void Book:setBook(cha
14、r *name, float pri, int num) strcpy(bookname, name);price=pri;number=num;void Book:borrow() if (number=0 ) cout 已沒存書,退出! endl;abort();number = number - 1;cout 借一次,現(xiàn)存書量為: number endl;void Book:restore() number = number + 1;cout 還一次,現(xiàn)存書量為: number endl;void Book:display() cout 存書情況: endl bookname: book
15、name endl price: price endl number: number endl; void main() char flag, ch;Book computer;computer.setBook( c+ 程序設(shè)計基礎(chǔ) , 32, 1000 );computer.display();ch = y; while ( ch = y ) cout flag; switch ( flag ) case b: computer.borrow(); break;case r: computer.restore(); cout ch; computer.display();定義一個Box (盒
16、子)類,在該類定義中包括數(shù)據(jù)成員: length (長) 、 width (寬)和 height (高) ;成員函數(shù): 構(gòu)造函數(shù) Box 設(shè)置盒子長、寬和高三個初始數(shù)據(jù);函數(shù)volume 計算并輸出盒子的體積。在 main 函數(shù)中,要求創(chuàng)建 Box 對象,并求盒子的體積。 解答:略。定義一個 Student 類,在該類定義中包括:一個數(shù)據(jù)成員 score (分?jǐn)?shù))及兩個靜態(tài)數(shù)據(jù)成員 total (總分)和學(xué)生人數(shù)count ;成員函數(shù) scoretotalcount(float s) 用于設(shè)置分?jǐn)?shù)、求總分和累計學(xué)生人數(shù);靜態(tài)成員函數(shù)sum() 用于返回總分;靜態(tài)成員函數(shù)average() 用于
17、求平均值。 在 main 函數(shù)中,輸入某班同學(xué)的成績,并調(diào)用上述函數(shù)求全班學(xué)生的總分和平均分。 解答: 類聲明如下,實現(xiàn)代碼略。 class student public:void scoretotalcount( float ) ;static float sum();static float average();private: float score; static float total; static float count; ; float student:total = 0; float student:count = 0;定義一個表示點的結(jié)構(gòu)類型Point 和一個由直線方程y
18、= ax + b 確定的直線類Line 。結(jié)構(gòu)類型Point 有 x 和 y 兩個成員,它們分別表示點的橫坐標(biāo)和縱坐標(biāo)。 Line 類有兩個數(shù)據(jù)成員a和b,它們分別表示直線方程中的系數(shù)a和b。Line類有一個成員函數(shù)print用于顯示直線方程;友員函數(shù)setPoint(Line &l1,Line &l2) 用于求兩條直線的交點。在main 函數(shù)中,建立兩個直線對象,分別調(diào)用 print 函數(shù)顯示兩條直線的方程,并調(diào)用函數(shù) setPoint 求這兩條直線的交點。解答: 結(jié)構(gòu)和類聲明如下,實現(xiàn)代碼略。 struct Point/ 點結(jié)構(gòu) float x; float y; ; class Line
19、/ 線類 public: line( float , float );/構(gòu)造函數(shù)void print()/顯示直線方程friend point setpoint(line &l1,line &l2);/ 求兩條直線交點private: float a, b;第 6 章 運算符重載6.1 選擇題在下列運算符中,能重載的是( a,c,d )(d) delete(d) &=(a) !(b) sizeof(c) new在下列運算符中,不能重載的是( c )。(a) (c) &下列關(guān)于運算符重載的描述中, (a) 可以改變參與運算的操作數(shù)個數(shù)(c) 可以改變運算符原來的結(jié)合性下列函數(shù)中,能重載運算符的函
20、數(shù)是(a) 成員函數(shù)(b) 構(gòu)造函數(shù) TOC o 1-5 h z 不能用友員函數(shù)重載的是( a )(a) =(b) =下面描述中,錯誤的是( b )。d ) 是正確的。可以改變運算符原來的優(yōu)先級(d) 不能改變原運算符的語義( b,c )。析構(gòu)函數(shù) (d) 友員函數(shù))(c) +=(d) !=(a) 只有系統(tǒng)預(yù)先定義的運算符才可能被重載 (b) 使用類型轉(zhuǎn)換函數(shù)不能把一個類轉(zhuǎn)換為另一個類 (c) 使用類型轉(zhuǎn)換函數(shù)可以把類轉(zhuǎn)換為基本類型 (d) 類型轉(zhuǎn)換函數(shù)只能定義為一個類的成員函數(shù),不能定義為類的友員函數(shù)6.2 閱讀下列程序,寫出執(zhí)行結(jié)果#include class T public :T()
21、a = 0; b = 0; c = 0; T( int i , int j , int k ) a = i; b =j ; c = k; void 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;void main()
22、T obj1( 1,2,3 ), obj2( 5,5,5 ), obj3;int a , b , c;obj3 = obj1 * obj2;obj3.get( a, b, c );cout ( objl * obj2 ): t a = a t b = b t c = c t endl; (obj2 * obj3 ).get( a, b, c );cout ( obj2 * obj3 ): t a = a t b = b t c = c t endl;)答案:(objl * obj2 ): a = 5 b = 10 c = 15(obj2 * obj3 ): a = 25 b = 50 c =
23、75#include class vector public:vector() vector(int i,int j) x = i ; y = j ; friend vector operator + ( vector v1, vector v2 ) vector tempvector ;tempvector.x = vl.x + v2.x ;tempvector.y = vl.y + v2.y ;return tempvector ;void display() cout ( x , y ) endl ; private:int x , y ;void main() vector v1( 1
24、, 2 ), v2( 3, 4 ), v3 ;cout v1 = ;v1.display() ;cout v2 = ;v2.display() ;v3 = v1 + v2 ;cout v3 = v1 + v2 = ;v3.display() ;答案:v1 = ( 1, 2 )v2 = ( 3, 4 )v3 = v1 + v2 = ( 4, 6 )6.3 編程題分別使用成員函數(shù)和友員函數(shù)編程序重載運算符“ +” , 使該運算符能實現(xiàn)兩個字符串的連接。解答:略。1 , y 2 ,y 3 ) ,則它們之間的加、減和積分別定設(shè)向量 X = ( x 1 ,x 2 ,x 3) 和 Y = ( y 義為:X
25、 + Y = ( xX - Y = ( xX * Y = x 編程序?qū)\算符“解答:略。1 + y1 , x 2 + y2 , x 3 + y3 )1 - y 1 , x 2 - y 2 , x 3 - y 3 )1 * y 1 + x2 * y2 + x3 * y 3+” 、 “ - ”和“* ”重載,實現(xiàn)向量之間的加、減、乘運算。定義一個類 nauticalmile_kilometer , 它包含兩個數(shù)據(jù)成員 kilometer (千米) 和 meter(米) 。還包含一個構(gòu)造函數(shù)對數(shù)據(jù)成員初始化;成員函數(shù) print 用于輸出數(shù)據(jù)成員kilometer 和 meter 的值;類型轉(zhuǎn)換函數(shù)
26、double() 實現(xiàn)把千米和米轉(zhuǎn)換為海里(1 海里=1.852 千米 ) 。編寫 main 函數(shù),測試類 nauticalmile_kilometer 。解答:#include const double n = 1.852;/定義海里與千米和米的轉(zhuǎn)換系數(shù)(1 海里 =1.852 千米 )class nauticalmile_kilometer public:nauticalmile_kilometer( int km,double m ) kilometer = km; meter = m; void print() coutkilometer=kilometertmeter=meteren
27、dl; operator double();private:int kilometer;double meter;nauticalmile_kilometer:operator double() return ( meter / 1000 + double( kilometer ) / n; void main() nauticalmile_kilometer obj( 100,50 );obj.print();cout nauticalmile= double( obj ) endl;第7章 繼 承7.1 選擇題.在C+中,類與類之間的繼承關(guān)系具有(c )。(a) 自反性 (b) 對稱性(c
28、) 傳遞性 (d) 反對稱性2下列關(guān)于類的繼承描述中, ( a,b ) 是錯誤的。派生類可以訪問基類的所有數(shù)據(jù)成員,調(diào)用基類的所有成員函數(shù)派生類也是基類,所以基類具有派生類的全部屬性和方法繼承描述類的層次關(guān)系,派生類可以具有與基類相同的屬性和方法一個基類可以有多個派生類,一個派生類可以有多個基類當(dāng)一個派生類公有繼承一個基類時,(a) public 成員(c) protected 成員當(dāng)一個派生類私有繼承一個基類時,( b )。(a) public 成員(c) protected 成員當(dāng)一個派生類保護繼承一個基類時,( c )。(a) public 成員(c) protected 成員基類中的所
29、有公有成員成為派生類的 ( a )(b)private 成員(d) 友員基類中的所有公有成員和保護成員成為派生類(b)private 成員(d) 友員基類中的所有公有成員和保護成員成為派生類(b)private(d) 友員6不論派生類以何種方式繼承基類,都不能使用基類的(a) public 成員 (b)private(c) protected 成員 (d)public7下面描述中,錯誤的是( b, c )。成員( b )。成員成員和 protected 成員在基類定義的 public 成員在公有繼承的派生類中可見,也能在類外被訪問在基類定義的 public 和 protected 成員在私有繼
30、承的派生類中可見, 在類外可以被訪問在基類定義的 public 和 protected 成員在保護繼承的派生類中不可見在派生類中不可見的成員要變成可訪問的需進(jìn)行訪問聲明8.在C+中,不能被派生類繼承的函數(shù)是(b,c )。(a) 成員函數(shù)(b) 構(gòu)造函數(shù)(c) 析構(gòu)函數(shù)(d)靜態(tài)成員函數(shù)9在創(chuàng)建派生類對象時,構(gòu)造函數(shù)的執(zhí)行順序是( d )。對象成員構(gòu)造函數(shù)、基類構(gòu)造函數(shù)、派生類本身的構(gòu)造函數(shù)派生類本身的構(gòu)造函數(shù)、基類構(gòu)造函數(shù)、對象成員構(gòu)造函數(shù)基類構(gòu)造函數(shù)、派生類本身的構(gòu)造函數(shù)、對象成員構(gòu)造函數(shù)基類構(gòu)造函數(shù)、對象成員構(gòu)造函數(shù)、派生類本身的構(gòu)造函數(shù)10當(dāng)不同的類具有相同的間接基類時,有特點 ( d
31、)。各派生類對象將按繼承路線產(chǎn)生自己的基類版本派生類對象無法產(chǎn)生自己的基類版本為了建立惟一的間接基類版本,應(yīng)該必須改變類格為了建立惟一的間接基類版本,應(yīng)該聲明虛繼承7.2 閱讀下列程序,寫出執(zhí)行結(jié)果#include class BASE public:void get( int i,int j,int k,int l )a = i; b = j; x = k; y = l; void print()cout a = a t b = b t x = x t y = y endl;int a,b;protected:int x, y;class A: public BASE public:void
32、 get( int i, int j, int k, int l )BASE obj3;obj3.get( 50, 60, 70, 80 );obj3.print();= i; b = j; x = k; y = l;= a + b + obj3.a ; v = y - x + obj3.b;void print()cout a = a t b = b t x = x t y = y endl;cout u = u t v = v endl;private:int u, v ;void main() BASE obj1;A obj2;obj1.get( 10, 20, 30, 40 );obj
33、2.get( 30, 40, 50, 60 );obj1.print();obj2.print();答案:a = 50b=60 x=70y = 80a = 10b=20 x=30y = 40a = 30b=40 x=50y = 60u = 120 v = 702. #include class BASE1 public:BASE1( int i ) cout 調(diào)用基類 BASE1 的構(gòu)造函數(shù): i endl ; ;class BASE2 public:BASE2( int j ) cout 調(diào)用基類 BASE2的構(gòu)造函數(shù): j endl ; ;class A: public BASE1, pu
34、blic BASE2 public:A( int a, int b, int c, int d ) : BASE2(b), BASE1(c), b2(a), b1(d) cout 調(diào)用派生類A 的構(gòu)造函數(shù): a+b+c+d endl; private:BASE1 b1;BASE2 b2; ;void main() A obj( 1, 2, 3, 4 ); 答案:調(diào)用基類BASE1的構(gòu)造函數(shù):3調(diào)用基類BASE2的構(gòu)造函數(shù):2調(diào)用基類BASE1的構(gòu)造函數(shù):4調(diào)用基類BASE1的構(gòu)造函數(shù):1調(diào)用派生類A 的構(gòu)造函數(shù):107.3 編程題1定義一個rectangle 類,它包含兩個數(shù)據(jù)成員 lengt
35、h 和 width ;以及包含用于求長方形面積的成員函數(shù)。再定義rectangle 的派生類 rectangular ,它包含一個新數(shù)據(jù)成員 height 和用來求長方體體積的成員函數(shù)。在 main 函數(shù)中,使用兩個類,求某個長方形 的面積和某個長方體的體積。解答:#includeclass rectanglepublic:rectangle( float l,float w ) length = l;width = w; float area() return( length*width ); float getlength() return length; float getwidth()
36、 return width; private:float length;float width;class rectangular:public rectangle public:rectangular( float l,float w,float h ) : rectangle( l,w ) height = h; float getheight() return height; float volume() return area() *height; private:float height;void main() rectangle obj1( 2,8 );rectangular ob
37、j2( 3,4,5 );cout length= obj1.getlength() t width= obj1.getwidth() endl;cout rectanglearea= obj1.area() endl;cout length= obj2.getlength() t width= obj2.getwidth();cout t height= obj2.getheight() endl;cout rectangularvolume= obj2.volume() endl;2. 假設(shè)某銷售公司有銷售經(jīng)理和銷售員工,月工資的計算辦法是:銷售經(jīng)理的固定月薪為 8000 元并提取銷售額的千
38、分之五作為工資;銷售員工只提取銷售額的千分之五作為工資。編一程序,定義一個基類employee,它包含三個數(shù)據(jù)成員number、name和salary ,以及用于輸入編號和姓名的構(gòu)造函數(shù)。由 employee 類派生 salesman 類,再由 salesman類派生 salesmanager 類。 salesman 類包含兩個新數(shù)據(jù)成員 commrate 和 sales , 還包含用于輸入銷售額并計算銷售員工工資的成員函數(shù)pay() 和用于輸出的成員函數(shù) print() 。salesmanager 類包含新數(shù)據(jù)成員 monthlypay ,及用于輸入銷售額并計算銷售經(jīng)理工資的成員函數(shù) pay
39、() 、用于輸出的成員函數(shù)print() 。在 main 函數(shù)中,測試你的類結(jié)構(gòu),求若干個不同員工的工資。解答:略 。3試寫出所能想到的所有形狀(包括二維的和三維的),生成一個形狀層次類結(jié)構(gòu)。生成的層次結(jié)構(gòu)以 Shape作為基類,并由此派生出TwoDimShap聯(lián)和ThreeDimShape類。它們的派生類是不同形狀類。定義層次結(jié)構(gòu)中的每一個類,并用 main 函數(shù)進(jìn)行測試。解答:略。第 8 章 虛函數(shù)與多態(tài)性8.1 選擇題( d )調(diào)用虛函數(shù)。(c)對象名(d)基類指針.在C+用,要實現(xiàn)動態(tài)聯(lián)編,必須使用(a) 類名 (b) 派生類指針 TOC o 1-5 h z 2下列函數(shù)中,可以作為虛函
40、數(shù)的是( c,d )。(d) 析構(gòu)函數(shù)(d) 部分相同;(d) 抽象類(a) 普通函數(shù)(b) 構(gòu)造函數(shù) (c) 成員函數(shù)3在派生類中,重載一個虛函數(shù)時,要求函數(shù)名、參數(shù)的個數(shù)、參數(shù)的類型、參數(shù)的(b) 相同(c) 相容( b ) 聲明了 fun 為純虛函數(shù)。(b)virtual void fun()=0(d)virtual void fun() ( d )。純基類 (c) 派生類順序和函數(shù)的返回值( b )(a) 不同4下面函數(shù)原型聲明中,(a) void fun()=0;(c) virtual void fun()5若一個類中含有純虛函數(shù),則該類稱為(a) 基類(b)6假設(shè) Aclass 為
41、抽象類,下列聲明 ( a,c,d ) 是錯誤的。(a) Aclass fun( int ) ;(b)Aclass * p ;(c) int fun( Aclass ) ;(d)Aclass Obj ;7下面描述中,正確的是( b,d )。(a) 虛函數(shù)是沒有實現(xiàn)的函數(shù)(c) 抽象類是只有純虛函數(shù)的類(b)純虛函數(shù)的實現(xiàn)在派生類定義(d)抽象類指針可以指向不同的派生類8.2 閱讀下列程序,寫出執(zhí)行結(jié)果#include class Bclass public:Bclass( int i, int j ) x = i; y = j; virtual int fun() return 0 ; prot
42、ected:int x, y ;class Iclass:public Bclass public :Iclass(int i, int j, int k):Bclass(i, j) z = k; int fun() return ( x + y + z ) / 3; private :int z ;void 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 =
43、&obj;cout fun() endl;答案:0505#include 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() coutx = xty = x * x = x*xendl; ;class B:public BASE public:void fun() cout x = x t y = y endl;cout y = x / y
44、 = x / y getxy( 10 );pb - fun();pb = &obj2;pb - getxy( 100, 20 );pb - fun();答案:x = 10 y = x*x = 100 x = 100 y = 20y = x / y = 58.3 編程題由于球體和圓柱體都可以看使用虛函數(shù)編寫程序求球體和圓柱體的體積及表面積。作由圓繼承而來,所以可以定義圓類circle 作為基類。在circle 類中定義一個數(shù)據(jù)成員radius 和兩個虛函數(shù)area() 和 volume() 。 由 circle 類派生 sphere 類和 column 類。 在派生類中對虛函數(shù)area() 和
45、volume() 重新定義,分別求球體和圓柱體的體積及表面積。解答:#include 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;class sphere:public circle public:sphere( double r ):circle( r ) double area() return 4
46、.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 * radius * ( height + radius ); double volume() return PI * radius * radius * height;priva
47、te:double height;void main() circle *p;sphere sobj(2);p = &sobj;cout 球體: endl;cout 體積= volume() endl;cout 表面積 = area() endl;column cobj( 3,5 );p = &cobj;cout 圓柱體 : endl;cout 體積= volume() endl;cout 表面積 = area() endl;2某學(xué)校對教師每月工資的計算規(guī)定如下:固定工資+課時補貼。教授的固定工資為5000 元,每個課時補貼 50 元。副教授的固定工資為 3000 元,每個課時補貼 30 元。
48、講師的固定工資為2000 元,每個課時補貼 20 元。定義教師抽象類,派生不同職稱的教師類,編寫程序求若干個教師的月工資。解答:略。3.改寫第7章習(xí)題7.3第3題,把Shape類定義為抽象類,提供共同操作界面的純虛 函數(shù)。TwoDimShap聯(lián)和ThreeDimShape類仍然抽象類,第 3層具體類才能提供全部函數(shù) 的實現(xiàn)。在測試函數(shù)中,使用基類指針實現(xiàn)不同派生類對象的操作。解答:略。第9章 模 板選擇題1關(guān)于函數(shù)模板,描述錯誤的是( a )。函數(shù)模板必須由程序員實例化為可執(zhí)行的函數(shù)模板函數(shù)模板的實例化由編譯器實現(xiàn)一個類定義中,只要有一個函數(shù)模板,則這個類是類模板類模板的成員函數(shù)都是函數(shù)模板,
49、類模板實例化后,成員函數(shù)也隨之實例化2下列的模板說明中,正確的是( a,c )。template template template template 3假設(shè)有函數(shù)模板定義如下:template Max( T a, T b ,T &c) c = a + b ; 下列選項正確的是( a,b )。(a) float x, y; float z;(b) int x, y, z;Max( x, y, z );Max( x, y, z );(c) int x, y; float z;Max( x, y, z );4關(guān)于類模板,描述錯誤的是建立類模板對象的實例化過程為(a) 基類 派生類(c) 模板類 對
50、象6.在C+用,容器是一種(d )標(biāo)準(zhǔn)類(b) 標(biāo)準(zhǔn)對象(d) float x; double y, z;Max( x, y, z );( d )。一個普通基類不能派生類模板類模板從普通類派生,也可以從類模板派生根據(jù)建立對象時的實際數(shù)據(jù)類型,編譯器把類模板實例化為模板類函數(shù)的類模板參數(shù)須通過構(gòu)造函數(shù)實例化( c )。構(gòu)造函數(shù) 對象(d) 模板類 模板函數(shù)標(biāo)準(zhǔn)函數(shù) (d) 標(biāo)準(zhǔn)類模板9.2 閱讀下列程序,寫出執(zhí)行結(jié)果#include template void fun( T &x, T &y )T temp;temp = x; x = y; y = temp;void main()int i ,
51、 j;= 10; j = 20;fun( i, j );cout i = i t j = j endl;double a , b;a = 1.1; b = 2.2;fun( a, b );cout a = a t b = b endl;答案:i=20 j=10a=2.2 b=1.1. #include template class base public:base( T i , T j ) x = i; y = j; T sum() return x + y; private:T x , y; ;void main() base obj2(3.3,5.5);cout obj2.sum() en
52、dl;base obj1(3,5);cout obj1.sum() endl;答案:8.889.3 編程題1使用函數(shù)模板實現(xiàn)對不同類型數(shù)組求平均值的功能,并在main() 函數(shù)中分別求一個整型數(shù)組和一個單精度浮點型數(shù)組的平均值。解答:略。. 建立結(jié)點包括一個任意類型數(shù)據(jù)域和一個指針域的單向鏈表類模板。在 main() 函 數(shù)中使用該類模板建立數(shù)據(jù)域為整型的單向鏈表,并把鏈表中的數(shù)據(jù)顯示出來。解答:#include template class List public:List( T x ) data = x; void append( List *node ) node-next = this
53、;next = 0;List *getnext() return next; T getdata() return data; private:T data;List *next;void main() int i, idata, n, fdata;cout n;cout fdata;List headnode( fdata );List *p, *last;last = &headnode;for( i=1; i idata;p = new list ( idata );p-append( last );last = p; cout 鏈表已經(jīng)建立! endl;cout 鏈表中的數(shù)據(jù)為: en
54、dl;p = &headnode;while( p ) cout getdata() getnext();.學(xué)習(xí)使用STL編程。選擇合適的組件,重做第5章習(xí)題5.3第4題,建立職工信息鏈表,并完成題中要求的各種操作。解答:略。第10章輸入/輸出流10.1 選擇題1.下列類中(b,d )不是輸入輸出流類iostream 的派生類。(a)fstream (b)ofstream (c)strstream.在下列選項中(b,c,d ) 是ostream類的對象。(a)cin(b)cerr(c)clog. read函數(shù)的功能是從輸入流中讀取(d )。(d)ostrstream(d)cout(a) 一個字
55、符(b)當(dāng)前字符 (c) 一行字符(d)指定若干個字符4.下列選項中,用于清除基數(shù)格式位設(shè)置以十六進(jìn)制輸出的語句是(b )(a) coutsetf(ios:dec,ios:basefield);(b) coutsetf(ios:hex,ios:basefield);(c) coutsetf( ios:hex,ios:basefield);5下列格式控制符,在iostream.h 中定義的是( a,d )iomanip.h 中定義的是( b,c )(a)endl6下列串流類,在(d)octsstream.h 中定義的是(b)setfill(c)setwstrstream.h 中定義的是( b,d
56、 )( a,c )(a)istringstream7 包含類 fstream (a)fstream.h(b)istrstream定義的頭文件是(b)ofstream.h8以寫的方式打開文件ifstream infile(ifstream infile(ofstream infile(fstream infile(9假定已定義浮點型變量outfile 中去,正確的語句是” D:file.dat “D:file.dat “D:file.dat “D:file.dat “D:file.datdata ,以二進(jìn)制方式把( c )。(c)ostringstream) 。(c)ifstream.h錯誤的語
57、句是( a,b,c,d ), ios:in );”, ios:in);, ios:out);, ios:in|ios:out );data 的值寫入輸出文件流對象(d)ostrstream(d)iostream.houtfile.write( float* ) &data , sizeof( float );outfile.write( float* ) &data , data );outfile.write( char* ) &data , sizeof( float );outfile.write( char* ) &data , data );.2 閱讀下列程序,寫出執(zhí)行結(jié)果#incl
58、ude void main() double x = 123.456;cout.width( 10 );cout.setf( ios : dec, ios : basefield );cout x endl;cout.setf( ios : left );cout x endl;cout.width( 15 );cout.setf( ios:right , ios:left );cout x endl;cout.setf( ios:showpos );cout x endl;cout -x endl;cout.setf( ios : scientific );cout x endl;答案:12
59、3.456123.456123.456+123.456-123.456+1.234560e+002#include void main() double x = 123.45678;cout.width( 10 );cout ( # );cout x endl;cout.precision( 5 );cout x endl;cout.setf( ios:showpos );cout x endl;cout.setf( ios:scientific );cout x endl;答案:#123.457123.46+123.46+1.23457e+002#include #include void
60、main() double x = 123.456789;cout setiosflags( ios:fixed | ios:showpos ) x endl;cout setw( 12 ) setiosflags( ios:right );cout setprecision( 3 ) -x endl;cout resetiosflags( ios:fixed | ios:showpos ) setiosflags( ios:scientific );cout setprecision( 5 ) x endl;答案:+123.456789-123.4571.23457e+00210.3 編程題
溫馨提示
- 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)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年電銀項目發(fā)展計劃
- 木質(zhì)復(fù)合材料批發(fā)考核試卷
- 果蔬銷售渠道拓展與維護考核試卷
- 臥式自動翻洗過濾機行業(yè)相關(guān)投資計劃提議范本
- 光纖用GECL4行業(yè)相關(guān)投資計劃提議
- 煤化工相關(guān)行業(yè)投資方案范本
- 彈射玩具包裝設(shè)計創(chuàng)新考核試卷
- 電子健康記錄系統(tǒng)的安全保障技術(shù)研究
- 教育基地展示系統(tǒng)行業(yè)相關(guān)投資計劃提議范本
- 2024年界首農(nóng)商銀行社會招聘筆試真題
- 家具廠安全生產(chǎn)操作規(guī)程大全
- 神經(jīng)病學(xué)-緒論課件
- 危險化學(xué)品全表
- 胸膜疾病課件
- 公路施工技術(shù)全套課件
- 胸痹心痛中醫(yī)診療方案及臨床路徑
- 泛讀2unit2-music
- 中學(xué)生防溺水安全教育課件(PPT 44頁)
- 2019版外研社高中英語選擇性必修二Unit 1 Growing up 單詞表
- 有機金屬化學(xué)1
- JIT標(biāo)準(zhǔn)作業(yè)作業(yè)指導(dǎo)書
評論
0/150
提交評論