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

Virtual Function and Pure Virtual Function

The document discusses virtual functions and pure virtual functions in C++. Virtual functions allow dynamic binding at runtime based on the actual object type. Pure virtual functions must be defined in derived classes and make a class abstract without object creation.

Uploaded by

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

Virtual Function and Pure Virtual Function

The document discusses virtual functions and pure virtual functions in C++. Virtual functions allow dynamic binding at runtime based on the actual object type. Pure virtual functions must be defined in derived classes and make a class abstract without object creation.

Uploaded by

Amit Kapoor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Virtual Function (Run time polymorphism): - When we use the same function name in both

base and derived classes, the function in base class is declared as virtual using the keyword virtual
preceding its normal declaration. When a function is made virtual, C++ determines which function to
use at run time based on the type of object pointed to by the base pointer, rather than the type of
the pointer. Thus, by making the base pointer to point to different objects, we can execute different
versions of the virtual function.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
//virtual function
class abc
{
int a,b;
public:

virtual void get(void)


{
cout<<"enter a,b";
cin>>a>>b;
}
virtual void show(void)
{
cout<<"a="<<a<<"\t"<<"b="<<b<<endl;
}
};
class xyz:public abc
{
int x,y;
public:
void get(void)
{
cout<<"enter x, y";
cin>>x>>y;
}
void show(void)
{
cout<<"x="<<x<<"\t"<<"y="<<y<<endl;
}
};
int main()
{
clrscr();
abc *p; //pointer of base class abc

abc ob1; //object of base class abc


p=&ob1; //pointer holds address of object of base class abc
p->get(); //call get() of abc
p->show(); // call show() of abc
xyz ob2; //object of derived class xyz
p=&ob2; //pointer holds address of object of derived class xyz
p->get(); //call get() of xyz
p->show(); // call show() of xyz
getch();
return 0; }

Output: -
enter a,b 45 67
a=45 b=67
enter x, y 18 62

Rules for Virtual Functions: - When virtual functions are created for implementing late binding,
we should observe some basic rules that satisfy the compiler requirements: -
1. The virtual functions must be member of some class.
2. They cannot be static member.
3. They are accessed by using object pointers.
4. A virtual function can be a friend function of another class.
5. A virtual function in a base class must be defined, even though it may not be used.
6. The prototype of the base class version of a virtual function and all other derived class versions
must be identical. If two functions with same name have different prototypes, C++ considers them as
overloaded functions, and virtual function mechanism is ignored.
7. We cannot have virtual constructors but we can have virtual destructors.
8. While a base pointer can point to any type of the derived object, the reverse is not true. That is to
say, we cannot use a pointer to a derived class to access an object of the base type.
9. When a base pointer points to a derived class, incrementing or decrementing it will not make it
point to the next object of the derived class. It is incremented or decremented only relative to its
base type. Therefore, we should not use this method to move the pointer to the next object.
10. If a virtual function is defined in the base class, it needs not to be necessarily redefined in the
derived class. In such cases, calls will invoke the base function.

Pure Virtual Function: - A pure virtual function is a function declared in a base class that has no
definition relative to the base class. In such cases, the compiler requires each derived class to either
define the function or re-declare it as a pure virtual function. A class containing pure virtual function
cannot be used to declare any objects of its own. Such classes are called abstract classes. The main
objective of an abstract class is to provide some traits to the derived classes and to create a base
pointer required for achieving run time polymorphism.

Program: - Program to illustrate the concept of pure virtual function.


#include<iostream.h>
#include<conio.h>
class geometry
{
public:
virtual float area()=0; //pure virtual function
};
class traingle:public geometry
{
float b,h,ar;
public:
void get(void)
{
cout<<"enter base and height of traingle";
cin>>b>>h;
}
float area(void)
{
ar=(b*h)/2;
return ar;
}
};
class rectangle:public geometry
{
float l,b,ar;
public:
void get(void)
{
cout<<"enter length and breadth of rectangle";
cin>>l>>b;
}
float area(void)
{
ar=l*b;
return ar;
}
};
int main()
{ clrscr();
geometry *g;
float ar1,ar2;
traingle t;
t.get();
g=&t;
ar1=g->area();
rectangle r;
r.get();
g=&r;
ar2=g->area();
cout<<"Area of traingle="<<ar1<<endl;
cout<<"Area of rectangle="<<ar2<<endl;
getch(); return 0;
}
Explanation: - In the program, class geometry contains a pure virtual function therefore its object
cannot be created. Now to use base class functionality we define derived class.

You might also like