// copy constructor
#include <iostream>
#include <string>
class people
{
public:
people( std::string n ) : name( n ) {}
people( std::string n, const people &p );
~people() {}
void setAge( int a ) { age = a; }
void setGender( char g ) { gender = g; }
void setName( std::string n ) { name = n; }
int getAge() { return age; }
char getGender() { return gender; }
std::string getName() { return name; }
private:
std::string name;
char gender;
int age;
};
people::people( std::string n, const people &p ) : name( n ), age( p.age ), gender( p.gender ) {}
void outputPeople( people &p )
{
std::cout << "Name: " << p.getName() << '\n'
<< "Age: " << p.getAge() << '\n'
<< "Gender: " << p.getGender() << "\n\n";
}
void copyConstructor()
{
people person_1( "Dave" );
person_1.setAge( 28 );
person_1.setGender( 'M' );
people person_2( "Steve", person_1 );
people person_3 = person_2;
person_3.setName( "Pete" );
outputPeople( person_1 );
outputPeople( person_2 );
outputPeople( person_3 );
}
I'm on about line 43. Has it always been possible to copy a class like this? Or is it new to C++11. I'm sure I've tried to copy a class before and ended up coding an overloaded operator, or used a function.