In this article inheritance in cpp we give the information about The process of creating the new class from the old class is called inheritance.

Inheritance in CPP:

Inheritance in C++:-

Reusability is an important feature of OOP. Inheritance saves time and money, reduces frustration and increases reliability.

Definition: “The process of creating the new class from the old class is called inheritance.”

The old class declared by the names such as base class or parent class or super class.
The new class declared by the names such as subclass or derived class or child class.

In inheritance, we add the property and behaviour of one class to another class without modifying the class. In such a situation, some of the second class already has its own property or behaviour, while by inheritance the second class also gets the property and behaviour of the first class.

Like in the real world, children inherit property and behavior from their parents, some of which are their own (such as their name) and some are inherited from the parents (such as surname). A program can have more than one base class and derive class, so one derive class will be a base class for another and one such base class will be a derive class for another class.

For example:-

Just as the grandfather will be the base for the father while the father will be the base for the child. But the father would derive for the grandfather while the child would derive for the father.

Inheritance in C++:-

Inherit a base class into derive-class:-

Syntax:-

class    derive-class-name  :   visibility-mode     base-class-name

{

……….

……….      // members of derived class

………..

};

The colon tells the user that the derived-class-name is derived from the base-class-name. Here visibility-mode refers to the access specifier. Which can be of private, public and protected types. The default visibility mode is private. visibility modes specifies whether the features the base class are privately derived or publicly derived

This means, in C++ we can inherit a class in three ways. So before introducing inheritance, we will discuss about these methods.

Access specifiers for inheritance in C++:-

  1. Inheritance private mode
  2. Inheritance public mode
  3. Inheritance Protected mode

Visibility Mode in Inheritance

Inheritance Private Mode:-

When you inherit a class from private mode, then all the members of the base class become private members for the derive class, which can be accessed only by the members of the derive-class (public members of the base class from the public member of the derivative class, not derive from the object of the class).Keep in mind, private members will not be accessed from any object nor derived from class members (private or public).

Class derived class-name: private base-class-name

{

Derived class member;

}

Because the private specifier would have been by default, so the private keyword would be optional here. Such as,

Class derived-class-name: base-class-name

{

Derive class members;

}

A private member of a class cannot be inherited. Both private member and private mode are different.

2. Inheritance Public Mode:

When a class inherits from a public specifier, the public member of the base class can be accessed from both the derived class’s members and its object. Also they can be inherited in the next class as well.

Here we can access the public members of base class in two ways (from derivative class members and its object)

Inheritance in CPP:

Its syntax is given below –

 Class derive-class-name: public base-class-name

{

            Derived class members;

}

But if we want to access private members of base class then we replace private mode with protected mode-

  1. Inheritance Protected Mode:-

As we know that private members of a class cannot be inherited. That is, a member of a derived class cannot access a private member of a base class. If we want to access private members then we have to declare all the members in public. But by doing this the feature of data-hiding is not obtained.

To overcome this problem, a third specifier has been given in C++ which is called protected specifier. Declared in the protected specifier, members can access the members of the derived class while objects cannot access these members. In this way the feature of data-hiding is also obtained and the rule of OOPs is not violated.

Inheritance in C++:-

Its syntax is given below –

 class derive-class-name : protected base-class-name

class class_name

{

  private:

     private member;. // Accessible only same class member

  protected:

    protected member; // accessible derive class

  public:

    public member; //accessible own class and derive

};

Protected members can be accessed from derived class members and derived class objects.

Inheritance in CPP:

Source Code: 

// Simple Inheritance Program
#include<iostream.h>
#include<conio.h>
class rect // base class
{
public:
int l,b; //data member
void getlb() // member function
{
cout<<“\n Enter the 2 numbers: “;
cin>>l>>b;
}
void area() // member function
{
cout<<“\n Area of Rect: “<<l*b;
}
};
class volume:public rect // derived class
{
private:
int h; // data member
public:
void geth() // member function
{
cout<<“\n Enter the number: ” ;
cin>>h;
}
void volume1()
{
cout<<“\n Volume: “<<l*b*h;
}
};
void main()
{
volume p;
clrscr();
p.getlb();
p.area();
p.geth();
p.volume1();
getch();
}

OUTPUT:

Enter the 2 numbers: 10

5

Area of Rect: 50

Enter the number: 2

Volume: 100

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *