Contain instance of Derived Class in instance of Base Class

Started by
11 comments, last by SiCrane 11 years, 4 months ago
This is the example code they gave in the problem that should work if I implement the class structure properly:

There are supposed to be three class definitions:
-BaseClass
-DerivedClass1 which inherits BaseClass
-DerivedClass2 which inherits BaseClass

And here is some sample code that utilizes these classes and calls some AddChild and RemoveChild methods I need to write.


//create the main object
BaseClass* testParentClass = new DerivedClass1(3, 4);
//create child objects
BaseClass* testChildClass1 = new DerivedClass2("john", 1, 2);
BaseClass* testChildClass2 = new DerivedClass2("mark", 7, 5);
//add child objects
testParentClass->AddChild(testChildClass1);
testParentClass->AddChild(testChildClass2);
testParentClass->RemoveChild(testChildClass1);


And though I understand everything else, I have no idea how to approach this part. At first I thought you'd just have an array of type BaseClass within the definition of BaseClass, but the way the example code add's children isn't as if it's an array at all. It references the added children by name. I can't think of any way this could work.

At no point is anything referenced with an index. Not during the add or a remove. So isn't this something different from an array or vector?
Advertisement
If you have a vector of BaseClass pointers, you can add to the vector without needing an array index just by using push_back(). You can remove an element that you know the pointer value for by finding the pointer value in the vector, for example by using find(), and calling erase() on that element (or using swap and pop if order isn't important).
Have vector of BaseClass in BaseClass and add elements to it via push_back() when AddChild function is called;
For the remove part, an gonna say just this. for(...){ if(name == name){remove}; }

Tho by looking at it, it seems values passed to constructor are used for something, and you gave no info about that.

If this problem is from "internet", could you share a link to it?
Not from the net. Just working with someone who is giving me problems to figure out.

I have this problem solved though. What you guys suggested worked perfectly but I ended up doing my own way (with a linked list)

Not because I think that's better, but just because I wanted to learn linked lists at the same time. Two birds one stone.
This code crashes on the 'list->prev = elem;' line and I can't figure out why. This linked list implementation is my own throwing together based on several tutorials I've seen around the web.. so it's undoubtedly horrible.


#include <iostream>
using namespace std;
template <class T>
struct Node
{
Node* prev;
Node* next;
T data;
};
class BaseClass
{
public:
int x, y;
Node<BaseClass*>* list;
void AddChild(BaseClass* obj)
{
Node<BaseClass*>*elem = new Node<BaseClass*>;
elem->prev = NULL;
elem->next = list;
elem->data = obj;
//crash occurs here
list->prev = elem;
list = elem;
}
void RemoveChild(BaseClass* obj)
{
for(Node<BaseClass*>*elem = list; elem != NULL; elem=elem->next)
{
if(elem->data = obj)
{
Node<BaseClass*>*prev = elem->prev;
Node<BaseClass*>*next = elem->next;
if(prev != NULL) prev->next = next;
if(next != NULL) next->prev = prev;
delete elem;
if(prev == NULL && next == NULL) list = NULL;
}
}
}
};
class DerivedClass: public BaseClass
{
public:
int memberValue;
DerivedClass(int a, int b)
{
x = a;
y = b;
}
};
int main(int argc, char* argv[])
{
BaseClass* testBaseClass = new DerivedClass(3, 4);
cout << testBaseClass->y << endl;
BaseClass* testDerivedClass = new DerivedClass(7, 8);
testBaseClass->AddChild(testDerivedClass);
}
You never initialize list, so you're trying to write to a random memory location. You also never check to see if list is valid, but that's something you can't do unless you initialize it first.
How would I go about initializing it?

I tried doing it manually by making a Node with prev = NULL and next = NULL and setting the list equal to this manually created node in main, but it didn't change anything about the behavior.
The constructor for the class is the usual place to initialize member variables.
It's still hanging on the final line of main (the line that tries to RemoveChild)


#include <iostream>
using namespace std;
template <class T>
struct Node
{
Node* prev;
Node* next;
T data;
};
class BaseClass
{
public:
int x, y;
Node<BaseClass*>* list;
void AddChild(BaseClass* obj)
{
Node<BaseClass*>*elem = new Node<BaseClass*>;
elem->prev = NULL;
elem->next = list;
elem->data = obj;
list->prev = elem;
list = elem;
}
void RemoveChild(BaseClass* obj)
{
for(Node<BaseClass*>*elem = list; elem != NULL; elem=elem->next)
{
if(elem->data = obj)
{
Node<BaseClass*>*prev = elem->prev;
Node<BaseClass*>*next = elem->next;
if(prev != NULL) prev->next = next;
if(next != NULL) next->prev = prev;
delete elem;
if(prev == NULL && next == NULL) list = NULL;
}
}
}
BaseClass()
{
Node<BaseClass*>*root = new Node<BaseClass*>;
root->next = NULL;
root->next = NULL;
list = root;
}

};
class DerivedClass: public BaseClass
{
public:
int memberValue;
DerivedClass(int a, int b)
{
x = a;
y = b;
}
};
int main(int argc, char* argv[])
{
BaseClass* testBaseClass = new DerivedClass(3, 4);
cout << testBaseClass->y << endl;
BaseClass* testDerivedClass = new DerivedClass(7, 8);
testBaseClass->AddChild(testDerivedClass);
cout << testBaseClass->list->data->x;
BaseClass* testDerivedClass2 = new DerivedClass(5,13);
testBaseClass->AddChild(testDerivedClass2);
cout << testBaseClass->list->next->data->x;
testBaseClass->RemoveChild(testDerivedClass2);
}
Your code has multiple problems (hint: turn the warning level on your compiler higher), but the one causing your program to crash is probably the fact that when you delete a node your code keeps on going through the loop. This is bad because you just rendered your loop variable invalid.

This topic is closed to new replies.

Advertisement