Single Inheritance in C++

In "Single Inheritance" their is only one base-class and one derived-class.

LOGO


Basically their is only one "Parent-class"(BASE CLASS) from which  the properties / behavior will be inherited to the "Child-class"(DERIVED CLASS).


Block-Diagram
BLOCK DIAGRAM

Syntax:-
class Parent
{
//Body of the Parent class
};

class Child : access_specifier Parent
{
//Body of the Child class
};


Example of Single-Inheritance
#include<iostream>
using namespace std;

class Parent
{
 protected:
  int a=10;
};

class Child : public Parent //Inheriting the class
{
 public:
  int b=2;
  void put()
  {
   cout<<"Sum is "<<a+b;
   //Using 'a' from the Parent class
  }
};

int main()
{
 Child x; //'x' is the object of class "Child"
 x.put(); //Calling the function "put()"
}



Also visit ----

Introduction to Inheritance

Multiple Inheritance


For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog by clicking the follow button. Thank you for your support and time...

Post a Comment

0 Comments