Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.

Similar presentations


Presentation on theme: "Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed."— Presentation transcript:

1 Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.

2 Inheriting Multiple Base Classes It is possible for a derived class to inherit two or more base classes. For example, in this short program, derived inherits both basel and base2:

3 Inheriting Multiple Base Classes General definition : class derivedName: access1 base1, access2 base2 { The member list of derivedName class };

4 An example of multiple base classes 1. // An example of multiple base classes. 2. #include 3. using namespace std; 4. class base1 { 5. protected: 6. int x; 7. public: 8. void showx() { cout << x << "\n";} 9. }; 10. class base2 { 11. protected: 12. int Y; 13. public: 14. void showy() { cout<< y << "\n"; } 15. }; 16. // Inherit multiple base classes. 17. class derived: public base1, public base2 { 18. public: 19. void set(int i, int j) { x= i; y =j; }; 20. int main(){ 21. derived ob; 22. ob.set(10, 20);//provided by derived 23. ob.showx();//from basel 24. ob.showy();//from base2 25. return 0; 26. } 10 20

5 Constructors, Destructors, and Inheritance 1. when are base class and derived class constructor and destructor functions called? 2. how can parameters be passed to base class constructor functions?

6 When Constructor and Destructor Functions Are Executed? It is possible for a base class, a derived class, or both, to contain constructor and/or destructor functions. It is important to understand the order in which these functions are executed ◦ when an object of a derived class comes into existence ◦ when it goes out of existence.

7 Example 1 1. #include 2. using namespace std; 3. class base { 4. public: 5. base(){ cout << "Constructing base\n";} 6. --base(){ cout << "Destructing base\n"; } 7. }; 8. class derived: public base { 9. public: 10. derived() { cout << "Constructing derived\n";} 11. ~derived() { cout << "Destructing derived\n";} 12. }; 13. int main() 14. { 15. derived ob; 16. // do nothing but construct and destruct ob 17. return 0; 18. } Constructing base Constructing derived Destructing derived Destructing base General Rule constructor functions are executed in the order of their derivation. Destructor functions are executed in reverse order of derivation.

8 Example 2 1. #include 2. using namespace std; 3. class base { 4. public: 5. base() { cout << "Constructing base\n"; } 6. ~ base() { cout << "Destructing base\n"; } 7. }; 8. class derived1: public derived1{ 9. public: 10. derived1() {cout << "Constructing derived1\n"; } 11. ~ derived1() {cout << "Destructing derived1\n"; } 12. }; 13. class derived2: public derived1 { 14. public: 15. derived2{ cout << "Constructing derived2\n"; } 16. ~ derived2{ cout << "Destructing derived2\n";} 17. }; 1. int main() { 2. derived2 ob; 3. // construct and destruct ob 4. return 0;} Constructing base Constructing derived1 Constructing derived2 Destructing derived2 Destructing derived1 Destructing base

9 The same general rule applies in situations involving multiple base classes 1. #include 2. using namespace std; 3. class base1{ 4. public: 5. base1() { cout << "Constructing base1\n";} 6. ~ base1() { cout << "Destructing base1\n"; } 7. }; 8. class base2{ 9. public: 10. base2() { cout << "Constructing base2\n"; } 11. ~ base2() { cout << "Destructing base2\n"; } 12. }; 13. class derived: public base1, public base2 { 14. public: 15. derived() { cout << "Constructing derived\n";} 16. ~derived() { cout << "Destructing derived\n";} 17. } Constructing base1 Constructing base2 Constructing derived Destructing derived Destructing base2 Destructing base1 1.int main() { 2.derived2 ob; 3.// construct and destruct ob 4.return 0;} constructors are called in order of derivation, left to right, as specified in derived's inheritance list. Destructors are called in reverse order, right to left.

10 Second Example Constructing base2 Constructing base1 Constructing derived Destructing derived Destructing base1 Destructing base2 1.int main() { 2.derived2 ob; 3.// construct and destruct ob 4.return 0;}

11 Passing Parameters to Base Class Constructors The general form of this expanded declaration is shown here: base1 through baseN are the names of the base classes inherited by the derived class. Notice that a colon separates the constructor function declaration of the derived class from the base classes, and that the base classes are separated from each other by commas, in the case of multiple base classes. derived-constructor(arg-list) : base1 (arg-list), base2(arg-list), …. baseN(arg-list) { body of derived constructor }

12 Example 1 1. #include 2. using namespace std; 3. class base{ 4. protected: 5. int i ; 6. public: 7. base( int x ) { i = x; cout << "Constructing base\n";} 8. ~ base() { cout << "Destructing base\n"; } 9. }; 10. class derived: public base { 11. int j; 12. public: 13. //derived uses x;" y is passed along to base. 14. derived(int x, int y): base(y) 15. { j = x; cout << "Constructing derived\n"; } 16. ~ derived(){ cout << "Destructing derived\n"; } 17. void show(){cout << i <<<< "\n"; } 18. }; 1.int main() { 2. derived ob(3,4); 3. ob.show(); //displays 4 3 4. return 0; 5.} derived's constructor is declared as taking two parameters, x and y. However, derived( ) uses only x; y is passed along to base( ). In general, the constructor of the derived class must declare the parameter(s) that its class requires, as well as any required by the base class.

13 Example 2 uses multiple base classes 1. #include 2. using namespace std; 3. class base1{ 4. protected: 5. int i ; 6. public: 7. base1( int x ) { i = x; cout << "Constructing base1\n";} 8. ~ base1() { cout << "Destructing base1\n"; } 9. }; 10. class base2{ 11. protected: 12. int k; 13. public: 14. base2( int x ) { k = x; cout << "Constructing base2\n";} 15. ~ base2() { cout << "Destructing base2\n"; } 16. }; 17.class derived: public base 1, public base2{ 18.int j; 19.public: 20.derived(int x, int y, int z ): base1(y), base2(z) 21. { j = x; 22. cout << "Constructing derived\n"; } 23.~ derived(){ cout << "Destructing derived\n";} 24. void show(){cout << i <<“ ”<<j<< “ ” << k << "\n"; } 25.}; 26.int main() { 27. derived ob(3,4,5); 28. ob.show(); //displays 4 3 5 29. return 0; 30.}

14 Example 3: Derived take no arguments but base1() and base2() 1. #include 2. using namespace std; 3. class base1{ 4. protected: 5. int i ; 6. public: 7. base1( int x ) { i = x; cout << "Constructing base1\n";} 8. ~ base1() { cout << "Destructing base1\n"; } 9. }; 10. class base2{ 11. protected: 12. int k; 13. public: 14. base2( int x ) { k = x; cout << "Constructing base2\n";} 15. ~ base2() { cout << "Destructing base2\n"; } 16. }; 17.class derived: public base 1, public base 2{ 18.int j; 19.public: /* Derived constructor uses no parameters, but still must be declared as taking them to pass them along to base classes. */ 17.derived(int x, int y): base1(x), base2(y) 18. { cout << "Constructing derived\n"; } 19.~ derived(){ cout << "Destructing derived\n";} 20. void show(){cout << i <<“ ”<<j<< “ ” << k << "\n"; } 21.}; 22.int main() { 23. derived ob(3,4); 24. ob.show(); //displays 3 4 25. return 0; 26.}

15 Notice The constructor function of a derived class is free to use any and all parameters that it is declared as taking, whether or not one or more are passed along to a base class. Put differently, just because an argument is passed along to a base class does not produce its use by the derived class as well. For example, this fragment is perfectly valid: class derived: public base{ int j; public: // derived uses both x and y and then passes them to base. derived(int x, int y): base(x, y) { j = x*y; cout << "Constructing derived\n"; } };


Download ppt "Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed."

Similar presentations


Ads by Google