Created
February 11, 2017 21:10
-
-
Save pedropedruzzi/ce7d99d98e78ead072d6164b82c83526 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> | |
using Age = unsigned int; | |
using Name = std::string; | |
using Country = std::string; | |
template <class PersonSubtype> | |
class Person | |
{ | |
public: | |
Person(Age age_, Name name_, Country country_) | |
: age(age_) | |
, name(name_) | |
, country(country_) {} | |
Age age; | |
Name name; | |
Country country; | |
bool from_south_america() { | |
return this->country == "Brazil"; | |
} | |
bool from_sao_paulo() { | |
return static_cast<PersonSubtype*>(this)->from_sao_paulo(); | |
} | |
void dump() { | |
std::cout << this->name << " is " << (this->from_south_america() ? "" : "not ") | |
<< "South American and comes from " << (this->from_sao_paulo() ? "Sao Paulo" : "elsewhere") | |
<< std::endl; | |
} | |
}; | |
struct Brazilian : public Person<Brazilian> | |
{ | |
using Person<Brazilian>::Person; | |
bool from_sao_paulo() { | |
return true; | |
} | |
}; | |
struct Lithuanian : public Person<Lithuanian> | |
{ | |
using Person<Lithuanian>::Person; | |
bool from_sao_paulo() { | |
return false; | |
} | |
}; | |
int main() { | |
Brazilian ricardo(30, "Ricardo", "Brazil"); | |
Lithuanian egle(24, "Egle", "Lithuania"); | |
ricardo.dump(); | |
egle.dump(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment