944,018 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 3427
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 19th, 2006
0

copy constructor and 2 args constructor help

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
Nov 19th, 2006
0

Re: copy constructor and 2 args constructor help

i guess i dont know how to to do is pass them by value and implement a copy constructor on those classes
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
Nov 20th, 2006
0

Re: copy constructor and 2 args constructor help

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();
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
Nov 20th, 2006
0

Re: copy constructor and 2 args constructor help

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 20th, 2006
0

Re: copy constructor and 2 args constructor help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
Nov 20th, 2006
0

Re: copy constructor and 2 args constructor help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
Nov 20th, 2006
0

Re: copy constructor and 2 args constructor help

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();
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
Nov 21st, 2006
0

Re: copy constructor and 2 args constructor help

  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
Nov 21st, 2006
0

Re: copy constructor and 2 args constructor help

media and entertainment are abstract classes
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
Nov 21st, 2006
0

Re: copy constructor and 2 args constructor help

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: 2 dimensional array class
Next Thread in C Forum Timeline: error: too many arguments to function `mysql_query'





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC