Virtual Classes

Sometimes we may have a derived class with two base classes, which is derived from two other base classes. In this situation, a problem may arise of ambiguous. Using virtual class we can removes the ambiguous data and avoids redundancy.

Virtual Functions

When we use the same function name in both the base and derived classes than virtual function is use, it is declared in base class. When a function is made as virtual CPP determines which function to use at run time based on the type of object pointed by the base pointer.

Rules for Virtual Functions:

  1. They are accessed by using object pointers.
  2. We cannot have virtual constructors
  3. Virtual function is defined in base class
  4. Access using object of base class
  5. If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class.

Pure Virtual Function:

Virtual Functions are not used for any purpose other than providing declaration. A do nothing functions is a pure virtual function.

    virtual void display( )=0;

C Language Feedback, Questions, Suggestions, Discussion.