Hi everyone! I'm currently trying to learn C++. I wrote a small program to get a little practice and now I have a question about static data members. Here is my code:
#include <iostream>
usingnamespace std;
class myClass //define myClass
{
private:
int x;
int y;
staticint used;//how many of the myClass objects is there
public:
myClass(); //general constructor
myClass(int,int);//constructor taking values
~myClass(); //clean up
void setX(int);//returns the x value
void setY(int);//returns the y value
int getX(); //return x value
int getY(); //return y value
int getSum(); //return x + y
int getUsed(); //retrieves how many times has the class been used
myClass operator +(myClass); //overloading the + operator
};
int myClass::used = 0; //initialize used to 0
myClass::myClass()
{
//init data members to 0 and increse used by 1
x = 0;
y = 0;
used++;
}
myClass::myClass(int a, int b)
{
// init data members to values given and increse used by 1;
x = a;
y = b;
used++;
}
myClass::~myClass()
{
//clean up x & y and decrese used by 1
delete &x;
delete &y;
used--;
}
void myClass::setX(int a)
{
x = a;
}
void myClass::setY(int a)
{
y = a;
}
int myClass::getX()
{
return x;
}
int myClass::getY()
{
return y;
}
int myClass::getSum()
{
return x + y;
}
int myClass::getUsed()
{
return used;
}
myClass myClass::operator+(myClass param)
{
//allows the myClass object to be added with another myClass object
myClass temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main()
{
myClass mClassA;
myClass mClassB(3,4);
myClass mClassC;
mClassA.setX(5);
mClassA.setY(6);
mClassC = mClassA + mClassB;
cout << "mClassA.x = " << mClassA.getX()
<< "\nmClassA.y = " << mClassA.getY()
<< "\nmClassA.sum = " << mClassA.getSum() << endl
<< mClassA.getUsed() << endl
<< "\n\nmClassB.x = " << mClassB.getX()
<< "\nmClassB.y = " << mClassB.getY()
<< "\nmClassB.sum = " << mClassB.getSum() << endl
<< mClassB.getUsed() << endl
<< "\n\nmClassC.x = " << mClassC.getX()
<< "\nmClassC.y = " << mClassC.getY()
<< "\nmClassC.sum = " << mClassC.getSum() << endl
<< mClassC.getUsed() << endl
<< "\n\n\nTotal # of myClass used = " << mClassC.getUsed()
<< endl;
cin.get();
cin.get();
return 0;
}
My question is this. If I created 3 myClass objects why is it returning that only 2 were created? Not expecting someone to give me the answer but a nudge in the right direction would be appreciated. Also any other tips or advice are welcome. Thanks in advance!
Your result is misleading:
You have neglected to take into account the copy constructor (which is used in the operator+ function) - so the used variable does not get incremented when the copy is made - BUT the destructor decrements the used count when the copy is destroyed.
The Total # of myClass used value that you print out is also misleading - it is really a
printout of the number of myClass objects still in existance at that point (which would have said
3 if you had accounted for the copy constructor) - but really 5 had been created and 2 already destroyed.
PS:
This is wrong
1 2 3 4 5 6 7
myClass::~myClass()
{
//clean up x & y and decrese used by 1
delete &x; //not correct
delete &y; //not correct
used--;
}
x and y were never allocated using the new operator.
Thanks helios, I'll go over constructors and deconstructors again. I thought that lines 44 and 45 might be a problem. I tried delete x; and delete y; but it gave me the error
type 'int' argument given to 'delete', expected pointer
. Looking back and thinking about it I can see where I went wrong. I didn't declare them as pointers so I shouldn't of used delete. Should I even worry about cleaning up x and y? I think I should just not sure how to go about it. Gives me something to think about. Any more nudges are appreciated. Thanks again.
Thanks for the insight guestgulkan. I honestly didn't even think of that. I appreciate it. I'm glad I found this site. I've learned a lot just going through the forums. Keep the nudges coming. :-)