FACULTY OF ENGINEERING & TECHNOLOGY
Preeti Singh
Department of Computer Science & Engineering Rama University, Kanpur
Lecture-28
CSPS103: Object Oriented Programming
[email protected]
OBJECTIVES
In this lecture, you will learn to:
Virtual Function
Rules of Virtual Function
Virtual function Example
VIRTUAL FUNCTION
A virtual function is a member function that is declared within a base class and redefined by a derived class.
In order to make a function virtual, you have to add keyword virtual in front of a function definition.
When a class containing a virtual function is inherited, the derived class redefines the virtual function relative to the derived class.
The virtual function within the base class defines the form of the interface to that function.
Each redefinition of the virtual function by a derived class implements its operation as it relates specifically to the derived class.
That is, the redefinition creates a specific method.
When a virtual function is redefined by a derived class, the keyword virtual is not needed.
A virtual function can be called just like any member function.
However, what makes a virtual function interesting, and capable of supporting run-time polymorphism, is what happens when a virtual function is called through a pointer.
When a base pointer points to a derived object that contains a virtual function and that virtual function is called through that pointer, C++ determines which version of that function will be executed based upon the type of object being pointed to by the pointer. And this determination is made at run time.
Therefore, if two or more different classes are derived from a base class that contains a virtual function, then when different objects are pointed to by a base pointer, different versions of the virtual function are executed.
RULES OF VIRTUAL FUNCTION
Virtual functions must be members of some class.
Virtual functions cannot be static members.
They are accessed through object pointers.
They can be a friend of another class.
A virtual function must be defined in the base class, even though it is not used.
The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two
functions with the same name but different prototypes, C++ will consider them as the overloaded functions.
We cannot have a virtual constructor, but we can have a virtual destructor
Consider the situation when we don't use the virtual keyword.
VIRTUAL FUNCTION EXAMPLE
#include <iostream.h>
{
public:
virtual void display() {
cout << "Base class is invoked"<<endl;
} };
class B:public A {
public:
void display() {
cout << "Derived Class is invoked"<<endl;
} };
int main() {
A* a; //pointer of base class B b; //object of derived class a = &b;
a->display(); //Late Binding occurs }
REFERENCES
Kernighan, Brian W., and Dennis M. Richie. The C Programming Language. Vol. 2. Englewood Cliffs: Prentice- Hall, 1988.
King, Kim N., and Kim King. C programming: A Modern Approach. Norton, 1996.
Bjrane Stroustrup, “C++ Programming language”,3rd edition, Pearson education Asia(1997)
LaforeR.”Object oriented Programming in C++”,4th Ed. Techmedia,New Delhi(2002).
YashwantKenetkar,”Let us C++”,1stEd.,Oxford University Press(2006)
B.A. Forouzan and R.F. Gilberg,CompilerScience,”A structured approach using C++” Cengage Learning, New Delhi.
https://www.javatpoint.com/cpp-tutorial
https://www.tutorialspoint.com/cplusplus/index.htm
https://ambedkarcollegevasai.com/wp-content/uploads/2019/03/CPP.pdf
https://onlinecourses.nptel.ac.in/noc20_cs07/unit?unit=3&lesson=19
MULTIPLE CHOICE QUESTION
Multiple Choice Question:
Q1. Polymorphism means
a) Many forms b) Only one form c) Hiding data d) None of them
MULTIPLE CHOICE QUESTION
Multiple Choice Question:
Q2. How many types of polymorphism are there?
a) 1 b) 2 c) 3 d) 4
MULTIPLE CHOICE QUESTION
Multiple Choice Question:
Q3. What is the other name of compile-time polymorphism?
a) Static polymorphism b) Dynamic polymorphism c) Executing polymorphism d) Non-executing polymorphism
MULTIPLE CHOICE QUESTION
Multiple Choice Question:
Q4. What is the other name of run-time polymorphism?
a) Static polymorphism b) Dynamic polymorphism c) Executing polymorphism d) Non-executing polymorphism
MULTIPLE CHOICE QUESTION
Multiple Choice Question:
Q5. Which of the following is the scope resolution operator?
a) . b) * c) ::
d) ~
Summary
In this lecture, you learned that:
A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword.
It is used to tell the compiler to perform dynamic linkage or late binding on the function.