Lecture # 5 Inheritance
Inheritance is probably the most powerful feature of object oriented programming which is used in achieving software reusability . Inheritance is the process of creating new classes, called derived classes from existing or base classes . The derived class inherits all the capabilities of the base class but can add its own functionalities and refinements. Consider the following figure.
Derived class (e.g. Horse ) Arrow means “Derived from” Function a Function b Function c Function b Function a defined only in derived class Base class ( e.g. Animal )
Base class does not inherit any class. Derived class does inherit any other class or classes. Single inheritance Derived class only Inherits from one base class Multiple inheritance Derived class can Inherit from multiple base classes In this case base classes may be possibly unrelated from real life point of view
“ is-a” vs. “has-a” relationship “is-a” Inheritance Derived class object treated as base class object Example: Car is a vehicle Car (derived class) inherits vehicle (base class) “has-a” Composition Object contains one or more objects of other classes as members Example: Car has a steering wheel ( car and wheel are classes )
Base Classes and Derived Classes
Inheritance hierarchy for university Community Members. Single inheritance CommunityMember Employee Student Administrator Teacher AdministratorTeacher Staff Faculty Alumnus Single inheritance Single inheritance Multiple inheritance
Inheritance hierarchy for Shapes. Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron
Base class does not inherit any class. Derived class does inherit any other class or classes.
Example: Inheritance #include <conio.h> #include <iostream.h> class arithmatic { protected : int a,b; public : void add(int x, int y) { a=x; b=y; cout<<&quot; \n Addition of a & b is : &quot;<<a+b; } };
class operation1 : public arithmatic { public : void mul(int x, int y) { a=x; b=y; cout<<&quot; \n Multiplication of a & b is :”<<a*b; } };
void main() { operation1 op1; int m,n; cout<<&quot; Enter number 1 :&quot;; cin>>m; cout<<&quot; Enter number 2 :&quot;; cin>>n; op1.mul(m,n); op1.add(m,n); getch(); }
Important Note: One thing is to be noted that whenever the object of derived class is created the constructor of both , the derived and base classes are called automatically.
Constructors and Destructors in Derived Classes Instantiating derived-class object Chain of constructor calls Derived-class constructor invokes base class constructor Implicitly or explicitly Base class of inheritance hierarchy Last constructor called in chain First constructor body to finish executing Initializing data members Each base-class constructor initializes data members Inherited by derived class
Destroying derived-class object Chain of destructor calls Reverse order of constructor chain Destructor of derived-class called first Destructor of next base class up hierarchy next Continue up hierarchy until final base reached After final base-class destructor, object removed from memory
protected Access Specifier A protected member can be accessed by member functions in its own class or In any class derived from its own class. It cannot be accessed from functions outside these classes such as main( ) function.
The moral is that if you are writing a class that you suspect might be used at any point in the future, as a base class for other classes, then any functions or data that the derived class might need to access should be made protected rather than private. This ensures that class is “ Inheritance Ready ”. Table summarizes the above situation as follows.
Inheritance and Accessibility no no yes private no yes yes protected yes yes yes public Accessible from objects outside class Accessible from derived class Accessible from own class Access Specifier
Overriding member functions
You can use member functions in a derived class that have the same name as those in the base class. You might want to do this so that calls in your program work the same way for objects of both base and derived classes.
Example: function overriding #include <conio.h> #include <iostream.h> class arithmatic { protected : int a,b; public : void add(int x, int y) { a=x; b=y; cout<<&quot; \n Addition of a & b is : &quot;<<(a+b); } };
class operation1 : public arithmatic { public : void add(int x, int y) { cout<<&quot;\n Derived class add function called.\n&quot;; arithmatic :: add(x,y); } };
void main() { operation1 op1; arithmatic arith1; int m,n; cout<<&quot; Enter number 1 :&quot;; cin>>m; cout<<&quot; Enter number 2 :&quot;; cin>>n; op1.add(m,n); cout<<&quot;\n**************************\n&quot;; arith1.add(m,n); getch(); }
The arithmatic and operation1 classes has same functions with the name , the same arguments and same return type . When we call the function from main() as op1.add(m,n); how does the compiler know which function to follow? Answer is when the same function(s) exists in both the base and derived class, the function in the derived class will be executed.
Objects of the base class donot know anything about the derived class and will always use the base class functions as in program the call : arith1.add(m,n); When the functions with the same name appears in base and derived classes we say that derived class function overrides the base class function.
Multiple Inheritance
A class can be derived from more than one base class. This is called multiple inheritance. Figure shows the above comment. Base class A Base class B Derived class C
The syntax for multiple inheritance is similar to that for single inheritance. The above figure relationship is expressed like this : class A // Base Class A { }; class B // Base Class B { }; class C : public A, public B // C is derived from A and B { };
Example: Multiple Inheritance #include <conio.h> #include <iostream.h> class operation1 { protected : int a,b; public : void add(int x, int y) { a=x; b=y; cout<<&quot; \n Addition of a & b is : &quot;<<(a+b); } };
class operation2 { protected : int a,b; public : void sub(int x, int y) { a=x; b=y; cout<<&quot; \n subtraction of a & b is : &quot;<<(a-b); } };
class arithmatic : public operation1, public operation2 { private : int a,b; public : void mul(int x, int y) { a=x; b=y; cout<<&quot; \n Multiplication of a & b is : &quot;<<a*b; } };
void main() { arithmatic arith1; int m,n; cout<<&quot; Enter number 1 :&quot;; cin>>m; cout<<&quot; Enter number 2 :&quot;; cin>>n; arith1.mul(m,n); arith1.add(m,n); arith1.sub(m,n); getch(); }
In the above program arithmatic class inherits two other classes i.e. operation1 and operation2 as follows : class arithmatic : public operation1, public operation2 operation1 class implements addition and operation2 class implements subtraction While arithmatic class implements multiplication which inherits already made classes for addition and subtraction.
What are the possible alternatives to implement division operation (function) in the previous approach of multiple inheritance. Give at least two alternatives?
Ambiguity in Multiple Inheritance Consider the scenario(situation) if two base classes have functions with the same name, while a class derived from both base classes has no function with this name. How do objects of the derived class access the correct base class function? The name of the function alone is insufficient, since the compiler cannot figure out which of the two functions are meant. Next example demonstrates this situation.
class A { public : void show(int x, int y) { cout<<“\n Class A :”; } }; class B { public : void show(int x, int y) { cout<<“\n Class B :”; } }; class C : public A, public B { };
void main() { C obj; obj.show( ); // Ambiguous statement--- will not compile obj.A :: show( ); obj.B :: show( ); getch(); } The problem is resolved using the scope –resolution operator to specify the class in which the function lies. Thus obj.A :: show( ); refers to the version of show( ) that is in the A class while obj.B :: show( ); refers to the function show( ) that is in the B class. The scope resolution operator resolved the ambiguity and the compiler is happy now.
ThanXX…

Lecture 5 Inheritance

  • 1.
    Lecture # 5Inheritance
  • 2.
    Inheritance is probablythe most powerful feature of object oriented programming which is used in achieving software reusability . Inheritance is the process of creating new classes, called derived classes from existing or base classes . The derived class inherits all the capabilities of the base class but can add its own functionalities and refinements. Consider the following figure.
  • 3.
    Derived class (e.g. Horse ) Arrow means “Derived from” Function a Function b Function c Function b Function a defined only in derived class Base class ( e.g. Animal )
  • 4.
    Base class doesnot inherit any class. Derived class does inherit any other class or classes. Single inheritance Derived class only Inherits from one base class Multiple inheritance Derived class can Inherit from multiple base classes In this case base classes may be possibly unrelated from real life point of view
  • 5.
    “ is-a” vs.“has-a” relationship “is-a” Inheritance Derived class object treated as base class object Example: Car is a vehicle Car (derived class) inherits vehicle (base class) “has-a” Composition Object contains one or more objects of other classes as members Example: Car has a steering wheel ( car and wheel are classes )
  • 6.
    Base Classes andDerived Classes
  • 7.
    Inheritance hierarchy foruniversity Community Members. Single inheritance CommunityMember Employee Student Administrator Teacher AdministratorTeacher Staff Faculty Alumnus Single inheritance Single inheritance Multiple inheritance
  • 8.
    Inheritance hierarchy forShapes. Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron
  • 9.
    Base class does not inherit any class. Derived class does inherit any other class or classes.
  • 10.
    Example: Inheritance #include<conio.h> #include <iostream.h> class arithmatic { protected : int a,b; public : void add(int x, int y) { a=x; b=y; cout<<&quot; \n Addition of a & b is : &quot;<<a+b; } };
  • 11.
    class operation1 :public arithmatic { public : void mul(int x, int y) { a=x; b=y; cout<<&quot; \n Multiplication of a & b is :”<<a*b; } };
  • 12.
    void main() {operation1 op1; int m,n; cout<<&quot; Enter number 1 :&quot;; cin>>m; cout<<&quot; Enter number 2 :&quot;; cin>>n; op1.mul(m,n); op1.add(m,n); getch(); }
  • 13.
    Important Note: Onething is to be noted that whenever the object of derived class is created the constructor of both , the derived and base classes are called automatically.
  • 14.
    Constructors and Destructorsin Derived Classes Instantiating derived-class object Chain of constructor calls Derived-class constructor invokes base class constructor Implicitly or explicitly Base class of inheritance hierarchy Last constructor called in chain First constructor body to finish executing Initializing data members Each base-class constructor initializes data members Inherited by derived class
  • 15.
    Destroying derived-class objectChain of destructor calls Reverse order of constructor chain Destructor of derived-class called first Destructor of next base class up hierarchy next Continue up hierarchy until final base reached After final base-class destructor, object removed from memory
  • 16.
    protected AccessSpecifier A protected member can be accessed by member functions in its own class or In any class derived from its own class. It cannot be accessed from functions outside these classes such as main( ) function.
  • 17.
    The moral isthat if you are writing a class that you suspect might be used at any point in the future, as a base class for other classes, then any functions or data that the derived class might need to access should be made protected rather than private. This ensures that class is “ Inheritance Ready ”. Table summarizes the above situation as follows.
  • 18.
    Inheritance and Accessibilityno no yes private no yes yes protected yes yes yes public Accessible from objects outside class Accessible from derived class Accessible from own class Access Specifier
  • 19.
  • 20.
    You can usemember functions in a derived class that have the same name as those in the base class. You might want to do this so that calls in your program work the same way for objects of both base and derived classes.
  • 21.
    Example: function overriding#include <conio.h> #include <iostream.h> class arithmatic { protected : int a,b; public : void add(int x, int y) { a=x; b=y; cout<<&quot; \n Addition of a & b is : &quot;<<(a+b); } };
  • 22.
    class operation1 :public arithmatic { public : void add(int x, int y) { cout<<&quot;\n Derived class add function called.\n&quot;; arithmatic :: add(x,y); } };
  • 23.
    void main() {operation1 op1; arithmatic arith1; int m,n; cout<<&quot; Enter number 1 :&quot;; cin>>m; cout<<&quot; Enter number 2 :&quot;; cin>>n; op1.add(m,n); cout<<&quot;\n**************************\n&quot;; arith1.add(m,n); getch(); }
  • 24.
    The arithmatic andoperation1 classes has same functions with the name , the same arguments and same return type . When we call the function from main() as op1.add(m,n); how does the compiler know which function to follow? Answer is when the same function(s) exists in both the base and derived class, the function in the derived class will be executed.
  • 25.
    Objects of thebase class donot know anything about the derived class and will always use the base class functions as in program the call : arith1.add(m,n); When the functions with the same name appears in base and derived classes we say that derived class function overrides the base class function.
  • 26.
  • 27.
    A class canbe derived from more than one base class. This is called multiple inheritance. Figure shows the above comment. Base class A Base class B Derived class C
  • 28.
    The syntax formultiple inheritance is similar to that for single inheritance. The above figure relationship is expressed like this : class A // Base Class A { }; class B // Base Class B { }; class C : public A, public B // C is derived from A and B { };
  • 29.
    Example: Multiple Inheritance#include <conio.h> #include <iostream.h> class operation1 { protected : int a,b; public : void add(int x, int y) { a=x; b=y; cout<<&quot; \n Addition of a & b is : &quot;<<(a+b); } };
  • 30.
    class operation2 {protected : int a,b; public : void sub(int x, int y) { a=x; b=y; cout<<&quot; \n subtraction of a & b is : &quot;<<(a-b); } };
  • 31.
    class arithmatic :public operation1, public operation2 { private : int a,b; public : void mul(int x, int y) { a=x; b=y; cout<<&quot; \n Multiplication of a & b is : &quot;<<a*b; } };
  • 32.
    void main() {arithmatic arith1; int m,n; cout<<&quot; Enter number 1 :&quot;; cin>>m; cout<<&quot; Enter number 2 :&quot;; cin>>n; arith1.mul(m,n); arith1.add(m,n); arith1.sub(m,n); getch(); }
  • 33.
    In the aboveprogram arithmatic class inherits two other classes i.e. operation1 and operation2 as follows : class arithmatic : public operation1, public operation2 operation1 class implements addition and operation2 class implements subtraction While arithmatic class implements multiplication which inherits already made classes for addition and subtraction.
  • 34.
    What are thepossible alternatives to implement division operation (function) in the previous approach of multiple inheritance. Give at least two alternatives?
  • 35.
    Ambiguity in MultipleInheritance Consider the scenario(situation) if two base classes have functions with the same name, while a class derived from both base classes has no function with this name. How do objects of the derived class access the correct base class function? The name of the function alone is insufficient, since the compiler cannot figure out which of the two functions are meant. Next example demonstrates this situation.
  • 36.
    class A {public : void show(int x, int y) { cout<<“\n Class A :”; } }; class B { public : void show(int x, int y) { cout<<“\n Class B :”; } }; class C : public A, public B { };
  • 37.
    void main() {C obj; obj.show( ); // Ambiguous statement--- will not compile obj.A :: show( ); obj.B :: show( ); getch(); } The problem is resolved using the scope –resolution operator to specify the class in which the function lies. Thus obj.A :: show( ); refers to the version of show( ) that is in the A class while obj.B :: show( ); refers to the function show( ) that is in the B class. The scope resolution operator resolved the ambiguity and the compiler is happy now.
  • 38.