Runtime polymorphism - Defiition - Polymorphism and Abstract Classes

Q.  Runtime polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.
- Published on 17 Jul 15

a. True
b. False

ANSWER: True
 

    Discussion

  • Nihal   -Posted on 12 Oct 15
    Runtime polymorphism is achieved only when a virtual function is accessed through a pointer to the base class. You can call derived class function through base class pointer type. It is also called as late binding. The keyword virtual on a member function in base class indicates to the compiler that binding will be done at run time.
    Example:
    class BaseClass
    {
    public:
    virtual void show()
    {
    cout<<" virtual function in base class \n";
    }
    };

    class DerivedClass: public BaseClass
    {
    public:
    void show()
    {
    cout<<"Function overriding in Derived \n";
    }
    };

    int main(void)
    {
    BaseClass *bp = new DerivedClass;
    bp->show(); // RUN-TIME POLYMORPHISM
    return 0;
    }

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)