1. Get rid of the return statements in your constructors.
2. Prefer to initialize your values in the constructors using initialization lists:
Code:
point(double v) : x(v), y(v)
{
}
3. Any functions/methods that do not modify the values within the class should be const:
Code:
// accessors
double getx() const {
return x;
}
double gety() const {
return y;
}
...
// operations
double fromorigin() const {
return sqrt(x*x + y*y);
}
double distance( point p) const {
return sqrt( (p.getx() - x) * (p.getx() -x)
+(p.gety() - y) * (p.gety() -y) );
}
4.
Originally Posted by
co8check
2)Operators == and != to compare points
Let's take a stab at one of these (it's really quite easy). Maybe this can give you a clue as to how to begin an attempt at the other.
Code:
class point
{
...
public:
...
bool operator==(const point& rhs)
{
return x == rhs.x && y == rhs.y;
}
};
5. The distance and fromorigin methods are basically the same (distance from a point either given or assumed to be (0,0)). These can be combined using a default argument to give a single distance method that will either provide the distance to a given point or, lacking that given point, the origin.
Code:
class point{
...
public:
...
double distance(const point& p = point(0,0)) const
{
return sqrt( (p.getx() - x) * (p.getx() -x) +(p.gety() - y) * (p.gety() -y) );
}
};
You can now call the single distance method as follows:
Code:
cout << "P1's distance from the origin is: " << p1.distance() << endl;
cout << "P1's distance from point P2 is: " << p1.distance(p2) << endl;
6.
Originally Posted by
co8check
5)Modification to the ostream to allow the point to print in polar form.
Here is an example of overloading the ostream operator<< for a custom class
Code:
class foo
{
...
public:
std::ostream& print(std::ostream& os) const
{
return os << /* Your specific print stuff here */;
}
};
std::ostream& operator<<( std::ostream & os, const foo& rhs)
{
return rhs.print(os);
}
Try to work some on the others and post your attempts and specific questions.
[edit]
BTW, from the Forum Guidlines:
2. Use descriptive subject lines. Do not put URGENT!, or NEED HELP NOW, etc. in your title; it will not make people look at it any faster. Doing this makes many old time helpers on this board not look at the post at all.
[/edit]