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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Difference between Virtual function and Pure virtual function in C++
- 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++
- How to call function within function in C or C++
- Virtual Destructor
- Can virtual functions be private in C++?
- Can virtual functions be inlined?
- Pure virtual destructor in C++
- Advanced C++ | Virtual Constructor
- Virtual base class in C++
- Virtual destruction using shared_ptr in C++
Improved By : xdsarkar