copy constructor and 2 args constructor help

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

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
  #11
Nov 21st, 2006
  1. public:
  2. /**
  3.   * Default constructor for the Media class
  4.   */
  5. Media(void);
  6. /**
  7.   * Copy constructor for the Media class
  8.   *
  9.   * @param audio Audio object
  10.   */
  11. Media(const Media& media);
  12. /**
  13.   * Returns the barcode of the current instance
  14.   *
  15.   * @returns barcode
  16.   */
  17. const string& getBarcode(void) const;
  18.  
  19. virtual string getMediaType(void) const = 0;
  20.  
  21. /**
  22.   * Returns the speed of the current instance
  23.   *
  24.   * @returns speed
  25.   */
  26. const string& getSpeed(void) const;
  27. /**
  28.   * Sets the barcode of the current instance
  29.   *
  30.   * @param barcode
  31.   */
  32. void setBarcode(const string& barcode);
  33. /**
  34.   * Sets the speed of the current instance
  35.   *
  36.   * @param speed
  37.   */
  38. void setSpeed(const string& speed);
  39. /**
  40.   * Compares the supplied parameter to the current instance
  41.   *
  42.   * @param obj Media object to compare to
  43.   * @returns true if the current instance is the same as the supplied parameter
  44.   */
  45. bool operator ==(const Media& obj) const;
  46. /**
  47.   * Calls the operator== and returns its inverse
  48.   *
  49.   * @param media Media object to compare to
  50.   * @returns true if the current instance is different to the supplied parameter
  51.   */
  52. bool operator !=(const Media& media) const;
  53.  
  54. virtual Media * createCopy(void) const = 0;
  55. /**
  56.   * Reads in values of the instance variables from the console
  57.   *
  58.   * @returns 0 if the read worked, -1 if it failed
  59.   */
  60. virtual int read(void);
  61. /**
  62.   * Reads in values of the instance variables from a tokeniser object
  63.   *
  64.   * @param tok Tokeniser object
  65.   * @returns 0 if the read worked, -1 if it failed
  66.   */
  67. virtual int read(Tokeniser& tok);
  68. /**
  69.   * Writes instance variables to the console
  70.   *
  71.   * @returns 0 if the write worked, -1 if it failed
  72.   */
  73. virtual int write(void) const;
  74. /**
  75.   * Writes instance variables to a file stream
  76.   *
  77.   * @param outf Output file stream
  78.   * @returns 0 if the write worked, -1 if it failed
  79.   */
  80. virtual int write(ofstream& outf) const = 0;
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
  #12
Nov 21st, 2006
I have tried this in my constructor it compiles but does not seem right to me any thoughts?
Entry::Entry(const Media& media, const Entertainment& entertainment)
{
 
  _media = media.createCopy();
  _entertainment =   entertainment.createCopy();
  Entry *entry;
  entry = new Entry(media, entertainment);
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: copy constructor and 2 args constructor help

 
0
  #13
Nov 22nd, 2006
  Entry *entry;
  entry = new Entry(media, entertainment);
What are you trying to do here? You've already assigned the private members of the class (_media and _entertainment). And once the function returns, the newly allocated Entry object is lost (memory leak)
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
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
  #14
Nov 22nd, 2006
Originally Posted by GloriousEremite View Post
  Entry *entry;
  entry = new Entry(media, entertainment);
What are you trying to do here? You've already assigned the private members of the class (_media and _entertainment). And once the function returns, the newly allocated Entry object is lost (memory leak)
I am trying to store in the current Entry object.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,758
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: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: copy constructor and 2 args constructor help

 
0
  #15
Nov 22nd, 2006
Again I ask if it is necessary to create the Media class as abstract.? It is a compile time error to try to instantiate an object of an abstract data type.
  1. struct A
  2. {
  3. int data;
  4. virtual void createCopy(const A & rhs) const = 0;
  5. };
  6.  
  7. A a; //error, cannot instantiate an object of type A since A is an abstract data type.
  8.  
  9. class B
  10. {
  11. A a; //error, cannot instantiate an object of type A even as a data member
  12. };

However, you can declare a pointer to abstract class and use that to demonstrate polymorphism.
  1. class C : public A
  2. {
  3. C() : data(0) {}
  4. C(int i) : data(i) {}
  5. void createCopy(const A & rhs) { data = rhs.data};
  6. };
  7.  
  8. int main()
  9. {
  10. A * pA; //this is a pointer to type A, not an A object. This is legal.
  11. C c(5);
  12. C c1;
  13. pA = &c1;
  14. pA->createCopy(c); //copy data from c into c1 using the virtual function called createCopy() and the pointer to base class A, which happens to be abstract. This is a pretty lame example, but I think it
  15. makes the point.
  16. }
In your case I think you need something like this, though:
  1. class A
  2. {
  3. int data;
  4.  
  5. public:
  6. A(const A & rhs)
  7. {
  8. createCopy(const A & rhs);
  9. }
  10. void getData() { return data};
  11. void createCopy(const A & rhs) {data = rhs.getData();}
  12. };
  13.  
  14. class B
  15. {
  16. //basically same a class A, don't make A or B abstract
  17. };
  18.  
  19. class C
  20. {
  21. A a;
  22. B b;
  23.  
  24. public:
  25. A(const C & rhs)
  26. {
  27. if(isValid(rhs))
  28. {
  29. a.createCopy(rhs);
  30. b.createCopy(rhs);
  31. }
  32. }
  33. bool isValid(const C & rhs) { //whatever};
  34. };

However, in your case it doesn't appear as if the Entry class is derived from the Media and the Entertainment class based on what's posted so far, so I see no reason to have the Media and the Entertainment class abstract, unless it is required in some other portion of your instructions.
Last edited by Lerner; Nov 22nd, 2006 at 7:12 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
  #16
Nov 23rd, 2006
Thanks i finnaly did it
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();
}
}
}
Entry::Entry(const Media& media, const Entertainment& entertainment)
{
Entry entry;
if (entry.isValid())
{
_media = entry.getMedia()->createCopy();
_entertainment = entry.getEntertainment()->createCopy();
}

}
Reply With Quote Quick reply to this message  
Reply

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




Views: 3072 | Replies: 15
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC