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
|
#include <iostream>
#include <string>
struct animal
{
animal( std::string type, std::string name, std::string sound )
: type_(type), name_(name), sound_(sound) {}
animal( const animal& that )
: type_( that.type() ), name_( clone_prefix + that.name() ), sound_( that.sound() ) {}
const std::string& name() const noexcept { return name_ ; }
const std::string& type() const noexcept { return type_ ; }
const std::string& sound() const noexcept { return sound_ ; }
private:
std::string type_ ;
std::string name_ ;
std::string sound_ ;
static constexpr char clone_prefix[] = "(clone of) " ;
friend std::ostream& operator<< ( std::ostream& stm, const animal& a )
{ return stm << "animal{ " << a.type() << ", " << a.name() << ", " << a.sound() << "! }" ; }
};
int main()
{
const animal fido( "dog", "fido", "woof" ) ;
std::cout << fido << '\n' ; // animal{ dog, fido, woof! }
const animal clone_of_fido(fido) ;
std::cout << clone_of_fido << '\n' ; // animal{ dog, (clone of) fido, woof! }
}
|