0% found this document useful (0 votes)
5 views2 pages

4 Virtual-Functions12

Virtual functions are member functions in a base class that can be overridden in derived classes, allowing for runtime polymorphism. They are declared with the 'virtual' keyword and must be accessed through a base class pointer or reference to ensure the correct function is called. Key rules include that virtual functions cannot be static, must have the same prototype in both classes, and are not mandatory to override in derived classes.

Uploaded by

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

4 Virtual-Functions12

Virtual functions are member functions in a base class that can be overridden in derived classes, allowing for runtime polymorphism. They are declared with the 'virtual' keyword and must be accessed through a base class pointer or reference to ensure the correct function is called. Key rules include that virtual functions cannot be static, must have the same prototype in both classes, and are not mandatory to override in derived classes.

Uploaded by

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

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.
 The resolving of a function call is done at runtime.

Rules for Virtual Functions


The rules for the virtual functions in C++ are as follows:
1. Virtual functions cannot be static.
2. A virtual function can be a friend function of another class.
3. Virtual functions should be accessed using a pointer or reference of base class type to
achieve runtime polymorphism.
4. The prototype of virtual functions should be the same in the base as well as the
derived class.
5. They are always defined in the base class and overridden in a derived class. It is not
mandatory for the derived class to override (or re-define the virtual function), in that
case, the base class version of the function is used.

Virtual Functions

#include <iostream>
using namespace std;

class base {
public: virtual void print() { cout << "print base class\n";
}

void show()
{ cout << "show base class\n";
}
};

class derived : public base {


public: void print() { cout << "print derived class\n"; }

void show()
{ cout << "show derived class\n"; }
};

int main()
{
base*s;
derived d;
s = &d;

// Virtual function, binded at runtime


s->print();
return 0;
}

You might also like