< Previous | Contents | Next >

Introducing the Heap Data Member

Program

The Heap Data Member program defines a new type of critter with a data member that is a pointer, which points to an object stored on the heap. The class defines a few new member functions to handle situations in which an object is destroyed, copied, or assigned to another object. The program destroys, copies, and assigns objects to show that the objects behave as youd expect, even with data members pointing to values on the heap. Figure 9.6 shows the results of the Heap Data Member program.

304 Chapter 9 n Advanced Classes and Dynamic Memory: Game Lobby



image

Figure 9.6

Objects, each with a data member that points to a value on the heap, are instantiated, destroyed, and copied.


You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 9 folder; the filename is heap_data_member.cpp.

//Heap Data Member

//Demonstrates an object with a dynamically allocated data member


#include <iostream> #include <string>

using namespace std; class Critter

{

public:

Critter(const string& name = "", int age = 0);

~Critter(); //destructor prototype Critter(const Critter& c); //copy constructor prototype

Critter& Critter::operator=(const Critter& c); //overloaded assignment

op

void Greet() const;

Working with Data Members and the Heap 305



private:

string* m_pName; int m_Age;

};


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)

{

delete m_pName;

m_pName = new string(*(c.m_pName)); m_Age = c.m_Age;

}

return *this;

}


void Critter::Greet() const

{

306 Chapter 9 n Advanced Classes and Dynamic Memory: Game Lobby



cout << "I’m " << *m_pName << " and I’m " << m_Age << " years old.\n"; cout << "&m_pName: " << cout << &m_pName << endl;

}


void testDestructor();

void testCopyConstructor(Critter aCopy); void testAssignmentOp();


int main()

{

testDestructor(); cout << endl;


Critter crit("Poochie", 5); crit.Greet(); testCopyConstructor(crit); crit.Greet();

cout << endl; testAssignmentOp();

return 0;

}


void testDestructor()

{

Critter toDestroy("Rover", 3); toDestroy.Greet();

}


void testCopyConstructor(Critter aCopy)

{

aCopy.Greet();

}


void testAssignmentOp()

{

Critter crit1("crit1", 7);

Critter crit2("crit2", 9); crit1 = crit2;

Working with Data Members and the Heap 307



crit1.Greet(); crit2.Greet(); cout << endl;


Critter crit3("crit", 11); crit3 = crit3; crit3.Greet();

}