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.
- Virtual Function in C++
- Default arguments and virtual function
- C++ interview questions on virtual function and abstract class
- When are Constructors Called?
- When is copy constructor called?
- Function Overloading vs Function Overriding in C++
- Virtual Destructor
- Virtual destruction using shared_ptr in C++
- Advanced C++ | Virtual Constructor
- Virtual base class in C++
- Pure virtual destructor in C++
- Can virtual functions be private in C++?
- Can static functions be virtual in C++?
- Can virtual functions be inlined?
- exp() function C++
Improved By : xdsarkar