Need help with difficult copy constructor for derived class
May 11, 2014 at 3:37am UTC
I have been looking at examples of copy constructors for derived classes but I really don't understand how I'm supposed write this one. I have three classes LinkList, CD, and Media. I have written copy constructors for Media and LinkList but not CD which is a derived class of Media and has a LinkList for its member variable. Please any help with this would be greatly appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
template <class T>
class LinkList
{
private :
struct ListNode
{
T value1;
struct ListNode *next;
};
ListNode *head; // List head pointer
public :
//***Constructor***
LinkList();
LinkList(const LinkList<T> &);
//***Destructor***
~LinkList();
//***LinkList Operations***
//....operation functions
};
//***Constructor***
template <class T>
LinkList<T>::LinkList()
{
head = NULL;
}
//***Copy Constructor***
template <class T>
LinkList<T>::LinkList( const LinkList &listObj )
{
head = NULL;
ListNode *nodePtr;
nodePtr = listObj.head;
while (nodePtr != NULL)
{
appendNode(nodePtr->value1);
nodePtr = nodePtr->next;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
class Media
{
private :
string title;
string length;
public :
//***Constructors***
Media();
Media(string, string);
Media(const Media &obj);
//***destructor***
~Media();
//***Mutators***
void setTitle(string);
void setLength(string);
//***Accessors***
string getTitle();
string getLength();
//Overloaded Operators
bool operator < (const Media &);
bool operator > (const Media &);
bool operator != (const Media &);
bool operator == (const Media &right);
};
/*****Implimentation*********/
//***Constructors***
Media::Media()
{
title = " " ;
length = " " ;
}
//***Constructors***
Media::Media(string t, string l)
{
title = t;
length = l;
}
//***Copy Constructor***
Media::Media(const Media &obj)
{
title = obj.title;
length = obj.length;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
//LinkList structure for CD class
struct CdContence
{
string song;
string length;
};
class CD : public Media
{
public :
LinkList<CdContence> Cd;
public :
//***Constructors***
CD(string, string);
CD();
//***destructor***
~CD();
//***Mutators***
void setCD(string, string, string, string);
//***Accessors***
LinkList<CdContence> getCD();
//Overloaded Operators
bool operator < (CD &);
bool operator > (CD &);
bool operator != (CD &);
bool operator == (CD &);
};
/*****Implimentation*********/
//***Constructors***
CD::CD(string T, string L)
{
setTitle(T);
setLength(L);
LinkList<CdContence>Cd;
cout<<"CD CONSTRUCTOR2" <<endl;
}
CD::CD() : Media()
{
LinkList<CdContence>Cd;
cout<<"CD CONSTRUCTOR" <<endl;
}
CD::CD(const CD &obj) :Media(obj)
{
//not sure what to put here since the member variable is
// a linklist
}
May 11, 2014 at 5:16am UTC
Is this right? I'm gettin' desperate here!
1 2
CD::CD(const CD &obj) :Media(obj),Cd(obj.Cd)
{}
I put it in and the program froze after I entered a data for a second CD. Please anyone...
May 11, 2014 at 7:55am UTC
Neither Media, nor CD need explicitly defined copy constructors. The defaulted versions will be adequate. As I mentioned other-thread, the copy assignment operator for
LinkList should be defined.
In the CD constructors you are defining a local-to-the-constructor
LinkList which immediately goes out of scope. This doesn't harm anything, but it certainly isn't necessary.
1 2 3 4 5
//***Constructors***
CD::CD(string T, string L) : Media(T, L)
{
cout<<"CD CONSTRUCTOR2" <<endl;
}
is the most efficient way to define this constructor.
Last edited on May 11, 2014 at 7:57am UTC
Topic archived. No new replies allowed.