Explain virtual function in c++ with program

Virtual Function in C++

Virtual functions, one of advanced features of OOP is one that does not really exist but it appears real in some parts of a program. This section deals with the polymorphic features which are incorporated using the virtual functions.

The general syntax of the virtual function declaration is:

class use_detined_name
{
private:
public:
virtual return_type function_name1 (arguments); 
virtual return_type function_name2(arguments); 
virtual return_type function_name3( arguments);
};

To make a member function virtual, the keyword virtual is used in the methods while it is declared in the class definition but not in the member function definition. The keyword virtual precedes the return type of the function name. The compiler gets information from the keyword virtual that it is a virtual function and not a conventional function declaration.

For. example, the following declararion of the virtual function is valid.

class point 
{
intx; 
inty; 
public:
virtual int length ( ); 
virtual void display ( );
};

Remember that the keyword virtual should not be repeated in the definition if the definition occurs outside the class declaration. The use of a function specifier virtual in the function definition is invalid.

For example

class point 

intx ;
inty ; 
public:
virtual void display ( );
};
virtual void point: : display ( ) //error
{
Function Body
}

Polymorphism refers to the property by which objects belonging to different classes are able to respond to the same message, but different forms. An essential requirement of polymorphism is therefore the ability to refer to objects without any regard to their classes.

When we use the same function name in both the base and derived classes, the function in the bas class is declared as virtual using the keyword virtual preceding its normal declaration.

When a function is made virtual, C++ determines which function to use at runtime based on the type of object pointed to by the base pointer, rather than the type of the pointer. Thus, by making the base pointer to point to different objects, we can execute different versions of the virtual function.

Program:

#include<iostream.h> 
#include<conio.h>
class Base
{
public:
void display()
{
cout<<”Display Base”;
}
virtual void show()
{
cout<<”Show Base”;
}
};
class Derived : public Base
{
public:
void display()
{
cout<<”Display Derived”;
}
void show()
{
cout<<”show derived”;
}
};
void main()
{
Base b;
Derived d;
Base *ptr;
cout<<”ptr points to Base”; ptr=&b;
ptr->display(); //calls Base ptr->show(); //calls Base
cout<<”ptr points to derived”; ptr=&d;
ptr->display(); //calls Base ptr->show(); //class Derived
}
Output:
ptr points to Base 
Display Base Show Base
ptr points to Derived Display Base
Show Derived


When ptr is made to point to the object d, the statement ptr->display(); calls only the function associated with the Base i.e.. Base::display()

where as the statement ptr->show();

calls the derived version of show(). This is because the function display() has not been made virtual in the Base class.

Rules For Virtual Functions:

When virtual functions are created for implementing late binding, observe some basic rules that satisfy the compiler requirements.

1.The virtual functions must be members of some class.

2.They cannot be static members.

3.They are accessed by using object pointers.

4.A virtual function can be a friend of another class.

5.A virtual function in a base class must be defined, even though it may not be used.

6.The prototypes of the base class version of a virtual function and all the derived class versions must be identical. C++ considers them as overloaded functions, and the virtual function mechanism is ignored.

7.We cannot have virtual constructors, but we can have virtual destructors.

8.While a base pointer points to any type of the derived object, the reverse is not true. i.e. we cannot use a pointer to a derived class to access an object of the base class type.

9.When a base pointer points to a derived class, incrementing or decrementing it will not make it to point to the next object of the derived class. It is incremented or decremented only relative to its base type. Therefore we should not use this method to move the pointer to the next object.

10.If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. In such cases, calls will invoke the base function.



Md Ashraf

'KNOWLEDGE WITH ASHRAF' is the platform where you find all the type of knowledge especially on programming based. Our goal is to give you a deeper grasp of technology in specifics that will help you increase your knowledge.

Previous Post Next Post

Contact Form