Home
Refer
Jobs
Alumni
Resume
Notifications

What is the difference between a virtual function and a pure virtual function in C++?

🚀 Best Answers Get Featured in our LinkedIn Community based on Your Consent, To Increase Your Chances of Getting Interviewed. 🚀


Answer:
In C++, a virtual function is a member function that can be redefined in a derived class, while a pure virtual function is a virtual function that must be overridden in a derived class to create instances of that class. Additionally, a class containing a pure virtual function is an abstract class, which means that it cannot be instantiated on its own, but only through a derived class implementation.Overriding a virtual function involves redefining the function in a derived class using the keyword "override." This allows the derived class implementation to be used instead of the base class implementation when the function is called through a pointer of the base class type.On the other hand, a pure virtual function is declared using the keyword "virtual" and "= 0" at the end of the function signature. This creates a virtual function that has no implementation and must be overridden by any deriving class before it can be instantiated.In summary, the main difference between a virtual function and a pure virtual function is that the former can be used to create objects of its class but can be overridden by derived classes, while the latter cannot be used to create objects on its own and must be overridden by any deriving class to create its objects.
References:

© 2024 Referral Solutions, Inc. Incorporated. All rights reserved.