User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,573 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,573 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1100 | Replies: 4
Reply
Join Date: Oct 2007
Posts: 3
Reputation: ecestd1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ecestd1 ecestd1 is offline Offline
Newbie Poster

Help help! how to implement a copy constructor in c++ for ADT queues??

  #1  
Oct 28th, 2007
how do you implement a copy constructor for this pointer-based ADT
queue
#include "Queuep.h"
#include <cassert>
#include <new>
 using namespace std;

Queue::Queue () : backPtr (0), frontPtr(0)
{
}

Queue::Queue(const Queue& Q) throw(OutOfStorageException)
{
     //implement here

}//end copy constructor

Queue::~Queue()
{
while ( !isEmpty() )
 {
     dequeue();

}//end while
}//end destructor

    assert( (backPtr == 0) && (frontPtr == 0) )
Last edited by Ancient Dragon : Oct 28th, 2007 at 9:30 am. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: help! how to implement a copy constructor in c++ for ADT queues??

  #2  
Oct 28th, 2007
Erm. Copy all relivant data?
By the looks of it start at the back pointer and iterate to the front pointer assigning the values at each of the nodes (from Q), to your object along the way.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: help! how to implement a copy constructor in c++ for ADT queues??

  #3  
Oct 28th, 2007
Learn to format your code better so that you can see what you're doing. Only takes a couple seconds to hit that space bar.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 3
Reputation: ecestd1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ecestd1 ecestd1 is offline Offline
Newbie Poster

Re: help! how to implement a copy constructor in c++ for ADT queues??

  #4  
Oct 29th, 2007
thanks
Reply With Quote  
Join Date: Oct 2007
Posts: 3
Reputation: ecestd1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ecestd1 ecestd1 is offline Offline
Newbie Poster

Help Re: help! how to implement a copy constructor in c++ for ADT queues??

  #5  
Oct 29th, 2007
I did implement the copy constructor but still its not working what could be the problem?

/** @file
*
* @course CS1521
* @section 1
*
* Pointer-based <tt>Queue</tt> class implementation.<br>
* Adapted from page 352-4 in Carrano 5e.
*
* @author Frank M. Carrano
* @author Steve Holtz
* @date 3/11/2007
* @version 5.1 */

#include "QueueP.h"
#include <cassert> // for assert
#include <new> // for bad_alloc
#include <iostream>
//typedef std::queue<QueueItemType> Queue;
using namespace std;
//private:{Queue::Queue(const Queue& Q)}


Queue::Queue() : backPtr(0), frontPtr(0)//copy constructor initializes the data members
{
} // end default constructor

Queue::Queue(const Queue& Q) throw(OutOfStorageException)// because the compiler-generated default constructorwould not necessarily initialize data members to appropriate values, you must create your own. pg 198
//:backPtr(const Queue& Q)
//:frontPtr (const Queue& Q)
if (Q.frontPtr == 0)
frontPtr = 0; // original Queue is empty

else
{ // copy first node
try
{
frontPtr = new QueueNode;
frontPtr->item = Q.frontPtr->item;

// copy rest of Queue
QueueNode *newPtr = frontPtr; // new Queue pointer
// newPtr points to last node in new Queue
// origPtr points to nodes in original Queue
for (QueueNode *origPtr = Q.frontPtr->next;
origPtr != 0;
origPtr = origPtr->next)
{ newPtr->next = new QueueNode;
newPtr = newPtr->next;
newPtr->item = origPtr->item;
} // end for

newPtr->next = 0;
} // end try
catch (bad_alloc e)
{
throw OutOfStorageException(
"OutOfStorageException: Cannot allocate memory in copy constructor.");
} // end catch
} // end if
} // end copy constructor

Queue::~Queue()
{
while (!isEmpty() )
{
dequeue();
} // end while
assert ( (backPtr == 0) && (frontPtr == 0) );
} // end destructor

bool Queue::isEmpty() const
{
return backPtr == 0;
} // end isEmpty

void Queue::enqueue(const QueueItemType& newItem)
throw(OutOfStorageException)
{
try
{
QueueNode *newPtr = new QueueNode;

newPtr->item = newItem;

newPtr->next = 0;

if (isEmpty() )
{
frontPtr = newPtr;
}
else
{
backPtr->next = newPtr;
} // end if

backPtr = newPtr;
}
catch(bad_alloc e)
{
throw OutOfStorageException("Memory allocation failed.");
} // end try/catch
} // end enqueue

void Queue::dequeue() throw(OutOfDataException)
{
if (isEmpty() )
{
throw OutOfDataException("Empty queue, cannot dequeue");
}
else
{ // queue is not empty; remove front
QueueNode *tempPtr = frontPtr;
if (frontPtr == backPtr) // special case?
{ // yes, one node in queue
frontPtr = 0;
backPtr = 0;
}
else
{
frontPtr = frontPtr->next;
} // end if
tempPtr->next = 0; // defensive strategy
delete tempPtr;
// }
} // end if
} // end dequeue

void Queue::dequeue(QueueItemType& queueFront)
throw(OutOfDataException)
{
if (isEmpty() )
{
throw OutOfDataException("Empty queue, cannot dequeue");
}
else
{ // queue is not empty; retrieve front
queueFront = frontPtr->item;
dequeue(); // delete front
} // end if
} // end dequeue

void Queue::getFront(QueueItemType& queueFront) const
throw(OutOfDataException)
{
if (isEmpty() )
{
throw OutOfDataException("Empty queue, cannot getFront");
}
else
{
// queue is not empty; retrieve front
queueFront = frontPtr->item;
} // end if
} // end getFront
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC