In the beginning of this chapter, I mentioned you should never use class variables directly, outside a class. You’ve just learned how to alter a variable through a method with an argument, but what if you wanted to retrieve the value of the variable? The answer is simple. Use the method result:
class movingCircle {
Circle c;
Vec2 speed;
5 void init(float r, Vec2 pos, Vec2 speed) {
c.r = r ;
c.pos = pos ;
this->speed = speed;
}
10
float getRadius() {
return c.r;
}
}
15
// somewhere in your application
movingCircle mc;
mc.init(0.1, p, Vec2(0.3, -0.5));
float radius = mc.getRadius();
At the end of this example, the float radius will have a value of 0.1. This is how you create a method with a result: