Friday, July 31, 2009

How to use new operater in a C++ class?

Does anyone know how to use and implement the new operator in a class? I have a test on this in 8 hours and I barely understand classes. I understand pointers a little better. Can someone give me a quick simple demo? Basically my test is going to give me a class and I will have to insert a new operator line to get it to work, it will most likely deal with arrays. I need to pass this test to pass the class, I'm desperate. I use visual C++ 2008, just anything that can compile.

How to use new operater in a C++ class?
The 'new' operator is utilized when you want to declare a dynamic variable. This variable does not have a name; it can only be accessed through the pointer that you declared it with. For example, in your 'main' function you might write:





int *p, *q; // Declare p and q are each int pointers.


p = new int; // Declare that p points to an unnamed variable of type int.


q = new int[10]; // Declare that q points to an unnamed array of 10 variables of type int.





Before the dynamic variable goes out of scope, you need to write:


delete p; // Free the memory for the unnamed int pointed to by p.


delete [ ] q; // Free the memory for the unnamed int array pointed to by q.











Suppose you have already declared and defined a Distance class. In your 'main' function you might write:





Distance *d1, *d2; // Declare d1 and d2 are each Distance pointers.


d1 = new Distance; // Declare that d1 points to an unnamed object of type Distance.


d2 = new Distance[10]; // Declare that d2 points to an unnamed array of 10 objects of type Distance.





Suppose that 'getDistance()' is one of the public member functions that you declared in your definition of the Distance class. To CALL that function (from your 'main' function) for each successive Distance object in the array, you might write:


for (int i=0; i%26lt;10; i++)


d2[i].getDistance();





Before the dynamic variable goes out of scope, you need to write:


delete d1; // Free the memory for the unnamed Distance object pointed to by d1.


delete [ ] d2; // Free the memory for the unnamed Distance array pointed to by d2.








A separate issue: You can declare dynamic variables inside a class. Suppose your Distance class has only pointers as private data members:


class Distance


{


private:


int *ft; // pointer to int


float *in; // pointer to float


public:


Distance(); // constructor


// Declare your other class member functions here:


};





// Define your class constructor:


Distance::Distance()


{ feet = new int; // Declare an unnamed int pointed to by the pointer 'feet'.


*feet = 0; // Initialize variable pointed to by 'feet' to value of zero.


inches = new float; // Declare an unnamed float pointed to by the pointer 'inches'.


*inch = 0.0; // Initialize variable pointed to by 'inches' to value of zero.


}





Or instead, you could declare a Distance class that has a constructor in which the pointers declare an array of 5 ints pointed to by 'feet' and an array of 5 floats pointed to by 'inches'. In that case you would initialize each of the array members using a for loop.





Distance::Distance()


{


feet = new int[5];


inches = new float[5];


for (int i=0; i%26lt;5; i++)


{ feet[i] = 0; inches[i] = 0.0; }


}


No comments:

Post a Comment