




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、文檔供參考,可復制、編制,期待您的好評與關注! 程序設計(C+語言)期中考試試題班級:_ 學號:_ 姓名:_7 / 7一、簡答題(每小題4分,共20分)1. 在C+中聲明類時,如何做到多個實例共享一個全局變量?2. 引用和指針之間有什么區(qū)別?3. 什么是抽象類?析構函數可以聲時為虛函數嗎?如果可以,在什么情況下使用?4. 什么是多態(tài)性?多態(tài)性是如何實現的? 5. 構造函數與析構函數的功能是什么? 在繼承層次上,構造函數和析構函數的調用順序如何? 二、程序改錯題 (每小題5分,共20分)1. 下面的程序是否有錯誤,如果有錯,請說明原因并改正。# include <iostream.h>
2、; int * FuncOne() int * pint = new int(5); count <<"the value of pInt in FuncOne is:"<<* pint<<endl; return pint; int main() int * pint = FuncOne(); cout <<"the value of pInt back in main is:"<< * pint << endl return 0; 2. 下面的程序是否有錯誤,如果有錯,請說明原因
3、并改正。struct A1 int i; ; class A2 int i; ; int main() A1 a1; a1.i = 0; A2 a2; a2.i = 0; 3. 下面的程序是否有錯誤,如果有錯,請說明原因并改正。int main() char szTest = "hello" const char* psz = szTest; psz0 = b; 4. 下面的程序是否有錯誤,如果有錯,請說明原因并改正。class Shape() public: Shape(); virtual Shape(); virtual Shape(const Shape&)
4、; 三、程序閱讀題(每小題5分,共20分)1. 分析下面的程序,并寫出運行結果。class A public: virtual void func() cout << "I am in base" << endl; ;class B : public A public: virtual void func() cout << "I am in derived" << endl; ;void main() B* bb = new B; bb->func(); A* aa = bb; aa->fun
5、c(); 以上程序的輸出結果是 。2. 分析下面的程序,并寫出運行結果。class Sample public:int v;Sample() ;Sample(int n):v(n) ;Sample( Sample & x) v = 2 + x.v ; ;Sample PrintAndDouble( Sample o) cout << o.v <<endl; o.v = 2 * o.v; return o;int main() Sample a(5);Sample b = a;cout << b.v << endl;Sample c = P
6、rintAndDouble( b );cout << c.v << endl;Sample d;d = a;cout << d.v << endl;以上程序的輸出結果是 。3. 分析下面的程序,并寫出運行結果。class A public:int val;A(int = 0) val = n; ; A& GetObj() return *this; ;main() A a; cout <<a.val << endl;a.GetObj() = 5;cout << a.val << endl;以
7、上程序的輸出結果是 。4. 分析下面的程序,并寫出運行結果。class B private: int nBVal;public: B ( int n ) nBVal = n; void Print() cout << "nBVal="<< nBVal << endl; ;class D: public B private : int nDVal;public: D( int n) : B(3*n) nDVal = n;void Print() B:Print(); cout << "nDVal="<&
8、lt;nDVal<<endl; ;main() B * pb = new B(2); pb->Print(); D d(4); d.Print (); B * p = &d; p->Print(); delete pb;以上程序的輸出結果是 。四、程序填空題(每小題10分,共20分)1. 填空使程序能編譯通過,并寫出運行的輸出結果。class MyString private:char * p;public:MyString( char * s ) p = new charstrlen(s)+1;strcpy(p,s);MyString( MyString &a
9、mp; o ) strcpy( p, o.p);MyString() delete p; void Copy( char * s) p = new charstrlen(s)+1;strcpy(p,s);const char * c_str() ;main() MyString s1("This"), s2 =s1;s2.Copy ( "Hello");cout << s1.c_str () << endl << s2.c_str () ;該程序輸出結果為: 。2. 填空使程序能編譯通過,并寫出運行的輸出結果。#inc
10、lude <iostream.h>template < >class myclass T i;public: myclass (T a) i = a; void show( ) cout << i << endl; ;void main() myclass< > obj("This"); obj.show();該程序輸出結果為: 。五、程序設計題(共20分)設有如下定義的幾個類,其中,Graphic是個抽象類,它定義了平面封閉圖形應該具有的運算求面積getArea,它可以有任意多子類,如Circle和Rectang
11、le便是它的兩個子類。GraphicContainer是一個包含Graphic對象的類,該類有兩個數據成員,其中:m_buffer是個數組,用于存放不同的Graphic對象;m_sum用來表示該數組中實際存放元素的個數,即Graphic對象的總數?,F在要求你完成該類的求所有Graphic對象總面積函數getAllArea的實現代碼。為了完成該函數,允許你在其它相關類中增加方法及其實現。程序設計(C+語言)期中考試參考答案一、簡答題(每小題5分,共20分)6. 在C+中聲明類時,如何做到多個實例共享一個全局變量?聲明一個類靜態(tài)成員變量。 7. 引用和指針之間有什么區(qū)別?引用是一個別名,而指針是一
12、個保存地址的變量。8. 什么是抽象類?析構函數可以聲時為虛函數嗎?如果可以,在什么情況下使用?如果一個類中包括純虛函數,則該類為抽象類,抽象類不能實例化,主要是作為接口定義。一般情況下類的析構函數都定義成虛函數,主要是考慮在使用基類指針操作派生類對象時保證類的析構順序。9. 什么是多態(tài)性?多態(tài)性是如何實現的? 函數多態(tài)性是指用多個含義重載一個函數的能力,即允許創(chuàng)建多個名稱相同的函數??赏ㄟ^改變同名函數變元的類型或個數來實現。10. 構造函數與析構函數的功能是什么? 在繼承層次上,構造函數和析構函數的調用順序如何? 構造函數用來初始化。析構函數用來做清除工作,一般包括內存釋放。在繼承層次上,構造
13、函數和析構函數的調用順序為:構造函數是先基類,后派生類;析構函數是先派生類,后基類。二、程序改錯題 (每小題5分,共20分)5. 下面的程序是否有錯誤,如果有錯,請說明原因并改正。# include <iostream.h> int * FuncOne() int * pint = new int(5); count <<"the value of pInt in FuncOne is:"<<* pint<<endl; return pint; int main() int * pint = FuncOne(); cout &
14、lt;<"the value of pInt back in main is:"<< * pint << endl return 0; 有錯誤,內存泄漏。# include <iostream.h> int FuncOne() int * pint = new int(5); cout <<"the value of pInt in FuncOne is: "<<* pint<<endl; int temp = *pint; delete pint; return temp;
15、int main() int theint = FuncOne(); cout <<”the value of pInt back in main is:”<<theint << endl; return 0; 6. 下面的程序是否有錯誤,如果有錯,請說明原因并改正。struct A1 int i; ; class A2 int i; ; int main() A1 a1; a1.i = 0; A2 a2; a2.i = 0; 有錯誤,類定義中未顯示權限定義符缺省為private。 struct A1 int i; ; class A2 public:int
16、 i; ; int main() A1 a1; a1.i = 0; A2 a2; a2.i = 0; 7. 下面的程序是否有錯誤,如果有錯,請說明原因并改正。int main() char szTest = "hello" const char* psz = szTest; psz0 = b; 有錯誤,psz是一字符串指針,該指針指向的內容是常量,指針指向的內容不能被修改。int main() char szTest = "hello" char* const psz = szTest; psz0 = b; 8. 下面的程序是否有錯誤,如果有錯,請說明原
17、因并改正。class Shape() public: Shape(); virtual Shape(); virtual Shape(const Shape&); 有錯誤,不能聲明一個拷貝構造函數為虛擬函數。 class Shape() public: Shape(); virtual Shape(); Shape(const Shape&); 三、程序閱讀題(每小題5分,共25分)5. 分析下面的程序,并寫出運行結果。class A public: virtual void func() cout << "I am in base" <&
18、lt; endl; ;class B : public A public: virtual void func() cout << "I am in derived" << endl; ;void main() B* bb = new B; bb->func(); A* aa = bb; aa->func(); 以上程序的輸出結果是:I am in derived I am in derived 6. 分析下面的程序,并寫出運行結果。class Sample public:int v;Sample() ;Sample(int n):v(n
19、) ;Sample( Sample & x) v = 2 + x.v ; ;Sample PrintAndDouble( Sample o) cout << o.v <<endl; o.v = 2 * o.v; return o;int main() Sample a(5);Sample b = a;cout << b.v << endl;Sample c = PrintAndDouble( b );cout << c.v << endl;Sample d;d = a;cout << d.v <&
20、lt; endl;以上程序的輸出結果是:79205 7. 分析下面的程序,并寫出運行結果。class A public:int val;A(int n = 0) val = n; ; A& GetObj() return *this; ;main() A a; cout <<a.val << endl;a.GetObj() = 5;cout << a.val << endl;以上程序的輸出結果是:05 8. 分析下面的程序,并寫出運行結果。class B private: int nBVal;public: B (
21、 int n ) nBVal = n; void Print() cout << "nBVal="<< nBVal << endl; ;class D: public B private : int nDVal;public: D( int n) : B(3*n) nDVal = n;void Print() B:Print(); cout << "nDVal="<<nDVal<<endl; ;main() B * pb = new B(2); pb->Print(); D d
22、(4); d.Print (); B * p = &d; p->Print(); delete pb;以上程序的輸出結果是:nBVal=2nBVal=12nDVal=4nBVal=12 四、程序填空題(每1小題10分,共20分)3. 填空使程序能編譯通過,并寫出運行的輸出結果。class MyString private:char * p;public:MyString( char * s ) p = new charstrlen(s)+1;strcpy(p,s);MyString( MyString & o ) p = new charstrlen(o.p ) + 1
23、;strcpy( p, o.p);MyString() delete p; void Copy( char * s) if (p!=NULL) delete p;p = new charstrlen(s)+1;strcpy(p,s);const char * c_str() return p;main() MyString s1("This"), s2 =s1;s2.Copy ( "Hello");cout << s1.c_str () << endl << s2.c_str () ;該程序輸出結果為:ThisHell
24、o 4. 填空使程序能編譯通過,并寫出運行的輸出結果。#include <iostream.h>template < class T >class myclass T i;public: myclass (T a) i = a; void show( ) cout << i << endl; ;void main() myclass< char * > obj("This"); obj.show();該程序輸出結果為:This 五、程序設計題(共20分)設有如下定義的幾個類,其中,Graphic是個抽象類,它定義了
25、平面封閉圖形應該具有的運算求面積getArea,它可以有任意多子類,如Circle和Rectangle便是它的兩個子類。GraphicContainer是一個包含Graphic對象的類,該類有兩個數據成員,其中:m_buffer是個數組,用于存放不同的Graphic對象;m_sum用來表示該數組中實際存放元素的個數,即Graphic對象的總數?,F在要求你完成該類的求所有Graphic對象總面積函數getAllArea的實現代碼。為了完成該函數,允許你在其它相關類中增加方法及其實現。#include<iostream.h>class Graphic public: virtual d
26、ouble getArea()=0;class Triangle: public Graphic protected:double height, width;public:Triangle(double h, double w) height=h; width=w; double getArea() return height*width*0.5; ;class Rectangle: public Graphic protected:double height, width; public: Rectangle(double h, double w) height=h; width=w; d
27、ouble getArea() return height*width; ;class Circle: public Graphic protected: double redius; public: Circle(double r) redius=r; double getArea() return redius*redius*3.14; ;class GraphicContainer private:Graphic *m_buffer; int m_sum; public: GraphicContainer(int sum)m_sum=sum;m_buffer=new Graphic *m_sum;char select;double he
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 海邊防護欄施工組織計劃方案
- 城市小學校本課程開發(fā)計劃
- 以弗魯姆期望理論為鑰開啟初中生思想品德課興趣之門
- 以審美體驗為核奏響中小學音樂欣賞教學新樂章
- 保險公司總務處資料歸檔計劃
- 幼兒園后勤管理崗位職責
- 2025年“書香校園”創(chuàng)新閱讀競賽方案計劃
- 石油天然氣工程投標質量保證及售后服務計劃
- 外貿企業(yè)疫情防控措施提升
- 浙江省桐鄉(xiāng)市第一中學2025年高二下化學期末綜合測試模擬試題含解析
- 2024年中國中式養(yǎng)生水行業(yè)發(fā)展趨勢洞察報告
- (完整版)自由泳教案
- 《困境兒童風險評估規(guī)范》
- 國開2024年《機械設計基礎》形考任務1-4答案
- CJT121-2000 再生樹脂復合材料檢查井蓋
- 生物的分子進化和分子遺傳學
- 北京小型實驗室建設計劃書
- 高校大型儀器設備利用評價研究-以華南農業(yè)大學為例的中期報告
- 鋼結構橋梁的設計與建造:鋼結構橋梁的設計與施工要點與技術
- 中醫(yī)四大經典備考刷題庫匯總(800題)
- 外科護理學課程說課課件
評論
0/150
提交評論