大学C++面向对象2015试题及参考答案.doc_第1页
大学C++面向对象2015试题及参考答案.doc_第2页
大学C++面向对象2015试题及参考答案.doc_第3页
大学C++面向对象2015试题及参考答案.doc_第4页
大学C++面向对象2015试题及参考答案.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

; 试卷装订线 装订线内不要答题,不要填写考生信息试卷装订线 姓 名学 号专业班级学院重点大学考试试卷(A卷)2014 2015 学年 2 学期 面向对象程序设计课程 闭卷 时间120分钟, 学时, 学分,总分100分,占总评成绩100% 年 月 日题号一二三四五六七八九十合计满分20182438100得分得分 一、单选题(每小题2分,共20分,本题答案填入下表中)小题号12345678910答案1. 在C+中,下列语句,错误的是( )。A. int a(3); B. int a3;C. int &a; D. int *a;2. 派生类中的成员函数可以直接访问基类的( ) 。A. 公有成员 B. 私有成员C. 公有和保护成员 D. 保护成员3. 在C+中,关于类的析构函数,正确的说法是( )。A能带形式参数 B函数体中必须有 delete 语句C可以被重载 D无形参,也不可重载4. 一个类拥有多个构造函数,则这些构造函数之间为( )。A重复关系 B拷贝关系C重载关系 D继承关系5. C+中声明常量的关键字是( )。Aextern Bconst Cpublic Dvolatile6. 一个函数功能不太复杂,但要求被频繁调用,该函数应该设计成( ) 。A.内联函数 B.重载函数C.递归函数 D.嵌套函数7. 以下基类中的成员函数表示纯虚函数的是( )。A. virtual void tt()=0; B. void tt(int)=0; C. virtual void tt(int); D. virtual void tt(int). 8. 可以在类外用p.a的形式访问派生类对象p的基类成员a,其中a是( )。A公有继承的公有成员; B公有继承的私有成员;C公有继承的保护成员; D私有继承的公有成员。 9. 对于任意一个类,析构函数的个数最多为( ) A.0 B.1 C.2 D.310. 对于在类中定义的静态数据成员count,下面正确的说法是( ) Acount在类外进行初始化 B该类的每个对象中都有一个独立的静态数据成员countC. 对象创建时产生count Dcount既可以在类外初始化,也可以在类内初始化得分B)10,10 C)20,10 D)20,20得分二、改错题(共3小题,每小题6分,共18分)1. 下面的程序在VC6.0上编译提示error C2440: type cast : cannot convert from class Complex to float,No user-defined-conversion operator available that can perform this conversion,请指出错误原因并改正。#include using namespace std;class Complexpublic:Complex(float r=0,float i=0)real=r;imag=i; void print()cout(real,imag)endl;private:float real,imag;int main()Complex a(2.2f, 4.4f);a.print();coutfloat(a)*0.5endl;return 0;2. 下面的程序在VC6.0上编译提示error C2662: getX : cannot convert this pointer from const class CTest to class CTest &,请指出错误原因并改正。#includeusing namespace std;class CTestprivate: int x; public: CTest(int x) this-x = x; int getX() return x; ;int main() const CTest obj(5); coutobj.getX()endl; return 0;3. 下面的程序在VC6.0上编译无错,运行结果出错:“p1:葺葺葺葺葺葺葺葺”,请指出错误原因并改正。#include #includeusing namespace std;class STRING public:STRING(char *s)ptr=new charstrlen(s)+1;strcpy(ptr,s);STRING() delete ptr; void print() coutptrendl; private:char *ptr; int main() STRING p1(book); STRING p2(pen); p1=p2; coutp1:; p1.print(); return 0;得分三、读程序,给出程序的输出结果。(每小题6分,共24分)【1】#include using namespace std;class Personpublic: Person() coutConstructor of Personendl; Person() coutDestructor of Personendl; ;class Student: public Personpublic: Student() coutConstructor of Studentendl; Student() coutDestructor of Studentendl; ;class Teacher: public Personpublic: Teacher() coutConstructor of Teacherendl; Teacher() coutDestructor of Teacherendl; ;int main() Student s; Teacher t; return 0;试卷装订线 装订线内不要答题,不要填写考生信息试卷装订线 试卷装订线 装订线内不要答题,不要填写考生信息试卷装订线 【2】#include 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 main() B b(2); b.dispb(); return 0;【3】#include class Count static int count ; public: Count( ) coutcount+ ; static int Getc( ) return count ; Count( ) count-; ; int Count:count = 5; int main() Count c1,c2,c3,c4 ; coutCount:Getc( )endl; return 0; 【4】#include using namespace std;class MyClass public: MyClass(int i=0)cout1;MyClass(const MyClass&x)cout2;MyClass& operator=(const MyClass&x)cout3; return *this; MyClass()cout。class fraction private:int fz; /分子int fm; /分母public:;3.创建一个表示雇员信息的employee类,其中包含数据成员name、empNo和salary,分别表示雇员的姓名、编号和月薪。再从employee类派生出3个类worker、technician和salesman,分别代表普通工人、科研人员、销售人员。三个类中分别包含数据成员productNum、workHours和monthlysales,分别代表工人每月生产产品的数量、科研人员每月工作的时数和销售人员每月的销售额。要求各类中都包含成员函数pay,用来计算雇员的月薪,并假定:普通工人的月薪=每月生产的产品数每件产品的赢利20%科研人员的月薪=每月的工作时数每小时工作的酬金 销售人员的月薪=月销售额销售额提成。 (14分) 重点大学考试试题答案(A卷)2014 2015 学年 2 学期 面向对象程序设计课程一、单选题(每小题2分,共20分)CCDCB AAABA二、改错题(共3小题,每小题6分,共18分)1. main()函数第3句出错,因为类Complex无类类型转换函数,将Complex对象转换成float。改正:在Complex中类增加:operator float() return real; 2. main()函数第2句出错,因为常对象obj不能调用非常成员函数,改正:在CTest类中:将CTest(int x)换成fCTest(int x) const。或者将main()中const CTest obj(5);换成CTest obj(5);3. 当程序执行对象p2的析构函数时,对象p1的数据成员ptr出现了所谓的“指针悬挂问题”,这说明C+中提供给对象的默认的赋值运算符并不是万能的,解决的办法就是重载赋值运算符“=”,使对象不但能浅拷贝,还能实现深层拷贝。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;三、读程序,给出程序的输出结果。(每小题6分,共24分)【1】 Constructor of PersonConstructor of StudentConstructor of PersonConstructor of TeacherDestructor of TeacherDestructor of PersonDestructor of StudentDestructor of Person【2】12,2【3】56789【4】1123444 四、编程题(共38分=12分+12分+14分)1. 解答:#includeclass Test 4分 int x,y; public: void init(int,int); void print();void Test:init(int i,int j) 4分 x=i;y=j;void Test:print() 4分 cout测试结果:x-y=x-y(istream & is,fraction & f) 4分isf.fzf.fm;return is;3.解答:#includeusing namespace std;class employee 4分protected:char name20;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,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:setempNo(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的月薪为salarysalary=emp-salary)return *this;else return *emp;class worker : public employee 4分public:worker(char*,int,int);void setproductNum(int n) productNum=n;int getproductNum() return productNum;void pay();private:int productNum;static float proper; /每件产品的利润;float worker:proper=20; /假设每件产品的利润为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 employee 4分public: technician(char*,int,float);void sethours(float h) workHours=h;float gethours() return workHours;void pay();private:float workHours;static float payperhour; /科研人员每小时工作的酬金; float technician:payperhour=40; /假设科研人员每小时工作的酬金为40元technician:technician(char *name,int no,float hours) :employee(name,no)workHours=hours;void

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论