Describe abstract base class - C++

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
       }
};
What is matrix class? Describe it with example and uses - C++
What is matrix class? Describe it with example and uses - A matrix is simply a 2-D array, which is widely used in scientific programming to perform calculations.....
What are the characteristics of friend functions? - C++
What are the characteristics of friend functions? - A friend function is not in the scope of the class n which it has been declared as friend....
What is a friend function? - C++
What is a friend function? - Private data members cannot be accessed from outside the class. ...
Post your comment