SlideShare a Scribd company logo
C++ Inheritance
C++ Inheritance
 In C++, inheritance is a process in which one object
acquires all the properties and behaviors of its parent
object automatically. In such way, you can reuse, extend
or modify the attributes and behaviors which are defined
in other class. 20
 In C++, the class which inherits the members of another
class is called derived class and the class whose members
are inherited is called base class. The derived class is the
specialized class for the base class.
C++ Inheritance
 Inheritance is an essential part of OOP. Its big payoff is that it permits
code reusability. Once a base class is written and debugged, it need
not be touched again, but, using inheritance, can nevertheless be
adapted to work in different situations. Reusing existing code saves
time and money and increases a program’s reliability.
 Inheritance can also help in the original conceptualization of a
programming problem, and in the overall design of the program.
 An important result of reusability is the ease of distributing class
libraries. A programmer can use a class created by another person or
company, and, without modifying it, derive other classes from it that
are suited to particular situations.
Advantage of C++ Inheritance
 Code reusability: Now you can reuse the members of your
parent class. So, there is no need to define the member
again. So less code is required in the class.
Types Of Inheritance
C++ supports five types of inheritance:
 o Single inheritance
 o Multiple inheritance
 o Hierarchical inheritance
 o Multilevel inheritance
 o Hybrid inheritance
Types Of Inheritance
Derived Classes
 A Derived class is defined as the class derived from the base class.
 The Syntax of Derived class:
1. class derived_class_name :: visibility-mode base_class_name
2. {
3. // body of the derived class.
4. }
Derived Classes
 Where,
 derived_class_name: It is the name of the derived class.
 visibility mode: The visibility mode specifies whether the features of the
base class are publicly inherited or privately inherited. It can be public or
private.
 base_class_name: It is the name of the base class.
visibility mode
 When the base class is privately inherited by the derived
class, public members of the base class becomes the
private members of the derived class. Therefore, the
public members of the base class are not accessible by the
objects of the derived class only by the member functions
of the derived class.
 When the base class is publicly inherited by the derived
class, public members of the base class also become the
public members of the derived class. Therefore, the
public members of the base class are accessible by the
objects of the derived class as well as by the member
functions of the base class.
visibility mode
Note:
 In C++, the default mode of visibility is private.
 The private members of the base class are never
inherited.
C++ Single Inheritance
 Single inheritance is defined as the inheritance in which a derived class is
inherited from the only one base class.
 Where 'A' is the base class, and 'B' is the derived class.
C++ Single Level Inheritance Example:
Inheriting Fields
1. #include <iostream>
2. using namespace std;
3. class Account {
4. public:
5. float salary = 60000;
6. };
7. class Programmer: public Account {
8. public:
9. float bonus = 5000;
10. };
11. int main(void) {
12. Programmer p1;
13. cout<<"Salary: "<<p1.salary<<endl;
14. cout<<"Bonus: "<<p1.bonus<<endl;
15. return 0;
16. }
Output:
Salary: 60000
Bonus: 5000
C++ Single Level Inheritance Example:
Inheriting Methods
1. #include <iostream>
2. using namespace std;
3. class Animal {
4. public:
5. void eat() {
6. cout<<"Eating..."<<endl;
7. }
8. };
9. class Dog: public Animal
10. {
11. public:
12. void bark(){
13. cout<<"Barking...";
14. }
15. };
C++ Single Level Inheritance Example:
Inheriting Methods
16. int main(void) {
17. Dog d1;
18. d1.eat();
19. d1.bark();
20. return 0;
21. }
Output:
Eating...
Barking...
Another Example
1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. int a = 4;
6. int b = 5;
7. public:
8. int mul()
9. {
10. int c = a*b;
11. return c;
12. }
13. };
Another Example Continue……
8. int mul()
9. {
10. int c = a*b;
11. return c;
12. }
13. };
14.
15. class B : private A
16. {
17. public:
18. void display()
19. {
20. int result = mul();
21. std::cout <<"Multiplication of a
and b is : "<<result<< std::endl;
Continue……
 22. }
 23. };
 24. int main()
 25. {
 26. B b;
 27. b.display();
 28.
 29. return 0;
 30. }
 Output:
 Multiplication of a and b is : 20

More Related Content

What's hot

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
Ronak Chhajed
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++
Nikunj Patel
 
Inheritance
InheritanceInheritance
InheritanceTech_MX
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
MASQ Technologies
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
Anant240318
 
Inheritance
InheritanceInheritance
Inheritance
Munsif Ullah
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
sushamaGavarskar1
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
abhilashagupta
 
Inheritance
InheritanceInheritance
Inheritance
prabhat kumar
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
SadiqullahGhani1
 
inheritance
inheritanceinheritance
inheritance
Amir_Mukhtar
 
Inheritance
InheritanceInheritance
Inheritance
Burhan Ahmed
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
Nivegeetha
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
 
Inheritance
InheritanceInheritance
Inheritance
Mustafa Khan
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop InheritanceHadziq Fabroyir
 

What's hot (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Inheritance
InheritanceInheritance
Inheritance
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
Inheritance
InheritanceInheritance
Inheritance
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 

Similar to Inheritance

Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
WaqarRaj1
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
LadallaRajKumar
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
Praveen M Jigajinni
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
Ganesh Amirineni
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
NAVANEETCHATURVEDI2
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Shweta Shah
 
Inheritance
InheritanceInheritance
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
study material
 
Inheritance and its types explained.ppt
Inheritance and its types  explained.pptInheritance and its types  explained.ppt
Inheritance and its types explained.ppt
SarthakKumar93
 
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++
Ameen Sha'arawi
 
Inheritance
Inheritance Inheritance
Inheritance
sourav verma
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
AteeqaKokab1
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
 
OOP C++
OOP C++OOP C++
OOP C++
Ahmed Farag
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
Ganesh Karthik
 
OOP.ppt
OOP.pptOOP.ppt
Only oop
Only oopOnly oop
Only oop
anitarooge
 
Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03
sivakumarmcs
 

Similar to Inheritance (20)

Inheritance
InheritanceInheritance
Inheritance
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Inheritance and its types explained.ppt
Inheritance and its types  explained.pptInheritance and its types  explained.ppt
Inheritance and its types explained.ppt
 
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
Inheritance Inheritance
Inheritance
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
OOP C++
OOP C++OOP C++
OOP C++
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
OOP.ppt
OOP.pptOOP.ppt
OOP.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Only oop
Only oopOnly oop
Only oop
 
Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03
 

More from zindadili

Namespaces
NamespacesNamespaces
Namespaces
zindadili
 
Namespace1
Namespace1Namespace1
Namespace1
zindadili
 
Exception handling
Exception handlingException handling
Exception handling
zindadili
 
Exception handling
Exception handlingException handling
Exception handling
zindadili
 
Templates2
Templates2Templates2
Templates2
zindadili
 
Templates1
Templates1Templates1
Templates1
zindadili
 
Virtual function
Virtual functionVirtual function
Virtual function
zindadili
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
zindadili
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
zindadili
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
zindadili
 
Function overloading
Function overloadingFunction overloading
Function overloading
zindadili
 
Aggregation
AggregationAggregation
Aggregation
zindadili
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
zindadili
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritance
zindadili
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritance
zindadili
 
Abstraction1
Abstraction1Abstraction1
Abstraction1
zindadili
 
Abstraction
AbstractionAbstraction
Abstraction
zindadili
 
Access specifier
Access specifierAccess specifier
Access specifier
zindadili
 
Friend function
Friend functionFriend function
Friend function
zindadili
 
Enum
EnumEnum
Enum
zindadili
 

More from zindadili (20)

Namespaces
NamespacesNamespaces
Namespaces
 
Namespace1
Namespace1Namespace1
Namespace1
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Templates2
Templates2Templates2
Templates2
 
Templates1
Templates1Templates1
Templates1
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Aggregation
AggregationAggregation
Aggregation
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritance
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritance
 
Abstraction1
Abstraction1Abstraction1
Abstraction1
 
Abstraction
AbstractionAbstraction
Abstraction
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Friend function
Friend functionFriend function
Friend function
 
Enum
EnumEnum
Enum
 

Recently uploaded

The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 

Recently uploaded (20)

The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 

Inheritance

  • 2. C++ Inheritance  In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class. 20  In C++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class.
  • 3. C++ Inheritance  Inheritance is an essential part of OOP. Its big payoff is that it permits code reusability. Once a base class is written and debugged, it need not be touched again, but, using inheritance, can nevertheless be adapted to work in different situations. Reusing existing code saves time and money and increases a program’s reliability.  Inheritance can also help in the original conceptualization of a programming problem, and in the overall design of the program.  An important result of reusability is the ease of distributing class libraries. A programmer can use a class created by another person or company, and, without modifying it, derive other classes from it that are suited to particular situations.
  • 4. Advantage of C++ Inheritance  Code reusability: Now you can reuse the members of your parent class. So, there is no need to define the member again. So less code is required in the class.
  • 5. Types Of Inheritance C++ supports five types of inheritance:  o Single inheritance  o Multiple inheritance  o Hierarchical inheritance  o Multilevel inheritance  o Hybrid inheritance
  • 7. Derived Classes  A Derived class is defined as the class derived from the base class.  The Syntax of Derived class: 1. class derived_class_name :: visibility-mode base_class_name 2. { 3. // body of the derived class. 4. }
  • 8. Derived Classes  Where,  derived_class_name: It is the name of the derived class.  visibility mode: The visibility mode specifies whether the features of the base class are publicly inherited or privately inherited. It can be public or private.  base_class_name: It is the name of the base class.
  • 9. visibility mode  When the base class is privately inherited by the derived class, public members of the base class becomes the private members of the derived class. Therefore, the public members of the base class are not accessible by the objects of the derived class only by the member functions of the derived class.  When the base class is publicly inherited by the derived class, public members of the base class also become the public members of the derived class. Therefore, the public members of the base class are accessible by the objects of the derived class as well as by the member functions of the base class.
  • 10. visibility mode Note:  In C++, the default mode of visibility is private.  The private members of the base class are never inherited.
  • 11. C++ Single Inheritance  Single inheritance is defined as the inheritance in which a derived class is inherited from the only one base class.  Where 'A' is the base class, and 'B' is the derived class.
  • 12. C++ Single Level Inheritance Example: Inheriting Fields 1. #include <iostream> 2. using namespace std; 3. class Account { 4. public: 5. float salary = 60000; 6. }; 7. class Programmer: public Account { 8. public: 9. float bonus = 5000; 10. }; 11. int main(void) { 12. Programmer p1; 13. cout<<"Salary: "<<p1.salary<<endl; 14. cout<<"Bonus: "<<p1.bonus<<endl; 15. return 0; 16. } Output: Salary: 60000 Bonus: 5000
  • 13. C++ Single Level Inheritance Example: Inheriting Methods 1. #include <iostream> 2. using namespace std; 3. class Animal { 4. public: 5. void eat() { 6. cout<<"Eating..."<<endl; 7. } 8. }; 9. class Dog: public Animal 10. { 11. public: 12. void bark(){ 13. cout<<"Barking..."; 14. } 15. };
  • 14. C++ Single Level Inheritance Example: Inheriting Methods 16. int main(void) { 17. Dog d1; 18. d1.eat(); 19. d1.bark(); 20. return 0; 21. } Output: Eating... Barking...
  • 15. Another Example 1. #include <iostream> 2. using namespace std; 3. class A 4. { 5. int a = 4; 6. int b = 5; 7. public: 8. int mul() 9. { 10. int c = a*b; 11. return c; 12. } 13. };
  • 16. Another Example Continue…… 8. int mul() 9. { 10. int c = a*b; 11. return c; 12. } 13. }; 14. 15. class B : private A 16. { 17. public: 18. void display() 19. { 20. int result = mul(); 21. std::cout <<"Multiplication of a and b is : "<<result<< std::endl;
  • 17. Continue……  22. }  23. };  24. int main()  25. {  26. B b;  27. b.display();  28.  29. return 0;  30. }  Output:  Multiplication of a and b is : 20