< Prev
Next >

Polymorphism in C++

Polymorphism means having many forms. In C++, polymorphism allows us to access an object in multiple ways, such as - An object accessed in such multiple ways is said to be accessed polymorphically or polymorphic in nature.

Accessing an object polymorphically

  1. A pointer variable can refer to any object of its own class and also any object of its subclass(in inheritance).

  2. //Base class
    class A
    //Derived class class B is subclass of class A
    class B extends A	
    int main()
    A ob1;
    B ob2;
    //Base class pointer
    A *aptr;
    //Base class pointer pointing to the base class object.
    aptr = &ob1;
    //Base class pointer pointing to the derived class object.
    aptr = &ob2;

    Program Analysis

Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next