Difference between virtual function and inline function in C++

Virtual function: Virtual function is a member function which is declared within a base class and is redefined by a derived class.

Inline function: Inline function is a normal function which is defined by the keyword inline, it is a short function which is expanded by the compiler and its arguments are evaluated only once.

The syntax of defining the function inline in C++ is:

inline return-type function-name(parameters)
{
    // function code
} 

Difference between virtual function and inline function are as follows:

Virtual function

Inline function

1. Virtual function must be declared in public section of class. 1. Inline function is a normal function which is defined by the keyword inline.
2. Virtual function cannot be static. 2. Inline function can also be non-static.
3. Virtual function is defined in base class. 3. Inline function are the short length functions that are automatically made the inline functions without using the inline keyword inside the class.
4. Virtual function are decrease the efficiency of code. 4. Inline function are used to increase the efficiency of code.
5. Virtual function is to run time polymorphism. 5. Inline function is to compile time polymorphism.
6. Virtual function may consists of virtual destructor but it cannot have a virtual constructor.  6. Inline function can also consist of inline constructor.
7. Virtual may use for dynamic linkage. 7. Inline function is used to reduce the function call overhead.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :