Reference (C++) - Polymorphic Behaviour

Polymorphic Behaviour

Continuing the relationship between references and pointers (in C++ context), the former exhibit polymorphic capabilities, as one might expect:

#include using namespace std; class A { public: A {} virtual void print { cout << "This is class A\n"; } }; class B: public A { public: B {} virtual void print { cout << "This is class B\n"; } }; int main { A a; A& refToa = a; B b; A& refTob = b; refToa.print; refTob.print; return 0; }

The source above is valid C++ and generates the following output:
This is class A

This is class B

Read more about this topic:  Reference (C++)

Famous quotes containing the word behaviour:

    ... into the novel goes such taste as I have for rational behaviour and social portraiture. The short story, as I see it to be, allows for what is crazy about humanity: obstinacies, inordinate heroisms, “immortal longings.”
    Elizabeth Bowen (1899–1973)