by Dinesh Thakur Category: Classes in C++

A virtual function is a function that is declared as virtual in a base class. A virtual function is always preceded by the keyword virtual. Virtual functions employ late binding by allocating memory space during execution time and not during compilation time. In this case when a pointer of the base class is defined in a main() function and derived class object's address is passed on to the base class pointer, then calling the overridden function will invoke the derived class member function and not the base class member function as mentioned earlier. Following is a program to illustrate the use of virtual functions.

Example

 

In this example also there is a base class consumer and a derived class transaction. Both have overridden member functions getdata() and display(). Member data are the same as discussed in the earlier example. The functions in the base class have been declared virtual.

#include <iostream.h>

#include <conio.h>
const int size=10;
#include <string.h>
class consumer
{
       protected :
                         char name[size];
                         int code;
                         char tel[size];
       public:
                         consumer()
                    {
                         strcpy(name, " ");
                         code=0;
                         strcpy(tel, " ");
                    }
                         virtual void getdata()
                    {
                         cout<<"Enter Name : ";
                         cin>>name;
                         cout<<"Enter Code : ";
                         cin>>code;
                         cout<<"Enter Telephone Number ";
                         cin>>tel;
                     }
                          virtual void display()
                       {
                           cout<<"\n ";
                           cout<<"Name : "<<name<<endl;
                           cout<<"Code : "<<code<<endl;
                           cout<<"Telephone : "<<tel<<endl;
                        }
};
             class transaction: public consumer
       {
             protected:
                                  int qty;
                                  float price;
                                  float total;
             public:
                                  transaction()
                         {
                                  qty=0;
                                  price=0;
                                  total=0;
                         }
                                  void getdata()
                        {
                                  consumer::getdata();
                                  cout<<"Enter Quantity : ";cin>>qty;
                                  cout<<"Enter Price : ";cin>>price;
                                  cout<<endl;
                          }
                                  void display()
                          {
                                   consumer::display();
                                   cout<<"Quantity : "<<qty<<endl;
                                   cout<<"Price : "<<price<<endl;
                                   cout<<"\n ";
                                   cout<<"Total Price : "<<qty*price<<endl;
                          }
            };
                        void main()
               {
                        clrscr();
                        consumer* o1;
                        transaction o2;
                        cout<<"\n" ;
                        o1=&o2;
                        o1->getdata();
                        o1->display();
                        getch();
               }

Virtual Function in C++ Programming

It is observed that when the address of the derived class object o2 is passed on to the base class pointer o1 and the base class pointer o1 is used to invoke the overridden member functions getdata() and display(), the derived class member functions are invoked.

 



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles