0% found this document useful (0 votes)
11 views14 pages

Virtual Functions

Virtual functions are member functions in a base class that can be overridden in derived classes, allowing for runtime polymorphism. They ensure the correct function is called for an object, regardless of the reference type used. Virtual destructors in C++ help prevent memory leaks by properly freeing memory allocated by derived class objects when accessed through base class pointers.

Uploaded by

Marsha Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

Virtual Functions

Virtual functions are member functions in a base class that can be overridden in derived classes, allowing for runtime polymorphism. They ensure the correct function is called for an object, regardless of the reference type used. Virtual destructors in C++ help prevent memory leaks by properly freeing memory allocated by derived class objects when accessed through base class pointers.

Uploaded by

Marsha Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Virtual Functions

A virtual function (also known as virtual methods) is a member function that is declared within a
base class and is re-defined (overridden) by a derived class. When you refer to a derived class object
using a pointer or a reference to the base class, you can call a virtual function for that object and
execute the derived class’s version of the method.

 Virtual functions ensure that the correct function is called for an object, regardless of the
type of reference (or pointer) used for the function call.

 They are mainly used to achieve Runtime polymorphism.

 Functions are declared with a virtual keyword in a base class.


Virtual destructors in c++

Virtual Destructor in C++ is a member function that frees up the memory allocated by the object of a
child class or derived class when it is removed from the memory using the parent class pointer
object. Virtual destructor in C++ is mainly responsible for resolving the memory leak problem.

You might also like