




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、第4章 類與對象一、選擇題1.標志著C+從面相過程變成面相對象的主要措施是(D)。 A.增加了新的運算符 B.允許函數重載,設置默認參數C.規定函數聲明必須用原型 D.引用了類和對象的概念2.有關類的說法錯誤的是(D)。 A.類是一種用戶自定義的數據類型B.只有類中的成員函數才能存取類中的私有數據C.在類中如果不作特殊說明,所指的數據均為私有類型D. 在類中如果不作特殊說明,所指的成員函數均為公有類型3.有關類和對象的說法錯誤的是(C)。A.對象是類的一個實例B.任何一個對象只能屬于一個具體的類C.一個類只能有一個對象D.類與對象的關系和數據類型與變量的關系相似4.下列關于構造函數的描述中,錯
2、誤的是(D)。A.構造函數的函數名與類名相同 B.構造函數可以重載C.構造函數可以設置默認參數 D.構造函數必須指定類型說明5.對任意一個類,析構函數的個數最多為(B)個。A.0 .B.1 C.2 D.46.通常拷貝構造函數的參數是(D)。 A.某個對象名 B.某個對象的成員名 C.某個對象的指針名 D.某個對象的引用名7.已知A是一個類,則執行語句A a;時,將自動調用該類的(B)。A.有參構造函數 B.無參構造函數C.拷貝構造函數 D.賦值構造函數二、簡答題1.比較C+中的結構(struct)和類(class)的概念的相同和不同之處。答:C+中的結構體與類的概念相同點:結構體與類都屬于用戶
3、自定義類型。C+中的結構體與類的概念不同點:結構體定義沒有函數部分,而類定義包含數據部分和函數部分。結構體定義中的數據都是公有權限,而類定義限制了成員的訪問權限。2.類中的公有(public)成員和私有(private)成員有什么區別?答:類中的共有成員既可以被類的成員函數訪問,也可以在類外的程序中通過對象被訪問;私有成員只能被該類的成員函數和友元函數來訪問。3.構造函數和析構函數的作用是什么?答:構造函數的作用是創建對象時為數據成員分配存儲空間并賦初值。析構函數的作用是在撤銷對象時清除并釋放內存空間。4.構造函數是否可以重載?為什么?析構函數呢?答:構造函數可以重載,因為對于不同的參數輸入需
4、要有相應的構造函數與之匹配;而析構函數不可以重載。5.拷貝構造函數的作用是什么?何時調用拷貝構造函數?答:拷貝構造函數的作用是對象之間的復制。拷貝構造函數在以下三種情況下會被復制:(1)用已經存在的對象去初始化創建同類的一個新對象。(2)對象作為函數的參數(將實參對象拷貝給形參對象)。(3)函數的返回值為一個對象(將返回對象拷貝給一個臨時對象)。6.分析下面的程序,寫出運行結果。程序():#include <iostream.h>class exapint x,y; public:exap(int a,int b) x=a;y=b; exap(exap &P)x=P.x;
5、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; cout<<"A="<<A.geta()<<","<<A.getb()<<endl;cout<<"B="<<B.geta()<<","<<B.getb()<<en
6、dl;B.set(10,20);cout<<"B="<<B.geta()<<","<<B.getb()<<endl;運行結果:1,21,210,20程序(2):#include <iostream.h>#include <string.h>class example1public:example1()cout<<"constructing example1."<<endl; example1()cout<<"
7、;destructing example1."<<endl;class example2public:example2()cout<<"constructing example2 ."<<endl; example2()cout<<"destructing example2."<<endl;void main()example1 stu1; example2 tea1; cout<<"end in main"<<endl;運行結果:const
8、ructing example1.constructing example2.destructing example2.destructing example1.end in main 7.設類A的定義如下: class A int a; 若用類A定義了兩個對象x1,x2,它們各自的數據成員a是同一個變量嗎?取值是否可以不同?答:它們各自的數據成員a不是同一個變量,在內存中占有各自不同的存儲空間,所以數值上是可以相同的。8.下面是一個產品類Product的定義。完成成員函數的定義,并用數據測試這個類。 class Product char *name; / 產品名稱 int price; /
9、產品單價 int quantity; / 剩余產品數量 public: Product(char *n, int p, int q); Product(); void buy( int money); / 購買產品 void get () const; / 返回剩余產品數量;答:Product類成員函數及主函數定義如下:Product: Product(char *n, int p, int q) name=new charstrlen(n)+1; strcpy(name,n); price=p; quantity=q; cout<<”constructing Product”&l
10、t;<name<<endl;Product: Product cout<<”destructing Product”<<name<<endl; delete name;void Product:buy(int money) if(quantity<=0) cout<<”sorry,the quantity is empty.”<<endl; return;int number=money/price;if (number>quantity) cout<<”the quantity”<&l
11、t;”that using”<<money<<”buys is not enough!”<<endl; return;else cout<<”the”<<money<<”buys”<<number<<” ”<<name<<endl; quantity-=number;void Product:get() constcout<<”the quantity is ”<<quantity<<endl;void main() Product cup
12、(“cup”,3,100);cup.buy(120);cup.get();三、編程題1.設計一個長方體類,有長、寬、高三個屬性,用成員函數計算;計算并輸出長方體的體積和表面積。#include <iostream.h>#include <string.h>class cuboid int length; int width; int high; public: cuboid(int l=0,int w=0,int h=0) length=l; width=w; high=h; int getv() return length*width*high;int gets()
13、return 2*(length*width+width*high+length*high);void print() cout<<”the cuboid is”<<length<<”*”<<width<<”*”<<high<<endl; cout<<”the volume is ”<<getv()<<endl;cout<<”the surface area is”<<gets()<<endl;void main() cuboid A(4,
14、5,6); A.print();2.設計一個點類point,有x,y兩個整型的數據成員,用成員函數返回x,y的值。使用測試程序驗證此類。#include <iostream.h>class point int x; int y;public: point(int px,int py) x=px y=py int getx() return x int gety()return y void main() point A(3,5); cout<<”the point is (”<<A.getx()<
15、<”,”<<A.gety()<<”)”<<endl;3.編寫一個程序,設計一個滿足以下要求的data類,其數據成員為year,month和day;類中有兩個公有函數setdate( )和display( ),前一個用來設置日期,后一個用來顯示日期。在主程序中定義該類的兩個對象,設置兩個具體日期,然后輸出這兩個日期。#include <iostream.h>class data int year; int month; int day; public: data(int y=1900,int m=1,int d=1) year=y; mont
16、h=m; day=d;void print() cout<<”the data is:”<<year<<”/”<<month<<”/”<<day<<endl;void addoneday() int a13=0,31,28,31,30,31,30,31,31,30,31,30,31; if(year%4=0&&year%100!=0)|(year%400=0) a2=29; if(day=amonth) if(month=12) year+; month=1; day=1;else month+
17、; day=1;else day+; cout<<”the day add one.”<<endl;void setdata(int y,int m,int d) year=y; month=m; day=d; cout<<”the data is reset.”<<endl;void main() data d1(1965,2,28); d1.print(); d1.addoneday(); d1.print();4.設計一個學生類,包括學號、姓名、兩門課成績,要求輸出學生數據,計算平均分。若在測試程序中定義了一個對象數組,對類的構造函數有什么
18、要求?#include <iostream.h>#include <string.h>class student int num;char *name;int score1,score2;public: student(int n,char *na,int s1,int s2) num=n; name=new charstrlen(na)+1; strcpy(name,na);score1=s1;score2=s2;student() num=0; name=0; score1=0; score2=0;void init(int n,char *na,int s1,in
19、t s2) num=n; name=new charstrlen(na)+1; strcpy(name,na);score1=s1;score2=s2;student() delete name;int getavg() return (score1+score2)/2;void print() cout<<”學生學號:”<<num<<” 姓名:”<<name<<”平均成績:”<<getavg()<<endl;void main() student x(1001,”王明”,79,82); x.print();5.在上題程序的基礎上,定義一個拷貝構造函數,進行學生類對象的復制,并在主函數中測試拷貝構造函數。#include <iostream.h>#include <string.h>class student int num;char *name;int sco
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 藝術品市場行業規范建設考核試卷
- 鐵路職工職業素養與維護意識培養考核試卷
- 突發心肌梗塞急救方法
- 器官移植麻醉核心要點
- 外科疼痛規范化管理與患者教育
- 外科切口護理
- 低體溫新生兒護理
- 2025年天然氣管道建設社會穩定風險評估與風險評估實踐總結與展望報告
- 2025年即時配送行業配送路徑優化與成本控制創新方案報告
- 2025年教育精準扶貧中的師資隊伍建設與提升路徑報告
- 禾川x3系列伺服說明書
- 細胞生物學(全套1047張課件)
- 架空乘人裝置專項設計(一采區運輸下山)
- 六年級下冊“快樂讀書吧”練習題試題及答案
- 手術部位感染目標性監測分析情況報告
- ★教導型組織-行動管理模式(三)
- 城市二次供水改造項目可行性研究報告
- 朗文英語2B英語復習資料
- 固定資產報廢情況說明
- 珠算三級四級試題
- 酒水采購合同15505
評論
0/150
提交評論