SlideShare a Scribd company logo
INHERITANCE
INHERITANCE
• When creating a class, instead of writing completely new data
members and member functions, the programmer can designate
that the new class should inherit the members of an existing
class. This existing class is called the base class, and the new
class is referred to as the derived class.
• The idea of inheritance implements the is a relationship. For
example, mammal IS-A animal, dog IS-A mammal hence dog
IS-A animal as well and so on.
Base & Derived Classes:
• A class can be derived from more than one classes, which
means it can inherit data and functions from multiple base
classes.
• . A class derivation list names one or more base classes and has
the form:
class derived-class: access-specifier base-class
• Where access-specifier is one of public, protected, or private,
and base-class is the name of a previously defined class. If the
access-specifier is not used, then it is private by default.
• Consider a base class Shape and its derived class Rectangle as
follows:
#include <iostream>
// Base class
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
Limitations of derived class
A derived class inherits all base class methods with the following
exceptions:
• Constructors, destructors and copy constructors of the base
class.
• Overloaded operators of the base class.
• The friend functions of the base class.
Types of Inheritance
• Forms of Inheritance
• Single Inheritance: It is the inheritance hierarchy wherein one
derived class inherits from one base class.
Multiple Inheritance: It is the inheritance hierarchy wherein
one derived class inherits from multiple base class(es)
Hierarchical Inheritance: It is the inheritance hierarchy
wherein multiple subclasses inherit from one base class.
Multilevel Inheritance: It is the inheritance hierarchy wherein
subclass acts as a base class for other classes.
Hybrid Inheritance: The inheritance hierarchy that reflects any
legal combination of other four types of inheritance.
Inheritance
Multiple Inheritances
• A C++ class can inherit members from more than one class and
here is the extended syntax:
class derived-class: access baseA, access baseB....
Where access is one of public, protected, or private and would
be given for every base class and they will be separated by
comma as shown above. Let us try the following example:
Example
#include <iostream>
// Base class Shape
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Base class PaintCost
class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
};
// Derived class
class Rectangle: public Shape, public PaintCost
{
public:
int getArea()
{
return (width * height);
}
};
int main(void)
{
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
// Print the total cost of painting
cout << "Total paint cost: $" << Rect.getCost(area) << endl;
return 0;
}
Output
Total area: 35
Total paint cost: $2450
C++ Overloading
• C++ allows you to specify more than one definition for a
function name or an operator in the same scope, which is
called function overloading and operator overloading
respectively.
• When you call an overloaded function or operator, the
compiler determines the most appropriate definition to use by
comparing the argument types you used to call the function or
operator with the parameter types specified in the definitions.
The process of selecting the most appropriate overloaded
function or operator is called overload resolution.
Function overloading in C++:
• You can have multiple definitions for the same function name in
the same scope.
• The definition of the function must differ from each other by
the types and/or the number of arguments in the argument list.
• You can not overload function declarations that differ only by
return type.
• Following is the example where same function print() is being
used to print different data types
#include <iostream>
using namespace std;
class printData
{
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
};
int main(void)
{
printData pd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");
return 0;
Output
Printing int: 5
Printing float: 500.263
Printing character: Hello C++
Operators overloading in C++:
• Overloaded operators are functions with special names the
keyword operator followed by the symbol for the operator being
defined.
• Like any other function, an overloaded operator has a return
type and a parameter list.
Box operator+(const Box&);
declares the addition operator that can be used to add two Box
objects and returns final Box object.
#include <iostream>
using namespace std;
class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
void setBreadth( double bre )
{
breadth = bre;
}
void setHeight( double hei )
{
height = hei;
}
// Overload + operator to add two Box objects.
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
void setBreadth( double bre )
{
breadth = bre;
}
void setHeight( double hei )
{
height = hei;
}
// Overload + operator to add two Box objects.
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Main function for the program
int main( )
{
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Box Box3; // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// Add two object as follows:
Box3 = Box1 + Box2;
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
Output
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

More Related Content

PPTX
Computer programming(C++): Structures
PPTX
OOPS IN C++
PPTX
29csharp
PPT
Lecture02
PPTX
Ppt on this and super keyword
PPT
Super keyword.23
PPTX
File Handling in Java Oop presentation
PDF
Basic i/o & file handling in java
Computer programming(C++): Structures
OOPS IN C++
29csharp
Lecture02
Ppt on this and super keyword
Super keyword.23
File Handling in Java Oop presentation
Basic i/o & file handling in java

What's hot (20)

PPT
inheritance
PPTX
Class, object and inheritance in python
PPTX
Introduce oop in python
PPTX
32sql server
PPTX
inheritance in C++
PPTX
encapsulation, inheritance, overriding, overloading
PPTX
Multiple inheritance
PPTX
Constructor in java
PDF
Lecture 24
PDF
PPT
Synapseindia dot net development
PPTX
Inheritance in c++
PPTX
30csharp
PPTX
Inheritance in c++
PPTX
Dynamic method dispatch
PPT
Inheritance : Extending Classes
PPTX
Inheritance
PPTX
Inheritance
PPTX
Object Oriented Programming with C#
PPTX
Packages and interface
inheritance
Class, object and inheritance in python
Introduce oop in python
32sql server
inheritance in C++
encapsulation, inheritance, overriding, overloading
Multiple inheritance
Constructor in java
Lecture 24
Synapseindia dot net development
Inheritance in c++
30csharp
Inheritance in c++
Dynamic method dispatch
Inheritance : Extending Classes
Inheritance
Inheritance
Object Oriented Programming with C#
Packages and interface
Ad

Viewers also liked (20)

PPTX
Oprerator overloading
PPTX
Switch statement mcq
PPTX
Power Of Trustee Engagement 5.10.11
PPTX
Inborn errors of carbohydrate metabolism
PDF
4 Genetics - types of inheritance (by Lizzy)
PPT
Inborn errors of metabolism
PPT
inborn error of metabolism
PPTX
Inborn errors of metabolism
PPT
5. metabolic and genetic disorders; pediatric pathology
PPTX
Inbornerrorsofmet
PPTX
3.4 inheritance
PPTX
fruit drop
PPT
09 Lecture Ppt
PPTX
Mendelian Inheritance
PPTX
Types of Inheritance
PPTX
Inborn error of metabolism
PPTX
Modes of inheritance-Dr.Gourav
PPTX
Basis of Genetic Inheritance
PPT
Inbornerrorsofmetabolism
PPTX
Human genetic inheritance patterns
Oprerator overloading
Switch statement mcq
Power Of Trustee Engagement 5.10.11
Inborn errors of carbohydrate metabolism
4 Genetics - types of inheritance (by Lizzy)
Inborn errors of metabolism
inborn error of metabolism
Inborn errors of metabolism
5. metabolic and genetic disorders; pediatric pathology
Inbornerrorsofmet
3.4 inheritance
fruit drop
09 Lecture Ppt
Mendelian Inheritance
Types of Inheritance
Inborn error of metabolism
Modes of inheritance-Dr.Gourav
Basis of Genetic Inheritance
Inbornerrorsofmetabolism
Human genetic inheritance patterns
Ad

Similar to Inheritance (20)

PPT
week14 (1).ppt
PPTX
C++ Presen. tation.pptx
PPTX
PPT
Java oops PPT
PPT
025466482929 -OOP with Java Development Kit.ppt
PPT
02-OOP with Java.ppt
PPT
ODP
Ppt of c++ vs c#
PPTX
Presentation 3rd
PPTX
Chapter 2 OOP using C++ (Introduction).pptx
PPTX
OOP C++
PPTX
Class and object
PPTX
Lecture-10_PHP-OOP.pptx
PDF
Class notes(week 6) on inheritance and multiple inheritance
PPTX
C++ presentation
PPTX
Multiple file programs, inheritance, templates
PPTX
OOPS Characteristics (With Examples in PHP)
PPTX
JAVA Module 2____________________--.pptx
PPTX
Java chapter 5
week14 (1).ppt
C++ Presen. tation.pptx
Java oops PPT
025466482929 -OOP with Java Development Kit.ppt
02-OOP with Java.ppt
Ppt of c++ vs c#
Presentation 3rd
Chapter 2 OOP using C++ (Introduction).pptx
OOP C++
Class and object
Lecture-10_PHP-OOP.pptx
Class notes(week 6) on inheritance and multiple inheritance
C++ presentation
Multiple file programs, inheritance, templates
OOPS Characteristics (With Examples in PHP)
JAVA Module 2____________________--.pptx
Java chapter 5

More from Parthipan Parthi (20)

PPTX
802.11 mac
PPT
Ieee 802.11 wireless lan
PPTX
Computer network
PPT
Ieee 802.11 wireless lan
PPTX
Session 6
PPT
Queuing analysis
PPTX
Alternative metrics
PPT
PPT
Ieee 802.11 wireless lan
PPTX
Data structures1
PPTX
Data structures 4
PPTX
Data structures2
PPTX
Data structures 3
PPT
Input output streams
PPT
Io stream
DOC
Dbms lab questions
PPTX
REVERSIBLE DATA HIDING WITH GOOD PAYLOAD DISTORTIONPpt 3
PDF
New syllabus combined_civil_services
DOC
802.11 mac
Ieee 802.11 wireless lan
Computer network
Ieee 802.11 wireless lan
Session 6
Queuing analysis
Alternative metrics
Ieee 802.11 wireless lan
Data structures1
Data structures 4
Data structures2
Data structures 3
Input output streams
Io stream
Dbms lab questions
REVERSIBLE DATA HIDING WITH GOOD PAYLOAD DISTORTIONPpt 3
New syllabus combined_civil_services

Recently uploaded (20)

PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Safety Seminar civil to be ensured for safe working.
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
737-MAX_SRG.pdf student reference guides
PPT
Mechanical Engineering MATERIALS Selection
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PPT
Total quality management ppt for engineering students
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Construction Project Organization Group 2.pptx
DOCX
573137875-Attendance-Management-System-original
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Embodied AI: Ushering in the Next Era of Intelligent Systems
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
R24 SURVEYING LAB MANUAL for civil enggi
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Safety Seminar civil to be ensured for safe working.
Fundamentals of safety and accident prevention -final (1).pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Internet of Things (IOT) - A guide to understanding
737-MAX_SRG.pdf student reference guides
Mechanical Engineering MATERIALS Selection
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Total quality management ppt for engineering students
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Construction Project Organization Group 2.pptx
573137875-Attendance-Management-System-original

Inheritance

  • 2. INHERITANCE • When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. • The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
  • 3. Base & Derived Classes: • A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. • . A class derivation list names one or more base classes and has the form: class derived-class: access-specifier base-class • Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default. • Consider a base class Shape and its derived class Rectangle as follows:
  • 4. #include <iostream> // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; }
  • 5. protected: int width; int height; }; // Derived class class Rectangle: public Shape { public: int getArea() { return (width * height); } };
  • 6. int main(void) { Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; return 0; }
  • 7. Limitations of derived class A derived class inherits all base class methods with the following exceptions: • Constructors, destructors and copy constructors of the base class. • Overloaded operators of the base class. • The friend functions of the base class.
  • 8. Types of Inheritance • Forms of Inheritance • Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class. Multiple Inheritance: It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es) Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherit from one base class. Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes. Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other four types of inheritance.
  • 10. Multiple Inheritances • A C++ class can inherit members from more than one class and here is the extended syntax: class derived-class: access baseA, access baseB.... Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above. Let us try the following example:
  • 11. Example #include <iostream> // Base class Shape class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; }
  • 12. protected: int width; int height; }; // Base class PaintCost class PaintCost { public: int getCost(int area) { return area * 70; } };
  • 13. // Derived class class Rectangle: public Shape, public PaintCost { public: int getArea() { return (width * height); } }; int main(void) { Rectangle Rect; int area;
  • 14. Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; // Print the total cost of painting cout << "Total paint cost: $" << Rect.getCost(area) << endl; return 0; }
  • 15. Output Total area: 35 Total paint cost: $2450
  • 16. C++ Overloading • C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. • When you call an overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.
  • 17. Function overloading in C++: • You can have multiple definitions for the same function name in the same scope. • The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. • You can not overload function declarations that differ only by return type. • Following is the example where same function print() is being used to print different data types
  • 18. #include <iostream> using namespace std; class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; }
  • 19. void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; // Call print to print integer pd.print(5); // Call print to print float pd.print(500.263); // Call print to print character pd.print("Hello C++"); return 0;
  • 20. Output Printing int: 5 Printing float: 500.263 Printing character: Hello C++
  • 21. Operators overloading in C++: • Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined. • Like any other function, an overloaded operator has a return type and a parameter list. Box operator+(const Box&); declares the addition operator that can be used to add two Box objects and returns final Box object.
  • 22. #include <iostream> using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; }
  • 23. void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } // Overload + operator to add two Box objects. Box operator+(const Box& b) { Box box; box.length = this->length + b.length;
  • 24. void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } // Overload + operator to add two Box objects. Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth;
  • 25. box.height = this->height + b.height; return box; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; // Main function for the program int main( ) { Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box
  • 26. Box Box3; // Declare Box3 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // box 2 specification Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0);
  • 27. // volume of box 1 volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; // volume of box 2 volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; // Add two object as follows: Box3 = Box1 + Box2; // volume of box 3 volume = Box3.getVolume(); cout << "Volume of Box3 : " << volume <<endl; return 0; }
  • 28. Output Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400