Inheritance in C++

Inheritance in C++

1. Inheritance is the process of inheriting behaviour and properties of existing class into a new class.

2. Inheritance is the ability to create a new class by making use of the existing class.

3. It helps in reducing the development time.

4. It allows new classes to acquire the properties of an existing class.

5. It helps in reusing the code.

6. Existing classes are called base classes or parent classes or super classes or old classes while the new classes are called derived classes or child classes or subclasses.

7. Inheritance defines an "is-a" relationship among classes.


Syntax
class Base_Class
{
  //base class code.
};

class Derived_Class:Visibility_Mode Base_Class
{
  //derived class code.
};

For Example
class Car
{
  //base class code.
};

class SportsCar: public Car
{
  //derived class code.
};

Types of Inheritance in C++

1. Single Inheritance.
2. Multiple Inheritance.
3. Multi-level Inheritance.
4. Hierarchical Inheritance.
5. Hybrid Inheritance.


Single Inheritance in C++

When a subclass is derived from a single-parent class is called single inheritance.


For Example
class Car
{
  //base class code.
};

class SportsCar: public Car
{
  //derived class code.
};

Multiple Inheritance in C++

1. When a subclass is derived from more than one base class is called multiple inheritance.

2. The relation of one child with two parents is shown in multiple inheritance.


For Example
class A
 {

 };

class B
 {

 };
 
 class C: public A, public B
 {
  
 };

Multi-level Inheritance in C++

1. When a subclass is derived from a superclass and superclass is derived from another superclass and so on is called multi-level inheritance.

2. It shows the relation of Grand Parent to Parent and the relation of Parent to Child.


For Example
class A
 {
 
 };
 
class B: public class A
 {

 };
 
class C: public B
 {
 
 };

Hierarchical Inheritance in C++

1. The tree structure like inheritance is called hierarchical inheritance.

2. The relation of multiple children with one parent is shown in hierarchical inheritance.


For Example
class A
 {
 
 };
 
class B1: public A
 {

 };
 
class B2: public A
 {
 
 };

Hybrid Inheritance in C++

The combination of multiple and multi-level inheritance is called hybrid inheritance.


For Example
class A
 {
 
 };
 
class B: public A
 {
 
 };
 
class C
 {
 
 };
 
class D: public B, public C
 {
 
 };

Visibility Modes (Access Specifiers) in C++

1. Private

2. Protected

3. Public


Private Access Specifier

1. The private access specifier is the default access specifier in C++ class.


2. Member variables declared within private access specifier can only be accessed by its member function declared within the class.


3. It cannot be accessed outside the class.


4. So, private access specifier is used for implementing encapsulation (data hiding).


5. When we want to protect the important data from the user then we use private access specifier.


Protected Access Specifier

1. Protected access specifier and function overriding are used in the case of inheritance when user wants to access data of different classes in the inheritance.


2. Child class can directly access protected members of its parent class.


Public Access Specifier

1. Public access specifier allows the access of data members from inside the class and also from outside the class.

2. Each and every data member and member function declared with public access specifier can be accessed from anywhere in the program.

3. Generally, member functions are declared with public access specifier so that the user can access it and make some operations on the objects.

4. is a relationship is always implemented as a public inheritance because object of derived class wants to access the public members of base class.


Types of Users of any class

1. User1 who will create object of the class.
2. User2 who will derived new class from the class.


Availability vs Accessibility

Each and every member of class (whether it is private member, protected member or public member) will available to object of that class.

But only public member of the class can be accessible to object of that class.


Accessibility Diagram in Inheritance in C++

Inheritance-Accessibility-Diagram-in-c++-learning-mania

When to use private and protected access specifiers?

For Example
class Array
{
private:
  int a[10];
  
public:
  void insert(int index, int value)
  {
    a[index] = value;
  }
};

class STACK: private Array       // we can also use protected here.
{
  int top;
  
public:
  void push(int value)
  {
    insert(top, value);
  }
};

int main()
{
  STACK S1;
  
  S1.push(35);
  S1.insert(2, 99);        // error when there is public inheritance.
}

Previous
Next Post »