/* 1 */ #include /* 2 */ #include /* 3 */ using namespace std; /* 4 */ // --------------------------------------------------- class definition /* 5 */ class MyObject { /* 6 */ public: /* 7 */ static int numObjs; /* 8 */ MyObject(const char *n = "--default--"); // default constructor /* 9 */ MyObject(const MyObject& rhs); // copy constructor /* 10 */ ~MyObject(); // destructor /* 11 */ string getName() const { return name; } /* 12 */ void setName(const string newName) { name = newName; } /* 13 */ friend ostream& operator<<(ostream& output, const MyObject& obj); /* 14 */ private: /* 15 */ string name; /* 16 */ int id; /* 17 */ }; /* 18 */ // ------------------------------------------------ default constructor /* 19 */ MyObject::MyObject(const char *n) : name(n) /* 20 */ { /* 21 */ id = ++numObjs; /* 22 */ cout << "MyObject Default constructor: " << *this << endl; /* 23 */ } /* 24 */ // --------------------------------------------------- copy constructor /* 25 */ MyObject::MyObject(const MyObject& rhs) : name(rhs.name) /* 26 */ { /* 27 */ id = ++numObjs; /* 28 */ cout << "MyObject Copy constructor: " << *this << endl; /* 29 */ } /* 30 */ // -------------------------------------------------------- destructor /* 31 */ MyObject::~MyObject() /* 32 */ { /* 33 */ cout << "MyObject Destructor: " << *this << endl; /* 34 */ } /* 35 */ // ----------------------------------------------------------- put to /* 36 */ ostream& operator<<(ostream& output, const MyObject& obj) /* 37 */ { /* 38 */ // output in format: ("object name",id) /* 39 */ return output << "(\"" << obj.name << "\"," << obj.id << ")"; /* 40 */ } /* 41 */ //---------------------------------------------------- static variables /* 42 */ int MyObject::numObjs = 0; // static member for all objects in class /* 43 */ static MyObject staticObj("I'm static, outside of main"); /* 44 */ //---------------------------------------------------------- prototypes /* 45 */ MyObject getMaxMyObj(const MyObject o1, const MyObject o2); /* 46 */ int cmpMyObj(const MyObject o1, const MyObject o2); /* 47 */ void swapMyObj(MyObject& o1, MyObject& o2); /* 48 */ //--------------------------------------------------------------- main /* 49 */ int main () /* 50 */ { /* 51 */ cout << "--PART 1: Start of main--" << endl; /* 52 */ cout << "--Defining o1, o2(Bob)--" << endl; /* 53 */ MyObject o1, o2("Bob"); /* 54 */ cout << "--Defining o3(o2)--" << endl; /* 55 */ MyObject o3(o2); /* 56 */ cout << "--Defining array of 3 objects--" << endl; /* 57 */ MyObject array[3]; /* 58 */ cout << "\n--PART 2: call function using call-by-value," /* 59 */ " return int--" << endl; /* 60 */ cmpMyObj(o1, o3); /* 61 */ cout << "--call function using call-by-value, return MyObject--" << endl; /* 62 */ getMaxMyObj(o1, o3); /* 63 */ cout << "\n--PART 3: o1: " << o1 << endl; /* 64 */ { /* 65 */ cout << " --entering new block, define new o1(Sally)--" << endl; /* 66 */ MyObject o1("Sally"); /* 67 */ o1.setName("Sally"); /* 68 */ cout << " o1: " << o1 << "\to2: " << o2 << endl; /* 69 */ cout << " --call swap function using call-by-reference--" << endl; /* 70 */ swapMyObj(o1, o2); /* 71 */ cout << " --were their values swapped?--" << endl; /* 72 */ cout << " o1: " << o1 << "\to2: " << o2 << endl; /* 73 */ cout << " --leaving new block--" << endl; /* 74 */ } /* 75 */ cout << "o1: " << o1 << endl; /* 76 */ cout << "\n--PART 4: Define reference var.: { MyObject& oref=o1; } --" /* 77 */ << endl; /* 78 */ { /* 79 */ MyObject& oref=o1; /* 80 */ } /* 81 */ cout << "-- was anything constructed/destructed?--" << endl; /* 82 */ cout << "\n--PART 5: new and delete--" << endl; /* 83 */ cout << "--use new to create one object, then array of 2 objects--" /* 84 */ << endl; /* 85 */ MyObject *op1 = new MyObject, *op2 = new MyObject[2]; /* 86 */ cout << "--use delete to remove that one object--" << endl; /* 87 */ delete op1; /* 88 */ cout << "--use delete [] to remove that array of 2 objects --" << endl; /* 89 */ delete [] op2; /* 90 */ cout << "\n--LAST PART: End of main--" << endl; /* 91 */ return 0; /* 92 */ } /* 93 */ //---------------------------------------------------------- cmpMyObj /* 94 */ int cmpMyObj(const MyObject o1, const MyObject o2) /* 95 */ { /* 96 */ string name1 = o1.getName(), name2 = o2.getName(); /* 97 */ if ( name1 == name2 ) /* 98 */ return 0; /* 99 */ else if ( name1 < name2 ) /* 100 */ return -1; /* 101 */ else /* 102 */ return 1; /* 103 */ } /* 104 */ //---------------------------------------------------------- getMaxMyObj /* 105 */ MyObject getMaxMyObj(const MyObject o1, const MyObject o2) /* 106 */ { /* 107 */ string name1 = o1.getName(), name2 = o2.getName(); /* 108 */ if ( name1 >= name2 ) /* 109 */ return o1; /* 110 */ else /* 111 */ return o2; /* 112 */ } /* 113 */ //---------------------------------------------------------- swapMyObj /* 114 */ void swapMyObj(MyObject& o1, MyObject& o2) /* 115 */ { /* 116 */ MyObject tmp(o1); /* 117 */ o1.setName(o2.getName()); /* 118 */ o2.setName(tmp.getName()); /* 119 */ }