Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Wednesday 19 June 2013

Hybrid Inheritance Using Virtual Base Class C++ Program

We have discussed in my previous post, what is Inheritance and its different types in C++, so today we are going to discuss about its fourth type, that is, Hybrid Inheritance.

In Object Oriented Programming, the root meaning of inheritance is to establish a relationship between objects. In Inheritance, classes can inherit behavior and attributes from pre-existing classes, called Base Classes or Parent Classes.The resulting classes are known as derived classes or child classes

There are 5 types of Inheritances used in C++:

1) Single Inheritance
2) Multilevel Inheritance
3) Hierarchical Inheritance
4) Hybrid Inheritance
5) Multiple Inheritance

There could be situations where we need to apply two or more types of inheritance to design a program. For example, consider the case of processing the student results. Assume that we have to give weight-age for sports before finalizing the results. The weight-age for sports is stored in a separate class called sports. In this case, we will need both Multiple and Multilevel Inheritance. 

Consider another case, where all three kinds of inheritance, namely, Multilevel  Multiple and Hierarchical Inheritance, are needed. This is illustrated in the following picture. The 'child' has two direct base classes 'parent1' and 'parent2' which themselves have a common base class 'grandparent'. The child inherits from 'grandparent' via two separate paths. It can also inherit directly as shown by the red line. The 'grandparent' is sometimes known as the Indirect Base Class.


Hybrid Inheritance C++ Program

Inheritance by the above method directly might pose some problems. All the public and protected members of 'grandparent' are inherited into 'child' twice, first via 'parent1' and second via 'parent2'. This means child would have duplicate sets of members inherited from 'grandparents'. This introduces ambiguity and should be avoided.

Hence we use a method called virtual base class. C++ takes necessary care to see that only one copy of that class is inherited, regardless of how many inheritance paths exist between the virtual class and derived class.

The format of using virtual base class is:

class A                    // Grandparent
{ ..
...
 };

class B : virtual public A                 // Parent1
{ ..
...
 };

class C : public virtual A                 // Parent2
{ ..
... 
};

class D : public B, public C              // Child
{ ..
.. 
};


Program Code For Hybrid Inheritance In C++

/* Double-Click To Select Code */

#include<iostream.h>
#include<conio.h>

class student
{
 protected:
  int roll;
 public:
  void get_number(int a)
  {
   roll= a;
  }
  
  void put_number()
  {
   cout<<"Roll Number: "<<roll<<endl;
  }
};

class test : virtual public student
{
 protected:
  float part1, part2;
 public:
  void get_marks(float x, float y)
  {
   part1 = x;
   part2 = y;
  }
  
  void put_marks()
  {
   cout<<"\nMarks Obtained: "<<endl;
   cout<<"Part1 = "<<part1<<endl<<"Part2 = "<<part2<<endl;
  }
};

class sports
{
 protected:
  float score;
 public:
  void get_score(float s)
  {
   score = s;
  }

  void put_score()
  {
   cout<<"\nSports Score: "<<score<<"\n\n";
  }
};

class result : public test, public sports
{
 private:
  float total;
 public:
  void display()
  {
   total = part1 + part2 + score;
   put_number();
   put_marks();
   put_score();
   cout<<"Total Score: "<<total<<"\n";
  }
};

void main()
{
 clrscr();
 result student;
 student.get_number(83);
 student.get_marks(95,98);
 student.get_score(9);
 student.display();
 getch();
}

Program Output:

Hybrid Inheritance Using Virtual Base Class C++ Program

Please Comment If You Liked The Post.


Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: Forum link code: