Predict the output of the following simple C++ program without any virtual function.
Base class invoke function Base class print function
Since there is no virtual function, run time polymorphic behavior doesn’t not work in the above code. Now predict the output of the following C++ program.
Base class invoke function Derived class print function
So polymorphic behaviour works even when a virtual function is called inside a non-virtual function. The output can be guessed from the fact that the function to be called is decided at run-time using the vptr and vtable.
This article is contributed by Sumit Jaiswal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- When is copy constructor called?
- When are Constructors Called?
- Difference between Virtual function and Pure virtual function in C++
- What happens when more restrictive access is given to a derived class method in C++?
- What happens if loop till Maximum of Signed and Unsigned in C/C++?
- What happens if we mix new and free in C++?
- Difference between virtual function and inline function in C++
- How to create a dynamic 2D array inside a class in C++ ?
- Inline namespaces and usage of the "using" directive inside namespaces
- Where is an object stored if it is created inside a block in C++?
- Default arguments and virtual function
- Virtual Function in C++
- C++ interview questions on virtual function and abstract class
- Can static functions be virtual in C++?
- Virtual functions in derived classes
- Virtual Destructor
- Advanced C++ | Virtual Constructor
- Advanced C++ | Virtual Copy Constructor
- Can virtual functions be inlined?
- Pure virtual destructor in C++
Improved By : xdsarkar