面向对象程序设计 第九讲派生与继承性 堂提要 第7章派生与继承性 71派生类 72多重继承 73类模板的派生
第 7章 派生与继承性 7.1 派生类 7.2 多重继承 7.3 类模板的派生 第九讲 派生与继承性
面向对象程序设计 派生类( derived class 为什么引入派生类自然地表示现实世 界,是复杂的系统层次化,提高代码的重用 性,增强语言功能,提高软件开发效益. 堂提要 例如关于雇员和车辆的层次关系 第7章派生与继承性 employee(雇员) 7.1派生类 72多重继承 eacher教师)Oice(行政)Woer(工人)73类模板的派生 Vehicle(车辆) Car(汽车) Sportscar(赛车
第 7章 派生与继承性 7.1 派生类 7.2 多重继承 7.3 类模板的派生 派 生 类 (derived class) 为什么引入派生类 自然地表示现实世 界,是复杂的系统层次化,提高代码的重用 性,增强语言功能,提高软件开发效益. 例如关于雇员和车辆的层次关系: Teacher(教师) Officer(行政) Worker(工人) Employee(雇员) Car(汽车) Sportscar(赛车) Vehicle(车辆)
面向对象程序设计 派生类定义方式 C⊥ass b ase public: 堂提要 protected 第7章派生与继承性 private }; 7.1派生类 72多重继承 blic 73类模板的派生 class derivedName: protected base.. private public、 protected和 private分别表示公有继 承、保护继承和私有继承
第 7章 派生与继承性 7.1 派生类 7.2 多重继承 7.3 类模板的派生 派生类定义方式 class base { public: …… protected: …… private: …… }; public class derivedName: protected base{..} private public、protected 和 private分别表示公有继 承、保护继承和私有继承
面向对象程序设计 例1基类车辆 vehicle. h(因篇幅所限,我们 将类的实现放到头文件之中) #include<iostream h> class vehicle public 堂提要 vehicle(int aw, int ats, float ap) weight(aw), topspeed( ats), price(ap)ii 第7章派生与继承性 void printed cout n weight: < weight <<"pd 7.1派生类 cout<<" in top speed"<< topspeed <s"mph":7,2多重继承 cout n price: $"< price 73类模板的派生 private int weight;∥重量 int topspeed,l高速度 float price;∥价格
第 7章 派生与继承性 7.1 派生类 7.2 多重继承 7.3 类模板的派生 例1 基类车辆vehicle.h (因篇幅所限,我们 将类的实现放到头文件之中) #include<iostream.h> class vehicle { public: private: }; vehicle(int aw, int ats, float ap) :weight(aw),topspeed(ats),price(ap) { } void print( ) { cout << "\n weight:" << weight << "pds"; cout << "\n top speed:" << topspeed << "mph"; cout << "\n price: $ "<< price; int weight; //重量 int topspeed; //最高速度 float price; //价格
面向对象程序设计 ∥公有派生类汽车carh #include "vehicle.h" class car: public vehicle i public car(int aw, int ats, float ap, int anc, int ahp) 堂提要 vehicle(aw, ats, ap), numbercylinders(anc) horsepower(ahp)ii 第7章派生与继承性 void printed vehicle: print();∥调用基类的函数 7.1派生类 cout<<" n cylinders:"<< numbercylinders:7.2多重继承 cout << n horsepower: < horsepower 73类模板的派生 private int numbercylinder;∥汽缸数 int horsepower; ∥力
第 7章 派生与继承性 7.1 派生类 7.2 多重继承 7.3 类模板的派生 //公有派生类汽车 car.h #include " vehicle.h" class car : public vehicle { public: private: }; car(int aw, int ats, float ap, int anc, int ahp) :vehicle(aw,ats,ap), numbercylinders(anc) , horsepower(ahp) { } void print( ) { vehicle::print( ); //调用基类的函数 cout << "\n cylinders:" << numbercylinders; cout << "\n horsepower:" << horsepower; int numbercylinders; //汽缸数 int horsepower; //马力