Let's clarify something here. The variable names loc1 and loc2 are used in 2 different contexts with 3 completely different meanings.
In the inherited main() function, they are pointers to Location objects. In @Ganado's most recent post, they are objects of type Location (not pointers).
However, in your computeDistance function, they are float values that are actually differences between the x (or y) coordinates of the two locations. These are the variables that I think should be renamed.
I'm a serious c++ noob so I'm definitely making stupid mistakes. |
Understood. But you are letting your noobness cloud your thinking about the problem. This program just does simple algebra in a 2-dimensional space.
In 2D math, each point has an X value and a Y value. Together, (X, Y) represents a point in space. You are creating a class "Location" to capture this concept. Each Location object contains an X value and a Y value, thus representing a point in 2D space.
When you construct a Location (a point), you give it initial values. So,
Location(10, 10)
means you want a point at coordinate (10, 10). However, you need to write the code that assigns these values. Your suggested code
1 2 3 4 5
|
Location::Location(int, int)
{
x = 10;
y = 89;
}
| |
always stores the values 10 and 89, so all Locations will be identical. That's why you need to capture the arguments with names and use them in the constructor body like @Ganado suggested and I previously showed you.
In 2D math, the distance between 2 points is sqrt(dx^2 + dy^2). But in order to get dx and dy, you need 2 points. Your computeDistance function is a member of the Location class, so when you call it, the object you call it on is already known. The argument passed in is the 2nd Location. So, with the call
loc1.computeDistance(loc2)
, [or, with pointers,
loc1->computeDistance(*loc2)
], you are calculating the distance between loc1 (the object on which you are calling the function) and loc2 (the argument).
The key is to understand the math that you are trying do do and then figure out how to put it into code. If you can't clearly understand the underlying math, you will end up confused and guessing.