| | |
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:
Solved Threads: 0
C Syntax (Toggle Plain Text)
public: /** * Default constructor for the Media class */ Media(void); /** * Copy constructor for the Media class * * @param audio Audio object */ Media(const Media& media); /** * Returns the barcode of the current instance * * @returns barcode */ const string& getBarcode(void) const; virtual string getMediaType(void) const = 0; /** * Returns the speed of the current instance * * @returns speed */ const string& getSpeed(void) const; /** * Sets the barcode of the current instance * * @param barcode */ void setBarcode(const string& barcode); /** * Sets the speed of the current instance * * @param speed */ void setSpeed(const string& speed); /** * Compares the supplied parameter to the current instance * * @param obj Media object to compare to * @returns true if the current instance is the same as the supplied parameter */ bool operator ==(const Media& obj) const; /** * Calls the operator== and returns its inverse * * @param media Media object to compare to * @returns true if the current instance is different to the supplied parameter */ bool operator !=(const Media& media) const; virtual Media * createCopy(void) const = 0; /** * Reads in values of the instance variables from the console * * @returns 0 if the read worked, -1 if it failed */ virtual int read(void); /** * Reads in values of the instance variables from a tokeniser object * * @param tok Tokeniser object * @returns 0 if the read worked, -1 if it failed */ virtual int read(Tokeniser& tok); /** * Writes instance variables to the console * * @returns 0 if the write worked, -1 if it failed */ virtual int write(void) const; /** * Writes instance variables to a file stream * * @param outf Output file stream * @returns 0 if the write worked, -1 if it failed */ virtual int write(ofstream& outf) const = 0;
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
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); }
•
•
Join Date: Jul 2006
Posts: 65
Reputation:
Solved Threads: 14
Entry *entry;
entry = new Entry(media, entertainment); "What are the roots that clutch, what branches grow
out of this stony rubbish?"
out of this stony rubbish?"
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
•
•
•
•
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)Entry *entry; entry = new Entry(media, entertainment);
•
•
Join Date: Jul 2005
Posts: 1,758
Reputation:
Solved Threads: 283
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.
However, you can declare a pointer to abstract class and use that to demonstrate polymorphism.
In your case I think you need something like this, though:
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.
C Syntax (Toggle Plain Text)
struct A { int data; virtual void createCopy(const A & rhs) const = 0; }; A a; //error, cannot instantiate an object of type A since A is an abstract data type. class B { A a; //error, cannot instantiate an object of type A even as a data member };
However, you can declare a pointer to abstract class and use that to demonstrate polymorphism.
C Syntax (Toggle Plain Text)
class C : public A { C() : data(0) {} C(int i) : data(i) {} void createCopy(const A & rhs) { data = rhs.data}; }; int main() { A * pA; //this is a pointer to type A, not an A object. This is legal. C c(5); C c1; pA = &c1; 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 makes the point. }
C Syntax (Toggle Plain Text)
class A { int data; public: A(const A & rhs) { createCopy(const A & rhs); } void getData() { return data}; void createCopy(const A & rhs) {data = rhs.getData();} }; class B { //basically same a class A, don't make A or B abstract }; class C { A a; B b; public: A(const C & rhs) { if(isValid(rhs)) { a.createCopy(rhs); b.createCopy(rhs); } } bool isValid(const C & rhs) { //whatever}; };
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.
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
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(); } }
![]() |
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'
Views: 3072 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for C
#include * ansi array arrays asterisks binarysearch calculate changingto char cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkwinlinux hacking histogram homework inches include incrementoperators input iso kernel keyboard km lazy linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue mysql number opendocumentformat opensource overwrite owf pattern pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string structures student systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






