SlideShare a Scribd company logo
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…

More Related Content

PPTX
Friend functions
PPTX
constructors and destructors in c++
PPTX
Virtual base class
PDF
C++ Multiple Inheritance
PPTX
inheritance c++
PDF
04. constructor & destructor
PDF
C++ questions And Answer
PPTX
A well-typed program never goes wrong
Friend functions
constructors and destructors in c++
Virtual base class
C++ Multiple Inheritance
inheritance c++
04. constructor & destructor
C++ questions And Answer
A well-typed program never goes wrong

What's hot (20)

PPTX
Constructors and destructors
PPT
C by balaguruswami - e.balagurusamy
PPTX
C Language (All Concept)
PPT
Operator overloading
PDF
05. inheritance
PPTX
C++ language
PPT
Object Oriented Technologies
PDF
‘go-to’ general-purpose sequential collections - from Java To Scala
PPTX
Chapter 7:Understanding Class Inheritance
PPTX
Tokens expressionsin C++
PPT
C++ OOP Implementation
PDF
Introduction to database-ER Model
PPT
Visula C# Programming Lecture 8
PPTX
Interoduction to c++
PPTX
Chapter 4:Object-Oriented Basic Concepts
PDF
C++ interview question
PPT
C++ Interview Questions
PPTX
Java abstract class & abstract methods
PDF
Top C Language Interview Questions and Answer
PPT
01 c++ Intro.ppt
Constructors and destructors
C by balaguruswami - e.balagurusamy
C Language (All Concept)
Operator overloading
05. inheritance
C++ language
Object Oriented Technologies
‘go-to’ general-purpose sequential collections - from Java To Scala
Chapter 7:Understanding Class Inheritance
Tokens expressionsin C++
C++ OOP Implementation
Introduction to database-ER Model
Visula C# Programming Lecture 8
Interoduction to c++
Chapter 4:Object-Oriented Basic Concepts
C++ interview question
C++ Interview Questions
Java abstract class & abstract methods
Top C Language Interview Questions and Answer
01 c++ Intro.ppt
Ad

Viewers also liked (6)

PPTX
Inheritance in oops
PPT
C++ Inheritance
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
PPTX
Inheritance
PPTX
Inheritance in c++
PPTX
Inheritance in C++
Inheritance in oops
C++ Inheritance
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance
Inheritance in c++
Inheritance in C++
Ad

Similar to Lecture 5 Inheritance (20)

PPT
Inheritance
PPT
Lecture 7 Templates, Friend Classes
PPT
3 functions and class
PPT
C++ polymorphism
PPTX
Chapter 2 OOP using C++ (Introduction).pptx
PPT
Java Generics
ODP
Ppt of c++ vs c#
PPTX
‫Chapter3 inheritance
PDF
Implementation of oop concept in c++
PPTX
OOPS IN C++
PPTX
Object Oriented Programming using C++ Unit 1
PPTX
05 Object Oriented Concept Presentation.pptx
PPTX
CPP Homework Help
PDF
Java Basic day-2
PPT
Inheritance, polymorphisam, abstract classes and composition)
PPTX
Inheritance
PDF
Object Oriented Programming notes provided
PPTX
Object Oriented Programming using C++(UNIT 1)
PDF
chapter-10-inheritance.pdf
PDF
Chapter27 polymorphism-virtual-function-abstract-class
Inheritance
Lecture 7 Templates, Friend Classes
3 functions and class
C++ polymorphism
Chapter 2 OOP using C++ (Introduction).pptx
Java Generics
Ppt of c++ vs c#
‫Chapter3 inheritance
Implementation of oop concept in c++
OOPS IN C++
Object Oriented Programming using C++ Unit 1
05 Object Oriented Concept Presentation.pptx
CPP Homework Help
Java Basic day-2
Inheritance, polymorphisam, abstract classes and composition)
Inheritance
Object Oriented Programming notes provided
Object Oriented Programming using C++(UNIT 1)
chapter-10-inheritance.pdf
Chapter27 polymorphism-virtual-function-abstract-class

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PPTX
Cloud computing and distributed systems.
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Empathic Computing: Creating Shared Understanding
PDF
cuic standard and advanced reporting.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation theory and applications.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Understanding_Digital_Forensics_Presentation.pptx
Electronic commerce courselecture one. Pdf
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
Cloud computing and distributed systems.
Network Security Unit 5.pdf for BCA BBA.
20250228 LYD VKU AI Blended-Learning.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation_ Review paper, used for researhc scholars
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Empathic Computing: Creating Shared Understanding
cuic standard and advanced reporting.pdf
Unlocking AI with Model Context Protocol (MCP)
Encapsulation theory and applications.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Lecture 5 Inheritance

  • 1. Lecture # 5 Inheritance
  • 2. 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.
  • 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 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
  • 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 and Derived Classes
  • 7. Inheritance hierarchy for university Community Members. Single inheritance CommunityMember Employee Student Administrator Teacher AdministratorTeacher Staff Faculty Alumnus Single inheritance Single inheritance Multiple inheritance
  • 8. Inheritance hierarchy for Shapes. 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: 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.
  • 14. 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
  • 15. 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
  • 16. 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.
  • 17. 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.
  • 18. 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
  • 20. 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.
  • 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 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.
  • 25. 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.
  • 27. 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
  • 28. 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 { };
  • 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 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.
  • 34. What are the possible alternatives to implement division operation (function) in the previous approach of multiple inheritance. Give at least two alternatives?
  • 35. 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.
  • 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.