大學(xué)C++面向?qū)ο?015精彩試題及參考問題詳解_第1頁
大學(xué)C++面向?qū)ο?015精彩試題及參考問題詳解_第2頁
大學(xué)C++面向?qū)ο?015精彩試題及參考問題詳解_第3頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、重點(diǎn)大學(xué)考試試卷A卷2014 2015學(xué)年2學(xué)期面向?qū)ο蟪绦蛟O(shè)計(jì)課程閉卷時(shí)間120分鐘,學(xué)時(shí),學(xué)分,總分100分,占總評(píng)成績(jī)100% 年 月曰-試卷裝訂線題號(hào)-一-二二三四五六七八九十合計(jì)總分為20182438100得分-得分項(xiàng)rL 題 擇 先一、得分項(xiàng)選擇題每一小題2分,共20分,此題答案填入下表中裝訂線內(nèi)不要答題,不要填寫考生信息小題號(hào)12345678910答案1.在C+中,如下語句,錯(cuò)誤的答案是 ()A. i nt a(3);B. i nt a3;C. i nt &a;D. i nt *a;2.派生類中的成員函數(shù)可以直接訪問基類的()A.公有成員B.私有成員C.公有和保護(hù)成員D.

2、保護(hù)成員3.在C+沖,關(guān)于類的析構(gòu)函數(shù),正確的說法是。試卷裝訂線A.能帶形式參數(shù)B .函數(shù)體中必須有 delete 語句C.可以被重載D.無形參,也不可重載4. 一個(gè)類擁有多個(gè)構(gòu)造函數(shù),如此這些構(gòu)造函數(shù)之間為。A.重復(fù)關(guān)系B.拷貝關(guān)系C.重載關(guān)系D.繼承關(guān)系5. C+中聲明常量的關(guān)鍵字是。A. extern B . const Cpublic Dvolatile6. 一個(gè)函數(shù)功能不太復(fù)雜,但要求被頻繁調(diào)用,該函數(shù)應(yīng)該設(shè)計(jì)成()7.以下基類中的成員函數(shù)表示純虛函數(shù)的是。A. virtual void tt()=0; B. void tt(i nt)=0;C. virtual void tt(in

3、t); D. virtual void tt(int)p的基類成員a,其中a是 。公有繼承的私有成員;私有繼承的公有成員。8. 可以在類外用p.a的形式訪問派生類對(duì)象A. 公有繼承的公有成員;BC.公有繼承的保護(hù)成員;D9. 對(duì)于任意一個(gè)類,析構(gòu)函數(shù)的個(gè)數(shù)最多為10. 對(duì)于在類中定義的靜態(tài)數(shù)據(jù)成員count,下面正確的說法是A. count在類外進(jìn)展初始化B. 該類的每個(gè)對(duì)象中都有一個(gè)獨(dú)立的靜態(tài)數(shù)據(jù)成員cou ntC. 對(duì)象創(chuàng)建時(shí)產(chǎn)生 countD. count既可以在類外初始化,也可以在類內(nèi)初始化得分B)10、改錯(cuò)題(共3小題,每一小題6分,共18分)1. error C2440: '

4、;type cast' : cannot convert from 'class plex' to 'float',No user-defined-conversion operator available that can perform this conversion,請(qǐng)指出錯(cuò)誤原因并改正。#in clude <iostream>using n amespace std;class plexpublic:plex(float r=0,float i=0)real=r; imag=i; void prin t()cout<<&#

5、39;('<<real<<','<<imag<<')'<<e ndl; private:float real,imag;int mai n() plex a(2.2f,4.4f);a.pri nt();cout<<float (a) *0.5<<e ndl;return 0;2. 下面的程序在 VC6.0 上編譯提示 error C2662:'getX' : cannot convert 'this' pointer from 'c

6、onst class CTest' to 'class CTest &,請(qǐng)指出錯(cuò)誤原因并改正。#in clude<iostream>using n amespace std;class CTestprivate:int x;public:CTest(i nt x) this->x = x; in t getX() return x; ;int mai n() const CTest obj(5);cout<<obj.getX()<<e ndl;return 0;3. 下面的程序在 VC6.0上編譯無錯(cuò),運(yùn)行結(jié)果出錯(cuò):“ p1:葺

7、葺葺葺葺葺葺葺,請(qǐng)指出錯(cuò)誤原 因并改正。#in clude <iostream>#in clude<cstri ng>using n amespace std;class STRING public:STRING(char *s)ptr= new charstrle n( s)+1;strcpy(ptr,s);STRING()delete ptr; void prin t()cout<<ptr<<e ndl; private:char *ptr;int mai n() STRING p1("book");STRING p2(&

8、quot;pe n");p1=p2;cout<<"p1:"p1.pri nt();return 0;-試卷裝訂線得分讀程序,給出程序的輸出結(jié)果每一小題6分,共24分裝訂線內(nèi)不要答題,不要填寫考生信息試卷裝訂線【1】#include <iostream>using n amespace std;class Pers on public:Pers on() cout<<"C on structor of Pers on"<<en dl; Pers on() cout<<"Dest

9、ructor of Pers on"<<en dl; ;class Stude nt: public Personpublic:Student() cout<<"Constructor of Student"<<endl; Student() cout<<"Destructor of Student"<<endl; ;class Teacher: public Personpublic:Teacher() cout<<"C on structor of Teach

10、er"<<e ndl; Teacher() cout<<"Destructor of Teacher"<<e ndl; ;int mai n()Stude nt s;Teacher t;return 0;-試卷裝訂線裝訂線內(nèi)不要答題,不要填寫考生信息試卷裝訂線2 #i nclude <iostream>using n amespace std;class A public:A (int i) x = i; void dispa () cout << x << "," pri

11、vate :int x ;class B : public A public:B(i nt i) : A(i+10) x = i; void dispb() dispa(); cout << x << en dl; private :int x ;int mai n()B b(2);b.dispb();return 0;【3】 #in clude<iostream.h>class Count static int count ;public:Count( ) cout<<co un t+ ;static int Getc( ) retur n c

12、ount ; Count( ) count- ;int Coun t:co unt = 5;int mai n() Cou nt c1,c2,c3,c4 ;cout<<Co un t:Getc( )<<e ndl; return 0;【4】#include<iostream>using n amespace std;class MyClasspublic:MyClass(int i=0)cout<<1;MyClass(co nst MyClass &x)cout<<2;MyClass & operator=(co ns

13、t MyClass &x)cout<<3; return *this;MyClass()cout<<4;int mai n()MyClass obj1(1),obj2(2),obj3(obj1);obj1=obj2;return 0;得分四、編程題共38分(12 分)1 下面是一個(gè)類的測(cè)試程序,請(qǐng)?jiān)O(shè)計(jì)出能使用如下測(cè)試程序的類。int mai n() Test a;a.i ni t(68,55);a.pri nt();return 0;其執(zhí)行結(jié)果為:測(cè)試結(jié)果:68-55=132 請(qǐng)為fraction 類分?jǐn)?shù)類定義如下重載運(yùn)算符函數(shù)注意函數(shù)原型(12分)加法運(yùn)算+。

14、賦值運(yùn)算=提取運(yùn)算。-試卷裝訂線class fraction private:int fz; /分子int fm; /分母public:;裝訂線內(nèi)不要答題,不要填寫考生信息試卷裝訂線3創(chuàng)建一個(gè)表示雇員信息的employee類,其中包含數(shù)據(jù)成員 name empNo和salary ,分別表示雇員的某某、編號(hào)和月薪。再?gòu)膃mployee類派生出3個(gè)類worker、technician和salesman,分別代表普通工人、科研人員、銷售人員。三個(gè)類中分別包含數(shù)據(jù)成員 productNum、workHours和monthlysales ,分別代表工人每月生產(chǎn)產(chǎn)品的數(shù)量、科研人 員每月工作的時(shí)數(shù)和銷售人

15、員每月的銷售額。要求各類中都包含成員函數(shù)pay,用來計(jì)算雇員的月薪,并假定:普通工人的月薪=每月生產(chǎn)的產(chǎn)品數(shù)x每件產(chǎn)品的贏利x20%科研人員的月薪=每月的工作時(shí)數(shù)x每小時(shí)工作的酬金銷售人員的月薪=月銷售額X銷售額提成。(14分)重點(diǎn)大學(xué)考試試題答案A卷20142015學(xué)年2學(xué)期面向?qū)ο蟪绦蛟O(shè)計(jì)課程一、 單項(xiàng)選擇題每一小題2分,共20分CCDCB AAABA二、改錯(cuò)題洪3小題,每一小題6分,共18分)1. main()函數(shù)第3句出錯(cuò),因?yàn)轭?plex無類類型轉(zhuǎn)換函數(shù),將plex對(duì)象轉(zhuǎn)換成float。改正:在plex中類增加:operator float() retur n real; 2. ma

16、in()函數(shù)第2句出錯(cuò),因?yàn)槌?duì)象 obj不能調(diào)用非常成員函數(shù),改正:在 CTest 類中:將 CTest(i nt x)換成 fCTest(i nt x) con st。或者將 mai n()中 con st CTest obj(5);換成 CTest obj(5);3. 當(dāng)程序執(zhí)行對(duì)象p2的析構(gòu)函數(shù)時(shí),對(duì)象 p1的數(shù)據(jù)成員ptr出現(xiàn)了所謂的“指針懸掛問題,這說明C+中提供應(yīng)對(duì)象的默認(rèn)的賦值運(yùn)算符并不是萬能的,解決的方法就是重載賦值運(yùn)算符“=,使對(duì)象不但能淺拷貝,還能實(shí)現(xiàn)深層拷貝。STRING & STRING:operator=(co nst STRING &s)if(th

17、is=&;s)return *this;delete ptr;ptr=new charstrle n( s.ptr)+1;strcpy(ptr,s.ptr);return *this;三、讀程序,給出程序的輸出結(jié)果。每一小題6分,共24分【1 】 Co nstructor of PersonCon structor of Stude ntCon structor of Pers onCon structor of TeacherDestructor of TeacherDestructor of Pers onDestructor of Stude ntDestructor of Pe

18、rs on【2】12,2【3】56789【4】1123444四、編程題共38分=12分+ 12分+14分1. 解答:#include<iostream.h>class Test 4 分 int x,y;public:void in it(i nt,i nt);void prin t();void Test:init(int i,int j) 4 分 x=i;y=j;void Test:print() 4分 cout<<"測(cè)試結(jié)果:"<<x<<"-"<<y<<"="

19、<<x-y<<e ndl;2. 解答: fraction operator +(fraction & f1,fraction & f2) 4分int nfz = f1.fz*f2.fm+f1.fm*f2.fz;int nfm = f1.fm*f2.fm;return fractio n(nfz,n fm); fracti on & fraction :operator =(fract ion & f) 4分fz=f.fz;fm=f.fm;return *this; istream & operator >>(istre

20、am & is,fracti on & f) 4分is>>f.fz>>f.fm;return is;3. 解答:#include<iostream>using n amespace std;class employee 4分protected:char n ame20;int empNo;float salary;public:employee(char *cp="李強(qiáng)",int no=1001); employee(employee&);void set name(char *cp);void setempNo(

21、i nt no);void setsalary(float sa);char*get name();int getempNo();float getsalary();void display();employee psalary(employee *emp);employee:employee(char *cp,i nt no) int i=0;while(*cp)n amei=*cp;i+;cp+;namei='0'empN o=no;employee:employee(employee & em) int i=0;while(em. namei)n amei=em

22、.n amei;i+;namei='0:empNo=em.empNo;salary=em.salary;void employee:set name(char *cp) int i=0;while(*cp) n amei=*cp;i+;cp+;namei='0'void employee:setempNo(i nt no)empN o=no;void employee:setsalary(float sa) salary=sa;char*employee:get name() return n ame;int employee:getempNo() return emp

23、No;float employee:getsalary() retur n salary;void employee:display() cout<<"工號(hào)為"<<empNo<<"的雇員"<<name<<"的月薪為"<<salary<<endl;employee employee:psalary(employee *emp)if(this->salary>=emp->salary)return *this;elsereturn *em

24、p;class worker : public employeepublic:worker(char*,i nt,i nt);void setproductNum(i nt n) productN um=n;int getproductNum() retur n productNum;void pay();private:int productNum;static float proper;每件產(chǎn)品的利潤(rùn);float worker:proper=20;/假設(shè)每件產(chǎn)品的利潤(rùn)為20元worker:worker(char *n ame,i nt n o,i nt pronum):employee(

25、name ,no)productNum=pr onum;void worker:pay()salary=productNum*proper*0.2;class tech nicia n : public employeepublic:tech nician( char*,i nt,float);void sethours(float h) workHours=h;float gethours() return workHours;void pay();private:float workHours;static float payperhour;科研人員每小時(shí)工作的酬金40元;float tech ni cia n:payperhour=40;/假設(shè)科研人員每小時(shí)工作的酬金為tech ni cia n:tech nici

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論