Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance -II.

Similar presentations


Presentation on theme: "Inheritance -II."— Presentation transcript:

1 Inheritance -II

2 Contents Multiple Inheritance 1 Virtual Inheritance 2 Case Study 3

3 Multiple Inheritance(多继承)
Bed +Weigh: int +sleep():void Sofa +watchTV():void SleeperSofa +foldOut():void A class can have more than one direct base class. The use of more than immediate base class is usually called multiple inheritance.(有多个基类)

4 Multiple Inheritance Bed Sofa SleeperSofa
+Weigh: int +sleep():void Sofa +watchTV():void SleeperSofa + foldOut():void class SleeperSofa: public Sofa, public Bed{ //… };

5 Multiple Inheritance class Bed{ public: Bed(){} void sleep() {
cout << "Sleep..\n"; } int weight; }; class Sofa{ public: Sofa(){} void watchTV() { cout << "Watching TV..\n"; } int weight; }; class SleeperSofa : public Bed, public Sofa{ public: SleeperSofa(){} void foldOut() { cout << "Fold out..\n"; } }; int main() { SleeperSofa ss; ss.sleep(); ss.watchTV(); ss.foldOut(); return 0; } Derived constructor without parameters

6 Multiple Inheritance Constructors and destructor of multiple inheritance Constructor definition Derived class’s constructor : base_class1’s constructor, base_class2’s constructor, base_class3’s constructor {} Constructing objects’ sequence: base_class1 object ->base_class2 object-> base_class3 object -> Derived class object Destructor definition Derived class’s destructor {} Destroying objects’ sequence: Derived class object -> base_class3 object ->base_class2 object -> base_class1 object

7 Multiple Inheritance class Bed{ public: Bed(int w):weight(w){}
void sleep() { cout << "Sleep..\n"; } int weight; }; class Sofa{ public: Sofa(int w):weight(w){} void watchTV() { cout << "Watching TV..\n"; } int weight; }; class SleeperSofa : public Bed, public Sofa{ public: SleeperSofa(int w):Bed(w),Sofa(w){} void foldOut() { cout << "Fold out..\n"; } }; int main() { SleeperSofa ss(5); ss.sleep(); ss.watchTV(); ss.foldOut(); return 0; } Derived constructor with parameters

8 Ambiguous Inheritance(模糊继承)
Multiple inheritance ambiguities(多继承的模糊性) int main() { SleeperSofa ss(5); ss.sleep(); ss.watchTV(); ss.foldOut(); return 0; } Bed’s weight? cout<<ss.weight; //ambiguity cout<<ss.Bed::weight; Sofa’s weight? When the base classes may have member functions with the same name, the function call of derived class object leads to ambiguity. (当两个基类成员名字相同时,派生类对象访问成员或函数时产生模糊)

9 Replicated Inheritance(重复继承)
Bed +sleep():void Sofa +watchTV():void SleeperSofa + foldOut():void Furniture -weigh: int Single inheritance Problem: Replicated Base Classes(重复基类) multiple inheritance

10 Replicated Inheritance
Bed +Sleep():void Sofa +watchTV():void SleeperSofa + foldOut():void Furniture -weigh: int Lattice structure (网格结构) Avoid replicated Base Classes(重复基类)?

11 Replicated Inheritance
class Furniture{ int weight; public: Furniture(int w) :weight(w){} int getWeight(){return weight;} }; class Sofa : public Furniture{ public: Sofa(int w) :Furniture(w){} void watchTV() {cout << "Watching TV..\n";} }; class Bed:public Furniture{ public: Bed(int w) :Furniture(w){} void sleep() { cout << "Sleep..\n";} }; class SleeperSofa : public Bed, public Sofa{ public: SleeperSofa(int w) :Bed(w), Sofa(w){} void foldOut() {cout << "Fold out..\n";} }; int main() { SleeperSofa ss(5); ss.sleep(); ss.watchTV(); ss.foldOut(); return 0; } Result: Constructing furniture... Constructing Bed ... Constructing Sofa ... Constructing SleeperSofa ...

12 Replicated Inheritance
int main() { SleeperSofa ss(5); ss.sleep(); ss.watchTV(); ss.foldOut(); return 0; } cout << ss.getWeight() << endl; //ambiguity Result: Constructing furniture... Constructing Bed ... Constructing Sofa ... Constructing SleeperSofa ... Problem: Replicated Base Classes solving

13 Virtual Inheritance(虚继承)
int main() { SleeperSofa ss(5); ss.sleep();ss.watchTV(); ss.foldOut(); cout << ss.getWeight() << endl; return 0; } class Furniture{ int weight; public: Furniture(int w) :weight(w){} int getWeight(){return weight;} }; class Bed:virtual public Furniture{ public: Bed(int w) :Furniture(w){} void sleep() { cout << "Sleep..\n";} }; virtual inheritance class SleeperSofa : public Bed, public Sofa{ public: SleeperSofa(int w) : Furniture(w), Bed(w), Sofa(w){} void foldOut() {cout << "Fold out..\n";} }; class Sofa :virtual public Furniture{ public: Sofa(int w) :Furniture(w){} void watchTV() {cout << "Watching TV..\n";} };

14 Virtual Inheritance Virtual Inheritance(虚继承)
The mechanism offers a way to save space and avoid ambiguity in multiply inheritance. (虚继承为多继承提供一种节省空间并避免模糊继承的方法) Virtual base class(虚基类) The common base class in this mechanism is called virtual base class. For example, Furniture is a virtual base class of Bed and Sofa. Constructing sequence: Furniture Bed Sofa SleeperSofa

15 Virtual Inheritance The constructing sequence of the virtual base classes In the same level, the virtual base classes are constructed according to the sequence of their declaration.(同级别虚基类按照声明顺序构造) class C : virtual public A, virtual public B{ .... }; constructing sequence: A -> B -> C When virtual base classes and non-virtual base classes exist the same level, virtual base classes are first constructed and non-virtual base classes are then constructed.(虚基类先于其它基类构造) class C : public A, virtual public B{ .... }; constructing sequence: B -> A -> C

16 Summary Be able to understand multiple inheritance
Be able to know why ambiguous inheritance happens Be able to define a virtual base class


Download ppt "Inheritance -II."

Similar presentations


Ads by Google