// Heap Data Member // Written By: // Last Modified: ----- Broken Version <-- delete that b4 turning in // // Description // Demonstrates an object with a dynamically allocated data member // Also touches on class destructor, copy constructor, // and overloaded assignment operator= // ------------------------------------------------------------------------- // include iostream and string XXXXXX XXXXXX XXXXXXXXXX; // should want to use namespace std // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // Declare class Critter -this stuff should be in a header file (Critter.h) // ---------------------------------------------------------------------------- class Critter { public: Critter(const string& name = "", int age = 0); ~Critter(); //destructor prototype Critter(const Critter& c); //copy constructor prototype Critter& operator=(const Critter& c); //overloaded assignment op void Greet() const; private: string* m_pName; int m_Age; }; // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // Define class Critter -this should be in an implementatino file (Critter.cpp) // ---------------------------------------------------------------------------- Critter::Critter(const string& name, int age) { cout << "Constructor called\n"; m_pName = new string(name); m_Age = age; } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- Critter::~Critter() //destructor definition { cout << "Destructor called\n"; delete m_pName; } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- Critter::Critter(const Critter& c) //copy constructor definition { cout << "Copy Constructor called\n"; m_pName = new string(*(c.m_pName)); m_Age = c.m_Age; } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- Critter& Critter::operator=(const Critter& c) //overloaded assignment op def { cout << "Overloaded Assignment Operator called\n"; if (this != &c) { XXXXXXXXXX; // delete m_pName; m_pName = new string(*(c.m_pName)); m_Age = XXXXXXXX; // set m_Age = to c.m_Age } return *this; } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- void Critter::Greet() const { cout << "I'm " << *m_pName << " and I'm " << m_Age << " years old. "; cout << "&m_pName: " << cout << &m_pName << endl; } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // main() program with testing functions // This typically would be in a testingProg.cpp file // ---------------------------------------------------------------------------- // Global function prototypes void testDestructor(); void testCopyConstructor(Critter aCopy); void testAssignmentOp(); // ---------------------------------------------------------------------------- // main --- this is where it all begins // ---------------------------------------------------------------------------- int main() { testDestructor(); cout << endl; Critter crit("Poochie", 5); XXXXXXXX; // call member function Greet() of crit XXXXXXXXX; // call global function: testCopyConstructor, send it crit crit.Greet(); // call member function Greet again cout << endl; XXXXXXXX; // test the Assignment Op return 0; } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- XXXXXX testDestructor() // TODO: return type should be void, see function prototype above { Critter toDestroy("Rover", 3); toDestroy.Greet(); // When this function ends the variable toDestroy goes out of scope // which will automatically call Critter destructor } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- XXXXXX testCopyConstructor(Critter aCopy) { // aCopy is a pass by value which requires the copy constructor of Critter to work aCopy.Greet(); } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- XXXXXX testAssignmentOp() { Critter crit1("crit1", 7); Critter crit2("crit2", 9); crit1 = crit2; crit1.Greet(); crit2.Greet(); cout << endl; Critter crit3("crit", 11); crit3 = crit3; crit3.Greet(); }