#include <iostream>
class Bar;
class Foo {
public:
Foo() : a_(0) {};
Foo(int a) : a_(a) {};
Foo(const Bar& bar_);
inline int getA() { return a_; };
virtual void print();
private:
int a_;
Foo* foo;
};
class Bar : public Foo {
public:
Bar() : Foo(), b_(0) {};
Bar(int a, int b) : Foo(a), b_(b) {};
inline int getB() { return b_; };
virtual void print() override { std::cout << "{a: " << getA() << ", b: " << getB() << "}\n"; };
private:
int b_;
};
Foo::Foo(const Bar& bar_) {
foo = new Bar(bar_);
}
void Foo::print() {
if (foo == 0)
std::cout << "{a: " << getA() << "}\n";
else
foo->print();
};
int main() {
Foo t1 = Bar(10,10);
Bar t2 = Bar(20,20);
Foo *t3 = new Bar(30,30);
t1.print();
t2.print();
t3->print();
delete t3;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKY2xhc3MgQmFyOwpjbGFzcyBGb28gewpwdWJsaWM6CiAgICBGb28oKSA6IGFfKDApIHt9OwogICAgRm9vKGludCBhKSA6IGFfKGEpIHt9OwogICAgRm9vKGNvbnN0IEJhciYgYmFyXyk7CgogICAgaW5saW5lIGludCBnZXRBKCkgeyByZXR1cm4gYV87IH07CiAgICB2aXJ0dWFsIHZvaWQgcHJpbnQoKTsKcHJpdmF0ZToKICAgIGludCBhXzsKICAgIEZvbyogZm9vOwp9OwoKY2xhc3MgQmFyIDogcHVibGljIEZvbyB7CnB1YmxpYzoKICAgIEJhcigpIDogRm9vKCksIGJfKDApIHt9OwogICAgQmFyKGludCBhLCBpbnQgYikgOiBGb28oYSksIGJfKGIpIHt9OwogICAgaW5saW5lIGludCBnZXRCKCkgeyByZXR1cm4gYl87IH07CiAgICB2aXJ0dWFsIHZvaWQgcHJpbnQoKSBvdmVycmlkZSB7IHN0ZDo6Y291dCA8PCAie2E6ICIgPDwgZ2V0QSgpIDw8ICIsIGI6ICIgPDwgZ2V0QigpIDw8ICJ9XG4iOyB9Owpwcml2YXRlOgogICAgaW50IGJfOwp9OwoKRm9vOjpGb28oY29uc3Qg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