Tutorial On Inheritance in c++ and Related Concepts

Inheritance is the most important feature in OOP (Object Oriented Programming).Inheritance can be defined as it is the process of linking of two or more classes in such a way so that their properties and functions can be shared i.e the capability of one class to share the characteristics of another class. Inheritance provides the reusability of code. When inheritance is done between two classes all the functions are inherited in another class , thus code is reused in another class .

The existing class whose properties and characteristics are shared by another class is known as the parent class which is also called base class or super class and the class which inherits the properties of another class is called derived class which is also called as child class or sub class.The derived class inherits all the characteristics of the base class but can add embellishments and refinements of its own. The base class remains unchanged by this process.

inheritance tutorial

Arrow means derived from , the head of arrow is always towards base class. Feature D is defined in derived class ,whereas feature A , feature B and  feature C are defined in base class which are also   accessible in derived class due to inheritance.

Derived class constructor

Constructors are not inherited, even though they have public visibility. However, the super reference can be used within the child’s constructor to call the parent’s constructor. In that case, the call to parent’s constructor must be the first statement.

Overriding

Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes or parent classes. The implementation in the subclass overrides (replaces) the implementation in the super class by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed. Some languages allow a programmer to prevent a method from being overridden

EXAMPLES

This example will explain the method to specify the derived class.

#include<iostream>

#include<conio.h>

using namespace std;

class BaseClass

{                                                                                                                                  // start of base class

int num1;

public:                                                                                                                    //public section of class

void setInt(int n);                                                                                             // function prototype

int getInt();                                                                                                         // function prototype

};                                                                                                                            // end of base class

 

class DerivedClass : public BaseClass {                                               //start of derived class

int num2;

public:

void setJ(int n);                                                                                         // function prototype

int mul();                                                                                                      // function prototype

};                                                                                                                    //end of derived class

void BaseClass::setInt(int n)                                                             // function declaration

{

num1 = n;

}

int BaseClass::getInt()                                                                          // function declaration

{

return num1;

}

void DerivedClass::setJ(int n)                                                         //function declaration

{

num2 = n;

}

int DerivedClass::mul()                                                                      // function declaration

{

cout<<“Result=”;

return (num2 * getInt());

}

 

int main()

{

DerivedClass ob;                                                                                              //derived class object

ob.setInt(10);                                                                                                    // load num1 in BaseClass

ob.setJ(4);                                                                                                          // load num2 in DerivedClass

cout << ob.mul()<<endl;                                                                            // displays 40

getch();

}

inheritance tutorial

ANOTHER EXAMPLE

This example will explain the way to access the base class members with derived  class object.

It also demonstrates that how we can use the concept of Overriding.
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class employee // base class
{
private:
string name;
int emp_no;
public:
void set()
{
cin>>name>>emp_no;
}
void display()
{
cout<<“n”<<“NAME:-“<<name<<“n”<<“EMPLOYE NO:-“<<emp_no;
}
};

class student : public employee //derived class
{
private :
int code;
public:
void set()
{
employee::set(); //calling for set of employee
cin>>code;
}
void display()
{
employee:: display(); //calling for display of employee
cout<<“nCODE:-“<<code;

}
};

int main()
{
student s1;
s1.set();
s1.display();
getch();
}

Class student inherits properties from its base class employee .Scope Resolution operator is used for the calling of set and display functions of a base class employee from its derived class student.Void display() is overridden .

inheritance tutorial

Scope of Private , Public and Protected Data Members

Private data members are accessed only within its own class , protected  data members can be accessed in its own class as well as its derived class but not in main function ,public data members can be accessed anywhere in the program.

ACCESSIBLITY MODES

Depending on access modifier ,there are three accessibility modes in inheritance

  • Public
  • Private
  • Protected

PUBLIC INHERITANCE

In public inheritance all the data members and member functions are publically inherited from  base class to derived class .In this inheritance the protected data members of the parent class becomes the protected data members of derived class and the public data members of the parent class becomes the public data members of derived class. A base class’s private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

NOTE: Public inheritance is the most used inheritance mode.

Block Diagram

inheritance tutorial

PROCTECTED  INHERITANCE

In protected inheritance all the data members and member functions are protectically inherited from  base class to derived class .In this inheritance the protected data members of the parent class becomes the protected data members of derived class and the public data members of the parent class becomes the protected data members of derived class.

Block Diagram

inheritance in c++Protected data remains protected but public data or member functions also becomes  protected as shown in block diagram

EXAMPLE

#include<iostream>

#include<conio.h>

using namespace std;

class A

{

private:

int a;

public:

void set()

{

cin>>a;

}

int display1()

{

return a;

}

};

class B:protected A

{

private:

int b;

public:

void set()

{

A::set();

cin>>b;

}

int display()

{

return b;

}

};

int main()

{

B s1;

s1.set();

cout<<“n a = “<<s1.display1()<<“b = “<<s1.display();        /*display1(); cannot be accessed in main                                                                                                                                                                                                because of protected inheritance*/

getch();

}

int display()1; becomes protected in  class B so it scope is limited i.e it can be used by any other derived class from it, but it cannot be accessed in main function. Given below is another example of this scenario.

inheritance in c++

EXAMPLE

#include<iostream>

#include<conio.h>

using namespace std;

class A

{

private:

int a;

public:

void set()

{

cin>>a;

}

int display1()

{

return a;

}

};

class B:protected A

{

private:

int b;

public:

void set()

{

cout<<“enter number 1:-“;

A::set();

cout<<“enter number 2:-“;

cin>>b;

}

int display()

{

return b;

}

};

 

class C:public B

{

public:

void display2()

{

cout<<“n c = “<<display1();                  /*Here display1() can be accessible…if class B is privately  derived

from A  then it cannot be accessed*/

}

};

int main()

{

C s1;

s1.set();

s1.display2();

getch();

}

inheritance in c++

PRIVATE  INHERITANCE

In private inheritance all the data members and member functions are privately inherited from  base class to derived class .In this inheritance the protected data members of the parent class becomes the private data members of derived class and the public data members of the parent class becomes also the private data members of derived class.

Block Diagram 

inheritance in c++

EXAMPLE

#include<iostream>

#include<conio.h>

using namespace std;

class A

{

private:

int a;

public:

void set()

{

cin>>a;

}

int display1()

{

return a;

}

};

 

class B:private A

{

private:

int b;

public:

void set()

{

A::set();

cin>>b;

}

int display()

{

return b;

}

};

class C:public B

{

public:

void display2()

{

cout<<“n c = “<<display1();                  /*Here display1() can be accessible…because class B is privately

derived from  A */

}

};

int main()

{

C s1;

s1.set();

s1.display2();

getch();

}                                                                                       

Now the scope of display1(); is limited upto only class B. That is why it cannot be accessed in class C.

inheritance in c++

EXAMPLE

#include<iostream>
#include<conio.h>
using namespace std;
class A
{
private:
int a;
public:
void set()
{
cout<<“n enter a:-“;
cin>>a;
}
void display()
{
cout<<“n a = “<<a;
}

};

class B:private A
{
private:
int b;
public:
void set()
{
A::set();
cout<<“n enter b:-“;
cin>>b;

}
void display()
{
A::display();
cout<<“n b = “<<b;
}

};
int main()
{
B s1;
s1.set();
s1.display();
getch();
}

inheritance in c++

TYPES OF INHERITANCE

The Various Types of Inheritance those are provided by C++ are as followings:

1.                Single Inheritance

2.                Multilevel Inheritance

3.                Multiple Inheritance

4.                Hierarchical Inheritance

5.                Hybrid Inheritance

1.    Single Inheritance

This is most simplest form of inheritance.In Single inheritance only one class is derived from one base class.

inheritance in c++

Syntax

class  A

{public:

int x;

};

class B: public A

{

public:

int y;

};

2    Multilevel Inheritance

In multilevel inheritance the super class for one is the sub class for the other

inheritance in c++

Syntax

class A{

public:

int x;

};

class B: public A{

public:

int y;

};

class C : public B  { };

EXAMPLE

#include<iostream>
#include<conio.h>
using namespace std;
class base
{
private:
int a;
public:
void set()
{
cout<<“Enter 1st number :-“;
cin>>a;
}
void display()
{
cout<<“n a = “<<a;
}

};

class derive1:public base
{
private:
int b;
public:
void set()
{
base::set();
cout<<“Enter 2nd number :-“;
cin>>b;
}
void display()
{
base::display();
cout<<“n b = “<<b;
}
};

class derive2:public derive1
{
private:
int c;
public:
void set()
{
derive1::set();
cout<<“Enter 3rd number :-“;
cin>>c;
}
void display()
{
derive1::display();
cout<<“n c = “<<c;
}
};
int main()
{
derive2 s1;
s1.set();
s1.display();
getch(); }

 

 

inheritance in c++ 

3. Multiple Inheritance

In multiple inheritance a single class can be derived from two base classes.

inheritance in c++

  Syntax

class A{

};

class B{

};

class C: public A, public B

{     };

EXAMPLE

#include<iostream>
#include<conio.h>
using namespace std;
class A
{
private:
int x;
public:
void set()
{
cout<<“ENTER value for x:-“;
cin>>x;
}
void display()
{
cout<<“n x = “<<x;
}
};

class B
{
private:
int y;
public:
void set()
{
cout<<“ENTER value for y:-“;
cin>>y;
}
void display()
{
cout<<“n y = “<<y;
}
};

class C : public A,public B //publically inherited from class A and class B
{
private:
int z;
public:
void set()
{
A::set();
B::set();
cout<<“ENTER value for z:-“;
cin>>z;
}
void display()
{
A::display();
B::display();
cout<<“n z = “<<z;
}
};

int main()
{
C s1;
s1.set();
s1.display();
getch();
}

inheritance in c++

 

4.Hierarchical Inheritance

In  Hierarchical Inheritance multiple classes are derived from only one base class .

inheritance in c++

 

Syntax

class A{

public:

int x;

};

class B: public A{

public:

int y;

};

class C : public A { };

EXAMPLE

#include<iostream>
#include<conio.h>
using namespace std;
class base
{
private:
int a;
public:
void set()
{
cout<<“n enter value of a:-“;
cin>>a;
}
void display()
{
cout<<“n a = “<<a;
}

};

class derive1:public base
{
private:
int b;
public:
void set()
{
base::set();
cout<<“n enter value of b:-“;
cin>>b;
}
void display()
{
base::display();
cout<<“n b = “<<b;
}
};

class derive2:public base
{
private:
int c;
public:
void set()
{
base::set();
cout<<“n enter value of c:-“;
cin>>c;
}
void display()
{
base::display();
cout<<“n c = “<<c;
}
};

int main()
{
derive2 s1;
s1.set();
s1.display();
derive1 s2;
s2.set();
s2.display();
getch();
}

inheritance in c++

5.Hybrid Inheritance

inheritance in c++

AMBIGUITY IN MULTIPLE INHERITANCE

Diamond Problem is the ambiguity in multiple inheritance , because of this reason multiple inheritance is avoided in higher languages.

What is Diamond Problem

In Diamond Problem ,two classes are derived from one base class ,and then another fourth class is derived from these two classes.The two derived classes act as a base class for the fourth derived class

inheritance in c++

DIAMOND PROBLEM AND ITS SOLUTION

#include<iostream>

#include<conio.h>

using namespace std;

class A

{

private:

int a;

public:

void set();

void display();

};

class B:public A

{

private:

int b;

public:

void set();

void display();

};

class C:public A

{

private:

int c;

public:

void display();

};

class D:public B,public C

{

private:

int d;

public:

void display();

};

int main()

{

D s1;

s1.set();

s1.display();

getch();

}

Since both class A and class C  are using the method set() from the base class, when calling the method set() from the object of class D  the call is ambiguous; the compiler can’t know(confuse) which implementation of set() to use, the one from the class B or the one from the class C.

inheritance in c++

SOLUTION

We have to made a set() method in the class B , class C and class D  and then call the set method by using a scope resolution operator  “ :: ” from class D, like B::set(),  C::set().then from class C and class D call the set() method of class A using scope resolution operator like A::set().

EXAMPLE

#include<iostream>
#include<conio.h>
using namespace std;
class A
{
private:
int a;
public:
void set()
{
cout<<“nEnter value of a:-“;
cin>>a;
}
void display()
{

cout<<“n a = “<<a;

}

};
class B:public A
{
private:
int b;
public:
void set()
{
A::set();
cout<<“nEnter value of b:-“;
cin>>b;
}
void display()
{
A::display();
cout<<“n b = “<<b;
}
};
class C:public A
{
private:
int c;
public:
void set()
{

cout<<“nEnter value of c:-“;
cin>>c;
}
void display()
{

cout<<“n c = “<<c;
}
};
class D:public B,public C
{
private:
int d;
public:
void set()
{
B::set();
C::set();
cout<<“nEnter value of d:-“;
cin>>d;
}
void display()
{
B::display();
C::display();
cout<<“n d = “<<d;
}
};
int main()
{
D s1;
s1.set();
s1.display();
getch();
}

inheritance in c++

 

6 thoughts on “Tutorial On Inheritance in c++ and Related Concepts”

Leave a Reply

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