Reputation: 555
I'm trying to create a small class that creates "dots" and calculates the distance between them. I first create two "dot"-objects and then pass the second one as an argument to a function contained in the first dot. This gives me access to the coordinates of the second dot, however I don't really get how I'm supposed to get the coordinates of the first dot - the one that receives the second dot as an arg. I figured it would look something like this, but apparently not:
class Punkt{ //"Punkt" = swedish for "dot"
private:
int x, y;
public:
//Konstruktor
Punkt(int firstX, int firstY): x(firstX), y(firstY){};
//Setters and getters
int getX(){return x;}
void setX(int newX){x = newX;}
int getY(){return y;}
void setY(int newY){y = newY;}
//Avstånd till annan punkt
double distance(Punkt annanPunkt);
};
#include "punkt.h"
#include <cmath>
double distance(Punkt annanPunkt){ //"annanpunkt" = "other dot"
return sqrt(pow((annanPunkt.getX() - self.getX), 2) +
pow((annanPunkt.getY() - self.getY, 2))
}
Upvotes: 1
Views: 6749
Reputation: 227478
First of all, you have declared distance
as a member function, so its definition needs to be in the Punkt
scope:
double Punkt::distance(Punkt annanPunkt){
Second, you probably don't need a copy of the argument. Pass a const
reference:
double Punkt::distance(const Punkt& annanPunkt){
As for accessing your own data members in the function, you can access them by name (x
or y
):
annanPunkt.getX() - x), 2)
Alternatively, this->x
, getX()
, this->getX()
etc. I find using this
everywhere too verbose.
Note that you will need to make your getters const
though:
int getX() const {return x;}
or remove them entirely: if you have no invariants to maintain, it may make sense to use public data members.
Note: I would probably make distance
a non-member function:
double distance(const Punkt& a, const Punkt& b);
Upvotes: 5
Reputation: 254631
First, the function definition needs to qualify the name, to indicate that it's defining the member function rather than a separate non-member function:
double Punkt::distance(Punkt annanPunkt)
^^^^^^^
Within the function, members can be accessed directly (e.g. getX
), or via this
(e.g. this->getX
). Note that this
is a pointer, hence ->
rather than .
getX
is a function, so you'll need to add ()
to call it and get the value.
Finally, you need to take care that the parantheses are in the right place, and end the statement with ;
Putting it all together, this should work:
double Punkt::distance(Punkt annanPunkt){
return sqrt(pow(annanPunkt.getX() - this->getX(), 2) +
pow(annanPunkt.getY() - this->getY(), 2));
}
You can remove this->
, or even access x
and y
directly, if you like brevity.
Upvotes: 1
Reputation: 21003
C++ does not have self
but this
, and in methods you can refer to members without any additional syntax, so you can use:
double distance(Punkt annanPunkt){ //"annanpunkt" = "other dot"
return sqrt(pow((annanPunkt.getX() - getX()), 2) +
pow((annanPunkt.getY() - getY(), 2))
}
Upvotes: 0
Reputation: 29744
In C++ in class member function you have access to all members of a class:
double Punkt::distance( Punkt annanPunkt) {
// ^ note: distance is member function of Punkt class
return sqrt( pow( ( annanPunkt.getX() - x), 2) +
// ^ this object x coordinate
pow( ( annanPunkt.getY() - y, 2))
// ^ this object y coordinate
}
Upvotes: 0