SlideShare a Scribd company logo
1 of 21
We will cover
 Inheritance
 Advantages of inheritance
 Categories of inheritance
 Single inheritance
 Multiple inheritance
 Multilevel inheritance
 Types of inheritance
 Public inheritance
 Private inheritance
 Protected inheritance
Inheritance
 A programming technique that is used to reuse an existing
class to build a new class
 Terminologies
 Base/parent/super class
 That is reused to create a new class
 Derived/child/parent sub class
 That inherits the functions and properties of an existing class
 Example
 Vehicle
 Bus Truck Motorcycle
Advantages
 Reusability
 Saves time & efforts
 Increase Program structure and reliability
Difference b/w different access specifiers
Access
Specifier
Accessible
from own
class
Accessible
from derived
class
Accessible
from objects
outside class
Public Yes Yes Yes
Protected Yes Yes No
Private Yes No No
Categories
 Single Multiple Multilevel
Parent
Child
Parent 1
Child
Parent 2
Child 1
Child/
Parent level 2
Parent Level 1
Child 2
Syntax
 Single inheritance
 class sub_class: specifier parent_class
 {
 body
 }
 Multiple inheritance
 class sub_class: specifier parent_class1 , specifier parent_class2
 {
 body
 }
 Multilevel inheritance
 class sub_class: specifier grandparent_class
 {
 body
 }
 class sub_class: specifier parent_class
 {
 body
 }

Example single inheritance
 class Person
 {
 protected:
 int id;
 Char name[50], address[100];
 public:
 Person()
 {
 id=0;
 name[0]=‘0’;
 address[0]= ‘0’;
 }
 void getinfo()
 {
 cout<<“ Enter ur id, name. address”;
 cin>>id;
 gets(name);
 gets(address);
 }
 Void showinfo()
 {
 cout<<“ your personal info is”;
 cout<<“ id is”<<id<<endl;
 cout<<“ name is”<<name<<endl;
 cout<<“ address is<<address<<endl;
 }
};
Continued…
 class Student: public Person
 {
 private:
 int rno, marks;
 public:
 Student()
 {
 Person:: Person();
 rno=marks=0;
 }
 void getedu()
 {
 cout<<“enter R# & marks”;
 cin>>rno>>marks;
 }
 void showedu()
 {
 cout<<“ ur education info is”;
 cout<<“ Roll no is”<<rmo<<endl;
 cout<<“ Marks are”<<marks<<endl;
 }
};
void main(void)
{
Student s;
s.getinfo();
s.getedu();
s.showinfo();
s.showedu();
getch();
}
Multilevel inheritance ( Example)
 class A
 {
 private:
 int a;
 public:
 void in()
 {
 cout<<“ Enter value for a”;
 cin>>a;
 }
 void out()
 {
 cout<<“ value of a is”<<a;
 }
 };
 class B : public A
 {
 private:
 int b;
 public:
 void in()
 {
 A::in();
 cout<<“ Enter value for b”;
 cin>>b;
 }
 void out()
 {
 A::out();
 cout<<“ value of b is”<<b;
 }
 };
Continued…
 class C : public B
 {
 private:
 int c;
 public:
 void in()
 {
 B::in();
 cout<<“ Enter value for c”;
 cin>>c;
 }
 void out()
 {
 B::out();
 cout<<“ value of c is”<<c;
 }
 };
 void main(void)
 {
 C obj;
 obj.in();
 obj.out();
 getch();
 }
Multiple inheritance ( Example)
 class A
 {
 private:
 int a;
 public:
 void in()
 {
 cout<<“ Enter value for a”;
 cin>>a;
 }
 void out()
 {
 cout<<“ value of a is”<<a;
 }
 };
 class B
 {
 private:
 int b;
 public:
 void input()
 {
 cout<<“ Enter value for b”;
 cin>>b;
 }
 void output()
 {
 cout<<“ value of b is”<<b;
 }
 };
Continued…
 class C : public A, public B
 {
 private:
 int c;
 public:
 void get()
 {
 A::in();
 B::input();
 cout<<“ Enter value for c”;
 cin>>c;
 }
 void show()
 {
 A::out();
 B::output();
 cout<<“ value of c is”<<c;
 }
 };
 void main(void)
 {
 C obj;
 obj.get();
 obj.show();
 getch();
 }
Ambiguity in multiple inheritance
 Ambiguity will be created in multiple inheritance
when the names of functions is similar in two or more
parent classes, so compiler can’t determine which
function to execute when the object of the derived
class attempts to execute such function
 Removal of such ambiguity
 By overloading
 By use of scope resolution operator
Types of inheritance
 Three types
 Public inheritance
 Private inheritance
 Protected inheritance
Public inheritance
 In which access level in both parent and child class is
same
 Public members become public in child class
 Private members become private in child class
 Protected members become protected in child class
 Syntax
 class child_class : public Parent_class
 {
 Body
 };
Example
 class Parent
 {
 public:
 int a;
 Private:
 int b;
 protected :
 int c;
 };
 class Child : public Parent
 {
 public:
 void in()
 {
 cout<<“ enter values for a & b”;
 cin>>a>>b;
 }
 void out()
 {
 cout<<“Values of a & b are”<<a<<b;
 }
 };
 void main(void)
 {
 Child obj;
 Obj.in();
 Obj.show();
 getch();
 }
Protected inheritance
 In which access level in both parent and child class is
restricted
 Public members become public in child class
 Private members become private in child class
 Protected members become protected in child class
 Syntax
 class child_class : protected Parent_class
 {
 Body
 };
Example
 {
 public:
 int a;
 Private:
 int b;
 protected :
 int c;
 };
 class Child : protected Parent
 {
 public:
 void in()
 {
 cout<<“ enter values for a & b”;
 cin>>a>>b;
 }
 void out()
 {
 cout<<“Values of a & b are”<<a<<b;
 }
 };
 void main(void)
 {
 Child obj;
 Obj.in();
 Obj.show();
 getch();
 }
Private
 In which access level in both parent and child class is
restricted
 Public members become private in child class
 Private members become private in child class
 Protected members become private in child class
 Syntax
 class child_class : protected Parent_class
 {
 Body
 };
Example
 {
 public:
 int a;
 Private:
 int b;
 protected :
 int c;
 };
 class Child : private Parent
 {
 public:
 void in()
 {
 cout<<“ enter values for a & b”;
 cin>>a>>b;
 }
 void out()
 {
 cout<<“Values of a & b are”<<a<<b;
 }
 };
 void main(void)
 {
 Child obj;
 Obj.in();
 Obj.show();
 getch();
 }

More Related Content

What's hot

What's hot (20)

Inheritance
InheritanceInheritance
Inheritance
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
 
polymorphism
polymorphismpolymorphism
polymorphism
 
Friend functions
Friend functions Friend functions
Friend functions
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
Single inheritance
Single inheritanceSingle inheritance
Single inheritance
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance
InheritanceInheritance
Inheritance
 

Similar to Inheritance

inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxurvashipundir04
 
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSINHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSVenugopalavarma Raja
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxDeepasCSE
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerssuser6f3c8a
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHPwahidullah mudaser
 
Inheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybridInheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybridVGaneshKarthikeyan
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)Redwan Islam
 
oop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfoop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfitxminahil29
 
Constructor&method
Constructor&methodConstructor&method
Constructor&methodJani Harsh
 

Similar to Inheritance (20)

Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
c++Inheritance.pdf
c++Inheritance.pdfc++Inheritance.pdf
c++Inheritance.pdf
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
 
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSINHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
Inheritance
InheritanceInheritance
Inheritance
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
Inheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybridInheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybrid
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
oop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfoop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdf
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
 

Recently uploaded

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
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
 
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
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
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
 
(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
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 

Recently uploaded (20)

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
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
 
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...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
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
 
(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...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 

Inheritance

  • 1.
  • 2. We will cover  Inheritance  Advantages of inheritance  Categories of inheritance  Single inheritance  Multiple inheritance  Multilevel inheritance  Types of inheritance  Public inheritance  Private inheritance  Protected inheritance
  • 3. Inheritance  A programming technique that is used to reuse an existing class to build a new class  Terminologies  Base/parent/super class  That is reused to create a new class  Derived/child/parent sub class  That inherits the functions and properties of an existing class  Example  Vehicle  Bus Truck Motorcycle
  • 4. Advantages  Reusability  Saves time & efforts  Increase Program structure and reliability
  • 5. Difference b/w different access specifiers Access Specifier Accessible from own class Accessible from derived class Accessible from objects outside class Public Yes Yes Yes Protected Yes Yes No Private Yes No No
  • 6. Categories  Single Multiple Multilevel Parent Child Parent 1 Child Parent 2 Child 1 Child/ Parent level 2 Parent Level 1 Child 2
  • 7. Syntax  Single inheritance  class sub_class: specifier parent_class  {  body  }  Multiple inheritance  class sub_class: specifier parent_class1 , specifier parent_class2  {  body  }  Multilevel inheritance  class sub_class: specifier grandparent_class  {  body  }  class sub_class: specifier parent_class  {  body  } 
  • 8. Example single inheritance  class Person  {  protected:  int id;  Char name[50], address[100];  public:  Person()  {  id=0;  name[0]=‘0’;  address[0]= ‘0’;  }  void getinfo()  {  cout<<“ Enter ur id, name. address”;  cin>>id;  gets(name);  gets(address);  }  Void showinfo()  {  cout<<“ your personal info is”;  cout<<“ id is”<<id<<endl;  cout<<“ name is”<<name<<endl;  cout<<“ address is<<address<<endl;  } };
  • 9. Continued…  class Student: public Person  {  private:  int rno, marks;  public:  Student()  {  Person:: Person();  rno=marks=0;  }  void getedu()  {  cout<<“enter R# & marks”;  cin>>rno>>marks;  }  void showedu()  {  cout<<“ ur education info is”;  cout<<“ Roll no is”<<rmo<<endl;  cout<<“ Marks are”<<marks<<endl;  } }; void main(void) { Student s; s.getinfo(); s.getedu(); s.showinfo(); s.showedu(); getch(); }
  • 10. Multilevel inheritance ( Example)  class A  {  private:  int a;  public:  void in()  {  cout<<“ Enter value for a”;  cin>>a;  }  void out()  {  cout<<“ value of a is”<<a;  }  };  class B : public A  {  private:  int b;  public:  void in()  {  A::in();  cout<<“ Enter value for b”;  cin>>b;  }  void out()  {  A::out();  cout<<“ value of b is”<<b;  }  };
  • 11. Continued…  class C : public B  {  private:  int c;  public:  void in()  {  B::in();  cout<<“ Enter value for c”;  cin>>c;  }  void out()  {  B::out();  cout<<“ value of c is”<<c;  }  };  void main(void)  {  C obj;  obj.in();  obj.out();  getch();  }
  • 12. Multiple inheritance ( Example)  class A  {  private:  int a;  public:  void in()  {  cout<<“ Enter value for a”;  cin>>a;  }  void out()  {  cout<<“ value of a is”<<a;  }  };  class B  {  private:  int b;  public:  void input()  {  cout<<“ Enter value for b”;  cin>>b;  }  void output()  {  cout<<“ value of b is”<<b;  }  };
  • 13. Continued…  class C : public A, public B  {  private:  int c;  public:  void get()  {  A::in();  B::input();  cout<<“ Enter value for c”;  cin>>c;  }  void show()  {  A::out();  B::output();  cout<<“ value of c is”<<c;  }  };  void main(void)  {  C obj;  obj.get();  obj.show();  getch();  }
  • 14. Ambiguity in multiple inheritance  Ambiguity will be created in multiple inheritance when the names of functions is similar in two or more parent classes, so compiler can’t determine which function to execute when the object of the derived class attempts to execute such function  Removal of such ambiguity  By overloading  By use of scope resolution operator
  • 15. Types of inheritance  Three types  Public inheritance  Private inheritance  Protected inheritance
  • 16. Public inheritance  In which access level in both parent and child class is same  Public members become public in child class  Private members become private in child class  Protected members become protected in child class  Syntax  class child_class : public Parent_class  {  Body  };
  • 17. Example  class Parent  {  public:  int a;  Private:  int b;  protected :  int c;  };  class Child : public Parent  {  public:  void in()  {  cout<<“ enter values for a & b”;  cin>>a>>b;  }  void out()  {  cout<<“Values of a & b are”<<a<<b;  }  };  void main(void)  {  Child obj;  Obj.in();  Obj.show();  getch();  }
  • 18. Protected inheritance  In which access level in both parent and child class is restricted  Public members become public in child class  Private members become private in child class  Protected members become protected in child class  Syntax  class child_class : protected Parent_class  {  Body  };
  • 19. Example  {  public:  int a;  Private:  int b;  protected :  int c;  };  class Child : protected Parent  {  public:  void in()  {  cout<<“ enter values for a & b”;  cin>>a>>b;  }  void out()  {  cout<<“Values of a & b are”<<a<<b;  }  };  void main(void)  {  Child obj;  Obj.in();  Obj.show();  getch();  }
  • 20. Private  In which access level in both parent and child class is restricted  Public members become private in child class  Private members become private in child class  Protected members become private in child class  Syntax  class child_class : protected Parent_class  {  Body  };
  • 21. Example  {  public:  int a;  Private:  int b;  protected :  int c;  };  class Child : private Parent  {  public:  void in()  {  cout<<“ enter values for a & b”;  cin>>a>>b;  }  void out()  {  cout<<“Values of a & b are”<<a<<b;  }  };  void main(void)  {  Child obj;  Obj.in();  Obj.show();  getch();  }