1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <string>
struct mammal {
enum hair_type { NONE, CURLY, SHORT, LONG } ;
explicit mammal( std::string name, int nlegs = 4,
hair_type hair = NONE, bool tailed = false )
: its_name(name), num_legs(nlegs), type_of_hair(hair), has_tail(tailed)
{ if( num_legs < 0 ) num_legs = 0 ; }
std::string name() const { return its_name ; }
int number_of_legs() const { return num_legs ; }
hair_type hair() const { return type_of_hair ; }
bool is_tailed() const { return has_tail ; }
std::string to_string() const {
static const std::string hair_type_str[] = { "none", "curly", "short", "long" } ;
return "mammal{ name:" + its_name + ", legs:" + std::to_string(num_legs) +
", hair:" + hair_type_str[type_of_hair] +
", tailed:" + ( has_tail ? "yes" : "no" ) + " }" ;
}
private:
std::string its_name ;
int num_legs = 4 ;
hair_type type_of_hair = NONE ;
bool has_tail = false ;
friend std::ostream& operator<< ( std::ostream& stm, const mammal& the_mammal ) {
return stm << the_mammal.to_string() ;
}
};
int main() {
const mammal ibex( "ibex", 4, mammal::SHORT, true ) ;
std::cout << ibex << '\n' ;
}
|