What are pure virtual functions?

What are pure virtual functions?

- Pure virtual functions are also called ‘do nothing functions’.
- Example :
virtual void abc() = 0;
- When a pure virtual function is declared in the base class, the compiler necessitates the derived classes to define those functions or redeclare them are pure virtual functions. The classes containing pure virtual functions cannot be used to declare objects of their own. Such classes are called as abstract base classes.

What are pure virtual functions?

- A pure virtual function is a function which has no definition in the base class. Its definition lies only in the derived class i.e it is compulsory for the derived class to provide definition of a pure virtual function. Since there is no definition in the base class, these functions can be equated to zero.
- The general form of pure virtual function is :
virtual type func-name(parameter-list) = 0;
- Consider the following example of base class Shape and classes derived from it viz Circle, Rectangle, Triangle etc.
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
       }
};
- Thus, base class Shape has pure virtual function draw(); which is overridden by all the derived classes.

What are pure virtual functions?

- Pure virtual function is the function in the base class with no body. Since no body, you have to add the notation =0 for declaration of the pure virtual function in the base class.
- The base class with pure virtual function can't be instantiated since there is no definition of the function in the base class.
- It is necessary for the derived class to override pure virtual function.
- This type of class with one or more pure virtual function is called abstract class which can't be instantiated, it can only be inherited.
class shape
{
   public: virtual void draw() = 0;
};

What is pure virtual function?

A pure virtual function is one which is initialized with 0 at the time of declaration.
What is a namespace? What happens when two namespaces having the same name?
What is a namespace? - A conceptual space for grouping identifiers, classes etc. for the purpose of avoiding conflicts....
What are the guidelines of using namespaces?
What are the guidelines of using namespaces? - To name a namespace, utilize the company name followed by the department and optionally followed by the features and technology used....
What is name lookup? - C++
What is name lookup? - A name lookup is a name in the specified logically grouped names in the namespace...
Post your comment