Created
December 26, 2018 08:39
-
-
Save Liam0205/da536959ea2ac76634fa2acce909216d to your computer and use it in GitHub Desktop.
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> | |
#include <string> | |
struct Person { | |
size_t age = 0; | |
std::string name; | |
Person& operator++() { // prefix | |
++age; | |
return *this; | |
} | |
Person operator++(int) { // postfix | |
Person res = *this; // trivial copy-constructor is called | |
this->operator++(); | |
return res; | |
} | |
// not necessary here, but necessary for classes whose private members are about to be printed. | |
friend std::ostream& operator<<(std::ostream& os, const Person& p); | |
}; | |
std::ostream& operator<<(std::ostream& os, const Person& p) { | |
return os << p.name << '\t' << p.age; | |
} | |
int main() { | |
Person liam; | |
liam.name = "Liam Huang"; | |
std::cout << liam << '\n'; | |
std::cout << ++liam << '\n'; | |
std::cout << liam++ << '\n'; | |
std::cout << liam << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment