1、私有成员保护成员3.在C+中,关于类的析构函数,正确的说法是( A.能带形式参数 BC.可以被重载 D4.一个类拥有多个构造函数,A.重复关系.函数体中必须有 delete.无形参,也不可重载则这些构造函数之间为(.拷贝关系 语句5.C.重载关系C+冲声明常量的关键字是A. extern B . const试卷装订线6.C . public D一个函数功能不太复杂,但要求被频繁调用,该函数应该设计成A.内联函数 B. 重载函数C.递归函数嵌套函数.volatile7.以下基类中的成员函数表示纯虚函数的是(A. virtual void tt()=O; B. void tt(i nt)=0C.
2、virtual void tt(i nt)D. virtual void tt(i nt)8.p的基类成员a,其中a是( )。公有继承的私有成员;私有继承的公有成员。)F面正确的说法是( )可以在类外用p.a的形式访问派生类对象A.公有继承的公有成员; BC.公有继承的保护成员; D9.对于任意一个类,析构函数的个数最多为(A.0 B.1 C.2 D.310.对于在类中定义的静态数据成员 count :A. count在类外进行初始化B. 该类的每个对象中都有一个独立的静态数据成员 countC.对象创建时产生 countB)10、改错题(共3小题,每小题count既可以在类外初始化,也可以在
3、类内初始化6分,共18分)1.下面的程序在 VC6.0 上编译提示 error C2440: type cast : cannot convert from class Complex to float,No user-defi ned-con version operator available that can perform this conversion ,请指出错误原因并改正。#in clude using n amespace std;class Complexpublic:Complex(float r=0,float i=0)real=r; imag=i; void prin
4、t()cout(real,imag)e ndl; private:float real,imag;int mai n() Complex a(2.2f, 4.4f);a.pri nt();coutfloat (a) *0.5return 0;2.下面的程序在 VC6.0 上编译提示 error C2662:getX cannot convert this pointer from const class CTestclass CTest &,请指出错误原因并改正。#in cludex = x;in t getX() return x; const CTest obj(5);obj.getX()
5、class STRING STRING(char *s)ptr= new charstrle n( s)+1;strcpy(ptr,s);STRING() delete ptr; coutptrprivate:char *ptr; STRING p1(book); STRING p2(pe np仁 p2;p1:pl.pri nt();读程序,给出程序的输出结果(每小题6分,共24分)【1】#include class Pers on public:Pers on() coutC on structor of Pers onen dl; Pers on() coutDestructor of P
6、ers on ;class Stude nt: public PersonStudent() coutConstructor of Studentendl; Student() coutDestructor of Studentclass Teacher:Teacher() coutC on structor of Teacher Teacher() coutDestructor of Teacherint mai n()Stude nt s;Teacher t;【2】 #i nclude class A public:A (int i) x = i;void dispa () cout x
7、,private :int x ;class B : public AB(i nt i) : A(i+10) x = i;void dispb() dispa(); cout en dl; private :B b(2);b.dispb();【3】 #in cludeclass Count static int count ;Count( ) coutco un t+ ;static int Getc( ) retur n count ; Count( ) count- ;int Coun t:co unt = 5;int mai n() Cou nt c1,c2,c3,c4 ;Co un t
8、:Getc( ) return 0;【4】#includeclass MyClassMyClass(int i=0)cout1;MyClass(co nst MyClass &x)cout2;MyClass & operator=(co nst MyClass &3; return *this; MyClass()cout4;MyClass obj1(1),obj2(2),obj3(obj1);obj1=obj2;四、编程题(共38分)1 下面是一个类的测试程序,请设计出能使用如下测试程序的类。 (12分) Test a;a.i ni t(68,55);其执行结果为:测试结果:68-55=13
9、(12 分)2 请为fraction 类(分数类)定义下列重载运算符函数(注意函数原型) 加法运算+。赋值运算=提取运算。class fraction private:试 卷 装 订 线int fz; / 分子int fm; / 分母3创建一个表示雇员信息的 employee类,其中包含数据成员 name empNo和salary ,分别表示雇员的姓名、编号和月薪。再从 employee类派生出3个类worker、technician和salesman,分别代表普通工人、科研人员、销售人员。三个类中分别包含数据成员 productNum、workHours和monthlysales ,分别代表
10、工人每月生产产品的数量、科研人 员每月工作的时数和销售人员每月的销售额。要求各类中都包含成员函数 pay,用来计算雇员的月薪,并假定:普通工人的月薪=每月生产的产品数x每件产品的赢利x 20%科研人员的月薪=每月的工作时数x每小时工作的酬金销售人员的月薪=月销售额X销售额提成。 (14 分)重点大学考试试题答案(A卷)2014 2015学年2学期面向对象程序设计课程一、 单选题(每小题 2分,共20分)CCDCB AAABA二、 改错题 洪3小题,每小题6分,共18分)1.main()函数第3句出错,因为类 Complex无类类型转换函数,将 Complex对象转换成float。改正:在Com
11、plex中类增加:operator float() retur n real;2.main()函数第2句出错,因为常对象 obj不能调用非常成员函数,在 CTest 类中:将 CTest(i nt x)换成 fCTest(i nt x) con st。或者将 mai n()中 con st CTest obj(5);换成 CTest obj(5);3.当程序执行对象p2的析构函数时,对象 p1的数据成员ptr出现了所谓的“指针悬挂问题” ,这说明C+中提供给对象的默认的赋值运算符并不是万能的,解决的办法就是重载赋值运算符“ =”,使对象不但能浅拷贝,还能实现深层拷贝。STRING & STRI
12、NG:operator=(co nst STRING &s)if(this=&;s)return *this;delete ptr;ptr=new charstrle n( s.ptr)+1;strcpy(ptr,s.ptr);return *this;三、 读程序,给出程序的输出结果。 (每小题6分,共24分)【1 】 Co nstructor of PersonCon structor of Stude ntCon structor of Pers onCon structor of TeacherDestructor of TeacherDestructor of Pers onDest
13、ructor of Stude nt【2】12,2【3】56789【4】1123444四、编程题(共 38分=12分+ 12分+14分)1.解答:#includeclass Test 4 分 int x,y;void in it(i nt,i nt);void prin t();void Test:init(int i,int j) 4 分 x=i;y=j;print() 4 分x-y=x-y(istream & is,fracti on &isf.fzf.fm;return is;3.解答:class employeeprotected:char n ame20;int empNo;floa
14、t salary;employee(char *cp=李强,int no=1001); employee(employee&void set name(char *cp);void setempNo(i nt no);void setsalary(float sa);char*get name();int getempNo();float getsalary();void display();employee compsalary(employee *emp);employee:employee(char *cp,i nt no) int i=0;while(*cp) n amei=*cp;i
15、+;cp+;namei=0empN o=no;employee(employee & em)while(em. namei) n amei=em .n amei;O:empNo=em.empNo;salary=em.salary;void employee:set name(char *cp)setempNo(i nt no) empN o=no;setsalary(float sa) salary=sa;char*employee:get name() return n ame;int employee:getempNo() return empNo;float employee:getsa
16、lary() retur n salary;display()工号为empNo的雇员name的月薪为salarysalary=emp-salary)elsereturn *emp;class worker : public employee worker(char*,i nt,i nt);void setproductNum(i nt n) productN um=n;int getproductNum() retur n productNum;void pay();int productNum;static float proper; 每件产品的利润float worker:proper=2
17、0; /假设每件产品的利润为 20元worker:worker(char *n ame,i nt n o,i nt pronum)employee( name ,no)productNum=pr onum;void worker:pay()salary=productNum*proper*0.2;class tech nicia n :tech nician( char*,i nt,float);void sethours(float h) workHours=h;float gethours() return workHours;float workHours;static float pa
18、yperhour; 科研人员每小时工作的酬金40元float tech ni cia n:payperhour=40; /假设科研人员每小时工作的酬金为tech ni cia n:tech nician( char *n ame,i nt no float hours) workHours=hours;void tech ni cia n: salary=workHours*payperhour;class salesma n : public employeesalesma n( char*,i nt,float);void setm on thlysales(i nt num) mon t
19、hlysales=nu m;int getm on thlysales() retur n mon thlysales;float mon thlysales;static float perce nt; /销售人员的销售提成float salesma n:perce nt=0.5; II假设销售人员的销售提成为 50%salesma n:salesma n( char *n ame,i nt n, float sn)employee( name ,n)mon thlysales=s n;void salesma n:salary=mon thlysales*perce nt;void main()worker w1(张三,1001,1000);technician t1(李四,1002,200);salesman s1(王五,1003,10000);w1.pay();t1.pay();s1.pay();工人w1.getname()w1.getsalary()元n科研人员t1.getiname()t1.getsalary()销售人员s1.getname()s1.getsalary()元 n
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1