1、c+继承与派生习题答案c+继承与派生习题答案1概念填空题1.1在C+中,三种派生方式的说明符号为 public 、 private 、 protected不加说明,则默认的派生方式为private 。1.2当公有派生时,基类的公有成员成为派生类的 公有成员 ;保护成员成为派生类的 保护成员 ;私有成员成为派生类的 不能直接访问成员 。当保护派生时,基类的公有成员成为派生类的 保护成员;保护成员成为派生类的 保护成员 ;私有成员成为派生类的 不能直接访问成员 。1.3 派生类的构造函数一般有3项工作要完成:首先 基类初始化 ,其次 成员对象初始化,最后 执行派生类构造函数体 。1.4多继承时,多
2、个基类中的同名的成员在派生类中由于标识符不唯一而出现 二义性 。在派生类中采用 虚基类 或 作用域分辨符 来消除该问题。2简答题2.1 派生类如何实现对基类私有成员的访问?2.2什么是类型兼容规则?3.6有如下类定义:class MyBASE int k;public: void set(int n) k=n; int get( ) const return k;class MyDERIVED: protected MyBASEprotected; int j;public: void set(int m,int n)MyBASE:set(m);j=n; int get( ) constret
3、urn MyBASE:get( )+j;则类MyDERIVED中保护成员个数是(B)。A4 B3 C2 D13.7程序如下:#includeusing namespace std;class A public: A( ) cout”A”;class B public:B( ) cout”B”; ;class C: public A B b;public: C( ) cout”C”;int main( ) C obj; return 0;执行后的输出结果是(D)。 ACBA BBAC CACB DABC3.8类O定义了私有函数F1。P和Q为O的派生类,定义为class P: protected
4、O; class Q: public O。(C)可以访问Fl。A O的对象 B P类内 C O类内 D Q类内3.9有如下类定义:class XAint x; public: XA(int n) x=n; ;class XB: public XA int y; public: XB(int a,int b);在构造函数XB的下列定义中,正确的是(B)。 AXB:XB(int a,int b):x(a),y(b) BXB:XB(int a,int b):XA(a),y(b) CXB:XB(int a,int b):x(a),XB(b) DXB:XB(int a,int b):XA(a),XB(b
5、) 3.10有如下程序:#includeusing namespace std;class Baseprivate: void fun1( ) const cout”fun1”;protected: void fun2( ) const cout”fun2”;public: void fun3( ) const cout”fun3”;class Derived : protected Base public: void fun4( ) const cout”fun4”;int main() Derived obj; obj.fun1( ); / obj.fun2( ); / obj.fun3(
6、 ); / obj.fun4( ); /其中没有语法错误的语句是(D )。A B C D4写出程序运行结果4.l#includeusing namespace std;class B1public: B1(int i) cout”constructing B1 “iendl; B1( ) cout”destructing B1 “endl; ;class B2 public: B2( ) cout”constructing B3 *”endl; B2( ) cout”destructing B3”endl; ;class C:public B2, virtual public B1 int j
7、;public: C(int a,int b,int c):B1(a),memberB1(b) ,j(c)private: B1 memberB1; B2 memberB2;int main( ) C obj(1,2,3); constructing B1 1constructing B3 *constructing B1 2constructing B3 *destructing B3destructing B1destructing B3destructing B14.2#includeusing namespace std;class Bpublic: void f1()coutB:f1
8、endl;class D:public Bpublic: void f1()coutD:f1endl;void f(B& rb) rb.f1();int main( ) D d; B b,&rb1=b,&rb2=d; f(rb1); f(rb2); return 0; B:f1 B:f15编程题5.1定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。#includeusing namespace std;const double PI=3.14159;class Point public: Point(double x=0, double y
9、=0) X=x;Y=y; void ShowPoint() cout( X,Y)endl;private: double X,Y;class Rectangle: public Point public: Rectangle(double w,double h,double x,double y):Point(x,y) width=w,height=h;Area(); void Area() area= width*height; void ShowArea() coutRectangle Area=areaendl; private: double width,height,area;cla
10、ss Circle: public Point public: Circle(double r,double x, double y):Point(x,y) radius=r;Area(); void Area() area= PI*radius*radius; void ShowArea() coutCircle Area=areaendl; private: double radius,area;int main() Rectangle r(10,8,0,0); Circle c(4,3,5); r.ShowArea(); c.ShowArea(); return 0;5.2设计一个建筑物
11、类Building,由它派生出教学楼Teach-Building和宿舍楼类Dorm-Building,前者包括教学楼编号、层数、教室数、总面积等基本信息,后者包括宿舍楼编号、层数、宿舍数、总面积和容纳学生总人数等基本信息。#include#includeusing namespace std;enum AMPMAM=1,PM;class Buildingpublic: Building(char *); void ShowBuilding() coutName; protected: char Name30;Building:Building(char *name) strcpy(Name,n
12、ame);class Teach_Building: public Building public: Teach_Building(char *,int,int,int,int); void ShowTeach_Building() Building:ShowBuilding(); cout No:No; cout Floors:Floors; cout ClassRooms:ClassRooms; cout Area:Areaendl; protected: int No,Floors,ClassRooms,Area;Teach_Building:Teach_Building(char *n
13、ame,int no,int fl,int cr,int ar):Building(name) No=no;Floors=fl;ClassRooms=cr;Area=ar;class Dorm_Building:public Building public: Dorm_Building(char *,int,int,int,int,int); void ShowDorm_Building() Building:ShowBuilding(); cout No:No; cout Floors:Floors; cout DormRooms:DormRooms; cout Area:Area; cou
14、t StudentNum:StudentNumendl; protected: int No,Floors,DormRooms,Area,StudentNum;Dorm_Building: Dorm_Building(char *name,int no,int fl,int dr,int ar,int sn):Building(name) No=no;Floors=fl;DormRooms=dr;Area=ar;StudentNum=sn;int main() Teach_Building tb(主教学楼,59,6,66,18000); Dorm_Building db(北苑男生宿舍,41,5
15、,75,3750,300); tb.ShowTeach_Building(); db.ShowDorm_Building(); return 0;5.3定义并描述一个Table类和一个Circle类,由它们共同派生出RoundTable类。(这题做得不太满意)#includeusing namespace std;class Tablepublic: Table(int legs) Legs=legs; protected: int Legs;class Circlepublic: Circle(int radius) Radius=radius; protected: int Radius;
16、class RoundTable:public Table,public Circlepublic: RoundTable(int legs,int radius):Table(legs),Circle(radius)protected:;int main() return 0;5.4利用第四章习题5.2 Clock类,派生一个带“AM”、“PM”的新时钟类NewClock。#includeusing namespace std;enum AMPMAM=1,PM;class Clockpublic: Clock(int=0, int=0, int s=0); void ShowTime() c
17、outHour:Minute:Second; private: int Hour,Minute,Second;Clock:Clock(int h, int m, int s) Hour=h;Minute=m;Second=s;class NewClock: public Clock public: NewClock(); NewClock(Clock c,AMPM ap):Clock(c) Ap=ap; void ShowTime() Clock:ShowTime(); cout(Ap=AM ? AM: PM)endl; private: AMPM Ap;int main() NewClock
18、 nc(Clock(8,23,34),AMPM(2); nc.ShowTime(); return 0;5.5利用NewClock类与Date类定义一个带日期的时钟类 ClockwithDate。对该类对象能进行增加减少秒数操作。#includeusing namespace std;enum AMPMAM=1,PM;class Clockpublic: Clock(int=0, int=0, int s=0); void ShowTime() coutHour:Minute:Second; protected: int Hour,Minute,Second;Clock:Clock(int h
19、, int m, int s) Hour=h;Minute=m;Second=s;class NewClock: public Clock public: NewClock(Clock c,AMPM ap):Clock(c) Ap=ap; void ShowTime() Clock:ShowTime(); cout(Ap=AM ? AM: PM)=1900 & y=1 &m=1 & d=1 & d=daysMonth)?d:1;bool Date:LeapYear() return (Year%400 = 0) | (Year%4=0 & Year%100!=0)? true:false;bo
20、ol Date:EndofMonth() if(Month=2 & LeapYear() return Day=29; else return Day=daysMonth;void Date:Increment(int n) int i; for(i=1;i=n;i+) if(EndofMonth() & Month=12) Year+; Month=Day=1; else if(EndofMonth() Month+;Day=1; else Day+;void Date:Decrement(int n) int i; for(i=1;i=n;i+) if(Day=1) if(Month=1)
21、 Year-; Month=12; Day=31; else if(Month=3) Day=LeapYear()?29:28;Month=2; else Day=days-Month; else Day-;void Date:ShowDate() coutYear-Month -Day endl;class ClockwithDate :public Date,public NewClockpublic: ClockwithDate(Date,Clock,AMPM); void IncrementSecond(int); void DecrementSecond(int); void Sho
22、wDateandTime() Date:ShowDate(); NewClock:ShowTime (); ;ClockwithDate:ClockwithDate(Date d,Clock c,AMPM ap):Date(d),NewClock(c,ap)void ClockwithDate:IncrementSecond(int n) int i; for(i=1;i=n;i+) Second+; if(Second=60) Second=0; Minute+; if(Minute=60) Minute=0; Hour+; if(Hour=24) Hour=0; Date:Incremen
23、t(1); void ClockwithDate:DecrementSecond(int n) int i; for(i=1;i=n;i+) Second-; if(Second=-1) Second=59; Minute-; if(Minute=-1) Minute=59; Hour-; if(Hour=-1) Hour=23; Date:Decrement(1); int main() ClockwithDate cd(Date(2007,2,28),Clock(23,59,34),AMPM(2); cd.ShowDateandTime(); cd.IncrementSecond(27);
24、 cd.ShowDateandTime(); cd.DecrementSecond(27); cd.ShowDateandTime(); return 0;5.6编写一个程序实现小型公司的工资管理。该公司主要有4类人员:经理(main-ager)、技术人员(technician)、销售员(salesman)、销售经理(salesmanager)。这些人员都是职员(employee),有编号、姓名、月工资信息。月工资的计算方法是:经理固定月薪8000元,技术人员每小时100元,销售员按当月销售额4提成,销售经理既拿固定月工资5000元也拿销售提成,销售提成为所管辖部门当月销售额的5。要求编程计算职员的月工资并显示全部信息。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1