內(nèi)蒙古科技大學(xué)C++第四章習(xí)題答案_第1頁
內(nèi)蒙古科技大學(xué)C++第四章習(xí)題答案_第2頁
內(nèi)蒙古科技大學(xué)C++第四章習(xí)題答案_第3頁
內(nèi)蒙古科技大學(xué)C++第四章習(xí)題答案_第4頁
內(nèi)蒙古科技大學(xué)C++第四章習(xí)題答案_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、一、選擇題1.標(biāo)志著C+從面向過程變成面向?qū)ο蟮闹饕胧┦牵?D )。A.增加了新的運算符        B.允許函數(shù)重載,設(shè)置默認(rèn)參數(shù)C.規(guī)定函數(shù)聲明必須用原型  D.引用了類和對象的概念 2.有關(guān)類的說法錯誤的是( D )。A.類是一種用戶自定義的數(shù)據(jù)類型B.只有類中的成員函數(shù)才能存取類中的私有數(shù)據(jù)C.在類中如果不作特殊說明,所指的數(shù)據(jù)均為私有類型D. 在類中如果不作特殊說明,所指的成員函數(shù)均為公有類型 3.有關(guān)類和對象的說法錯誤的是( C )。A.對象是類的一個實例B.任何一個對象只能屬于一個具體的類C.一個類只能有一

2、個對象D.類與對象的關(guān)系和數(shù)據(jù)類型與變量的關(guān)系相似 4.下列關(guān)于構(gòu)造函數(shù)的描述中,錯誤的是( D )。A.構(gòu)造函數(shù)的函數(shù)名與類名相同     B.構(gòu)造函數(shù)可以重載C.構(gòu)造函數(shù)可以設(shè)置默認(rèn)參數(shù)       D.構(gòu)造函數(shù)必須指定類型說明 5.對任意一個類,析構(gòu)函數(shù)的個數(shù)最多為( B )個。A.0    .B.1     C.2     D.4 6.通??截悩?gòu)造函數(shù)的參數(shù)是( D )。 A.某個對象名       

3、0;        B.某個對象的成員名   C.某個對象的指針名          D.某個對象的引用名 7.已知A是一個類,則執(zhí)行語句A a;時,將自動調(diào)用該類的( B )。A.有參構(gòu)造函數(shù)              B.無參構(gòu)造函數(shù)C.拷貝構(gòu)造函數(shù)              D.賦值構(gòu)造函數(shù)二、簡答題 1.比較C+中的結(jié)構(gòu)

4、(struct)和類(class)的概念的相同和不同之處。 答:相同處:struct和class都可以包含成員函數(shù)和數(shù)據(jù)成員,struct和class都可以繼承,struct和class都可以實現(xiàn)多態(tài)不同處:struct和class的默認(rèn)繼承訪問權(quán)限不同,struct默認(rèn)是public的,class默認(rèn)是private 2.類中的公有(public)成員和私有(private)成員有什么區(qū)別? 答:公有權(quán)限的數(shù)據(jù)成員或成員函數(shù)可以作為與外界聯(lián)系的公共接口,由外界訪問,稱為類的外部接口,外界只能通過公有成員這個借口與類發(fā)生聯(lián)系.共有成員不僅可以由該類的成員函數(shù)訪問,

5、還可以在類外的程序中通過對象來訪問. 私有成員只能通過該類的成員函數(shù)或友元函數(shù)訪問,在類外的程序中不能通過對象來訪問,對私有成員來說,來自類外的任何訪問都是非法的.通常將類中的數(shù)據(jù)成員全部或大部分定義為私有的,這是類封裝體最隱蔽的部分,實現(xiàn)了類內(nèi)部數(shù)據(jù)的有效保護,提高了類封裝數(shù)據(jù)的安全性.在類外,可以通過對象調(diào)用共有成員函數(shù),實現(xiàn)對私有成員的訪問. 3.構(gòu)造函數(shù)和析構(gòu)函數(shù)的作用是什么? 答:構(gòu)造函數(shù)是類中一個特殊的成員函數(shù),他的作用是為對象的數(shù)據(jù)成員分配存儲空間,對數(shù)據(jù)成員賦初值.析構(gòu)函數(shù)的作用與構(gòu)造函數(shù)正好相反,它用來完成對象被刪除前的一些內(nèi)存釋放工作.

6、0;4.構(gòu)造函數(shù)是否可以重載?為什么?析構(gòu)函數(shù)呢? 答:構(gòu)造函數(shù)可以重載,因為一個類可以定義無參數(shù)或者參數(shù)個數(shù)不同的多個構(gòu)造函數(shù).析構(gòu)函數(shù)不可以重載,因為析構(gòu)函數(shù)沒有參數(shù),不可以像構(gòu)造函數(shù)一樣重載. 5.拷貝構(gòu)造函數(shù)的作用是什么?何時調(diào)用拷貝構(gòu)造函數(shù)? 答:拷貝構(gòu)造函數(shù)的作用是用一個已存在的對象去初始化創(chuàng)建一個同類的新對象.拷貝構(gòu)造函數(shù)一般在三種情況下調(diào)用:1.用已存在的對象去初始化創(chuàng)建同類的一個新對象.2.對象作為函數(shù)的參數(shù)3.函數(shù)的返回值為一個對象 6.分析下面的程序,寫出運行結(jié)果。 程序():#include <iostream.h

7、>class exapint x,y; public:exap(int a,int b)      x=a;y=b;      exap(exap &P)x=P.x; y=P.y;int set(int x1,int y1)          x=x1; y=y1;int geta() return a; int getb() return b;   void main() exap A(1,2); exap B=A;  

8、   cout<<"A="<<A.geta()<<","<<A.getb()<<endl;cout<<"B="<<B.geta()<<","<<B.getb()<<endl;B.set(10,20);cout<<"B="<<B.geta()<<","<<B.getb()<<end

9、l;運行結(jié)果:A=1,2B=1,2B=10,20Press any key to continue    程序(2):#include <iostream.h>#include <string.h>class example1public:       example1()                     cout<<"constructing exam

10、ple1."<<endl;           example1()                   cout<<"destructing example1."<<endl; ;class example2public:       example2()          

11、60;        cout<<"constructing example2 ."<<endl;         example2()                   cout<<"destructing example2."<<endl; ;void main()       exa

12、mple1 stu1;    example2 tea1;    cout<<"end in main"<<endl;運行結(jié)果:constructing example1.constructing example2 .end in maindestructing example2.destructing example1.Press any key to continue  7.設(shè)類A的定義如下:     class A     &

13、#160;     int a;               若用類A定義了兩個對象x1,x2,它們各自的數(shù)據(jù)成員a是同一個變量嗎?取值是否可以不同? 答:是同一個變量,因為創(chuàng)建對象通過構(gòu)造函數(shù)構(gòu)造的數(shù)據(jù)成員都是同一個變量,可以取值不同,調(diào)用構(gòu)造函數(shù)時若傳遞的實參不同或者無參數(shù)時,即可以使構(gòu)造的數(shù)據(jù)成員取值不同. 8.下面是一個產(chǎn)品類Product的定義。完成成員函數(shù)的定義,并用數(shù)據(jù)測試這個類。    class Product   

14、      char *name;      / 產(chǎn)品名稱      int  price;       / 產(chǎn)品單價      int  quantity;    / 剩余產(chǎn)品數(shù)量    public:      Product(char *n, int p, int q);      Product();  

15、60;   void buy( int money);  / 購買產(chǎn)品        void get () const;      / 返回剩余產(chǎn)品數(shù)量;答:#include<iostream.h>#include<string.h>class Productpublic:   Product(char *n,int p,int q);/構(gòu)造函數(shù)Product();/析構(gòu)函數(shù)     void get(); /顯示剩余產(chǎn)品數(shù)量&#

16、160;    void buy(int money); /購買產(chǎn)品  private:char name20; /產(chǎn)品名稱                     int price;     /產(chǎn)品單價                int quantity;/剩余產(chǎn)品數(shù)量  Product:Product(char *n,int p,int

17、q)strcpy(name,n);price=p;quantity=q;Product:Product()cout<<"析構(gòu)函數(shù)已調(diào)用"<<endl;void Product:get()cout<<"產(chǎn)品名稱:"<<name<<endl;cout<<"產(chǎn)品單價:"<<price<<endl;     cout<<"剩余數(shù)量:"<<quantity<<end

18、l;void Product:buy(int money)quantity-=money;cout<<"已購買"<<money<<"此產(chǎn)品"<<endl;void main()char n10;int p,q;cout<<"請輸入產(chǎn)品名稱,產(chǎn)品單價,產(chǎn)品數(shù)量:"<<endl;    cin>>n>>p>>q;Product P1(n,p,q);P1.get();int money;cout<<&q

19、uot;請輸入您要購買的數(shù)量:"<<endl;cin>>money;P1.buy(money);P1.get();  三、編程題1.#include<iostream.h>#include<string.h>class Cuboidint length;int width;int height;public:Cuboid(int l=0,int w=0,int h=0)length=l;width=w;height=h;Cuboid()cout<<"析構(gòu)函數(shù)已調(diào)用"<<endl;vo

20、id areavolume(int length,int width,int height);void Cuboid:areavolume(int length,int width,int height)int area,volume;area=(length*width+width*height+length*height)*2;volume=length*width*height;cout<<"表面積為"<<area<<endl;cout<<"體積為"<<volume<<endl

21、;void main()int l,w,h;cout<<"請輸入長方體的長,寬,高:"<<endl;cin>>l>>w>>h;Cuboid C1(l,w,h);C1.areavolume(l,w,h);#include<iostream.h>#include<string.h>class Cuboidint length;int width;int height;public:Cuboid(int l=0,int w=0,int h=0)length=l;width=w;height=h;C

22、uboid()cout<<"析構(gòu)函數(shù)已調(diào)用"<<endl;void areavolume();void Cuboid:areavolume()cout<<"表面積為"<<(length*width+width*height+length*height)*2<<endl;cout<<"體積為"<<length*width*height<<endl;void main()int l,w,h;cout<<"請輸入長方體的長,

23、寬,高:"<<endl;cin>>l>>w>>h;Cuboid C1(l,w,h);C1.areavolume();2.#include <iostream.h>class pointint x;int y;public:point(int xp=0,int yp=0)x=xp;y=yp;int getx()return x;int gety()return y;void main()int x,y;cout<<"請輸入x和y的值:"<<endl;cin>>x>&

24、gt;y;point p1(x,y);cout<<"x的值為"<<p1.getx()<<",y的值為"<<p1.gety()<<endl;4. #include<iostream.h>#include<string.h>#define N 20class Studentlong ID;char nameN;int score1;int score2;public:Student(long ID1,char *n,int s1,int s2);Student()cout&l

25、t;<"析構(gòu)函數(shù)已調(diào)用"<<endl;void display();void average();Student:Student(long ID1,char *n,int s1,int s2)ID=ID1;strcpy(name,n);score1=s1;score2=s2;void Student:display()cout<<"學(xué)生信息:"<<endl;cout<<"姓名:"<<name<<endl;cout<<"學(xué)號:"

26、<<ID<<endl;cout<<"成績1:"<<score1<<endl;cout<<"成績2:"<<score2<<endl;void Student:average()cout<<"該學(xué)生兩門課的平均成績?yōu)?"<<(score1+score2)/2<<endl;void main()long ID;char nameN;int score1;int score2;cout<<"請輸入學(xué)生姓名,學(xué)號,第一科目成績,第二科目成績:"<<endl;cin>>name>>ID>>score1>>score2;Student S1(ID,name,score1,score2);S1.display();S1.average();5.#include<iostream.h>#include<string.h>#define N 20class Studentlong ID;char nameN;int score1;int score2;public:S

溫馨提示

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

最新文檔

評論

0/150

提交評論