Jump to content

  • Log In with Google      Sign In   
  • Create Account


Contain instance of Derived Class in instance of Base Class


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 sooner123   Members   -  Reputation: 241

Like
0Likes
Like

Posted 01 December 2012 - 10:06 AM

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?

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9126

Like
0Likes
Like

Posted 01 December 2012 - 10:45 AM

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).

#3 BaneTrapper   Members   -  Reputation: 945

Like
1Likes
Like

Posted 01 December 2012 - 11:06 AM

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, 01 December 2012 - 02:37 PM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#4 sooner123   Members   -  Reputation: 241

Like
0Likes
Like

Posted 01 December 2012 - 06:44 PM

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.

#5 sooner123   Members   -  Reputation: 241

Like
0Likes
Like

Posted 01 December 2012 - 06:54 PM

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);
}


#6 SiCrane   Moderators   -  Reputation: 9126

Like
1Likes
Like

Posted 01 December 2012 - 07:22 PM

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.

#7 sooner123   Members   -  Reputation: 241

Like
0Likes
Like

Posted 01 December 2012 - 08:04 PM

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.

#8 SiCrane   Moderators   -  Reputation: 9126

Like
0Likes
Like

Posted 01 December 2012 - 08:06 PM

The constructor for the class is the usual place to initialize member variables.

#9 sooner123   Members   -  Reputation: 241

Like
0Likes
Like

Posted 01 December 2012 - 08:12 PM

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);
}


#10 SiCrane   Moderators   -  Reputation: 9126

Like
1Likes
Like

Posted 01 December 2012 - 08:26 PM

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.

#11 sooner123   Members   -  Reputation: 241

Like
0Likes
Like

Posted 01 December 2012 - 10:45 PM

Yikes I was using a single equals in the RemoveChild as well.

#12 sooner123   Members   -  Reputation: 241

Like
1Likes
Like

Posted 02 December 2012 - 02:57 AM

This is the current state. So far it's passing all my tests. Thanks for the help.

#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();
}


#13 SiCrane   Moderators   -  Reputation: 9126

Like
0Likes
Like

Posted 02 December 2012 - 09:02 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS