SlideShare ist ein Scribd-Unternehmen logo
1 von 27
NAME - RITIK
ROLL NO. - UE198077
SUBJECT - PROGRAMMING FUNDAMENTAL
SUBMITTED TO - SUKHVIR SIR
TOPIC - INHERITANCE: EXTENDING CLASSES
B.E.(IT),1st year
Inheritance:
 The mechanism of deriving a new class from an old one is called Inheritance.
 The old class is referred to as the base class.
 The new one is called the derived class or subclass.
General form of defining a derived class
Syntax:
Class derived-class-name : visibility mode base-class-name
{
……..//
…….// members of derived class
…….//
};
The colon indicate that the derived-class-name is derived from the base-class-name. The visibility mode is
optional and, if present, may be either private or public. The default visibility mode is private.
Visibility mode specifies whether the features of the base class are privately derived or publicly derived.
Types of Inheritance
 Single Inheritance
 Multiple Inheritance
 Hierarchical Inheritance
 Multilevel Inheritance
 Hybrid Inheritance
Single Inheritance
 In single inheritance there exists single
base class and single derived class.
 It is the most simplest form of
inheritance.
Example of single inheritance
Output
Multiple inheritance
 Multiple inheritance occurs when a class
is inherit from more than one base class.
 Derived class can inherit features from
multiple base classes using multiple
inheritance.
Example of multiple inheritance
Output
Hierarchical inheritance
In this type of inheritance, more than one sub
class is inherited from a single base class. i.e.
more than one derived class is created from a
single base class.
Example of hierarchical inheritance
Output
Multilevel inheritance
In this type of inheritance, a derived class is
created from another derived class.
The class C serves as a base class for the
derived class B, which in turn serves as a base
class for the derived class A. The class B as
intermediate base class since it provides a link
for the inheritance between A and C the chain
ABC is known as inheritance path.
Class C {………}; //base class
Class B: public C {………}; //B derived from C
Class A: public B {……}; //A derived form B
Example of Multilevel inheritance
Output
Hybrid inheritance
Hybrid inheritance is implemented by
combining more than one type of
inheritance. For example: combining
hierarchical inheritance and multiple
inheritance.
Example of Hybrid inheritance
Output
Virtual base class
Consider a situation where three kind of inheritance, namely, multilevel, multiple and
hierarchical inheritance, are involved. This is illustrated in the figure.
The ‘child’ has two direct base classes ‘parent1’ and
‘Parent2’ which themselves have a common base class
‘grand parent’. The ‘child’ inherits the traits of ‘grandparent’
via two separate paths. It can also inherits the base class
directly. The ‘grand parent’ is sometimes referred to as the indirect base class.
This means ‘child’ is inherit the same property twice. To remove this duplication we
declare the base class ‘grandparent’ as the virtual while declaring the direct or
intermediate base class as shown:
Syntax
class A //grandparent
{ …….};
Class B1:virtual public A //parent1
{……..};
Class B2:public virtual A //parent2
{…….};
Class C:public B1,public B2 //child
{…….}; //only one copy of A will be inherited
Abstract classes
An abstract class is one that is not used to create objects. An abstract class is
designed only to act as a base class (to be inherited by other classes). It is a design
concept in program development and provides a base upon which other classes may
be built.
The concept can be explained through a simple example of an abstract base class
vehicle which may be declared in a program for deriving LMV (light motor vehicle),HMV
(heavy motor vehicle) and TW(two wheeler) derived classes. A pure virtual function
spec() may be set the specification of LMV as per its four wheel and smaller engine
and that of HMV as per its eight or more wheels and relatively larger engine.
Example
Class vehicle //abstract base class
{ private:
Datatype d1,d2;
public:
virtual void spec()=0; //pure virtual funtion
};
Class LMV : public vehicle
{ public:
void spec(){ ……. } //LMV definition of spec function
};
Example
Class HMV : public vehicle
{ public:
void spec(){ …….. } HMV definition of spec function
};
Class TW : public vehicle
{ public:
void spec(){ …….. } //TW definition of spec function
};
Advantages of inheritance
 Inheritance promote reusability. When a class inherits or derives another class, it
can access all functionality of inherited class.
 Reusability enhanced reliability. the base class code is already tested and
debugged.
 Inheritance helps to reduce code redundancy and supports code extensibility.
Disadvantages of inheritance
 Inherited functions work slower than the normal function as there is indirection.
 Improper use of inheritance lead to wrong solution.
 Inheritance increases the coupling between base class and derived class. A change
in base class will affects all the child classes.
Ritik (inheritance.cpp)

Weitere ähnliche Inhalte

Was ist angesagt?

Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
deepakskb2013
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
harshaltambe
 

Was ist angesagt? (20)

Inheritance
InheritanceInheritance
Inheritance
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
 
Oop inheritance
Oop inheritanceOop inheritance
Oop inheritance
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
OOP
OOPOOP
OOP
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance access specifier in oop
Inheritance access specifier in oopInheritance access specifier in oop
Inheritance access specifier in oop
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Inheritance
Inheritance Inheritance
Inheritance
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
 

Ähnlich wie Ritik (inheritance.cpp)

Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
JP2B1197685ARamSaiPM
 
E -COMMERCE.ppt
E -COMMERCE.pptE -COMMERCE.ppt
E -COMMERCE.ppt
classall
 

Ähnlich wie Ritik (inheritance.cpp) (20)

Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
 
E -COMMERCE.ppt
E -COMMERCE.pptE -COMMERCE.ppt
E -COMMERCE.ppt
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
iheritence.pptx
iheritence.pptxiheritence.pptx
iheritence.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritence
inheritenceinheritence
inheritence
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsInheritance and Polymorphism in Oops
Inheritance and Polymorphism in Oops
 
Inheritance
InheritanceInheritance
Inheritance
 

Kürzlich hochgeladen

AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 

Kürzlich hochgeladen (20)

Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 

Ritik (inheritance.cpp)

  • 1. NAME - RITIK ROLL NO. - UE198077 SUBJECT - PROGRAMMING FUNDAMENTAL SUBMITTED TO - SUKHVIR SIR TOPIC - INHERITANCE: EXTENDING CLASSES B.E.(IT),1st year
  • 2. Inheritance:  The mechanism of deriving a new class from an old one is called Inheritance.  The old class is referred to as the base class.  The new one is called the derived class or subclass.
  • 3. General form of defining a derived class Syntax: Class derived-class-name : visibility mode base-class-name { ……..// …….// members of derived class …….// }; The colon indicate that the derived-class-name is derived from the base-class-name. The visibility mode is optional and, if present, may be either private or public. The default visibility mode is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived.
  • 4. Types of Inheritance  Single Inheritance  Multiple Inheritance  Hierarchical Inheritance  Multilevel Inheritance  Hybrid Inheritance
  • 5. Single Inheritance  In single inheritance there exists single base class and single derived class.  It is the most simplest form of inheritance.
  • 6. Example of single inheritance
  • 8. Multiple inheritance  Multiple inheritance occurs when a class is inherit from more than one base class.  Derived class can inherit features from multiple base classes using multiple inheritance.
  • 9. Example of multiple inheritance
  • 11. Hierarchical inheritance In this type of inheritance, more than one sub class is inherited from a single base class. i.e. more than one derived class is created from a single base class.
  • 12. Example of hierarchical inheritance
  • 14. Multilevel inheritance In this type of inheritance, a derived class is created from another derived class. The class C serves as a base class for the derived class B, which in turn serves as a base class for the derived class A. The class B as intermediate base class since it provides a link for the inheritance between A and C the chain ABC is known as inheritance path. Class C {………}; //base class Class B: public C {………}; //B derived from C Class A: public B {……}; //A derived form B
  • 15. Example of Multilevel inheritance
  • 17. Hybrid inheritance Hybrid inheritance is implemented by combining more than one type of inheritance. For example: combining hierarchical inheritance and multiple inheritance.
  • 18. Example of Hybrid inheritance
  • 20. Virtual base class Consider a situation where three kind of inheritance, namely, multilevel, multiple and hierarchical inheritance, are involved. This is illustrated in the figure. The ‘child’ has two direct base classes ‘parent1’ and ‘Parent2’ which themselves have a common base class ‘grand parent’. The ‘child’ inherits the traits of ‘grandparent’ via two separate paths. It can also inherits the base class directly. The ‘grand parent’ is sometimes referred to as the indirect base class. This means ‘child’ is inherit the same property twice. To remove this duplication we declare the base class ‘grandparent’ as the virtual while declaring the direct or intermediate base class as shown:
  • 21. Syntax class A //grandparent { …….}; Class B1:virtual public A //parent1 {……..}; Class B2:public virtual A //parent2 {…….}; Class C:public B1,public B2 //child {…….}; //only one copy of A will be inherited
  • 22. Abstract classes An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class (to be inherited by other classes). It is a design concept in program development and provides a base upon which other classes may be built. The concept can be explained through a simple example of an abstract base class vehicle which may be declared in a program for deriving LMV (light motor vehicle),HMV (heavy motor vehicle) and TW(two wheeler) derived classes. A pure virtual function spec() may be set the specification of LMV as per its four wheel and smaller engine and that of HMV as per its eight or more wheels and relatively larger engine.
  • 23. Example Class vehicle //abstract base class { private: Datatype d1,d2; public: virtual void spec()=0; //pure virtual funtion }; Class LMV : public vehicle { public: void spec(){ ……. } //LMV definition of spec function };
  • 24. Example Class HMV : public vehicle { public: void spec(){ …….. } HMV definition of spec function }; Class TW : public vehicle { public: void spec(){ …….. } //TW definition of spec function };
  • 25. Advantages of inheritance  Inheritance promote reusability. When a class inherits or derives another class, it can access all functionality of inherited class.  Reusability enhanced reliability. the base class code is already tested and debugged.  Inheritance helps to reduce code redundancy and supports code extensibility.
  • 26. Disadvantages of inheritance  Inherited functions work slower than the normal function as there is indirection.  Improper use of inheritance lead to wrong solution.  Inheritance increases the coupling between base class and derived class. A change in base class will affects all the child classes.

Hinweis der Redaktion

  1. NOTE: To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image.