Last active
February 18, 2019 07:27
-
-
Save bravikov/1986169505696b766206e2b2089901b9 to your computer and use it in GitHub Desktop.
A reference to an object of a child class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A reference to an object of a child class | |
#include <iostream> | |
class A | |
{ | |
public: | |
virtual ~A() {} | |
virtual void print() { std::cout << "A" << std::endl; } | |
}; | |
class B: public A | |
{ | |
public: | |
void print() override { std::cout << "B" << std::endl; } | |
}; | |
int main() | |
{ | |
A a; | |
B b; | |
A& a2 = b; | |
A& a3 = a; | |
a.print(); | |
b.print(); | |
a2.print(); | |
a3.print(); | |
a3 = b; | |
a3.print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment