c++

C++ Virtual Function

In this article, we are going to learn about virtual functions with the help of some examples.

What are Virtual functions?

Virtual functions are those member functions of base class which you want to redefine or redesign in the derived class.

General Syntax of Virtual Functions :

virtual <fuction_returntype> <function_name>() { 
    // function body; 
}

What is the need of virtual function?

Let us understand this ques with an example :

#include <iostream>

using namespace std;

class Base { 
    public: 
    void disp(){ 
        cout << "\n base class function disp"; 
    } 
}; 

class Derived : public Base { 
    public: 
    void disp() { 
        cout << "\n derived class function disp"; 
    } 
}; 

int main() { 
    Base* p; 
    Base objb; 
    Derived objd;

    p = &objb; 
    p->disp(); 
    p = &objd; 
    p->disp(); 
    
    return 0; 
}

Output :

base class function disp 
base class function disp

As we can see in the above program it is not possible to access derived class function disp rather in both the cases only base class function disp is being called. To overcome this this problem the concept of virtual functions was introduced.

Now let us redesign the above code with the help of virtual functions :

#include <iostream>

using namespace std; 

class Base { 
    public: 
    virtual void disp(){ 
        cout << "\n base class function disp"; 
    } 
}; 

class Derived : public Base { 
    public: 
    void disp() { 
        cout << "\n derived class function disp"; 
    } 
}; 

int main() { 
    Base* p; 
    Base objb;
    Derived objd; 
    
    p = &objb; 
    p->disp(); 
    p = &objd; 
    p->disp(); 
    
    return 0; 
}

Output :

base class function disp 
derived class function disp

As we can see now we can access both the function i.e. of base class as well as that of derived class.


C++ Abstract class and Pure virtual Function

What is pure virtual function?

Pure virtual functions are those functions of C++ language for which we don’t need to write any body or we can say that we don’t need to define it we only need to declare it. It is declare by assigning 0 in the declaration.

For example:

virtual void p() = 0; // It is a pure virtual function.

What are abstract class?

An abstract class is a class which at least one pure virtual function in it.

Some salient features of abstract classes are:

  1. Abstract class can also contain normal functions as well as normal variables.
  2. Abstract class cannot be instantiated i.e. we can’t create objects of this class but pointers or reference of this class can be created.
  3. If an abstract class have a derived class then the derived class must implement all the virtual functions otherwise the derived class will also become an abstract class.
  4. We can’t create object of abstract class as we reserve a slot for a pure virtual function in Vtable, but we don’t put any address, so Vtable will remain incomplete.
  5. Abstract class is mainly used for upcastin so that its derived class can use its features.

Let us take an example of implementation of abstract and pure virtual function :

#include <iostream>

using namespace std; 

// Abstract class 
class Shape { 
    protected: 
        float l; 
    public: 
        void getData() { 
            cin >> l; 
        } 
        
        // virtual Function 
        virtual float calculateArea() = 0; 
}; 

class Square : public Shape { 
    public: 
        float calculateArea() { 
            return l*l; 
        } 
}; 

class Circle : public Shape { 
    public: 
        float calculateArea() { 
            return 3.14*l*l; 
        } 
}; 

int main() { 
    Square s; 
    Circle c; 
    
    cout << "Enter length to calculate the area of a square: "; 
    
    s.getData();

    cout<<"Area of square: " << s.calculateArea(); 
    cout<<"\nEnter radius to calculate the area of a circle: "; 
    
    c.getData(); 
    
    cout << "Area of circle: " << c.calculateArea(); 
    
    return 0; 
}

Output :

Enter length to calculate the area of a square: 4 
Area of square: 16 
Enter radius to calculate the area of a circle: 5 
Area of circle: 78.5