Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Monday 24 June 2013

Multiple Inheritance And Its C++ Program

Hi Guys,
Today we are going to discuss about the last type of Inheritance we have seen till yet, Multiple Inheritance.
Have you learnt about Inheritance  and its different types? Check Out Now:

There are 5 types of Inheritances used in C++:
1) Single Inheritance
2) Multilevel Inheritance
3) Hierarchial Inheritance
4) Hybrid Inheritance
5) Multiple Inheritance

A class can inherit the attributes of two or more classes as shown in fig. A. This is known as Multiple Inheritance. Multiple Inheritance allows us to combine the features of several existing classes as a starting point for defining new classes. It is like a child inheriting the physical features of one parent and the intelligence of the another.

Multiple Inheritance And Its C++ Program
fig. (A)
The syntax of a derived class with multiple base classes is as follows:

class D: visibility B1visibility B1...
{
          ..........
          .......... (Body Of D)
          ..........
};

where, visibility modes may be either public or private. The base classes are separated by commas.

Example:

   class P : public M, public N
   {
        public:
            void display(void);
   };

The following program shows the program code illustrating how all the three classes are implemented in Multiple Inheritance mode.

Multiple Inheritance C++ Program:

/* Double-Click To Select Code */
#include<iostream.h>
#include<conio.h>
class M
{
 protected:
  int m;
 public:
  void get_m(int x)
  {
   m = x;
  }
};

class N
{
 protected:
  int n;
 public:
  void get_n(int y)
  {
   n = y;
  }
};

class P : public M, public N
{
 public:
  void display()
  {
   cout<<"M = "<<m<<"\n\n";
   cout<<"N = "<<n<<"\n\n";
   cout<<"M*N = "<<m*n<<"\n";
  }
};

void main()
{
 clrscr();
 P p;
 p.get_m(10);
 p.get_n(20);
 p.display();
 getch();
}


Program Output:

Multiple Inheritance And Its C++ Program

Please Comment If You Liked The Post.


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.



Friday 7 June 2013

Hierarchical Inheritance And Its 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 third type, that is, Hierarchical 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) Hierarchial Inheritance
4) Hybrid Inheritance
5) Multiple Inheritance

So in Hierarchical Inheritance, we have 1 Parent Class and Multiple Child Classes, as shown in the pictorial representation given on this page, Inheritance. Many programming problems can be cast into a hierarchy where certain features of one level are shared by many others below that level. One example could be classification of accounts in a commercial bank or classification of students in a university.

In C++, such problems can be easily converted into hierarchies. The base class will include all the features that are common to the subclasses. A subclass can be constructed by inheriting the properties of the base class. A subclass can serve as a base class for the lower level classes and so on.

A derived class with hierarchical inheritance is declared as follows:

class A {.....};            // Base class
class B: public A {.....};             // B derived from A
class C: public A {.....};             // C derived from A

This process can be extended to any number of levels. Let us understand this concept by a simple C++ program:

C++ Program For Hierarchical Inheritance:

/* Double-Click To Select Code */

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

class polygon
{
 protected:
  int width, height;
 public:
  void input(int x, int y)
  {
   width = x;
   height = y;
  }
};

class rectangle : public polygon
{
 public:
  int areaR ()
  {
   return (width * height);
  }
};

class triangle : public polygon
{
 public:
  int areaT ()
  {
   return (width * height / 2);
  }
};

void main ()
{
 clrscr();
 rectangle rect;
 triangle tri;
 rect.input(6,8);
 tri.input(6,10);
 cout <<"Area of Rectangle: "<<rect.areaR()<< endl;
 cout <<"Area of Triangle: "<<tri.areaT()<< endl;
 getch();
}

Program Output:



Hierarchical Inheritance C++ Program

Please Comment If You Liked The Post.