0% found this document useful (0 votes)
129 views

Virtual Functions in C++

Virtual functions allow functions to be overridden in derived classes. They ensure the correct version of the function is called based on the object's actual type, not the reference type. Virtual functions are declared with the virtual keyword in the base class and can be overridden in derived classes. Pure virtual functions are declared with = 0 and make a class abstract, requiring derived classes to implement the pure virtual functions.

Uploaded by

Siva Kishore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

Virtual Functions in C++

Virtual functions allow functions to be overridden in derived classes. They ensure the correct version of the function is called based on the object's actual type, not the reference type. Virtual functions are declared with the virtual keyword in the base class and can be overridden in derived classes. Pure virtual functions are declared with = 0 and make a class abstract, requiring derived classes to implement the pure virtual functions.

Uploaded by

Siva Kishore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Virtual Functions in C++

Dr. Kuppusamy .P
Associate Professor / SCOPE
Virtual function
• A virtual function is a member function which 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 function.
• Virtual functions ensure that the correct function is called for an object, regardless
of the type of reference (or pointer) used for function call.
• They are mainly used to achieve Runtime polymorphism
• Functions are declared with a virtual keyword in base class.
• The resolving of function call is done at Run-time.
Virtual function
Rules for Virtual Functions
• Virtual functions cannot be static.
• A virtual function can be a friend function of another class.
• Virtual functions should be accessed using pointer or reference of base class type to achieve
run time polymorphism.
• The prototype of virtual functions should be the same in the base as well as derived class.
• 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 function
class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}

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

class derived : public base {


public:
void print()
{
cout << "print derived class" << endl;
}
Virtual function
void show()
{
cout << "show derived class" << endl;
}
};

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

// virtual function, binded at runtime


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();
} // print derived class
//show base class
Virtual function
• Runtime polymorphism is achieved only through a pointer (or reference) of base class type.
• Also, a base class pointer can point to the objects of base class as well as to the objects of
derived class.
• In above code, base class pointer ‘bptr’ contains the address of object ‘d’ of derived class.
• Late binding(Runtime) is done in accordance with the content of pointer (i.e. location
pointed to by pointer).
• Early binding(Compile time) is done according to the type of pointer, since print() function
is declared with virtual keyword so it will be bound at run-time (output is print derived
class as pointer is pointing to object of derived class ) and show() is non-virtual so it will be
bound during compile time(output is show base class as pointer is of base type ).
Pure Virtual function
• A pure virtual function is a virtual function in C++ for which we need not to write any
function definition and only we have to declare it. It is declared by assigning 0 in the
declaration.
• An abstract class is a class in C++ which have at least one pure virtual function.
• Abstract class can have normal functions and variables along with a pure virtual function.
• Abstract class cannot be instantiated, but pointers and references of Abstract class type can
be created.
• Abstract classes are mainly used for Upcasting, so that its derived classes can use its
interface.
• If an Abstract Class has derived class, they must implement all pure virtual functions, or
else they will become Abstract too.
Pure Virtual function
class B { //abstract class
public:
virtual void s() = 0; // Pure Virtual Function
};

class D:public B {
public:
void s() {
cout << "Virtual Function in Derived class\n";
}
};
int main() {
B *b;
D dobj;
b = &dobj;
b->s();
}
// output:Virtual Function in Derived class

You might also like