Question about static members

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
using namespace std;

class myClass //define myClass
{
    private:
       int x;
       int y;
       static int 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;
}


When I run the progam this is what I get:

mClassA.x = 5
mClassA.y = 6
mClassA.sum = 11


mClassB.x = 3
mClassB.y = 4
mClassB.sum = 7


mClassC.x = 8
mClassC.y = 10
mClassC.sum = 18



Total # of myClass used = 2



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!
Last edited on
There's one kind of constructor you're forgetting to implement.

By the way, lines 44 and 45 are problematic.
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.
Last edited on
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. :-)
Topic archived. No new replies allowed.