954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dynamic Allocating memory

I need some help figuring what I should do when I dynamically allocate memory to four instantces pointing to an animal class. After I allocate memory in the constructor, I must set it to "Lion", "Cat", "Dog", "Monkey". So far this is what I have written. Feedback would be great! The following code runs, however, I think I am not setting it correctly. I not quite sure?
//Animal.h
class Animal
{
private:
Animal *L;
Animal *C;
Animal *D;
Animal *M;

};
//Animal.cpp
Animal::Animal()
{
Animal *L = new Animal;
Animal *C = new Animal;
Animal *D = new Animal;
Animal *M = new Animal;
}

hellokitty88
Newbie Poster
6 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

It is not proper C++, have animal as a base class and derive dog, cat, man from the base class animal

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You