copy constructor and 2 args constructor help

Thread Solved
Reply

Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

copy constructor and 2 args constructor help

 
0
  #1
Nov 19th, 2006
Hi i am so lost and stressed please can some help me. My question is how do i code the follwoing

• Constructor: Copy The copy constructor should check the validity of the Entry object parameter through the use of the isValid method and copy the valid objects media and entertainment objects through the use of the createCopy functions.
• Constructor: 2 argument The 2-argument constructor takes a media and entertainment object as parameters. A copy should be made of the parameters through the createCopy function, if they are valid and stored in the current Entry object.
Entry class
Entry::Entry():_media(NULL),_entertainment(NULL){}
Entry::Entry(const Entry& entry):_media(NULL),_entertainment(NULL)
{
 
}
Entry::Entry(const Media& media, const Entertainment& entertainment)
{
 
}
Entry::~Entry()
{
delete _media;
delete _entertainment;
}
Media * Entry::getMedia()
{
 
return _media;
}
const Media * Entry::getMedia() const
{
 
return _media;
}
Entertainment * Entry::getEntertainment()
{
return _entertainment;
}
const Entertainment * Entry::getEntertainment() const
{
 
return _entertainment;
}
bool Entry::isValid() const
{
 
return _media != NULL && _entertainment != NULL;
 
}
bool Entry::operator ==(const Entry& obj) const
{
if (getEntertainment()->getEntertainmentType() != obj.getEntertainment()->getEntertainmentType())
{
return false;
}
if (getMedia()->getMediaType() != obj.getMedia()->getMediaType())
{
return false;
}
if (getEntertainment()->getTitle() != obj.getEntertainment()->getTitle())
{
return false;
}
 
return true;
}
bool Entry::operator !=(const Entry& entry) const
{
return !(*this==entry);
}

Entry header file
class Entry
{
Entertainment *_entertainment;
Media *_media;
public:
Last edited by NSta; Nov 19th, 2006 at 10:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Re: copy constructor and 2 args constructor help

 
0
  #2
Nov 19th, 2006
i guess i dont know how to to do is pass them by value and implement a copy constructor on those classes
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Re: copy constructor and 2 args constructor help

 
0
  #3
Nov 20th, 2006
Scrap that above quick reply i have done my copy constructor is this oksy
Entry::Entry(const Entry& entry):_media(NULL),_entertainment(NULL)
{

if(entry.isValid())
{
_media->createCopy();
_entertainment->createCopy();
}
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: copy constructor and 2 args constructor help

 
0
  #4
Nov 20th, 2006
Thanks for the code tags, but use of indentation would make the code a whole lot easier to read!

Per your instructions the copy contstructor in your third post looks appropriate, though you'll need to declare and define the createCopy() functions somewhere. I suspect that the createCopy() functions should be sent the appropriate values from entry, else how will they know what to copy into the appropriate members. Within the createCopy() functions I suspect you will want to use deep copy rather than shallow copy to copy the information given that _media and _entertainment are both pointers.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Re: copy constructor and 2 args constructor help

 
0
  #5
Nov 20th, 2006
Yeah i have two virtual functions (createCopy()) in the media and entertainment class and normal create copy functions in the bases classes extending media and entertainment that create a copy of itself on the heap and return a pointer reference to it.
Like:
Entertainment * Game::createCopy(void) const
{

              return new Game(*this); 

}
Last edited by NSta; Nov 20th, 2006 at 6:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Re: copy constructor and 2 args constructor help

 
0
  #6
Nov 20th, 2006
Oh yeah i have like 6 virtual constructors 2 of which are pure (in media and entertainment) do i need to create desturctors for them? Just the pure ones
Last edited by NSta; Nov 20th, 2006 at 7:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Re: copy constructor and 2 args constructor help

 
0
  #7
Nov 20th, 2006
quote=NSta;278471]Yeah i have two virtual functions (createCopy()) in the media and entertainment class and normal create copy functions in the bases classes extending media and entertainment that create a copy of itself on the heap and return a pointer reference to it.
Like:
Entertainment * Game::createCopy(void) const
{
 
             return new Game(*this); 
 
}

so in the game class my copy constructor is as follows (
The copy constructor must call the base class copy constructor and pass the parameter from the copy constructor to the base class copy constructor. The copy constructor must also copy the attributes from the other object.
)
Game::Game(const Game& game):Entertainment(game)
{
  _cdKey = game._cdKey;
  _sysRequirements =game._sysRequirements; 
  game.createCopy();
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Re: copy constructor and 2 args constructor help

 
0
  #8
Nov 21st, 2006
  1. Entry::Entry(const Entry& entry):_media(NULL),_entertainment(NULL)
  2. {
  3. if(entry.isValid())
  4. {
  5. if ( entry._media != NULL )
  6. {
  7. _media = entry._media->createCopy();
  8. }
  9.  
  10. if ( entry._entertainment != NULL )
  11. {
  12. _entertainment = entry._entertainment->createCopy();
  13. }
  14. }
  15. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Re: copy constructor and 2 args constructor help

 
0
  #9
Nov 21st, 2006
media and entertainment are abstract classes
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: copy constructor and 2 args constructor help

 
0
  #10
Nov 21st, 2006
Unless my memory is as cloudy as the weather this morning, you can't declare objects of an abstract class. Therefore, the Entry class could not have member variables of an abstract class type, though it can inherit attributes associated with an abstract base class. If the attribute is a member function, then that function needs to be overridden in the current class. However, that doesn't seem to be what is wanted in the instructions:

>>copy the valid (Entry) objects media and entertainment objects through the use of the createCopy functions.

Are you sure the Media and Entertainment classes are supposed to be abstract? If you are, then I'll have to bow out and let someone else provide assistance.

Posting the Media or Entertainment class code would be helpful, too.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC