CPP Program to Implement the Single Inheritance Concept

Aim:

To write a C++ program for implementing the Single inheritance concept.

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 another class is called the Parent or Base or Superclass.
  • 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


Purpose of Inheritance in C++

  1. Code Reusability
  2. Method Overriding (Hence, Runtime Polymorphism.)
  3. Use of Virtual Keyword

Access Control and Inheritance

  • A derived class can access all the non-private members of its base class.
  • Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

We can summarize the different access types according to – who can access them in the following way −

Accesspublicprotectedprivate
Same classyesyesyes
Derived classesyesyesno
Outside classesyesnono

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.

 

Basic Syntax of Inheritance:

class Subclass_name : access_mode Superclass_name
  • While defining a subclass like this, the super class must be already defined or atleast declared before the subclass declaration.
  • Access Mode is used to specify, the mode in which the properties of superclass will be inherited into subclass, public, privtate or protected.

Access Modifiers and Inheritance: Visibility of Class Members

  • Depending on the Access modifier used while inheritance, the availability of class members of Superclass in the subclass changes.
  • It can either be private, protected, or public.

1) 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

2) 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

3) Protected Inheritance

  • In protected mode, the public and protected members of Super class becomes protected members of Sub class.
class subclass : protected Superclass

Types of Inheritance in C++

In C++, we have 5 different types of Inheritance. Namely,

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance (also known as Virtual Inheritance)

Single Inheritance in C++

  • In this type of inheritance one derived class inherits from only one base class.
  • It is the simplest form of Inheritance.

Algorithm:

  1. Start the process.
  2. Define the base class with variables and functions.
  3. Define the derived class with variables and functions.
  4. Get two values in the main function.
  5. Define the object for derived class in the main function.
  6. Access member of the derived class and base class using the derived class object.
  7. Stop the process.

Program:

#include <iostream> 
using namespace std; 
class base    
{
protected:
int x;
public:
void getdata()
{
cout << "Enter the value of x = "; cin >> x;cout<<x;
}
};
class derive : public base    
{
private:
int y;
public:
void readdata()
{
cout << "\nEnter the value of y = "; cin >> y;cout<<y;
}
void product()
{
cout << "\nProduct = " << x * y;
}
};
int main()
{
derive a;     
a.getdata();
a.readdata();
a.product();
return 0;
} 

Execution:

Input
17 3

Output
Enter the value of x = 17
Enter the value of y = 3
Product = 51

Result:

Thus, the Implement the Single Inheritance Concept was executed successfully.

Leave a Comment