issue with "this" pointer

Hello, I have created the following program to find the area of a circle using the this pointer for an object.
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
#include <iostream>
#include <cmath>
using namespace std;

class Circle {
private:
	double radius;
	double area;
public:
	void calcArea() {
		area = 3.14 * pow(radius, 2);
	}
	double getArea() {
		this->calcArea();
		return area;
	}
	void setArea(double a) {
		area = a;
	}
};

int main() {
	Circle someCircle;
	someCircle.setArea(3.28);
	cout << someCircle.getArea() << endl;
	someCircle.setArea(4.2);
	cout << someCircle.getArea() << endl;
	return 0;
}


Obviously, you would never have a separate function for calculating the area and getting it, I was just trying to figure out how to use the this pointer. Anyway, I don't what I am doing wrong. Instead of getting the expected output, I just get this:

0
0


Help is appreciated. Thank you!
Wouldn't you want it to call a "calcRadius" after you call setArea? a "getter" function should not change any of the member variables, which is why you might usually see "const" at the end of a getter function.

Besides there's no point in your calling calcArea in your getArea function, it does nothing since area has already been calculated and stored into a variable.

The main problem seems to be that you never initialize radius, so your calcArea function gets messed up. You would have to update the radius when you call setArea.
______________________________________________
Edit: To answer your question about the this pointer, it is completely optional and does the same thing whether or not you have it.

I also would look into constructors so that you could just do something like Circle someCircle(100.0); and set the initial radius in the constructor as well.
Last edited on
@Ganado

Well, this isn't well programmed, I just did it this way to try to get the *this pointer to work. As far as I can tell, not initializing radius shouldn't make a difference because the default constructor should initialize everything to 0, and then the area should be set before it is used, so that shouldn't matter.

Edit (to respond to your edit): My problem is that the this pointer isn't working because I am doing something wrong. I am familiar with constructors, I simply programmed this as an example to learn about the this pointer.
Last edited on
not initializing radius shouldn't make a difference because the default constructor should initialize everything to 0


No... it doesn't. If you do not intialiaze vars, you get random garbage. You just happen to be getting 0 this time, it does not mean you will always get 0.

and then the area should be set before it is used, so that shouldn't matter.


If the radius is 0, then the area is going to be zero:

area = 3.14 * pow(radius, 2); <- if radius=0, that formula will result in 0

Since you never set radius to anything else... your area is zero.

The function is working exactly how you intended it to. Genado is correct... the only problem here is that you never give the circle a radius.




Also.. don't use pow to square stuff. It's like driving a nail with a sledgehammer. Just square it:

 
area = 3.14 * radius * radius;
I'm not sure about if an implicit default constructor that the compiler makes always initializes it to 0. I would not rely on this behavior as it probably isn't standard, but I could be wrong.

Even if it does initialize radius to 0, you never call anything that would re-calculate the radius once you call setArea. It's always better to define your own constructors.

Edit: in case that isn't clear
1
2
3
4
5
6
//radius is currently undefined, but let's say it's 0.
area = 3.14 * pow(radius, 2);
//area = 3.14 * pow(0, 2);
//area = 0

/// area will be zero no matter what 

When you call getArea, it erroneously re-calculates the area by calling getArea().
Last edited on
Oh... I see now. Sorry, I think I programmed a setArea function instead of a setRadius function, and that was my issue.
Topic archived. No new replies allowed.