Sign in to follow this  
Followers 0
sooner123

Contain instance of Derived Class in instance of Base Class

12 posts in this topic

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.

[code]
//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);
[/code]

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?
0

Share this post


Link to post
Share on other sites
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).
0

Share this post


Link to post
Share on other sites
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? Edited by BaneTrapper
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.

[code]
#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);
}
[/code]
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
The constructor for the class is the usual place to initialize member variables.
0

Share this post


Link to post
Share on other sites
It's still hanging on the final line of main (the line that tries to RemoveChild)

[code]
#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);
}
[/code]
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
This is the current state. So far it's passing all my tests. Thanks for the help.

[code]
#include <iostream>
using namespace std;
class BaseClass;
struct Node
{
Node* prev;
Node* next;
BaseClass* data;
};
class BaseClass
{
public:
int x, y;
//Node<BaseClass*>* list;
Node* list;
void AddChild(BaseClass* obj)
{
Node*elem = new Node;
elem->prev = NULL;
elem->data = obj;
if (list == NULL)
{
elem->next = NULL;
list = elem;
}
else
{
elem->next = list;
list->prev = elem;
list = elem;
}
}
void RemoveChild(BaseClass* obj)
{
for(Node*elem = list; elem != NULL; elem = elem->next)
{
if (elem->data == obj)
{
//if node to delete is head of list
if (elem->prev == NULL)
{
list = elem->next;
}
//if node to delete is tail of list
else if (elem->next == NULL)
{
elem->prev->next = NULL;
}
//if node to delete is not start or end
else
{
elem->prev->next = elem->next;
elem->next->prev = elem->prev;
}
delete elem;
return;
}
}
}
void ListChildren(int tabDepth=0)
{
Node*elem = new Node;
elem = list;
while(elem != NULL)
{
for (int i=0; i<tabDepth; i++) cout << " ";
cout << "child: " << elem->data->x << endl;
if (elem->data ->list != NULL)
{
elem->data->ListChildren(tabDepth+1);
}
elem = elem->next;
}
}
BaseClass()
{
list = NULL;
}
};
class DerivedClass: public BaseClass
{
public:
int memberValue;
DerivedClass(int a, int b)
{
x = a;
y = b;
}
};
int main()
{
BaseClass* root = new BaseClass;
BaseClass* child1 = new DerivedClass(1, 2);
root->AddChild(child1);
BaseClass* child11 = new DerivedClass(5, 6);
child1->AddChild(child11);
BaseClass* child111 = new DerivedClass(13, 14);
child11->AddChild(child111);
BaseClass* child112 = new DerivedClass(15, 16);
child11->AddChild(child112);
BaseClass* child113 = new DerivedClass(17, 18);
child11->AddChild(child113);
BaseClass* child12 = new DerivedClass(7,8);
child1->AddChild(child12);
BaseClass* child2 = new DerivedClass(3, 4);
root->AddChild(child2);
BaseClass* child21 = new DerivedClass(9, 10);
child2->AddChild(child21);
BaseClass* child22 = new DerivedClass(11, 12);
child2->AddChild(child22);
root->ListChildren();
}
[/code]
1

Share this post


Link to post
Share on other sites
There are still a few basic problems in your code. One is that you leak memory all over the place. Aside from the allocations you perform in main, when a BaseClass object is destroyed it doesn't free the list and in ListChildren() you allocate an object that you never use and then promptly leak. Bad things will also happen if you copy construct or assign your BaseClass objects; they should either be properly implemented or explicitly disabled. And since you store derived class objects in base class pointers you should have a virtual destructor.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0