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.
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.
- 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
- Virtual destruction using shared_ptr in C++
- Virtual base class in C++
- Advanced C++ | Virtual Constructor
- Can static functions be virtual in C++?
- Can virtual functions be inlined?
- Pure virtual destructor in C++
- Can virtual functions be private in C++?
- Advanced C++ | Virtual Copy Constructor
- Virtual functions in derived classes
- Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)
- Pure Virtual Functions and Abstract Classes in C++
Improved By : xdsarkar