answersLogoWhite

0

//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 = "")

{

cout <<

"Constructor called\n";

m_pName =

new

string(name);

}

~Critter()

//destructor

{

cout <<

"Destructor called\n";

delete

m_pName;

}

Critter(

const

Critter& c) //copy constructor

{

cout <<

"Copy Constructor called\n";

m_pName =

new

string; *m_pName =

c

.GetName();

}

Critter

& operator=(

const Critter& c) //overloaded assignment operator

{

cout <<

"Overloaded Assignment Operator called\n";

if

(

this == &c)

{

return

*

this;

}

else

{

*m_pName =

c

.GetName();

return

*

this;

}

}

string

GetName()

const { return *m_pName; }

void

SetName(

const string& name = "") { *m_pName = name; }

void

SayHi()

const { cout << "Hi, my name is " << GetName() << "\n"; }

private

:

string

* m_pName;

};

void

testDestructor();

void

testCopyConstructor(

Critter copy);

void

testAssignmentOp();

int

main()

{

testDestructor();

cout << endl;

Critter

crit(

"Poochie");

crit.SayHi();

testCopyConstructor(crit);

cout << endl;

testAssignmentOp();

return

0;

}

void

testDestructor()

{

Critter

crit(

"Rover");

crit.SayHi();

}

//passing object by value invokes its copy constructor

void

testCopyConstructor(

Critter copy)

{

copy

.SayHi();

}

void

testAssignmentOp()

{

Critter

crit1(

"crit1");

Critter

crit2(

"crit2");

crit1 = crit2;

crit1.SayHi();

crit2.SayHi();

cout <<

"Setting name of crit1 back to 'crit1'\n";

crit1.SetName(

"crit1");

crit1.SayHi();

crit2.SayHi();

Critter

crit(

"crit");

crit = crit;

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

What is message passing in c plus plus?

-define class with necessary data member &amp; member function. -create object of that class. -communication.


Is there anything similar between C and C plus plus with reference to data hiding?

No. Data hiding is a feature of object oriented programming. C does not support OOP, and therefore has no private member access. All members are public in C.


What is the order of initialization of data in C plus plus?

The general order of initialization is:Base class objects (if present)Member data objectsConstructor function code


What is data breakpoints in c and c?

Data breakpoints are breakpoints which will occur when some memory is set to a certain value. For example, you can set a breakpoint when i == 10 in a typical for loop to stop after the 10th iteration. You can also watch for changes to variables on the heap, like wait for a member of a class to be modified.


What are the arguments for and against Java's implicit heap storage recovery when compared with the explicit heap storage recovery required in C plus plus?

Sounds suspiciously like a homework question. :P


What is meant by heap in c or cpp?

If you see the word "heap" in the context of C/C++ programming, it is probably referring to one of two ideas. First, if it is written as "the heap", it is probably referring to dynamically allocated memory. We conceptualize memory as either being on "the stack" or "the heap" in main memory. Memory allocation from the heap happens when a call to malloc (or similar functions) are called in C, or when the "new" operator is used in C++. This is in contrast to statically allocated memory, which comes from the load module and is known at compile-time, or from the "stack" which is used at run-time to allocate local scope, or automatic, memory. Another usage of the word heap is a certain data structure called a heap. It is a very common data structure for priority queues and is crucial to the famous HeapSort algorithm. You can easily find more information on this data structure e.g. by searching for HeapSort.


What is a method in c plus plus?

In C++, methods are simply class member functions.


What is the difference between static data member and ordinary data member?

Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.


Write a program of binary heap in c or c language?

to implement operations on binary heap in c


User defined data type in c plus plus?

Use "typedef" : both in C and C++.


Can an integer data type in c plus plus stores character value?

no


What is the price of the book data structure using c and c plus plus by tanenbaum?

225 Rs. after discount........