| | |
copy constructor and 2 args constructor help
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
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 header file
• 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.
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
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(); } }
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
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.
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.
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
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:
Like:
Entertainment * Game::createCopy(void) const { return new Game(*this); }
Last edited by NSta; Nov 20th, 2006 at 6:53 pm.
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
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:
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.
)
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();
}•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
Entry::Entry(const Entry& entry):_media(NULL),_entertainment(NULL) { if(entry.isValid()) { if ( entry._media != NULL ) { _media = entry._media->createCopy(); } if ( entry._entertainment != NULL ) { _entertainment = entry._entertainment->createCopy(); } } }
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
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.
>>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.
![]() |
Similar Threads
- (C++) Writing a Copy Constructor?!? (C++)
- copy constructor problem (C++)
- need the usage of Copy Constructor (C++)
Other Threads in the C Forum
- Previous Thread: 2 dimensional array class
- Next Thread: error: too many arguments to function `mysql_query'
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking highest homework i/o inches include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming socketprogramming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






