Works & Examples of Virtual Functions in C++

Works & Examples of Virtual Functions in C++
Techiio-author
Written by Sagar RabidasFebruary 11, 2022
8 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Let's start the discussion with Virtual Functions in C++

Virtual Functions in C++

Virtual characteristic in c++ is a member feature described inside the base magnificence which you may redefine inside the derived class. So if in the programming you want to consult an item of the derived class using the reference of the bottom magnificence or pointer you can use the virtual characteristic for calling that item and then you could execute the characteristic of the derived class. A digital keyword is used to outline a virtual characteristic inside the programming. To reap runtime polymorphism within the software programs digital features are used. The characteristic name could be resolved at runtime simplest.

Here is the syntax for defining a virtual function in C + + programming language:

Syntax:

virtual void function_name()
  • Virtual is the mandatory keyword to be used before function name then function name that can be anything of user choice.

Some important rules to keep in mind while using and working with virtual functions in the C + + programming language:

  • A class cannot have a virtual constructor in the code but the same class can have a virtual destructor in the code.
  • Data type or we call it the prototype of the virtual function defined in the program must be the same in both the base class and derived class.
  • Virtual functions cannot be static.
  • The most important rule about virtual functions is that they must be defined in the base class only and must be overridden through the derived class.
  • To achieve run time polymorphism in your code you must access virtual function through either pointer or the base class reference.

How does Virtual Function Work?

The compiler does two things whenever there is a virtual function mentioned in the class of any given program:

If a class contains a virtual function then the compiler itself does two things:

  • The compiler will insert a virtual pointer (VPTR) as a data member of the class if an object of that class is created to point at the VTABLE of that particular class. Therefore, the compiler will do this every time a new object is created so that the new virtual pointer will be inserted as a data member of the class.
  • Another thing compiler will do is that even if the object is not created a static array of the function pointer will call VTABLE in which each table cell will contain the address of every virtual function defined in that class.

Examples of Virtual Functions in C++

As we have seen the syntax and working of virtual function now we will see how it works when we implement it in the code:

Example #1

Here is the C + + code to demonstrate the working example of the virtual function in the C + + programming language.

Code:

#include <iostream>
using namespace std;
class Automobile
{
public :
virtual void functionalities()
{ cout << " Loading Automobile functionalities. \n " ; }
} ;
class wheel : public Automobile
{
public:
void functionalities()
{ this -> Automobile :: functionalities();
cout << " Loading wheel functionalities. \n" ;
}
} ;
class glass : public Automobile
{
public:
void functionalities ()
{
this -> Automobile :: functionalities () ;
cout << " Loading glass functionalities. \n" ;
}
};
class RunAutomobile
{
public:
void loadfunctionalities ( Automobile *Automobile )
{
Automobile -> functionalities () ;
}
};
int main()
{
RunAutomobile *r = new RunAutomobile;
Automobile *w;
wheel b ;
glass g ;
w = &b ;
r -> loadfunctionalities ( w );
w = &g;
r -> loadfunctionalities ( w );
return 0;
}

Example #2

Here is the C ++ code to demonstrate the working example of the virtual function in the C++ programming language.

Code:

#include <iostream>
using namespace std;
// Declaring an Abstract class
class Pattern
{
protected:
float l;
public:
void getInput ()
{
cin  >> l ;
}
// virtual Function
virtual float totalArea () = 0;
};
class Squared : public Pattern
{
public:
float totalArea ()
{   return l * l ;  }
};
class Circle : public Pattern
{
public:
float totalArea ()
{ return 3.14 * l * l ; }
};
int main()
{
Squared s ;
Circle c;
cout << " Please enter the length for calculating the area of a square : " ;
s.getInput () ;
cout << " Hence the Area of square : " << s.totalArea () ;
cout << " \n Please enter radius for calculating the area of a circle : " ;
c.getInput () ;
cout << " Hence the Area of circle: " << c.totalArea () ;
return 0;
}

C
C++
Virtual Functions
VirtualFunctions C++
C++ Functions
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance