Call Us : +91-8376986802 (Viber, WhatsApp)

Describe abstract base class. Illustrate an example to explain it.



A pure virtual function is a function which does not have definition of its own. The classes which derive from this class need to provide definition for such function. A class containing at least one such pure virtual function is called as abstract base class. We can not create objects of abstract class because it contains functions which have no definition. We can create pointers and references to an abstract class.

Consider example of base class Shape and derived classes Circle, Rectangle, triangle etc.
The function Draw() is made pure virtual in the base class Shape. It is overridden by the derived classes. So the class Shape becomes abstract base class.

class Shape
{
int x, y;
public:
virtual void draw() = 0;
};

class Circle: public Shape
{
public:
draw()
{
//Code for drawing a circle
}
};

class Rectangle: public Shape
{
Public:
void draw()
{
//Code for drawing a rectangle
}
};

class Triangle: public Shape
{
Public:
void draw()
{
//Code for drawing a triangle
}
};

Leave a Reply