C++继承与派生_第1页
C++继承与派生_第2页
C++继承与派生_第3页
C++继承与派生_第4页
C++继承与派生_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

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

文档简介

第4章 继承与派生一、简答题1. 有以下程序结构,请分析访问属性。class A /A为基类public: void func1( ); int i;protected: void func2( ); int j;private:int k; ;class B: public A /B为A的公用派生类public : void func3( ) ;protected: int m;private :int n;class C: public B / C为B的公用派生类public:void func4( );private:int p;int main( ) A a; /a是基类A的对象 B b; /b是派生类B的对象C c; /c是派生类C的对象return 0; 问:(1)在main函数中能否用b.i,b.j 和b.k 访问派生类B对象b中基类A的成员?(2)派生类B中的成员函数能否调用基类A中的成员函数func1和func2?(3)派生类B中的成员函数能否访问基类A中的数据成员i,j,k?(4)能否在main函数中用c.i,c.j,c.k,c.m,c.n,c.p访问基类A的成员i,j,k,派生类B的成员m,n,以及派生类C的成员p?(5)能否在main函数中用c. func1( ),c. func2( ),c. func3( )和c. func4( )调用func1,func2,func3,func4成员函数?(6)派生类C的成员函数func4能否调用基类A中的成员函数func1,func2和派生类B中的成员函数func3?【答案要点】各成员在各类的范围内的访问权限如下表:类的范围func1ifunc2jkfunc3mnfunc4p基类A公用公用保护保护私有公用派生类B公用公用保护保护不可访问公用保护私有公用派生类C公用公用保护保护不可访问公用保护不可访问公用私有(1)在main 函数中能用b.i访问派生类B对象b中基类A的成员i,因为它在派生类B中是公用数据成员。不能用b.j访问派生类B对象b中基类A的成员j,因为它在派生类B中是保护数据成员,不能被类外访问。不能用b.k访问派生类B对象b中基类A的成员k,因为它是基类A的私用数据成员,只有基类A的成员函数可以访问,不能被类外访问。(2)派生类B中的成员函数能调用基类A中的成员函数func1和func2,因为func1、func2在派生类B中是公用成员和保护成员,可以被派生类的成员函数访问。(3)派生类B中的成员函数能访问基类A中的数据成员i、j,因为i、j在派生类B中是公用成员和保护成员,可以被派生类的成员函数访问。派生类B中的成员函数不能访问基类A中的数据成员k,因为它在派生类B中是不可访问的成员。(4)能在main 函数中用c.i访问基类A的成员i,不能用c.j、c.k访问基类A的成员j、k,因为它们在派生类C中是保护成员和私有成员,不能被类外访问。也不能用c.m、c.n访问派生类B的成员m、n,因为它们在派生类C中也是保护成员和私有成员,不能被类外访问。也不能用c.p访问派生类C中的私用成员p。(5)能在main函数中用c. func1()、c. func3()和c. func4()调用func1、func3、func4成员函数,因为它们在派生类C中是公用成员函数,可以在类外被访问。不能在main函数中用c. func2()调用func2成员函数,因为它在派生类C中是保护成员函数,不能在类外被访问。(6)派生类C的成员函数func4能调用基类A中的成员函数func1、func2和派生类中的成员函数func3,因为func1、func3在派生类C中是公用成员函数,func2在派生类C中是保护成员函数,都可以被派生类C的成员函数调用。2. 已给商品类及其多层的派生类。以商品类为基类。第一层派生出服装类、家电类、车辆类。第二层派生出衬衣类、外衣类、帽子类、鞋子类;空调类、电视类、音响类;自行车类、轿车类、摩托车类。请给出商品类及其多层派生类的基本属性和派生过程中增加的属性。【答案要点】按题意没有操作,所以只列出各个类的数据成员,也不再在main函数中对各类进行测试。#includeusing namespace std;class Commoditydouble price; /价格char name20; /商品名char manufacturer20; /生产厂家int items; /数量; class Clothing: public Commodity /服装类char texture20; /材料质地; class ElectricAppliance: public Commodity /家电类enum Black,Whitetype; /黑白家电; class Vehicle: public Commodity /车辆类int wheelNum; /车轮数量; class Shirt: public Clothing /衬衣类 enum Formal,Casualstyle; /式样:正式、休闲; class Garment: public Clothing /外衣类enum Jacket,Coatstyle; /式样:夹克、外套 ; class Hat: public Clothing /帽子类enum Winter,Summer,Spring,Autumnstyle; /季节风格; class Shoes: public Clothing /鞋子类enum Winter,Summer,Spring,Autumnstyle; /季节风格; class AirCondition: public ElectricAppliance /空调bool warmCool; /是否冷暖float power; /功率; class Television: public ElectricAppliance /电视类int size; /尺寸 bool isColor; /是否彩色 ; class Acoustics: public ElectricAppliance /音响类int speakerNum; /喇叭数目 float power; /功率 ; class Bicycle: public Vehicle /自行车类int speedGrades; /调速级数int wheelSize; /轮子大小 ; class Car: public Vehicle /轿车类float volume; /排气量bool isSkyLight; /是否有天窗int boxNum; /厢数; class Motorcycle: public Vehicle /摩托车类float volume; /排气量 ; int main() return 0; 二、编程题1定义一个国家基类Country,包含国名、首都、人口等属性,派生出省类Province,增加省会城市、人口数量属性。【程序参考代码】#includeusing namespace std;#includeclass Countrypublic:Country(char *n,char *c,double p) strcpy(name,n); strcpy(capital,c); population=p; void print() coutname,capital,populationendl; private:char name10,capital50;double population;class Province:private Country public: Province(char *n,char *c,double p,char *cc,double pp):Country(n,c,p) strcpy(provinceCapital,cc); provincePopulation=pp; void display() print(); coutprovinceCapital, provincePopulationendl;private:char provinceCapital50;double provincePopulation;int main() Province p(China,Beijing,1.36e+010,guang dong,1.05e+009); p.display(); return 0;2定义一个基类Person类,有姓名、性别、年龄,再由基类派生出学生类Student类和教师类Teacher类,学生类增加学号、班级、专业和入学成绩,教师类增加工号、职称和工资。【程序参考代码】#include #include using namespace std;class Person /声明公共基类Personpublic: void set() cin namesexage; /姓名、性别、年龄的输入void display() coutname=nametsex=sextage=stuIdstuClassprofessionscore; /学号、班级、专业、入学成绩的输入 void display() Person:display(); /姓名、性别、年龄的显示couttstuId=stuIdtstuClass=stuClasstprofession =professiontscore=scoreendl teachId title wage; /工号、职称、工资的输入void display() Person:display(); /姓名、性别、年龄的显示couttteachId=teachIdttitle=titletwage=wageendl; /工号、职称、工资的显示private: string teachId; /工号string title; /职称 float wage; /工资;int main( )Student student;Teacher teacher; coutPlease enter students name,sex,age,stuId,stuClass,profession and score:endl; student.input(); coutDisplay students name,sex,age,stuId,stuClass,profession and score:endl; student.display();coutPlease enter teachers name,sex,age,teachId, title and wage:endl; teacher.set(); coutDisplay teachers name,sex,age,teachId, title and wage:endl; teacher.display(); return 0;3设计一个基类Building类,有楼房的层数、房间数和总面积,再由基类派生出教学楼TeachBuilding类和宿舍楼类DormBuilding类,教学楼类增加教室数,宿舍楼类增加宿舍数、容纳学生总人数。【程序参考代码】#include using namespace std;class Building public: Building(int f,int r,double a) floors=f; rooms=r; area=a; protected: int floors;int rooms;double area;class TeachBuilding:public Buildingpublic: TeachBuilding (int f,int r,double a,int cr):Building(f,r,a) classrooms=cr; void show() coutfloors=floors rooms=rooms area=area classrooms=classroomsendlendl; private: int classrooms;class DormBuilding:public Building public: DormBuilding (int f,int r,double a,int d,int sc) :Building(f,r,a) dormitories =d; studcount =sc; void show() coutfloors=floors rooms=rooms area=area dormitories=dormitories studcount=studcountendlendl; private: int dormitories; int studcount; ;int main() TeachBuilding TB(6, 80,200,35); TB.show(); DormBuilding DB(6, 80,200,35,300); DB.show(); return 0;4定义一个基类汽车类,有型号、颜色、发动机功率、车速、重量、车牌号码,再由汽车类派生出客车类和货车类,客车类增加客车座位数、客运公司,货车类增加载货重量、货运公司。【程序参考代码】#include #include using namespace std;class Carpublic:void show();protected: string model; /型号 int color; /颜色 unsigned int motopower; /发动机功率 unsigned int speed; /车速 unsigned int weight; /重量 unsigned int id; /车牌号码;class Bus: public Carpublic: void show();Bus( string _model, int _color, unsigned int _motopower, unsigned int _speed,unsigned int _weight, unsigned int _id, unsigned int _seatnum, string _corp) model = _model; color = _color; motopower = _motopower; speed = _speed;weight = _weight; id = id; _seatnum = _seatnum; corp = _corp; private: unsigned int seatnum; /客车座位数 string corp; /客运公司;class Truck : public Carpublic: void show(); Truck(string _model, int _color, unsigned int _motopower, unsigned int _speed, unsigned int _weight, unsigned int _id,unsigned int _load, string _corp) model = _model; color = _color; motopower = _motopower; speed = _speed; weight = _weight; id = _id; load = _load; corp = _corp; private: unsigned int load; /载货重量string corp; /货运公司;void Car:show()cout型号:modelendl;cout颜色:colorendl; cout发动机功率:motopowerendl; cout车速:speedendl; cout重量:weightendl; cout车牌号码:idendl;void Bus:show()cout型号:modelendl; cout颜色:colorendl; cout发动机功率:motopowerendl; cout车速:speedendl; cout重量:weightendl; cout车牌号码:idendl; cout客车座位数:seatnumendl; cout客运公司:corpendl;void Truck:show() cout型号:modelendl;cout颜色:colorendl;cout发动机功率:motopowerendl;cout车速:speedendl; cout重量:weightendl;cout车牌号码:idendl;cout载货重量:loadendl;cout货运公司:corpendl;int main( int argc, char* argv ) Car *a = new Bus(黄海,3,300,100,2000,50,北京客运);Car *b = new Truck(东风,1,400,200,5000,3000,北京货运);cout=show();cout=show();system(pause);return 0;5定义一个Table类和Circle类,再由它们共同派生出RoundTable类。【程序参考代码】#includeusing namespace std;#includeclass Circlepublic:Circle(double r)radius=r;double getArea()return 3.1416*radius*radius;private:double radius;class Tablepublic:Table(double h)height=h;double getHeight()return height;private:double height;class RoundTable:public Table,public Circlepublic:RoundTable(double h,double r,char c):Table(h),Circle(r)color=new charstrlen(c) +1;strcpy(color,c);char* getColor()return color;private: char *color;int main()RoundTable rt(0.8,1.0,白色);cout圆桌的高:rt. getHeight()endl;cout圆桌面积:rt.getArea()endl;cout圆桌颜色:rt.getColor()endl;return 0;6在第4题的基础上,由客车类和货车类再派生出一个客货两用车类,一辆客货两用车既具有客车的特征(有座位,可以载客),又具有货车的特征(有装载车厢,可以载货)。要求将客货两用车类的间接共同基类汽车类声明为虚基类。【程序参考代码】#include #include using namespace std;class Carpublic:Car(string _model, int _color, unsigned int _motopower, unsigned int _speed,unsigned int _weight, unsigned int _id) model = _model; color = _color; motopower = _motopower; speed = _speed; weight = _weight; id = _id; void show();private: string model; int color; unsigned int motopower; unsigned int speed; unsigned int weight; unsigned int id;class Bus: virtual public Carpublic: Bus( string model, int color, unsigned int motopower, unsigned int speed,unsigned int weight, unsigned int id, unsigned int _seatnum,string _corp ): Car(model,color, motopower,speed,weight,id) seatnum = _seatnum; corp = _corp; void display();protected: unsigned int seatnum; string corp;class Wagon : virtual public Carpublic: Wagon( string model, int color, unsigned int motopower, unsigned int speed,unsigned int weight,unsigned int id, unsigned int _load,string _corp ): Car(model,color, motopower,speed,weight,id) load = _load; corp = _corp; void display();protected: unsigned int load; string corp;class StationWagon : public Bus,public Wagonpublic: StationWagon( string model, int color, unsigned int motopower, unsigned int speed,unsigned int weight,unsigned int id,unsigned int seatnum,unsigned int load, string corp ): Car(model,color, motopower,speed,weight,id),Bus(model,color, motopower,speed,weight,id,seatnum,corp),Wagon(model,color, motopower,speed,weight,id,load,corp) void display();void Car:show()cout型号:modelendl;cout颜色:colorendl; cout发动机功率:motopowerendl; cout车速:speedendl; cout重量:weightendl; cout车牌号码:idendl; void Bus:display()show(); cout客车座位数:seatnumendl; cout客运公司:corpendl;void Wagon:display() show();cout载货重量:loadendl;cout货运公司:corpendl;void StationWagon:display() show();cout客车座位数:seatnumendl;cout载货重量:loadendl;cout客运及货运公司:Bus:corpendl;int main( ) StationWagon sw(黄海,3,300,100,2000,50,3000,北京客货运);sw.display();system(pause);return 0;7设计一个雇员类Employee,存储雇员的编号、姓名和生日等信息,要求该类使用日期类作为成员对象,雇员类的使用如下:/定义一个雇员,其雇员号为1111,姓名为Tom,生日为1980年11月20日 Employee Tom(1111, Tom, 1980, 11, 20)Date today(1980, 11, 20); if (Tom.isBirthday(today) /判断今天是否为Tom的生日 /【程序参考代码】#include #include using namespace std;class Datepublic:Date() year=0; month=0; day=0; /无参构造函数Date(int mm, int dd, int yy) year=yy; month=mm; day=dd; /带参数的构造函数Date(const Date &d) year=d.year; month=d.month; day=d.day; /复制构造函数void setDate(const int, const int, const int); /修改日期的函数void display(); /输出日期的函数 int getYear(); /获取年份的函数int getMonth(); /获取月份的函数 int getDay(); /获取日信息的函数private:int year, month, day;/设置成员变量,mm:月份;dd:天数;yy:年份;void Date:setDate(const int mm, const int dd, const int yy)year=yy; month=mm; day=dd; void Date:display() coutmonth

温馨提示

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

最新文档

评论

0/150

提交评论