//--------------------------------------------------------------------- // Sınislausn á dæmi 5 á Vikublaği 3 í Tölvunarfræği 2, vor 2005 // // Hjálmtır Hafsteinsson, janúar 2005 //--------------------------------------------------------------------- #include #include using namespace std; class CAT { public: CAT(); CAT(int age, int weight, string name, string owner); CAT (const CAT &); ~CAT(); int GetAge() const { return *itsAge; } int GetWeight() const { return *itsWeight; } void SetAge(int age) { *itsAge = age; } string GetName() const { return *itsName; } string GetOwner() const { return *itsOwner; } void SetName(string name) { *itsName = name; } void SetOwner(string owner) { *itsOwner = owner; } private: int *itsAge; int *itsWeight; string *itsName; string *itsOwner; }; CAT::CAT() { itsAge = new int; itsWeight = new int; itsName = new string; itsOwner = new string; *itsAge = 5; *itsWeight = 9; *itsName = "Frissi"; *itsOwner = "Hjalmtyr"; } CAT::CAT(int age, int weight, string name, string owner) { itsAge = new int; itsWeight = new int; itsName = new string; itsOwner = new string; *itsAge = age; *itsWeight = weight; *itsName = name; *itsOwner = owner; } CAT::CAT(const CAT & rhs) { itsAge = new int; itsWeight = new int; itsName = new string; itsOwner = new string; *itsAge = rhs.GetAge(); *itsWeight = rhs.GetWeight(); *itsName = rhs.GetName(); *itsOwner = rhs.GetOwner(); } CAT::~CAT() { delete itsAge; itsAge = 0; delete itsWeight; itsWeight = 0; delete itsName; itsName = 0; delete itsOwner; itsOwner = 0; } int main() { CAT frisky( 3, 10, "Frisky", "C++"); cout << frisky.GetName() << "'s age: " << frisky.GetAge() << endl; cout << "Setting " << frisky.GetName() << " to 6..." << endl << endl; frisky.SetAge(6); cout << "Creating boots from frisky" << endl; CAT boots(frisky); boots.SetName("Boots"); cout << frisky.GetName() << "'s age: " << frisky.GetAge() << endl; cout << boots.GetName() << "'s age: " << boots.GetAge() << endl << endl; cout << "setting " << frisky.GetName() << " to 7..." << endl; frisky.SetAge(7); cout << frisky.GetName() << "'s age: " << frisky.GetAge() << endl; cout << boots.GetName() << "' age: " << boots.GetAge() << endl << endl; cout << "setting " << boots.GetName() << "'s owner to Hjalmtyr" << endl; boots.SetOwner("Hjalmtyr"); cout << frisky.GetName() << "'s owner: " << frisky.GetOwner() << endl; cout << boots.GetName() << "'s owner: " << boots.GetOwner() << endl; return 0; }