SlideShare a Scribd company logo
1 of 39
INHERITANCE
SESSION 1
SHIJITHA A S
CONTENTS
 Introduction
 Access specifiers and inheritance
 Types of inheritance
 Virtual base class
 Abstract classes
 Advantages & disadvantages - inheritance
2
3
Grand Parents
PARENT 1 PARENT 2
CHILD 11 CHILD 12 CHILD 21 CHILD 22
PARENT – CHILD RELATIONSHIP
INTRODUCTION
 INHERITANCE - An essential characteristics of object oriented programming.
 Existing classes are main components.
 New classes are created from existing class.
 i.e. properties of existing class are extended to new class.
 Existing classes are called BASE CLASS.
 Newley created classes are called DERIVED CLASS.
 Definition : The procedure of creating a new class from one or more existing
classes is termed as inheritance.
 Relationship between base and derived class is known as KIND OF
RELATIONSHIP.
 Accessibility : Object of derived class can access members of base class as well as
derived class, but object of base class cannot access members of derived classes.
4
5
MEMBER A
MEMBER B
MEMBER A
MEMBER B
MEMBER C
MEMBER D
BASE
CLASS
DERIVED CLASS
 REUSABILITY :It means the reuse of properties of base class in the derived
class.
 Reusability is achieved using inheritance.
 Base class is also called as : super class, parent class or ancestor class
 Derived class is also called as sub class, child class or descendent class.
 A class can be derived for more than one class and also it is possible to
derive a class from previously derived class.
6
7
MEMBER A
MEMBER B
MEMBER A
MEMBER B
MEMBER B
MEMBER C
BASE
CLASS
DERIVED CLASS
DERIVED CLASS
MEMBER A
MEMBER C
MEMBER D
ACCESS SPECIFIERS AND INHERITANCE
 Access specifiers : public, private and protected.
 Public: object can access the public members of the class
 Private: object cannot access the private members of the class
 Protected: only object of derived class can access the protected members of the
base class
Syntax :
class_name of derived class : access specifier name of base_class
{
…………
………….
………….
};
8
e.g.
(1) Class B : public A
{
//members of class B
};
Here class A – base class, class B --- derived
class which is derived publicly.
 Object of class B can access all the public
members of class A.
(2) Class B : private A //private derivation
{
//members of class B
};
Here class A – base class, class B --- derived
class which is derived privately.
(3) Class B : A //by default private
derivation
{
//members of class B
};
Here class A – base class, class B --- derived class
which is derived privately.
(4) Class B : protected A //protected
derivation
{
//members of class B
};
Here class A – base class, class B --- derived class
which is derived protected.
9
 When class is derived publicly, then all the public members of the base class can be
accessed directly in the derived class and it cannot access the private members of
the base class.
10
PUBLIC INHERITANCE
Public members
Private members
OBJECT
Base class Derived class
PUBLIC INHERITANCE
e.g.
Class A
{
public:
int x;
};
Class B : public A //derived class
{
public :
int y;
};
Void main()
{
B b;
b.x = 20;
b.y = 30;
cout<<“n Member of A : “<<b.x;
cout<<“n Member of B : “<<b.y;
}
Output :
Member of A :20
Member of B :30
11
 Object of privately derived class cannot directly access the public members of base
class
 Thus member functions are used to access members
12
PRIVATE INHERITANCE
Public members
OBJECT
Base class Derived class
Public members
functions
Private members
PRIVATE INHERITANCE
e.g.
Class A
{
public:
int x;
};
Class B : private A //derived class
{
public :
int y;
B( ) //constructor
{
x = 20;
y = 40;
}
void show( )
{ cout<<“n X= “<<x;
cout<< “n Y= “<<y;
}
};
Void main()
{
B b; //object creation
b . show( );
}
Output :
X =20
Y=40
13
 Object of privately derived class cannot directly access the public members of base
class
 Thus member functions are used to access members
14
PROTECTED INHERITANCE
Public members
OBJECT
Base class Derived class
Public members
functions
Private members
PROTECTED INHERITANCE
e.g.
Class A
{
public:
int x;
};
Class B : private A //derived class
{
public :
int y;
B( ) //constructor
{
x = 20;
y = 40;
}
void show( )
{ cout<<“n X= “<<x;
cout<< “n Y= “<<y;
}
};
Void main()
{
B b; //object creation
b . show( );
}
Output :
X =20
Y=40
15
TYPES OF INHERITANCE
1) Single Inheritance
2) Multiple Inheritance
3) Hierarchical Inheritance
4) Multilevel Inheritance
5) Hybrid Inheritance
6) Multipath Inheritance
16
SINGLE INHERITANCE
 Only one class is derived from a single class
 Further the derived class is not used as a base class.
 Newley created class receives entire characteristics of base class.
17
Class ABC Base class
Class abc Derived class
class Vehicle //base class
{
public:
void show()
{
cout<< "This is a Vehicle";
}
};
class Car : public Vehicle //derived
{
public:
void print()
{
cout<< "This is a Car";
}
};
18
Void main()
{
Car obj;
obj.show();
obj.print();
}
OUTPUT:
This is a Vehicle
This is a Car
Class Vehicle
Class Car
MULTIPLE INHERITANCE
 A class is derived from more than one base classes
19
Class A
Class E Derived class
Class B Class C
20
Class Vehicle
Class Car Derived class
Class Four_wheeler
Base classes
class Vehicle //base
{
public:
void show()
{
cout<< "This is a Vehicle";
}
};
class Four_Wheeler //base
{
public:
void display()
{
cout<< "This is a Four wheeler";
}
};
21
class Car : public Vehicle, Four_wheeler
{
public:
void print()
{
cout<< "This is a Car";
}
};
Void main()
{
Car obj;
obj.show();
obj.display();
obj.print();
}
OUTPUT:
This is a Vehicle
This is a Four wheeler
This is a Car
HIERARCHICAL INHERITANCE
 A- Base class, B,C & D derived
 Further B,C & D are not used for deriving classes
22
Class A
Class C
Class B Class D
23
Class Vehicle
Class Bus
Derived class
Class Car
Base classes
class Vehicle
{
public:
void show()
{
cout<< "This is a Vehicle";
}
};
class Car : public Vehicle
{
public:
void display()
{
cout<< "This is a Car";
}
};
24
class Bus : public Vehicle
{
public:
void print()
{
cout<< "This is a Bus";
}
};
Void main()
{
Car obj1;
Bus obj2;
obj1.show();
obj1.display();
obj2.show();
obj2.print();
}
OUTPUT:
This is a Vehicle
This is a Car
This is a Vehicle
This is a Bus
MULTILEVEL INHERITANCE
 A- Base class, B derived from A , C- derived form C
 C inherits all the features of A & B.
25
Class A
Class C
Class B
Base class1
Base class2
26
Class Vehicle
Class Four_Wheeler
Class Car
Base class1
Base class2
Derived class
class Vehicle
{
public:
void show()
{
cout<< "This is a Vehicle";
}
};
class Four_Wheel: public Vehicle
{
public:
void display()
{
cout<< "This is a Car";
}
};
27
class Car : public Four_wheel
{
public:
void print()
{
cout<< "This is a Bus";
}
};
Void main()
{
Car obj1;
Bus obj2;
obj1.show();
obj1.display();
obj2.show();
obj2.print();
}
OUTPUT:
This is a Vehicle
This is a Car
This is a Vehicle
This is a Bus
HYBRID INHERITANCE
 A- Base class, B derived from A , Class D - derived form B & C
 D inherits all the features of B & C directly and inherits features of class A indirectly
through B
28
Class A
Class D
Class B
Base class
Class C
Derived class
29
Class Vehicle
Class Four_Wheeler
Class Bus
Class Fare
Base classes
Derived classes
class Vehicle
{
public:
void show()
{
cout<< “This is a Vehicle";
}
};
class Four_Wheel: public Vehicle
{
public:
void display()
{
cout<< "This is a Four_Wheeler";
}
};
class Fare
{
public:
void dip()
{
cout<< “Fare is high";
}
};
30
class Bus : public Four_wheel, Fare
{
public:
void print()
{
cout<< “This is a Bus";
}
};
Void main()
{
Bus obj;
obj.print();
obj.dip();
obj.display();
obj.show();
}
OUTPUT:
This is a Bus
Fare is high
This is a Four Wheeler
This is a Vehicle
MULTIPATH INHERITANCE
 A- Base class, B and C derived from A , Class D - derived form B & C
 D inherits all the features of B & C directly and inherits features of class A indirectly
through B & C
 Ambiguity occurs : because the main base class is inherited twice.
 To avoid ambiguity a keyword “virtual” is used.
31
Class A
Class D
Class B
Main Base class
Class C
Derived class
32
Class Vehicle
Class Four_Wheeler
Class Bus
Class Fare
Base classes
Derived classes
class Vehicle
{
public:
void show()
{
cout<< “This is a Vehicle";
}
};
class Four_Wheel: public Vehicle
{
public:
void display()
{
cout<< "This is a Four_Wheeler";
}
};
class Fare : public Vehicle
{
public:
void dip()
{
cout<< “Fare is high";
}
};
33
class Bus : public Four_wheel, Fare
{
public:
void print()
{
cout<< “This is a Bus";
}
};
Void main()
{
Bus obj;
obj.print();
obj.dip();
obj.display();
obj.show();
}
OUTPUT:
This is a Bus
Fare is high
This is a Four Wheeler
This is a Vehicle
VIRTUAL BASE CLASS
 In multipath inheritance child class could have duplicate sets of members
inherited from a single base class.
 Use : To avoid ambiguity due to multipath inheritance.
 When classes are declared virtual  complier takes essential caution to
avoid duplication.
 Keyword “virtual” is used with the base class when it is inherited.
34
e.g.
Class A1
{
protected:
int a1;
};
Class A2 : virtual public A1
{
protected:
int a2;
};
Class A3 : virtual public A1
{
protected:
int a3;
};
35
e.g.
Class A4 : public A2,A3
{
protected:
int a4;
};
 Here using object of A4, it can access
a1,a2,a3 and a4.
Class A1
Class A4
Class A3
Class A2
ABSTRACT CLASS
 when a class is not used for creating objects then it is called abstract class.
 Abstract class can act as base class only.
 It is used for inheriting and not used for object creation.
 An abstract class gives a skeleton or structure using which other classes are
created.
36
37
Class A
{
public:
int x ;
class B
{
public:
int y ;
};
};
Class C : public A, A :: B
{
public:
int z ;
void show ( )
{
cout<<“X=“<<x;
cout<<“Y=“<<y;
cout<<“Z=“<<z;
}
C ( int j, int k, int l)
{
x=j;
y=k;
z=l;
}
};
Void main ( )
{
C c (4,7,1);
c.show();
}
OUTPUT:
X = 4
Y = 7
Z = 1
ADVANTAGES
 Provides reusability
 The derived classes extend the properties of base class
 Generate more dominant objects.
 Same Base class can be used by number of derived class.
 All Derived class has same properties of base class.
38
DISADVANTAGES
 Inappropriate use of inheritance makes programs more
complex.
 Invoking member functions using objects create more
compiler overhead.
 In Class hierarchy various data elements remains unused.
 All Memory allocated is not utilized.
39

More Related Content

Similar to MODULE2_INHERITANCE_SESSION1.ppt computer

Similar to MODULE2_INHERITANCE_SESSION1.ppt computer (20)

11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
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
 
Inheritance
InheritanceInheritance
Inheritance
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
iheritence.pptx
iheritence.pptxiheritence.pptx
iheritence.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
Inheritance
Inheritance Inheritance
Inheritance
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
c++Inheritance.pdf
c++Inheritance.pdfc++Inheritance.pdf
c++Inheritance.pdf
 

Recently uploaded

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 

Recently uploaded (20)

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 

MODULE2_INHERITANCE_SESSION1.ppt computer

  • 2. CONTENTS  Introduction  Access specifiers and inheritance  Types of inheritance  Virtual base class  Abstract classes  Advantages & disadvantages - inheritance 2
  • 3. 3 Grand Parents PARENT 1 PARENT 2 CHILD 11 CHILD 12 CHILD 21 CHILD 22 PARENT – CHILD RELATIONSHIP
  • 4. INTRODUCTION  INHERITANCE - An essential characteristics of object oriented programming.  Existing classes are main components.  New classes are created from existing class.  i.e. properties of existing class are extended to new class.  Existing classes are called BASE CLASS.  Newley created classes are called DERIVED CLASS.  Definition : The procedure of creating a new class from one or more existing classes is termed as inheritance.  Relationship between base and derived class is known as KIND OF RELATIONSHIP.  Accessibility : Object of derived class can access members of base class as well as derived class, but object of base class cannot access members of derived classes. 4
  • 5. 5 MEMBER A MEMBER B MEMBER A MEMBER B MEMBER C MEMBER D BASE CLASS DERIVED CLASS
  • 6.  REUSABILITY :It means the reuse of properties of base class in the derived class.  Reusability is achieved using inheritance.  Base class is also called as : super class, parent class or ancestor class  Derived class is also called as sub class, child class or descendent class.  A class can be derived for more than one class and also it is possible to derive a class from previously derived class. 6
  • 7. 7 MEMBER A MEMBER B MEMBER A MEMBER B MEMBER B MEMBER C BASE CLASS DERIVED CLASS DERIVED CLASS MEMBER A MEMBER C MEMBER D
  • 8. ACCESS SPECIFIERS AND INHERITANCE  Access specifiers : public, private and protected.  Public: object can access the public members of the class  Private: object cannot access the private members of the class  Protected: only object of derived class can access the protected members of the base class Syntax : class_name of derived class : access specifier name of base_class { ………… …………. …………. }; 8
  • 9. e.g. (1) Class B : public A { //members of class B }; Here class A – base class, class B --- derived class which is derived publicly.  Object of class B can access all the public members of class A. (2) Class B : private A //private derivation { //members of class B }; Here class A – base class, class B --- derived class which is derived privately. (3) Class B : A //by default private derivation { //members of class B }; Here class A – base class, class B --- derived class which is derived privately. (4) Class B : protected A //protected derivation { //members of class B }; Here class A – base class, class B --- derived class which is derived protected. 9
  • 10.  When class is derived publicly, then all the public members of the base class can be accessed directly in the derived class and it cannot access the private members of the base class. 10 PUBLIC INHERITANCE Public members Private members OBJECT Base class Derived class
  • 11. PUBLIC INHERITANCE e.g. Class A { public: int x; }; Class B : public A //derived class { public : int y; }; Void main() { B b; b.x = 20; b.y = 30; cout<<“n Member of A : “<<b.x; cout<<“n Member of B : “<<b.y; } Output : Member of A :20 Member of B :30 11
  • 12.  Object of privately derived class cannot directly access the public members of base class  Thus member functions are used to access members 12 PRIVATE INHERITANCE Public members OBJECT Base class Derived class Public members functions Private members
  • 13. PRIVATE INHERITANCE e.g. Class A { public: int x; }; Class B : private A //derived class { public : int y; B( ) //constructor { x = 20; y = 40; } void show( ) { cout<<“n X= “<<x; cout<< “n Y= “<<y; } }; Void main() { B b; //object creation b . show( ); } Output : X =20 Y=40 13
  • 14.  Object of privately derived class cannot directly access the public members of base class  Thus member functions are used to access members 14 PROTECTED INHERITANCE Public members OBJECT Base class Derived class Public members functions Private members
  • 15. PROTECTED INHERITANCE e.g. Class A { public: int x; }; Class B : private A //derived class { public : int y; B( ) //constructor { x = 20; y = 40; } void show( ) { cout<<“n X= “<<x; cout<< “n Y= “<<y; } }; Void main() { B b; //object creation b . show( ); } Output : X =20 Y=40 15
  • 16. TYPES OF INHERITANCE 1) Single Inheritance 2) Multiple Inheritance 3) Hierarchical Inheritance 4) Multilevel Inheritance 5) Hybrid Inheritance 6) Multipath Inheritance 16
  • 17. SINGLE INHERITANCE  Only one class is derived from a single class  Further the derived class is not used as a base class.  Newley created class receives entire characteristics of base class. 17 Class ABC Base class Class abc Derived class
  • 18. class Vehicle //base class { public: void show() { cout<< "This is a Vehicle"; } }; class Car : public Vehicle //derived { public: void print() { cout<< "This is a Car"; } }; 18 Void main() { Car obj; obj.show(); obj.print(); } OUTPUT: This is a Vehicle This is a Car Class Vehicle Class Car
  • 19. MULTIPLE INHERITANCE  A class is derived from more than one base classes 19 Class A Class E Derived class Class B Class C
  • 20. 20 Class Vehicle Class Car Derived class Class Four_wheeler Base classes
  • 21. class Vehicle //base { public: void show() { cout<< "This is a Vehicle"; } }; class Four_Wheeler //base { public: void display() { cout<< "This is a Four wheeler"; } }; 21 class Car : public Vehicle, Four_wheeler { public: void print() { cout<< "This is a Car"; } }; Void main() { Car obj; obj.show(); obj.display(); obj.print(); } OUTPUT: This is a Vehicle This is a Four wheeler This is a Car
  • 22. HIERARCHICAL INHERITANCE  A- Base class, B,C & D derived  Further B,C & D are not used for deriving classes 22 Class A Class C Class B Class D
  • 23. 23 Class Vehicle Class Bus Derived class Class Car Base classes
  • 24. class Vehicle { public: void show() { cout<< "This is a Vehicle"; } }; class Car : public Vehicle { public: void display() { cout<< "This is a Car"; } }; 24 class Bus : public Vehicle { public: void print() { cout<< "This is a Bus"; } }; Void main() { Car obj1; Bus obj2; obj1.show(); obj1.display(); obj2.show(); obj2.print(); } OUTPUT: This is a Vehicle This is a Car This is a Vehicle This is a Bus
  • 25. MULTILEVEL INHERITANCE  A- Base class, B derived from A , C- derived form C  C inherits all the features of A & B. 25 Class A Class C Class B Base class1 Base class2
  • 26. 26 Class Vehicle Class Four_Wheeler Class Car Base class1 Base class2 Derived class
  • 27. class Vehicle { public: void show() { cout<< "This is a Vehicle"; } }; class Four_Wheel: public Vehicle { public: void display() { cout<< "This is a Car"; } }; 27 class Car : public Four_wheel { public: void print() { cout<< "This is a Bus"; } }; Void main() { Car obj1; Bus obj2; obj1.show(); obj1.display(); obj2.show(); obj2.print(); } OUTPUT: This is a Vehicle This is a Car This is a Vehicle This is a Bus
  • 28. HYBRID INHERITANCE  A- Base class, B derived from A , Class D - derived form B & C  D inherits all the features of B & C directly and inherits features of class A indirectly through B 28 Class A Class D Class B Base class Class C Derived class
  • 29. 29 Class Vehicle Class Four_Wheeler Class Bus Class Fare Base classes Derived classes
  • 30. class Vehicle { public: void show() { cout<< “This is a Vehicle"; } }; class Four_Wheel: public Vehicle { public: void display() { cout<< "This is a Four_Wheeler"; } }; class Fare { public: void dip() { cout<< “Fare is high"; } }; 30 class Bus : public Four_wheel, Fare { public: void print() { cout<< “This is a Bus"; } }; Void main() { Bus obj; obj.print(); obj.dip(); obj.display(); obj.show(); } OUTPUT: This is a Bus Fare is high This is a Four Wheeler This is a Vehicle
  • 31. MULTIPATH INHERITANCE  A- Base class, B and C derived from A , Class D - derived form B & C  D inherits all the features of B & C directly and inherits features of class A indirectly through B & C  Ambiguity occurs : because the main base class is inherited twice.  To avoid ambiguity a keyword “virtual” is used. 31 Class A Class D Class B Main Base class Class C Derived class
  • 32. 32 Class Vehicle Class Four_Wheeler Class Bus Class Fare Base classes Derived classes
  • 33. class Vehicle { public: void show() { cout<< “This is a Vehicle"; } }; class Four_Wheel: public Vehicle { public: void display() { cout<< "This is a Four_Wheeler"; } }; class Fare : public Vehicle { public: void dip() { cout<< “Fare is high"; } }; 33 class Bus : public Four_wheel, Fare { public: void print() { cout<< “This is a Bus"; } }; Void main() { Bus obj; obj.print(); obj.dip(); obj.display(); obj.show(); } OUTPUT: This is a Bus Fare is high This is a Four Wheeler This is a Vehicle
  • 34. VIRTUAL BASE CLASS  In multipath inheritance child class could have duplicate sets of members inherited from a single base class.  Use : To avoid ambiguity due to multipath inheritance.  When classes are declared virtual  complier takes essential caution to avoid duplication.  Keyword “virtual” is used with the base class when it is inherited. 34
  • 35. e.g. Class A1 { protected: int a1; }; Class A2 : virtual public A1 { protected: int a2; }; Class A3 : virtual public A1 { protected: int a3; }; 35 e.g. Class A4 : public A2,A3 { protected: int a4; };  Here using object of A4, it can access a1,a2,a3 and a4. Class A1 Class A4 Class A3 Class A2
  • 36. ABSTRACT CLASS  when a class is not used for creating objects then it is called abstract class.  Abstract class can act as base class only.  It is used for inheriting and not used for object creation.  An abstract class gives a skeleton or structure using which other classes are created. 36
  • 37. 37 Class A { public: int x ; class B { public: int y ; }; }; Class C : public A, A :: B { public: int z ; void show ( ) { cout<<“X=“<<x; cout<<“Y=“<<y; cout<<“Z=“<<z; } C ( int j, int k, int l) { x=j; y=k; z=l; } }; Void main ( ) { C c (4,7,1); c.show(); } OUTPUT: X = 4 Y = 7 Z = 1
  • 38. ADVANTAGES  Provides reusability  The derived classes extend the properties of base class  Generate more dominant objects.  Same Base class can be used by number of derived class.  All Derived class has same properties of base class. 38
  • 39. DISADVANTAGES  Inappropriate use of inheritance makes programs more complex.  Invoking member functions using objects create more compiler overhead.  In Class hierarchy various data elements remains unused.  All Memory allocated is not utilized. 39