#include using namespace std; class person { public: int id; // id no. char* nameptr; // ptr to dyn alloc name person(); // default constructor person(int, char*); // another constructor ~person(); // destructor person(const person& in_pers); // copy constructor person& operator=(const person& in_pers); // overloaded asgt op }; // *********************************** ostream& operator<< (ostream &, const person &); // print operator person valval(person); person valvalx(person); person valref(person&); person valrefx(person&); person& refval(person); person& refref(person&); int main() { person pers1(9, "miriam"); cout << "pers1 is " << pers1 << endl << endl; person pers2 = pers1; cout << "pers2 is " << pers2 << endl << endl; person pers3; pers3 = pers1; cout << "pers3 is " << pers3 << endl << endl; person pers4(36, "sara"); pers3 = pers2 = pers4; cout << "pers4 is " << pers4 << endl; cout << "pers2 is " << pers2 << endl; cout << "pers3 is " << pers3 << endl << endl; cout << endl << "----------" << endl << endl; person pers_in(42, "input"); cout << "pers_in is " << pers_in << endl; person pers_out; cout << "pers_out is " << pers_out << endl << endl; pers_out = valval(pers_in); cout << endl << "----------" << endl << endl; pers_out = valvalx(pers_in); cout << endl << "----------" << endl << endl; pers_out = valref(pers_in); cout << endl << "----------" << endl << endl; pers_out = valrefx(pers_in); cout << endl << "----------" << endl << endl; pers_out = refval(pers_in); cout << endl << "----------" << endl << endl; pers_out = refref(pers_in); return 0; } // *********************************** person::person() // default constructor { id = 0; nameptr = 0; // no name, so no dyn memory needed cout << "** Called default constructor for " << *this << endl << endl; } person::person(int in_id, char* in_nameptr) // another constructor { if (in_nameptr == 0) nameptr = 0; else { nameptr = new char[strlen(in_nameptr)+1]; // allocate new memory strcpy(nameptr, in_nameptr); // ... and copy the name } id = in_id; cout << "** Called constructor for " << *this << endl << endl; } person::~person() // destructor { cout << "** Called destructor for " << *this << endl; if (nameptr != 0) { delete [] nameptr; // free dynamic memory nameptr = 0; // zero out the ptr } } person::person(const person& in_pers) // copy constructor { cout << "** Called copy constructor from " << in_pers << endl; if (in_pers.nameptr == 0) nameptr = 0; else { nameptr = new char[strlen(in_pers.nameptr)+1]; // allocate new memory strcpy(nameptr, in_pers.nameptr); // copy new name } id = in_pers.id; // Copy remaining fields cout << " New item is " << *this << endl << endl; } person& person::operator=(const person& in_pers) // overloaded asgt op { cout << "** Called overloaded asgt op from " << in_pers << endl; if (this != &in_pers) // Make sure not same object { if (nameptr != 0) { delete [] nameptr; // delete old name's memory nameptr = 0; // ... and zero the ptr } if (in_pers.nameptr == 0) nameptr = 0; else { nameptr = new char[strlen(in_pers.nameptr)+1]; // allocate new memory strcpy(nameptr, in_pers.nameptr); // copy new name } id = in_pers.id; // copy remaining fields } cout << " Result is " << *this << endl << endl; return *this; // return ref for chained asgt } ostream& operator<< (ostream& ostr, const person& in_pers) { ostr << &in_pers // address of object << ": " << in_pers.id << " - " << (in_pers.nameptr == 0 ? "" : in_pers.nameptr) << " (dyn mem at " << (void *) in_pers.nameptr << ")"; return ostr; } person valval (person x) { cout << "Hello! I am value in/value out" << endl << endl; person temp = x; return (temp); } person valvalx (person x) { cout << "Hello! I am value in/value out w/o a temp" << endl << endl; return (x); } person valref (person& x) { cout << "Hello! I am reference in/value out" << endl << endl; person temp = x; return (temp); } person valrefx (person& x) { cout << "Hello! I am reference in/value out w/o a temp" << endl << endl; return (x); } person& refval (person x) { cout << "Hello! I am value in/reference out" << endl << endl; return (x); } person& refref (person& x) { cout << "Hello! This is reference in/reference out" << endl << endl; return (x); }