| | |
Dynamic Allocating memory
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2006
Posts: 6
Reputation:
Solved Threads: 0
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;
}
//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;
}
It is not proper C++, have animal as a base class and derive dog, cat, man from the base class animal
![]() |
Similar Threads
- Safe Array (C++)
- Dynamic Mmeory Allocation with 2D Arrays (C++)
- regarding dynamic memory allocation (C)
- still available even after freeing dynamic allocated memory (C)
- Another question regarding memory (C)
- Why use Dynamic and Static Memory Allocation... (C)
- query: allocating memory to nested linked lists (C)
Other Threads in the C++ Forum
- Previous Thread: homework help
- Next Thread: problem to convert string to integer using Dev-c++
Views: 1642 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






