CPP Program to Implement the Concept of Virtual Function

Aim:

To write a C++ program to implement the concept of Virtual functions

Virtual Function

  • Giving new implementation of base class method into derived class and the calling of this new implemented function with derived class’s object is called function overriding.
  • Giving new implementation of derived class method into base class and the calling of this new implemented function with base class’s object is done by making base class function as virtual function.
  • Virtual function is used in situation, when we need to invoke derived class function using base class pointer.
  • We must declare base class function as virtual using virtual keyword preceding its normal declaration.
  • The base class object must be of pointer type so that we can dynamically replace the address of base class function with derived class function.
  • This is how we can achieve “Runtime Polymorphism”.
  • If we doesn’t use virtual keyword in base class, base class pointer will always execute function defined in base class.
  • A virtual function is a function in a base class that is declared using the keyword virtual.
  • Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don’t want static linkage for this function.
  • What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called.
  • This sort of operation is referred to as dynamic linkage, or late binding.

Algorithm:

  1. Start the program.
  2. Define a base class called the base and define a function called display as virtual in it.
  3. Derive a new class called derv1,derv2 using a base class called the base and define a           function called to display in the respective classes.
  4. Declare a base class pointer in the main function.
  5. Declare objects for derv1 and derv2 classes respectively.
  6. Assign a derv1 and derv2 obj to the base pointer
  7. Now, the display function shows the derv1 and derv2 class’ function respectively.

Program:

#include<iostream>
using namespace std;
class shape
{
protected:
double h,b;
public:
virtual void getdata()=0;	
virtual void disparea()=0;
};
class rectangle :public shape
{
public:
void disparea();
void getdata();	
};
void rectangle::disparea()
{
cout<<"AREA OF RECTANGLE:"<<h*b<<endl;
}
void rectangle::getdata()
{
double a , ba;
cout<<"ENTER LENGTH OF RECTANGLE:";
cin>>a;cout<<a;
cout<<"\nENTER BREADTH OF RECTANGLE:";
cin>>ba;
cout<<ba<<endl;
h=a;
b=ba;
}
class triangle :public shape
{
public:
void disparea();	
void getdata();
};
void triangle::disparea()
{
cout<<"AREA OF TRIANGLE :"<<0.5*h*b<<endl;
}
void triangle::getdata()
{
double a , ba;
cout<<"ENTER HEIGHT OF TRIANGLE:";
cin>>a;
cout<<a<<"\nENTER BASE OF TRIANGLE:";
cin>>ba;
cout<<ba<<endl;
h=a;
b=ba;
}
int main()
{
rectangle a;
shape *aptr;
aptr=&a;
aptr->getdata();
aptr->disparea();
triangle b;
shape *bptr;
bptr=&b;
bptr->getdata();
bptr->disparea();
return 0;
}

Execution:

Input :
5 8 3 8

Output :
ENTER LENGTH OF RECTANGLE:5
ENTER BREADTH OF RECTANGLE:8
AREA OF RECTANGLE:40
ENTER HEIGHT OF TRIANGLE:3
ENTER BASE OF TRIANGLE:8
AREA OF TRIANGLE:12

Result:

Thus, the Implement the concept of Virtual functions was executed successfully.

Leave a Comment