I am new to c++ and and our teachers have started Object oriented programming.. I am facing real trouble in understanding the concepts of constructor and destructor. Why are they used and how are they used..
They are called at the time the object is created. They can be used to initialize member variables and/or allocate memory for them.
MyClass(int xin,int yin)
x = xin;
y = yin; //there's a shortcut to doing this, look up initialization lists
MyClass() //default constructor (no parameter)
x = 0;
y = 0;
//nothing brewing here
No Destructor only Deletes the memory allocation.
if i would've known the answer, i wouldn't had started the thread......
Not quite right. A "destructor", like a "constructor", is just another function (except they don't return anything). It's important that you understand the functions themselves don't actually do anything unless you tell them to in your implementation.
The significance of them is WHEN THEY EXECUTE, not really what they do. They execute automatically at object creation (constructor) and when an object goes out of scope (destructor). This, rather than what they do, is really what makes a constructor/destructor what it is. What they need to do is largely determined by this timing.
Try to approach the situation from the perspective of "What needs to be done when I create/destroy an object?" and that should help you understand better.
Oops, this may be out of date due to overlap...
Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...
I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...