Wednesday 14 March 2018

Inheritance In C++

Inheritance is the property of deriving a new class from an existing class.
The existing class or old class is referred to as base class or super class or parent class.
The new class is referred to as derived class or subclass or child class.
Use : symbol to inherit a new class from existing class.
Advantage
1) Code reusability (You can reuse same code again and again)
2) Extensibility (New class is generated from existing class )
Syntax:
Class derived-class: visibility-mode base-class
{
};
Where visibility-mode is your access-modifier that may be private, public, protected.

Note:
Sub class can access both  super and sub class values but super class can access only its own class value.

Types:
There are five types of inheritance in c++, they are
·         Single Inheritance
·         Multiple Inheritance
·         Hierarchical Inheritance
·         Multilevel Inheritance
·         Hybrid Inheritance (also known as Virtual Inheritance)

1)Single Inheritance
In single inheritance, there is only one base class and one derived class. The Derived class gets inherited from its base class

Syntax:
Class A
{
//statements
};
Class B:public A
{
//statements
};
Example:
#include<iostream.h> 
#include<conio.h> 
 class A{ 
     public:
     int a,b;
   public: 
   void show()
   {
       cout<<"Hello"<<endl;
   }
 }; 
   class B:public A { 
   public: 
   void add()
       {
          cout<<"Enter two no"<<endl;
          cin>>a>>b;
          cout<<"sum is:"<<a+b<<endl;
       } 
   };      
void main()
{ 
   clrscr();
    B b1; 
     b1.show();
     b1.add();
   getch();
} 
Output:
Hello                                                                                                                          
Enter two no                                                                                                                   
23                                                                                                                             
45                                                                                                                             
sum is:68


2) Multiple Inheritance
In Multiple inheritance, a single derived class may inherit from two or more than two base classes



Syntax:
Class A
{
// statements
};
Class B
{
// statements
};
Class C:public A,public B
{
// statements
};
Example:
#include<iostream.h> 
#include<conio.h> 
    class A{ 
     public:
     int a,b;
   public: 
   void showParent1()
   {
       cout<<"Parent1"<<endl;
   }
 }; 
  class B{ 
     public:
     int c;
   public: 
   void showParent2()
   {
       cout<<"Parent2"<<endl;
   }
 };
   class C:public A ,public B{ 
   public: 
   void addChild()
       {
          cout<<"Enter two no"<<endl;
          cin>>a>>b;
          cout<<"sum is:"<<a+b<<endl;
       }
        void sqaureChild()
       {
          cout<<"Enter no"<<endl;
          cin>>c;
          cout<<"square is:"<<c*c<<endl;
       }
      
   };      
void main() { 
clrscr();
    C c1; 
     c1.showParent1();
     c1.showParent2();
       c1.addChild();
         c1.sqaureChild();
    getch(); 
} 
Output:
Parent1                                                                                                                        
Parent2                                                                                                                        
Enter two no                                                                                                                   
23                                                                                                                             
45                                                                                                                             
sum is:68   
Enter no                                                                                                                       
5                                                                                                                              
square is:25

3)Hierarchical Inheritance
In Hierarchical inheritance, multiple derived classes inherits from a single base class



Syntax:
class A{ 
      //statements
 }; 
  class B:public A{ 
     //statements
 };
   class C:public A { 
 //statements
   };      

Example:
#include<iostream.h> 
#include<conio.h> 
 class A{ 
     public:
     int a,b,c;
   public: 
   void showParent()
   {
       cout<<"Parent1"<<endl;
   }
 }; 
  class B:public A{ 
      public: 
   void addChild1()
       {
          cout<<"Enter two no"<<endl;
          cin>>a>>b;
          cout<<"sum is:"<<a+b<<endl;
       }
 };
   class C:public A { 
   public: 
      void sqaureChild2()
       {
          cout<<"Enter no"<<endl;
          cin>>c;
          cout<<"square is:"<<c*c<<endl;
       }
      
   };      
void main() { 
    A a1;
    a1.showParent();
    B b1;
    b1.addChild1();
    C c1; 
     c1.sqaureChild2();
getch();
} 
Output:
Parent1                                                                                                                                                                                                                                              
Enter two no                                                                                                                   
23                                                                                                                             
45                                                                                                                             
sum is:68   
Enter no                                                                                                                       
5                                                                                                                              
square is:25


4)Multilevel Inheritance
The classes can also be derived from the classes that are already derived. This type of inheritance is called multilevel inheritance
In this type of inheritance the derived class inherits from a class, which in turn inherits from some other class.

  Syntax:
class A{ 
   //Statements
 }; 
  class B:public A{ 
       //Statements
 };
   class C:public B{ 
   //Statements
 };      

  Example:
#include<iostream.h> 
#include<conio.h> 
 class A{ 
     public:
     int a,b;
   public: 
   void showParent()
   {
       cout<<"Parent1"<<endl;
   }
 }; 
  class B:public A{ 
   
   public: 
   int c;
   void addChild1()
       {
          cout<<"Enter two no"<<endl;
          cin>>a>>b;
          cout<<"sum is:"<<a+b<<endl;
       }
 };
   class C:public B{ 
   public: 
    void sqaureChild2()
       {
          cout<<"Enter no"<<endl;
          cin>>c;
          cout<<"square is:"<<c*c<<endl;
       }
   void mult()
   {
       cout<<"multiplication is:"<<a*b*c<<endl;
   }

    };      
void main() { 
     C c1; 
     c1.showParent();
     c1.addChild1();
     c1.sqaureChild2();
     c1.mult();
     getch();
} 
Output:
Parent1                                                                                                                        
Enter two no                                                                                                                   
4                                                                                                                              
5                                                                                                                              
sum is:9                                                                                                                       
Enter no 
5                                                                                                                              
square is:25                                                                                                                   
multiplication is:100

  
Hybrid Inheritance
  Hybrid Inheritance is implemented by combining more than one type of inheritance like    
1)Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.
2) Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance
3)  Hybrid inheritance is a combination of Hierarchical inheritance and Multiple Inheritance

Syntax: Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance
class A{ 
  //statements
 }; 
  class B:public A{ 
     //statements
 };
   class C:public A{ 
  //statements
  };  
  class D:public B,public C{ 
   //statements
  };  

Example: Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance
#include<iostream.h> 
#include<conio.h> 
 class A{ 
     public:
     int a,b,c;
   public: 
   void showParent()
   {
       cout<<"Parent1"<<endl;
   }
 }; 
  class B:public A{ 
   public: 
   int x;
   void addChild1()
       {
          cout<<"Enter two no"<<endl;
          cin>>a>>b;
          cout<<"sum is:"<<a+b<<endl;
       }
 };
   class C:public A{ 
   public:
   int y;
    void sqaureChild2()
       {
          cout<<"Enter no"<<endl;
          cin>>c;
          cout<<"square is:"<<c*c<<endl;
       }
  };  
  class D:public B,public C{ 
   public: 
    void multiChild3()
       {
          cout<<"Enter no"<<endl;
          cin>>x>>y;
cout<<"multiplication is:"<<x*y<<endl;       }
  };  
 
void main() { 
 
    A a1; 
     a1.showParent();
      D d1;
     d1.addChild1();
     d1.sqaureChild2();
     d1.multiChild3();
    
getch();
} 
Output:
Parent1                                                                                                                        
Enter two no                                                                                                                   
4                                                                                                                              
5                                                                                                                              
sum is:9                                                                                                                       
Enter no 
5                                                                                                                              
square is:25                                                                                                                   
Enter no                                                                                                                       
7                                                                                                                              
6
multiplication is:42


1 comment:

  1. Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.

    ReplyDelete

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...