January 30, 2013

Inheritance in C++ (OOPs)


Inheritance (reusability) of codes in C++ (OOPs) programming language

It is the process by which objects of one class acquire the properties of objects of another class. The old class is referred as Base CLASS and the class that inherits the Base Class if Derived CLASS


In addition to features acquired from the Base Class, a Derived Class can also define its own more features. In OOP’s the concept of Inheritance provides the idea of REUSABILITY of coding. This means that we can use features of an already defined CLASS without having to write the class again.

Base Class is also called as Super CLASS and Derived Class is also called as Sub CLASS. 
A very simple example to better understand is: Take an example of our family Father (HEAD) which here is equal to Base Class, and the children in the family here is Derived Class because always child try to acquire something from the head. So a child can acquire those properties and also some new things from the society.

Needs and Objectives of Inheritance:

1. In ensures the closeness with the real world models.
2. It extends the functionality of an existing CLASS.
3. It establishes a kind of relation between classes.
4. It helps in reuse of an existing class by a sub-class.
5. It implements transitive nature (If a Class B inherits properties of Class A, then all subclasses of B will automatically inherits the properties of A)

Applications:

1. Major advantage is reusability. Once a base class is written and debugged, it need not be touched again but can be used to work in different situations.
2. Reusing existing code saves time, money and increases the program reusability.
3. It also reduces frustration in complex programming. The programmer can add some enhancements the base class.
4. Inheritance simplifies the distribution of class libraries. So programmer can use a class created by another person or company and without modifying it, can derives another class from it which are situated to particular situations. There is no need to change the base class again and again in any circumstances i.e. you can write and debug a base class one time only.

Types of Inheritance:

single level inheritance sequence in C++ (OOPs)


Single Inheritance

When a derived class inherits only from one base class.




/* Program to show use of single level inheritance in arithmetic operations*/

#include<iostream.h>
#include<conio.h>
class TWO;
class ONE
{
      public:
             int a,b,sum,sub,mul;
      void enter()
      {
           cout<<"\n Enter any two interger value";
           cin>>a>>b;
           sum=a+b;
           sub=a-b;
           mul=a*b;
           }
};
class TWO:public ONE
{
      public:
             void display()
             {
                  cout<<"\n Addition is "<<sum;
                  cout<<"\n Subtraction is "<<sub;
                  cout<<"\n Multiplication is "<<mul;
                  }
                  };
main()
{
      class TWO t;
      t.enter();
      t.display();
      getch();
      }
output of program to show single level inheritance in arithmetic operation in C++ (OOPs)

/* Program to show use of single inheritance in students data*/

#include<iostream.h>
#include<conio.h>
class stud2;
class stud1
{
      public:
             char name[20],clas[10];
             int age,rol;
      void enter()
      {
           cout<<"\n Enter student's name and class";
           cin>>name>>clas;
           cout<<"\n Enter student's age and roll number";
           cin>>age>>rol;
           }
};
class stud2:public stud1
{
      public:
      void display()
      {
           cout<<"\n R.No.\tName\tAge\tClass";
           cout<<"\n "<<rol<<"\t"<<name<<"\t"<<age<<"\t"<<clas;
           }
           };
main()
{
      class stud2 s;
      s.enter();
      s.display();
      getch();
      }
output of program to show single level inheritance in student data in C++ (OOPs)

Multiple Inheritance

When a derived class inherits from multiple base classes.

multiple level inheritance sequence in C++ (OOPs)

/* Program to show use of multiple inheritance in arithmetic operations*/

#include<iostream.h>
#include<conio.h>
class two;
class three;
class one
{
      public:
             int a,b,sum;
      void add()
      {
           cout<<"\n Enter any two integer values";
           cin>>a>>b;
           sum=a+b;
           cout<<"\n Addition of "<<a<<" and "<<b<<" is "<<sum;
           }
};
class two
{
      public:
             int a,b,mul;
      void mult()
      {
           cout<<"\n\n Enter any two integer values";
           cin>>a>>b;
           mul=a*b;
           cout<<"\n Multiplication of "<<a<<" and "<<b<<" is "<<mul;
           }
};
class three:public one,public two
{
      public:
             int sub;
             void subt()
             {
                  sub=mul-sum;
                  cout<<"\n\n Subtraction of "<<sum<<" from "<<mul<<" is "<<sub;
                  }
};
main()
{
      class three t;
      t.add();
      t.mult();
      t.subt();
      getch();
      }
output of program to show use of multiple inheritance in arithmetic operations in C++ (OOPs)



Hierarchical Inheritance

When the properties of one class may be inherited by more than one class.

hierarchical inheritance sequence in C++ (OOPs)

/* Program to show use of hierarchical inheritance in arithmetic operations*/

#include<iostream.h>
#include<conio.h>
class B;
class C;
class D;
int a,b;
class A
{
      public:       
      void enter()
      {
             cout<<"\n Enter any two integer values";
             cin>>a>>b;
             }
};
class B:public A
{
      public:
      void add()
      {
           int sum=a+b;
           cout<<"\n\n Addition of "<<a<<" and "<<b<<" is "<<sum;
           }
};
class C:public A
{
     public:
     void subt()
     {
       int sub=a-b;
       cout<<"\n\n Subtraction of "<<a<<" from "<<b<<" is "<<sub;
           }
};
class D:public A
{
   public:
   void mult()
   {
     int mul=a*b;
     cout<<"\n\n Multiplication of "<<a<<" and "<<b<<" is "<<mul;
           }
};
main()
{
      class B b;
      class C c;
      class D d;
      b.enter();
      b.add();
      c.subt();
      d.mult();
      getch();
      }
output of program to show use of hierarchical inheritance in arithmetic program in C++ (OOPs)
        

      

multi level inheritance sequence in C++ (OOPs)


Multi-level Inheritance

When a derived class that itself inherits from another class. This form of Inheritance reflects the transitive nature of Inheritance.



/* Program to show use of multi-level inheritance in arithmetic operations*/

#include<iostream.h>
#include<conio.h>
class B;
class C;
class A
{
      public:
             int a,b;
      void enter()
      {
           cout<<"\n Enter any two integer values";
           cin>>a>>b;
           }
};
class B:public A
{
      public:
      void modu()
      {
           int mod=a%b;
           cout<<"\n Remainder of "<<a<<"/"<<b<<" is "<<mod;
           }
};
class C:public B
{
      public:
      void divi()
      {
           float div=float(a)/b;
           cout<<"\n\n Division of "<<a<<" and "<<b<<" is "<<div;
           }
};
main()
{
      class C c;
      c.enter();
      c.modu();
      c.divi();
      getch();
      }
output of program to show use of multi level inheritance in arithmetic operation in C++ (OOPs)
      

Hybrid Inheritance

When a derived class inherits from multiple base classes and all of its base classes inherits from a single base class.

hybrid inheritance sequence in C++ (OOPs)

/* Program to show use of hybrid inheritance in arithmetic operations*/


#include<iostream.h>
#include<conio.h>
class B;
class C;
class D;
int a,b,sum,sub,mul;
class A
{
      public: 
      void enter()
      {
           cout<<"\n Enter any two integer values";
           cin>>a>>b;
           }
};
class B:public A
{
      public:
      void add()
      {
           sum=a+b;
           cout<<"\n Addition of "<<a<<" and "<<b<<" is "<<sum;
           }
};
class C:public A
{
    public:
    void mult()
    {
     mul=a*b;
     cout<<"\n\n Multiplication of "<<a<<" and "<<b<<" is "<<mul;
           }
};
class D:public B,public C
{
  public:
  void subt()
  {
   sub=sum-mul;
   cout<<"\n\n Subtraction of "<<mul<<" from "<<sum<<" is "<<sub;
           }
};
main()
{
      class B b;
      class C c;
      class D d;
      b.enter();
      b.add();
      c.mult();
      d.subt();
      getch();
      }
output of program to show use of hybrid inheritance in arithmetic operation in C++ (OOPs)



AMBIGUITY

A lot of ambiguities can creep in, when a class inherits from multiple base classes. In these type of programs different class with same function name is called, so the compiler get confused when the function is called in the main function. So, to avoid from this problem some following techniques can be applied in the program to avoid confusion.

/* Program to show how to remove ambiguity from arithmetic operation*/

#include<iostream.h>
#include<conio.h>
class B;
class C;
class A
{
      public:
             int a;
      void enter()
      {
           cout<<"\n Enter any value of a";
           cin>>a;
           }
};
class B
{
      public:
             int b;
      void enter()
      {
           cout<<"\n Enter any value of b";
           cin>>b;
           }
};
class C:public A,public B
{
      public:
      void display()
      {
           int sum=a+b;
           cout<<"\n Addition of "<<a<<" and "<<b<<" is "<<sum;
           }
};
main()
{
      class C c;
      c.A::enter();
      c.B::enter();
      c.display();
      getch();
      }
output of program to remove ambiguity in case of arithmetic operation in C++ (OOPs)

/* Program to show ambiguity in student data*/

#include<iostream.h>
#include<conio.h>
class stud;
class user
{
      public:
             int n;
      void enter()
      {
           cout<<"\n How many student data you want to enter";
           cin>>n;
           }
};
class stud:public user
{
      public:
             int age[10],i;
             char name[20][10],clas[10][10];
      void enter()
      {
          for(i=1;i<=n;i++)
          {
            cout<<"\n Enter "<<i<<" student name, age and class";
            cin>>name[i]>>age[i]>>clas[i];
                            }
                            }
    void display()
    {
     cout<<"\n Sr.\tAge\tName\tClass";  
     for(i=1;i<=n;i++)
     {
      cout<<"\n "<<i<<"\t"<<age[i]<<"\t"<<name[i]<<"\t"<<clas[i];
                            }
                            }
};
main()
{
      class stud s;
      s.user::enter();
      s.stud::enter();
      s.display();
      getch();
      }
output of program to remove ambiguity in case of student data in C++ (OOPs)

No comments:

Post a Comment