C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Inheritance

C++ Inheritance

shape Definition

The key concept of C++ programming is Inheritance. CPP Inheritance is the creation of new class acquiring the properties and characteristics of already existing class. The class which gives its properties to newly generated class is called as Super class (or) Parent class (or) Base class. The class which inherits or takes the properties from the existing class is called as Sub class (or) Child class (or) Derived class. The child class can even have its own characteristics.

Advantages

  1. Code is re-used.
  2. Method over-riding.
  3. Reduces execution time.

shape Syntax

class child-class_name : access_modifier parent-class_name
Before declaring child class, parent class should be declared. Without declaration of super class CPP Inheritance cannot be applied. Access modifier represents the way of inheriting the child classes.

shape Conceptual figure

shape Example

[c] #include <iostream> using namespace std; class Triangle { protected: float base, height; public: Triangle(): base(0.0), height(0.0) { cout<<"Enter base: "; cin>>base; cout<<"Enter height: "; cin>>height; } }; // derived class class Area : public Triangle { public: float calc() { return (base*height)/2; } }; int main() { cout <<"Enter data for Triangle to find area.\n"; Area a; cout <<"Area = "<< a.calc(); return 0; }[/c] Output [c] Enter data for Triangle to find area. Enter base: 85 Enter height: 75 Area = 3187.5[/c]

Access modifiers for using inheritance

shape Description

Access modifiers decides whether the super class members are available for inheritance or not by sub class. public:
  • public members are accessible in its own class(Base class).
  • public members are accessible from derived class.
  • public members are accessible outside the derived class.
private:
  • private members are accessible in its own class(Base class).
  • private members are not accessible from derived class.
  • private members are not accessible outside the derived class.
protected:
  • protected members are accessible in its own class(Base class).
  • protected members are accessible from derived class.
  • protected members are not accessible outside the derived class.

shape Conceptual figure

Overriding

shape Description

Assume that both super class and sub class have same name and same number of arguments. Now, if object is created in sub class and member functions are given access, then the sub class member functions are taken into consideration not the super class. This process of accessing sub class member functions is called as overriding, as sub class overrides super class member functions.

Types of CPP Inheritance

shape Conceptual figure

Single Inheritance

shape Description

Single Child class inherits the properties from single Parent class.

Multi-level Inheritance

shape Description

A child class inherits from parent class which in turn inherits from another parent class.

Multiple Inheritance

shape Description

A single child class inherits from many parent classes.

Hierarchical Inheritance

shape Description

Multiple child classes derives from single parent class.

Hybrid Inheritance

shape Description

Hybrid inheritance is formed by the combination of multi-level and hierarchical inheritances.

shape Example

[c] //Example for multiple inheritance #include<iostream> using namespace std; //base class shape class Shape { public: void setLength(int l) { length = l; } void setBreadth(int b) { breadth = b; } protected: int length; int breadth; }; // Base class paint class Paint { public: int getCost(int area) { return area * 50; } }; // derived class class Rectangle: public Shape, public Paint { public: int getArea() { return (length * breadth); } }; int main(void) { Rectangle Rec; int area; Rec.setLength(15); Rec.setBreadth(20); area = Rec.getArea(); // Print the area of the object. cout << "Total area of building:" << Rec.getArea() << endl; // Print the total cost of painting cout << "Total paint cost: Rupees"<< Rec.getCost(area) << endl; return 0; }[/c] Output [c] Total area of building: 300 Total paint cost: Rupees15000[/c]

Summary

shape Key Points

  • CPP Inheritance is acquiring the properties of other existing class.
  • The class which gives properties is called parent class and which takes is known as child class.
  • Single, Multi-level, Multiple, Hierarchical and Hybrid inheritances are the types.

shape Programming Tips

To write the same code in multiple locations Inheritance is the best choice. Modern OOPs Languages does not allow multiple inheritance.