cybershock13045
n00b
- Joined
- Nov 10, 2007
- Messages
- 9
I have an assignment that i just cannot figure out
Consider the following class definition, in a file called "CS240.h":
#define MAX_STUDENTS 70;
class CS240 {
private:
int numStudents;
Student students[MAX_STUDENTS];
public:
CS240();
CS240(int num_students);
};
Now consider the following declaration statement, which creates a single instance of the CS240 class.
CS240 rocks;
a) Write the simplest C++ program you can write, which includes this declaration statement and causes the "rocks" object to be placed on the heap. If the rocks variable, declared this way, would never be placed on the heap, explain why not.
I know that to put an object like this on the heap you need a new constructor such as cs240 rocks = new cs240(); for the default constructor to be made. My professor assures me that there is some roundabout way of doing this by creating a new class but all i can think of is maybe if the new class i create can take the cs240 rocks declaration and manipulate it into cs240 rocks = new cs240(); which would only return a pointer to a cs240 object, but you need to declare an object as new in order to put it in the heap. can anyone suggest any roundabout ways of doing this that would use another class and create the cs240 rocks object on the heap while still having the cs240 rocks declaration?
thanks much for any help!
Consider the following class definition, in a file called "CS240.h":
#define MAX_STUDENTS 70;
class CS240 {
private:
int numStudents;
Student students[MAX_STUDENTS];
public:
CS240();
CS240(int num_students);
};
Now consider the following declaration statement, which creates a single instance of the CS240 class.
CS240 rocks;
a) Write the simplest C++ program you can write, which includes this declaration statement and causes the "rocks" object to be placed on the heap. If the rocks variable, declared this way, would never be placed on the heap, explain why not.
I know that to put an object like this on the heap you need a new constructor such as cs240 rocks = new cs240(); for the default constructor to be made. My professor assures me that there is some roundabout way of doing this by creating a new class but all i can think of is maybe if the new class i create can take the cs240 rocks declaration and manipulate it into cs240 rocks = new cs240(); which would only return a pointer to a cs240 object, but you need to declare an object as new in order to put it in the heap. can anyone suggest any roundabout ways of doing this that would use another class and create the cs240 rocks object on the heap while still having the cs240 rocks declaration?
thanks much for any help!