Why my program that uses increment operators doesn't work?

My goal is to create two objects, use the default constructor on one and instantiate the other with 8. Call the increment operator on each and print their values. Then assign the second to the first and print its values.
But after I write it the output is:
The radius of the first circle is: 5
And the radius of the second circle is: 9
The radius of the first circle is: 0
And the radius of the second circle is: 0
The radius of the first circle is: 0
And the radius of the second circle is: 0


The code is:
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
#include <iostream>
using namespace std;

class SimpleCircle
{
	public:
	SimpleCircle();
	SimpleCircle( int );
	SimpleCircle (const SimpleCircle & );
	~SimpleCircle() {}

	void SetRadius( int ) ;
	int GetRadius() const ;

	const SimpleCircle& operator++();
	const SimpleCircle operator++( int );
	SimpleCircle& operator= (const SimpleCircle &);
	
	private:
	int *itsRadius;
};

SimpleCircle::SimpleCircle()
{
	itsRadius = new int(5);
}


SimpleCircle::SimpleCircle( int radius )
{
	itsRadius = new int(radius);
}


SimpleCircle::SimpleCircle( const SimpleCircle & rhs)
{
	int val = rhs.GetRadius();
	itsRadius = new int(val);
}

SimpleCircle& SimpleCircle::operator= (const SimpleCircle & rhs )
{
	if (this == &rhs)
	return *this;
	*itsRadius = rhs.GetRadius();
	return *this;
}


const SimpleCircle& SimpleCircle::operator++()
{
	++(itsRadius);
	return *this;
}


const SimpleCircle SimpleCircle::operator++(int)
{
	SimpleCircle temp(*this);
	++(itsRadius);
	return temp;
	
}


int SimpleCircle::GetRadius() const
{
	 return *itsRadius; 
}

int main()
{	
	SimpleCircle CircleOne, CircleTwo(9);
	cout << "The radius of the first circle is: " << CircleOne.GetRadius()  << endl;
	cout << "And the radius of the second circle is: " << CircleTwo.GetRadius()  << endl;
	CircleOne++;
	++CircleTwo;
	cout << "The radius of the first circle is: " << CircleOne.GetRadius()  << endl;
	cout << "And the radius of the second circle is: " << CircleTwo.GetRadius()  << endl;
	CircleOne = CircleTwo;
	cout << "The radius of the first circle is: " << CircleOne.GetRadius() << endl;
	cout << "And the radius of the second circle is: " << CircleTwo.GetRadius()  << endl;

	return 0;

}

What am I doing wrong? I'm using Ubuntu 10.04 Linux and g++ to run this.
you're incrementing the POINTER, not incrementing what the pointer points to. Basically you're not incrementing the radius, you're making your pointer point to something that isn't the radius.

The proper way to increment would be ++(*itsRadius)

Although, there's really no reason at all to use a pointer here. Why not just have your radius as an int? It would save you a lot of hassle, uses less memory, and is faster.
Great, that worked. Thanks a lot! You the man :)
Topic archived. No new replies allowed.