实验八 继承与派生类

上传人:艾力 文档编号:36402626 上传时间:2018-03-28 格式:DOCX 页数:10 大小:790.70KB
返回 下载 相关 举报
实验八  继承与派生类_第1页
第1页 / 共10页
实验八  继承与派生类_第2页
第2页 / 共10页
实验八  继承与派生类_第3页
第3页 / 共10页
实验八  继承与派生类_第4页
第4页 / 共10页
实验八  继承与派生类_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《实验八 继承与派生类》由会员分享,可在线阅读,更多相关《实验八 继承与派生类(10页珍藏版)》请在金锄头文库上搜索。

1、实验八实验八 继承与派生类继承与派生类1 1实验目的要求实验目的要求(1)掌握单继承程序设计的基本方法。 (2)掌握多继承程序设计的基本方法。2 2实验内容实验内容(1) 下面程序定义一个 vehicle 类,并派生出 car 和 truck 两个派生类。 #include class vehicle protected:int wheels;double weight; public:void initialize(int whls, double wght);int get_wheels() return wheels; double get_weight() return weight;

2、 double wheel_loading() return weight/wheels; ;class car: public vehicle private:int passenger_load; public:void initialize(int whls, double wght, int people =4);int passengers() return passenger_load; ;class truck: public vehicle private: int passenger_load;double payload; public:void init_truck(in

3、t number =2, double max_load =24000.0);double efficiency();int passengers() return passenger_load; ;void vehicle:initialize(int whls, double wght) wheels=whls;weight=wght; void car:initialize(int whls, double wght, int people)wheels=whls;weight=wght;passenger_load=people; void truck:init_truck(int n

4、umber, double max_load) passenger_load=number;payload=max_load; double truck:efficiency() return payload/(payload+weight); void main() vehicle bicycle;bicycle.initialize(2,25);cout class vehicle protected:int wheels;double weight; public: vehicle(int whls, double wght) wheels=whls;weight=wght; ;int

5、get_wheels() return wheels; double get_weight() return weight; double wheel_loading() return weight/wheels; ;class car: public vehicle private:int passenger_load; public:car(int whls, double wght, int people):vehicle(whls, wght) wheels=whls;weight=wght;passenger_load=people; int passengers() return

6、passenger_load; ;class truck: public vehicle private: int passenger_load;double payload; public:truck(int whls,double wght):vehicle(whls,wght)void init_truck(int number =2, double max_load =24000.0);double efficiency();int passengers() return passenger_load; ;void truck:init_truck(int number, double

7、 max_load) passenger_load=number;payload=max_load; double truck:efficiency() return payload/(payload+weight); int main() vehicle bicycle(2,25);cout class vehicle private:int wheels;double weight; public: vehicle(int whls, double wght) wheels=whls;weight=wght; ;int get_wheels() return wheels; double

8、get_weight() return weight; double wheel_loading() return weight/wheels; ;class car: public vehicle private:int passenger_load; public:car(int whls, double wght, int people):vehicle(whls,wght) personpersonpassenger_load=people; int passengers() return passenger_load; ;class truck: public vehicle pri

9、vate: int passenger_load;double payload; public:truck(int whls,double wght):vehicle(whls,wght)void init_truck(int number =2, double max_load =24000.0);double efficiency();int passengers() return passenger_load; ;void truck:init_truck(int number, double max_load) passenger_load=number;payload=max_loa

10、d; double truck:efficiency() return payload/(payload+get_weight(); int main() vehicle bicycle(2,25);cout #include graduateteacherin-service_graduate#include class person protected: char name20; int birth_year; public: person(char *na, int year) strcpy(name,na); birth_year=year; int cal_age(int this_

11、year) return this_year-birth_year; ;class graduate :public person protected: int grade; char specialty20; public: graduate(char *na, int y, int g, char *spec):person(na,y) grade=g; strcpy(specialty,spec); void display(int this_year) cout #include #include class person protected: char name20; int bir

12、th_year; public: person(char *na, int year) strcpy(name,na); birth_year=year; int cal_age(int this_year) return this_year-birth_year; ;class graduate :virtual public person protected: int grade; char specialty220; public: graduate(char *na, int y, int g, char *spec):person(na,y) grade=g; strcpy(spec

13、ialty2,spec); void display(int this_year) cout“ graduate age grade specialtyn“; coutsetw(20)namesetw(5)cal_age(this_year); coutsetw(7)gradesetw(17)specialty2endl; ;class teacher :virtual public person protected: char title15; char specialty120; public: teacher(char *na, int y, char *ti, char *spec):

14、person(na,y) strcpy(title,ti); strcpy(specialty1,spec); void display(int this_year) cout“ teacher age title specialtyn“; coutsetw(20)namesetw(5)cal_age(this_year); coutsetw(14)titlesetw(17)specialty1endl; ;class in_service_graduate:public teacher, public graduate public:in_service_graduate(char *na,

15、 int y, char *ti, char *spec1, int g, char *spec2): teacher(na, y, ti, spec1),graduate(na,y , g, spec2),person(na,y)void display(int this_year) cout“ in_service_graduate age title work_specialty grade study_specialtyn“; coutsetw(20)namesetw(5)cal_age(this_year);coutsetw(8)titlesetw(17)specialty1; coutsetw(7)gradesetw(17)specialty2endl; ;int main() graduate gr(“zhang_ling“,1978,2001,“computer“); teacher te(“wang_qiang“, 1976,“tutor“,“electronics“)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号