+1 vote
in C Plus Plus by

What are virtual functions – Write an example? 

1 Answer

0 votes
by

Virtual functions are used with inheritance, they are called according to the type of object pointed or referred, not according to the type of pointer or reference. In other words, virtual functions are resolved late, at runtime. Virtual keyword is used to make a function virtual.

Following things are necessary to write a C++ program with runtime polymorphism (use of virtual functions)
1) A base class and a derived class.
2) A function with same name in base class and derived class.
3) A pointer or reference of base class type pointing or referring to an object of derived class.

For example, in the following program bp is a pointer of type Base, but a call to bp->show() calls show() function of Derived class, because bp points to an object of Derived class.

Related questions

+2 votes
asked Jun 19, 2019 in C Plus Plus by anonymous
+1 vote
+1 vote
asked Jan 20, 2021 in C Plus Plus by SakshiSharma
+2 votes
asked Jun 19, 2019 in C Plus Plus by anonymous
+2 votes
asked Jun 19, 2019 in C Plus Plus by anonymous
0 votes
asked Jun 9, 2020 in C Plus Plus by Robindeniel
...