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:

    The quality of moral behaviour varies in inverse ratio to the number of human beings involved.
    Aldous Huxley (1894–1963)