Single inheritance


In single inheritance only one class is derived from a base class

                           A - base class
                           |
                           B- derived class






C++single inheritance only one class can be derived from the base class. Access specifier can be private, protected or public.


Note:
           The objects need to be created for only derived class,there is no need to create object for base class.

Single Inheritance:

 Syntax

class A   // base class
{
    ..........
};
class B : acess_specifier A   // derived class
{
    ...........
} ;

C++ Single Inheritance Example

// inheritance.cpp
#include <iostream> 
using namespace std; 
class base    //single base class
{
   public:
     int x;
   void getdata()
   {
     cout << "Enter the value of x = "; cin >> x;
   }
 };
class derive : public base    //single derived class
{
   private:
    int y;
   public:
   void readdata()
   {
     cout << "Enter the value of y = "; cin >> y;
   }
   void product()
   {
     cout << "Product = " << x * y;
   }
 };
 
 int main()
 {
    derive a;     //object of derived class
    a.getdata();
    a.readdata();
    a.product();
    return 0;
 }         //end of program

Output
Enter the value of x = 3
Enter the value of y = 4
Product = 12
Explanation

In this program class derive is derived from the base class base. So the class derive inherits all the protected and public members of base class base i.e the protected and the public members of base class are accessible from class derive.
private members can’t be accessed, we haven’t used any private data members in the base class.
With the object of the derived class, we can call the functions of both derived and base class

Written by: LOGESHWARAN.B

Comments