Created
November 7, 2016 02:45
-
-
Save eltonvs/67b4dbe8c89dfcac3eba4baa209af730 to your computer and use it in GitHub Desktop.
aaaaaaaaaaaaaaaaaaaaaaaaa
This file contains hidden or 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
#include <iostream> | |
using namespace std; | |
class Base { | |
public: | |
Base() {} | |
virtual ~Base() {}; | |
virtual ostream &print(ostream &o) const = 0; | |
protected: | |
int id; | |
}; | |
class Derived: public Base { | |
public: | |
Derived() {} | |
Derived(int i) { | |
Base::id = i, id2 = 123; | |
} | |
~Derived(){} | |
ostream &print(ostream &o) const { | |
o << "id = " << Base::id << "\n"; | |
o << "id2: " << id2 << "\n"; | |
return o; | |
} | |
private: | |
int id2; | |
}; | |
class Derived2: public Base { | |
public: | |
Derived2() {} | |
~Derived2() {} | |
ostream &print(ostream &o) const { | |
o << "id = " << Base::id << "\n"; | |
o << "name = " << name << "\n"; | |
return o; | |
} | |
private: | |
string name = "Another type from another class to print"; | |
}; | |
ostream &operator<<(ostream &o, Base *d) { | |
d->print(o); | |
return o; | |
} | |
int main() { | |
Base *d = new Derived(20); | |
Base *d2 = new Derived2(); | |
cout << d; | |
cout << "--\n"; | |
cout << d2; | |
delete d; | |
delete d2; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment