大學C++面向?qū)ο蟪绦蛟O(shè)計-試題及參考答案_第1頁
大學C++面向?qū)ο蟪绦蛟O(shè)計-試題及參考答案_第2頁
大學C++面向?qū)ο蟪绦蛟O(shè)計-試題及參考答案_第3頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、重點大學考試試卷A卷20222022學年1學期面向?qū)ο蟪绦蛟O(shè)計課程閉卷-試卷裝訂線裝訂線內(nèi)不要答題,不要填寫考生信息試卷裝訂線名姓號學級班業(yè)專院學題號-一-二二二四五六七八九十合計總分值20182438100得分時間120分鐘,學時, 學分,總分100分,占總評成績100% 年 月曰2分,共20分,此題答案填入下表中得分單項選擇題每題1. 在C+中,以下語句,錯誤的選項是。A. int a3;B. int a3;C. int &a;D. int *a;2. 派生類中的成員函數(shù)可以直接訪問基類的。A.公有成員B.私有成員C.公有和保護成員D.保護成員3. 在C+中,關(guān)于類的析構(gòu)函數(shù),正確

2、的說法是A.能帶形式參數(shù)B.函數(shù)體中必須有delete語句C.可以被重載D.無形參,也不可重載4. 一個類擁有多個構(gòu)造函數(shù),那么這些構(gòu)造函數(shù)之間為A.重復關(guān)系B.拷貝關(guān)系C.重載關(guān)系D.繼承關(guān)系5. C+中聲明常量的關(guān)鍵字是。A. externB. const C. publicD. volatile6. 一個函數(shù)功能不太復雜,但要求被頻繁調(diào)用,該函數(shù)應該設(shè)計成7.以下基類中的成員函數(shù)表示純虛函數(shù)的是A. virtual void tt()=0 ; B. void tt(int)=0 ;C. virtual void tt(int) ; D. virtual void tt(int)8. 可以

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

4、rom 'class Complex' to 'float',No user-defined-conversion operatoravailable that can perform this conversion,請指出錯誤原因并改正。#include <iostream>using namespace std;class Complex public:Complex(float r=0,float i=0)real=r; imag=i; void print()coutvv'('vvrealvv','vvimag

5、vv')'vve ndl; private:float real,imag;;int main() Complex a(2.2f, 4.4f);a.print();coutvvfloat(a)*0.5vvendl;return 0;2. 下面的程序在 VC6.0 上編譯提示 error C2662: 'getX' : cannot convert 'this' pointer from 'const class CTest' to 'class CTest &,請指出錯誤原因并改正。#include<iost

6、ream>using namespace std;class CTestprivate:int x;public:CTest(int x) this->x = x; int getX() return x; ;int main() const CTest obj(5);cout<vobj.getX()vvendl;return 0;3. 下面的程序在 VC6.0上編譯無錯,運行結(jié)果出錯:“ pl :葺耳耳耳耳耳耳葺 ,請指岀錯誤原因并改正。 #include <iostream>#include<cstring>using namespace std;

7、class STRING public:STRING(char *s)ptr=new charstrlen(s)+1;strcpy(ptr,s);STRING() delete ptr; void print() coutvvptrvvendl; private:char *ptr;int main() STRING p1("book"); STRING p2("pen");P仁 p2;coutvv"p1:"p1.print();return 0;得分三、讀程序,給岀程序的輸岀結(jié)果。每題6分,共24分【1 】#include <

8、iostream>using namespace std;class Personpublic:Person() cout<v"Constructor of Person"<<endl; Person() cout<v"Destructor of Person"<<endl; ;class Student: public Personpublic:Student() cout<<"Constructor of Student"<<endl; Student() cout

9、<<"Destructor of Student"<<endl; ;class Teacher: public Personpublic:Teacher() cout<<"Constructor of Teacher"<<endl; Teacher() cout<<"Destructor of Teacher"<<endl; ;int main()Student s;Teacher t;return 0;-試卷裝訂線裝訂線內(nèi)不要答題,不要填寫考生信息試卷裝訂線-試

10、卷裝訂線裝訂線內(nèi)不要答題,不要填寫考生信息【2 】#include <iostream>using namespace std;class A public:A (int i) x = i; void dispa () cout << x << "," private : int x ;class B : public A public:B(int i) : A(i+10) x = i; void dispb() dispa(); cout << x << endl; private :int x ;int mai

11、n()B b(2);b.dispb(); return 0;【3】#include<iostream.h>class Count static int count ;public:Count( ) cout<<count+ ; static int Getc( ) return count ; Count( ) count-;int Count:count = 5;int main() Count c1,c2,c3,c4 ; cout<<Count:Getc( )<<endl; return 0;試卷裝訂線【4】#include<iostr

12、eam>using namespace std;class MyClasspublic:MyClass(int i=0)cout<<1;MyClass(const MyClass &x)cout<<2;MyClass & operator=(const MyClass &x)cout<<3; return *this;MyClass()cout<<4;int main()MyClass obj1(1),obj2(2),obj3(obj1);obj1=obj2;return 0;得分四、編程題共38分(12 分)1 下

13、面是一個類的測試程序,請設(shè)計岀能使用如下測試程序的類。int main() Test a; a.init(68,55);a.print();return 0;其執(zhí)行結(jié)果為: 測試結(jié)果:68-55=132請為fraction類分數(shù)類定義以下重載運算符函數(shù)注意函數(shù)原型12分加法運算+。賦值運算=。提取運算。class fraction private:int fz; / 分子int fm; / 分母public:V.試卷裝訂線裝訂線內(nèi)不要答題,不要填寫考生信息試卷裝訂線3.創(chuàng)立一個表示雇員信息的employee類,其中包含數(shù)據(jù)成員 name、empNo和salary,分別表示雇員的、編號和月薪。再

14、從 employee類派生出3個類 worker、technician和salesman,分別代表普通工人、科 研人員、銷售人員。三個類中分別包含數(shù)據(jù)成員productNum、workHours和monthlysales,分別代表工人每月生產(chǎn)產(chǎn)品的數(shù)量、科研人員每月工作的時數(shù)和銷售人員每月的銷售額。要求各類中都包含成員 函數(shù)pay,用來計算雇員的月薪,并假定:普通工人的月薪=每月生產(chǎn)的產(chǎn)品數(shù)X每件產(chǎn)品的贏利X20%科研人員的月薪=每月的工作時數(shù)x每小時工作的酬金銷售人員的月薪=月銷售額x銷售額提成。14分重點大學考試試題答案 A 卷2022 2022 學年 1 學期 面向?qū)ο蟪绦蛟O(shè)計課程一、單

15、項選擇題每題 2 分,共 20 分CCDCB AAABA二、改錯題 (共 3小題,每題 6 分,共 18分)1. main()函數(shù)第3句出錯,因為類 Complex無類類型轉(zhuǎn)換函數(shù),將Complex對象轉(zhuǎn)換成float。改正:在 Complex中類增加:operator float() return real; 2. main()函數(shù)第2句出錯,因為常對象 obj不能調(diào)用非常成員函數(shù),改正:在 CTest類中:將 CTest(int x換成 fCTest(int x) const?;蛘邔?main()中 const CTest obj(5)換成 CTest obj(5);3. 當程序執(zhí)行對象p

16、2的析構(gòu)函數(shù)時,對象 p1的數(shù)據(jù)成員ptr出現(xiàn)了所謂的“指針懸掛問題,這說明C+中提供給對象的默認的賦值運算符并不是萬能的,解決的方法就是重載賦值運算符“=,使對象不但能淺拷貝,還能實現(xiàn)深層拷貝。STRING& STRING:operator=(const STRING&s)if(this=&s)return *this;delete ptr;ptr=new charstrlen(s.ptr)+1;strcpy(ptr,s.ptr);return *this;三、讀程序,給出程序的輸出結(jié)果。 每題 6分,共 24 分【1 】 Constructor of PersonC

17、onstructor of StudentConstructor of PersonConstructor of TeacherDestructor of TeacherDestructor of PersonDestructor of StudentDestructor of Person【2】12,2【3】56789【4】 1 1 23444四、編程題共 38 分=12 分+12 分+14 分1. 解答: #include<iostream.h>class Test4 分 int x,y;public:void init(int,int);void print();void T

18、est:i nit(i nt i,i nt j) 4分 x=i;y=j;void Test:print() 4 分cout<<" 測試結(jié)果: "<<x<<"-"<<y<<"="<<x-y<<endl;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

19、 fraction(nfz,nfm); fraction & fraction:operator =(fraction & f)4分fz=f.fz;fm=f.fm;return *this; istream & operator >>(istream & is,fraction & f)4分is>>f.fz>>f.fm; return is;3. 解答: #include<iostream>using namespace std;class employee4 分protected:char name20;

20、int empNo;float salary;public:employee(char *cp="李強",int no=1001); employee(employee&); void setname(char *cp); void setempNo(int no);void setsalary(float sa); char*getname();int getempNo();float getsalary(); void display(); employee compsalary(employee *emp);employee:employee(char *cp

21、,int no) int i=0;while(*cp)namei=*cp;i+;cp+;namei='0'empNo=no;employee:employee(employee &em) int i=0;while(i) namei=i;i+;namei='0'empNo=em.empNo;salary=em.salary;void employee:setname(char *cp) int i=0;while(*cp) namei=*cp;i+;cp+;namei='0'void employee:sete

22、mpNo(int no) empNo=no;void employee:setsalary(float sa) salary=sa;char*employee:getname() return name;int employee:getempNo() return empNo;float employee:getsalary() return salary;void employee:display() cout<<" 工號為 "<<empNo<<" 的雇員 "<<name<<" 的月

23、薪為 "<<salary<<endl; employee employee:compsalary(employee *emp)if(this->salary>=emp->salary)return *this;elsereturn *emp;class worker : public employee4分public:worker(char*,int,int);void setproductNum(int n) productNum=n;int getproductNum() return productNum; void pay();pri

24、vate:int productNum;static float proper;/ 每件產(chǎn)品的利潤;float worker:proper=20;/ 假設(shè)每件產(chǎn)品的利潤為20 元worker:worker(char *name,int no,int pronum):employee(name,no) productNum=pronum;void worker:pay() salary=productNum*proper*0.2;class technician : public employee4 分public: technician(char*,int,float); void setho

25、urs(float h) workHours=h;float gethours() return workHours; void pay();private:float workHours;static float payperhour; / 科研人員每小時工作的酬金40 元;float technician:payperhour=40; / 假設(shè)科研人員每小時工作的酬金為 technician:technician(char *name,int no,float hours):employee(name,no) workHours=hours;void technician:pay() salary=workHours*payperhour;

溫馨提示

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

評論

0/150

提交評論