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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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)
GloriousEremite
Junior Poster in Training
65 posts since Jul 2006
Reputation Points: 108
Solved Threads: 14
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.
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.
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.
}
In your case I think you need something like this, though:
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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396