Virtual Function and Polymorphism
- Virtual functions are used in C++ to support polymorphic behavior.
- We are modifying the above program and will introduce you the concept of virtual function by following example:
#include <iostream.h>
#include <conio.h>
class Shape
{
protected:
double width, height;
public:
void set_data (double a, double b)
{
width = a;
height = b;
}
virtual double area()
{return 0;}
};
class Rectangle: public Shape
{
public:
double area ()
{
return (width * height);
}
};
int main ()
{
Shape *sPtr;
Rectangle Rect;
sPtr = &Rect;
sPtr -> set_data (5,3);
cout << sPtr -> area() << endl;
return 0;
}
Expected Output :-
15
- A member of a class that can be redefined in its derived classes is known as a virtual member.
- In order to declare a member of a class as virtual, we must precede its declaration with the keyword virtual.
- The member function area() has been declared as virtual in the base class because it is later redefined in each derived class.
- The advantage of having virtual function is that we are able to access area function of derived class by pointer variable of base class.
Pure Virtual Function and Abstract Class
- In above example, base class Shape member function area do not need any implementation because it is overriding in derived class.
- If this is the case, the C++ language permits the programmer to declare the function a pure virtual function.
- The C++ way of declaring a pure virtual function is to put the expression = 0 in the class declaration.
- For example, if a member function double area() is being declared pure virtual, then its declaration in its class looks like
virtual double area() = 0;
- A pure virtual function is sometimes called an abstract function, and a class with at least one pure virtual function is called an abstract class.
- The C++ compiler will not allow you to instantiate an abstract class.
- Abstract classes can only be sub-classed: that is, you can only use them as base classes from which to derive other classes.
- A class derived from an abstract class inherits all functions in the base class, and will itself be an abstract class unless it overrides all the abstract functions it inherits.
- The usefulness of abstract classes lies in the fact that they define an interface that will then have to be supported by objects of all classes derived from it.
#include <iostream.h>
#include <conio.h>
class Shape
{
protected:
double width, height;
public:
void set_data (double a, double b)
{
width = a;
height = b;
}
virtual double area() = 0;
};
class Rectangle: public Shape
{
public:
double area ()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
double area ()
{
return (width * height)/2;
}
};
int main ()
{
Shape *sPtr;
Rectangle Rect;
sPtr = &Rect;
sPtr -> set_data (5,3);
cout << "Area of Rectangle is " << sPtr -> area() << endl;
Triangle Tri;
sPtr = &Tri;
sPtr -> set_data (4,6);
cout << "Area of Triangle is " << sPtr -> area() << endl;
return 0;
}
Expected Output :-
Area of Rectangle is 15
Area of Triangle is 12
Tags:
mskuthar
Polymorphism
Virtual Function
Virtual Function and Polymorphism
Virtual Function and Polymorphism in C++.
www.mskuthar.blogspot.com