C++语言程序设计教程与实验 第十章继承与派生 vehicle(int in wheels, float in weight) Wheels=in wheels; weight=in weight int get wheelsofreturn wheels float get weightoreturn weight class car: public vehicle //派生类car 类的声明 private: //新增私有数据成员 int passenger load; public: //新增公有函数成 员
vehicle(int in_wheels,float in_weight) {wheels=in_wheels;weight=in_weight;} int get_wheels(){return wheels;} float get_weight(){return weight;} }; class car:public vehicle //派生类car 类的声明 { private: //新增私有数据成员 int passenger_load; public: //新增公有函数成 员 C++语言程序设计教程与实验 第十章 继承与派生
C++语言程序设计教程与实验 第十章继承与派生 private //新增私有数据成员 int passenger load float weight load pulic //新增公有成员函数 void init truck(int, float int passengers o float weight loads
{ private: //新增私有数据成员 int passenger_load; float weight_load; puclic: //新增公有成员函数 void init_truck(int,float); int passengers(); float weight_loads(); }; C++语言程序设计教程与实验 第十章 继承与派生
C++语言程序设计教程与实验 第十章继承与派生 car (int in wheel. float in weight, int people=5): vehicle(in wheel, in weight passenger load-people int get passengers ireturn passenger load; I vold main car bm(4,1000); //声明car类的对象 cout< The message of bm(wheels, weight, passengers): <endl cout<<bm. get wheels()<",";//访问派生类从基类继 承来的公有函数
car(int in_wheel,float in_weight,int people=5):vehicle(in_wheel,in_weight) {passenger_load=people;} int get_passengers(){return passenger_load;} }; void main() { car bm(4,1000); //声明car类的对象 cout<<"The message of bm(wheels,weight,passengers):"<<endl; cout<<bm.get_wheels()<<"," ; //访问派生类从基类继 承来的公有函数 C++语言程序设计教程与实验 第十章 继承与派生
C++语言程序设计教程与实验 第十章继承与派生 的公有数8 et weight()x",";∥问派生类从基类继承来 cout<<bl cout<< bm get passengers0<endl;∥访问派生类的公有函 数 程序的运行结果 The message of bm ( wheels, weight, passengers) 4.1000.5
C++语言程序设计教程与实验 第十章 继承与派生 cout<<bm.get_weight()<<","; //访问派生类从基类继承来 的公有函数 cout<<bm.get_passengers()<<endl;// 访问派生类的公有函 数 } 程序的运行结果: The message of bm(wheels,weight,passengers): 4,1000,5
C++语言程序设计教程与实验 第十章继承与派生 表101不同继新方式下基类成员的访属性 访问属性 继承方式 public protected private public public protected 不可访问的 protected protected protected 不可访问的 private private private 不可访问的
表10.1 不同继承方式下基类成员的访问属性 C++语言程序设计教程与实验 第十章 继承与派生 访问属性 继承方式 public protected private public public protected 不可访问的 protected protected protected 不可访问的 private private private 不可访问的