C++ Inheritance Example Program

C++ Inheritance Example

class Rectangle
class Area : public Rectangle
class Perimeter: public Rectangle

class Animal {
int legs = 4;
class Dog : public Animal { public:
int tail = 1;
int mainQ {
Dog d;
cout «”Legs are: “«d.legs«endl; cout« “Tail is: “«d.tail;


Inherit Definition – C++ inheritance example derive quality and characteristics from parents or ancestors. Like you inherit features of your parents.

  • Example: “She had inherited the beauty of her mother”
  • Inheritance in Object Oriented Programming can be described as a process of creating new classes from existing classes.

The process of obtaining the data members and methods from one class to another class is known as inheritance. It is one of the fundamental features of object oriented programming.

Inheritance in C++

Inheritance is the capability of one class to acquire properties and characteristics from another class. The class whose properties are inherited by other class is called the Parent or Base or Super class. And, the class which inherits properties of other class is called Child or Derived or Sub class.

Inheritance makes the code reusable. When we inherit an existing class, all its methods and fields become available in the new class, hence code is reused.

NOTE : All members of a class except Private, are inherited

Advantage of inheritance

  • If we develop any application using this concept than that application have following advantages,
  • Application development time is less.
  • Application take less memory.
  • Application execution time is less.
  • Application performance is enhance (improved).
  • Redundancy (repetition) of the code is reduced or minimized so that we get consistence results and less storage cost.
  • Use of Virtual Keyword

Inheritance Visibility Mode

  • Depending on Access modifier used while inheritance, the availability of class members of Super class in the sub class changes.

It can either be private, protected or public.

Public Inheritance
  • This is the most used inheritance mode. In this the protected member of super class becomes protected members of sub class and public becomes public.

class Subclass : public Superclass

Private Inheritance
  • In private mode, the protected and public members of super class become private members of derived class.

class Subclass : Superclass // By default its private inheritance

Protected Inheritance
  • In protected mode, the public and protected members of Super class becomes protected members of Sub class.

class subclass : protected Superclass

Single Inheritance Example

  • When a single class is derived from a single parent class, it is called Single inheritance. It is the simplest of all inheritance.

For example,

  • Animal is derived from living things
  • Car is derived from vehicle
  • Typist is derived from staff
  • Manager derived from employee
  • Circle derived from shapes
Read More Topics
Routing in IP network
TCP connection establishment in computer networks
The User Datagram Protocol (UDP)
Transport layer in the reference model

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply