Pure Virtual Function

download Pure Virtual Function

of 2

Transcript of Pure Virtual Function

  • 8/14/2019 Pure Virtual Function

    1/2

    What is Pure virtual function? Explain it with an example.

    A pure virtual function is a function which has no definition in the base class. Its

    definition lies only in the derived class ie it is compulsory for the derived class to providedefinition 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 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

    }

    };

  • 8/14/2019 Pure Virtual Function

    2/2

    Thus, base class Shape has pure virtual function draw(); which is overridden by all the

    derived classes.

    What are pure virtual functions?

    AnswerPure 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;};

    Question - What is a virtual base class?

    Answer

    An ambiguity can arise when several paths exist to a class from the same base class. This

    means that a child class could have duplicate sets of members inherited from a singlebase class.

    C++ solves this issue by introducing a virtual base class. When a class is made virtual,necessary care is taken so that the duplication is avoided regardless of the number of

    paths that exist to the child class.

    Question - What are virtual functions?

    Answer

    Polymorphism is also achieved in C++ using virtual functions. If a function with samename exists in base as well as parent class, then the pointer to the base class would call

    the functions associated only with the base class. However, if the function is made virtual

    and the base pointer is initialized with the address of the derived class, then the function

    in the child class would be called.

    Question - What are pure virtual functions?

    AnswerPure virtual functions are also called do nothing functions.

    e.g. virtual void abc() = 0;

    When a pure virtual fnction is declared in the base class, the compiler necessitates thederived 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.