2010上海交通大學(xué)C++期末試卷A_第1頁
2010上海交通大學(xué)C++期末試卷A_第2頁
2010上海交通大學(xué)C++期末試卷A_第3頁
2010上海交通大學(xué)C++期末試卷A_第4頁
2010上海交通大學(xué)C++期末試卷A_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、c+程序設(shè)計上 海 交 通 大 學(xué) 試 卷(a)( 2009 至 2010 學(xué)年 第_二_學(xué)期)班級號_ 學(xué)號_ 姓名 課程名稱 c+程序設(shè)計 成績 一 選擇題(每題1分,共10分)1、類cstudent的拷貝構(gòu)造函數(shù)的聲明語句為 。acstudent &cstudent (const cstudent other) bcstudent cstudent(const cstudent other)ccstudent (cstudent *other) dcstudent (const cstudent &other)2、類的友元函數(shù)能訪問該類的 。a私有成員b保護成員c所有成員

2、d公有成員3、下面關(guān)于靜態(tài)數(shù)據(jù)成員的描述中,正確的是 。 a靜態(tài)數(shù)據(jù)成員可以直接用類名調(diào)用b靜態(tài)數(shù)據(jù)成員可以在類體內(nèi)進行初始化c靜態(tài)數(shù)據(jù)成員不能受private控制符的作用d類的不同對象有不同的靜態(tài)數(shù)據(jù)成員值4、 當(dāng)使用fstream流類定義一個流對象并打開一個磁盤文件時,文件的隱含打開方式為_。aios:in bios:out cfstream:in | fstream:out d沒有指定打開方式5、若char p20=”hello world”;則輸出該字符串正確的語句是_。acout<<p20; bcout<<&p; ccout.<< *p;

3、dcout << p;6、在派生類中重新定義虛函數(shù)時,除了 方面,其他方面都必須與基類中相應(yīng)的虛函數(shù)保持一致。 a參數(shù)個數(shù)和類型 b函數(shù)體 c函數(shù)名稱 d返回類型7、類模板定義如下:template <class t, int low, int high>class array .;對該類模板實例化正確的是 。aarray<float, 0, 20> x;barray<int, int, int> x;ctemplate<int, 0, 20> x; darray<int, 0, int> x;我承諾,我將嚴(yán)格遵守考試紀(jì)律

4、。承諾人: 題號得分批閱人(流水閱卷教師簽名處) 8、公有成員提供了類對外部的接口,私有成員是類的內(nèi)部實現(xiàn),而 不許外界訪問,但允許派生類的成員訪問,這樣既有一定的隱藏能力,也提供了開放的接口。a公有成員 b.私有成員 c.私有成員函數(shù) d.保護成員9、假定ab為一個類,則執(zhí)行 ab a(2), b3, *p; 語句時共調(diào)用該類構(gòu)造函數(shù)的次數(shù)為 。 a. 1 b. 3 c. 4 d. 5 10、如果a是已經(jīng)定義好的一個類,函數(shù)f的原型為a f(). r2是a類的一個對象,在函數(shù)f中執(zhí)行return r2時,系統(tǒng)將自動調(diào)用 。a.缺省的構(gòu)造函數(shù) b.拷貝構(gòu)造函數(shù) c.賦值運算符重載函數(shù) d.不調(diào)

5、用任何函數(shù)二 看程序,寫結(jié)果(每題5分,共35分)1、請寫出下列程序運行結(jié)果class add friend add operator+(add op); friend add operator+(add &op, int n);public:add(int i = 0, int j = 0) a = i; b = j;void show() const cout << "a=" << a << ",b=" << b << endl;private:int a, b;add operat

6、or+(add op)+op.a; +op.b;return op;add operator+(add &op, int n)+op.a;+op.b;return op;void main()add obj(1, 2);obj.show();(obj+).show();obj.show();(+obj).show(); obj.show(); 2、請寫出下列程序運行結(jié)果class cconanddecon public:cconanddecon(char value) m_data = value;cout << "object " << m

7、_data <<" constructor" <<endl; cconanddecon(const cconanddecon &other) m_data = other.m_data - 1 ;cout << "object " << m_data <<" copy constructor" <<endl; cconanddecon operator=(const cconanddecon &right) if( this != &rig

8、ht ) m_data = right.m_data + 1 ;cout << "object 's new value is " << m_data <<" " <<endl; return *this;cconanddecon() cout <<"object " << m_data << " destructor" << endl; private:char m_data;void func(cconand

9、decon x);int main()cconanddecon *p = new cconanddecon('h');static cconanddecon c1( 'k');func(*p); delete p;return 0;void func(cconanddecon x)static cconanddecon c1 = x; cconanddecon c2 = c1;3、請寫出下列程序運行結(jié)果class cmakepublic:cmake(int n) m_data = n ; cout << "構(gòu)造 " <<

10、; m_data << endl; cmake(const cmake &obj) m_data = obj.m_data + 1; cout << "拷貝構(gòu)造" << m_data << endl;cmake() cout << "析構(gòu) " << m_data << endl; operator int() const return m_data; private:int m_data;cmake makeobject( int n )cmake p (n);r

11、eturn p;int main()cout << makeobject( 7 ) << endl;return 0;4、請寫出下列程序運行結(jié)果class basefly public: virtual void fly() cout << "n-class basefly:fly()-n" ;class birdfly: public basefly public: void fly() cout << "-class birdfly:fly()-n" ;class dragonfly: public b

12、asefly public: void fly() cout << "n-class dragonflyfly:fly()-n" ;void main() basefly *pbase, obase; birdfly *pbird = new birdfly(); pbase = pbird; cout << ” nbirdfly->”; pbase->fly(); dragonfly *pdragon = new dragonfly(); pbase = pdragon; obase = *pdragon; pbase->fly(

13、); pbird->fly(); pdragon->fly(); obase.fly();5、寫出下列程序執(zhí)行結(jié)果class point friend bool operator!=(const point &p1, const point &p2) return p1.x+p1.y  != p2.x+p2.y  private:int x, y; public: point(int a = 1, int b = 1) x = a; y = b; cout << "構(gòu)造point(" << x <

14、< "," << y << ")" << endl; point(const point &p)x = p.x  y = p.y cout << "拷貝構(gòu)造point(" << x << "," << y << ")" << endl; point() cout << "析構(gòu)point(" << x <&l

15、t; "," << y << ")" << endl; point &operator+() if (x < y) +x;else +y; return *this;void show() cout << " point(" << x << "," << y << ")" << endl; int getx() const return x int gety() c

16、onst return y  int main( )const point origin(10, 5);point point2(6, 7);int n = 0;while (point2 != origin) +point2; +n;point2.show();cout << “n= “ << n << endl;return 0;6、請寫出下列程序運行結(jié)果void func( int ) ;int main() for (int i = 30; i > 0; i /= 3)try func(i);cout << "i

17、 = " << i << endl; catch( int) cout << "exception: int " << endl; catch (double) cout << "exception: double " << endl; return 0;void func(int num ) if ( num % 3 ) throw 3; else if ( num % 5 ) throw 5.5; 7、寫出下列程序的輸出結(jié)果template <class t>

18、; class sample protected:t n; public: sample(t i) n = i; cout << "construct " << n << endl; sample() cout << "destruct " << n << endl; void disp()cout << "n=" << setfill ( '#' ) << setw ( 10 ) << n <

19、< endl; ; template <class t >class model: public sample<t> t m;public:model(t t1, t t2): sample<t>(t1) m = t2; cout << "construct " << m<< endl; model() cout << "destruct "<< m<< endl; void disp()cout << "n="

20、; << setfill ( '#' ) << setw ( 10 ) << n << ' ' << m << endl; operator t () const return n + m; ;int main() model<int> s (20, 30); s.disp(); cout << (int) s << endl;return 0;三 程序填空(每空2分,共30分)1、下列程序的輸出是:25 714請?zhí)羁?。class cconst publi

21、c:cconst(int d = 0) : size += d; void print() cout << len << “ “ << size <<endl; void show() cout << size << endl; private:const int len; static int size; int main()_ show();const cconst c(5);c.print(); cconst c2(7);c2.show();return 0;2、下面是處理二維平面上線段的類,其中的point是上一大

22、題第5題中定義的point類,請?zhí)羁?。class line point start;point end;public:line(int sx, int sy, int ex, int ey): _ int length() / 計算線段的長度 return _; ;3、下面的函數(shù)打開一個保存著一批字符串的文本文件,字符串間用空格分離,各字符串的長度均小于20。文件名作為參數(shù)傳入函數(shù)。函數(shù)讀出文件中的字符串,并輸出字符串到屏幕,每行一個,最后統(tǒng)計輸出在文件中一共有多少個字符串。void fr(char* fname) ifstream fin(fname);char a20;int cnt =

23、0;if ( !fin ) cout << "cant open file"return;while ( fin >> a ) cout << a << endl; +cnt ; fin.close() ;cout << "字符串總數(shù)為 " << cnt << endl;4、補充函數(shù)equal使得程序結(jié)果為:5 + 6 = 11;1.111 + 2.222 = 3.333_ t add ( ) ; int main()int i = 5, j = 6;double y1

24、= 1.111222, y2 = 2.22222222222;cout << i <<" + " << j <<" =" add(i, j) << endl;cout << setprecision(4) << y1< " + " << y2 <<" = " << add(y1, y2) << endl;return 0;四. 編程(共25分)1、設(shè)計一個學(xué)生類student,包

25、括姓名和三門課程成績,利用重載運算符”+“將所有學(xué)生的成績相加放在一個對象中,再對該對象求各門課程的平均分。即,運行下面測試程序時,能得到相應(yīng)的執(zhí)行結(jié)果。 (10分)void main() student s1("li", 78, 82, 86), s2("zheng", 75, 62, 89);  student s3("ma", 89, 87, 95), s4("xu", 54, 78, 66), s;  cout << &quo

26、t;輸出結(jié)果" << endl;  s1.disp();  s2.disp();  s3.disp();  s4.disp();  s=s1 + s2 + s3 + s4;  / 調(diào)用重載運算符  avg(s, 4);  / 友元函數(shù)求平均分    本測試程序的執(zhí)行結(jié)果如下:  輸出結(jié)果:  li 78 82 86  zheng 75 62 89  ma 89 87 95  xu 54 78 66  平均分 74 77 84 評分標(biāo)準(zhǔn): 正確定義數(shù)據(jù)成員(2分):姓名和三門課的成績 正確定義成員函數(shù)(3分):構(gòu)造函數(shù),disp函數(shù),avg函數(shù) 正確定義+重載函數(shù)(1分):定義為成員函數(shù)或友元函數(shù) 每個函數(shù)的實現(xiàn):各1分2、編寫一個程序,計算扇形面積和球體表面積。已知:圓周率 = 3.1415926 且并定義為所有對象共享的常量扇形面積 =

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論