资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
实验八实验八 继承与派生类继承与派生类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; 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(int 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 number, 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 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 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 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 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 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 max_load) passenger_load=number;payload=max_load; 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_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 birth_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(specialty2,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):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, 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“)
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号