* New post Constructor Destructor and Inheritance& Array , Virtual base class in c++ language

Saturday, 19 April 2014

0 Virtual Base Class & Polymorphism in c++ language


Virtual Base Class

Virtual base class : When two derived classes access only the single copy of the base class, then the base class is known as a virtual base class. When a multiply , multilevel and hierarchical inheritance are combined in a program then the need of virtual base class occurs. In the above diagram class D has two direct base classes B & C and origin these base classes B and C have a common base class A. The derived class D inherits the features of base class A by two derived classes B and C . Hence there is  a problem occurs , the derived class D inherits the features of base class two times by B and By C . i.e class A has a duplicate set  of members . It causes the ambiguity problem . When we use base class as virtual base class then the problem of ambiguity is avoided . And in the combined inheritance of multiple , multilevel and hierarchical , a single copy of the member is passed to the derived class .
Write a program to demonstrate the use of virtual base classes?
         Class A
         {
           Protected :
                              Int N[20];
           Public :
                       Void get N( )
                       {
                         cout <<"enter customer name :";
                         cin >>N;
                       }
           };
             Class B : Virtual Public A 
            {
               Protected : 
                             int pb;
               Public :
                          Void getpb( )
                          {
                             cout <<"enter previous balance :";
                              cin>>pb ;
                           }
                 };
                    Class C : Public Virtual A
                   {
                      Protected :
                                        int amt ;
                       Public :
                                  Void getamt ( )
                                  {
                                    cout <<"enter deposit amount :";
                                    cin >> amt  ;
                                  }
                     };
                       Class D :Public C : Public B
                        {
                           Protected :
                                            Int t ;
                           
                                       Void gettotal ( )
                                       {
                                         t =pbtamt ;
                                        }
                                  Void display ( )
                                  {
                                     cout <<"customer name ="<<N"\n\n";
                                     cout <<"total amount = "<<amt<<"+"<<pb<<"="<<t<<"\n\n";
                                  }
                         };
                          Void main ( )
                           {                                  // www.programmingking.in
                             clrscr ( );
                             D d1 ;
                             cout <<"*******************\n\n";
                              d1.getn( ) ;
                              d1.amt
                              d1.gettotal ( ) ;
                              cout <<"**************\n\n";
                              d1.display ( ) ;
                              cout <<****************\n\n";
                              getch ( );
                            }    
Virtual Function : A virtual function is a member function that is defined in both class in base class and derived class by perceding the keyword virtual . A virtual function is same as the member function but it does not exist in the program . The virtual is a function is a mechanism that is based on the concept of polymorphism .
If we want to perform different functionality by a same function in different classes then a virtual function is required.
         Class A
        {
           Public :
                      Virtual void display ( )
                      {
                        for ( int i = 1 ; i<=5 ;i++)
                      {
                       for ( int j= 1 ; j<=i ;j++)
                      {
                        cout <<"*";
                      }
                        cout <<"\n\n";
                      }
                }
       };
          Class B : Class A
          {
              Virtual void display ( )
                      {
                        for ( int i = 5 ; i>=1 ;i++)
                      {
                       for ( int j= 1 ; j<=i ;j++)
                      {
                        cout <<"*";
                      }
                        cout <<"\n\n";
                      }
                }
       };
          Void main ( )
          {
             A a1 ;
             B b1 ;
             A *p;
            clrscr ( );
            p =&a1 ;
            p->display ( );
           cout <<"===========\n\n";
           p = & b1 ;
           p -> display ( );
           getch ( );
          }
          output :
                           *
                           * *
                           * * *
                           * * * *
                           * * * * *
                         ===========
                            * * * * *
                            * * * * 
                            * * *  
                            * *
                            *
Polymorphism : It is one of the most important features of oop . It means the same function operator and objects behave differently in different places . There are three types of polymorphism :
  1. Virtual Function
  2. Function overloading
  3. Operator overloading
All these types of polymorphism are important to implement the concept  of polymorphism . In a c++ program , the overloaded function are invoked by matching arguments, type of argument and number of arguments . During compile time the compiler gets the information about these function so that the compiler can select the appropriate function for a call at compile time.This is called early binding or static binding
 Pure Virtual Function : It is a special type of function that is declared in base class with a keyword virtual . The function has no body or definition . It is initialized to zero in base class . And it is redeclare in the derived classes .Any base class cannot cannot use the pure virtual function . The base class cannot create any object to use pure virtual function . such type of class is called Abstracts base class . A pure virtual function can be declared as :
                                                  Virtual void function_name ( ) = 0;
              #include<iostream.h>
              #include<conio.h>
              Class Base 
             {
               virtual voidshow( ) = 0;
             };
             Class A :Public Base
             {
               Public :
                           Void show ( )
                           {
                             Char N[7] = "webtech";
                             for ( int i = 0; i<7 ; i++)
                            {
                               for ( int j =0 ;j<=i ;j+=)
                            {
                              cout <<N[j]<<"\t";
                            }
                              cout <<"\n\n";
                           }
                        }
              } a1 ;
                 Class B :Public Base
             {
               Public :
                           Void show ( )
                           {
                             Char N[7] = "webtech";
                             for ( int i = 0; i<7 ; i++)
                            {
                               for ( int j =0 ;j<=i ;j+=)
                            {
                              cout <<N[j]<<"\t";
                            }
                              cout <<"\n\n";
                           }
                        }
              } b1 ;
               Void main ( )                // www.programmingking.in
               {
                 clrscr ( );
                 base *p1,*p2 ;
                 p1 = &a1;
                 p2 = &b1 ;
                 cout <<"=======================\n\n";
                 p1 -> show( );
                 cout <<"=======================\n\n";
                 p2 ->show( );
                 getch ( );
                } 

0 comments:

Post a Comment

 

PROGRAMMINGqueen Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates

Blogger Widgets