D
Darren Grant
Hi there,
I'm learning C++, and just wrote a 2D point class. Would some kind soul
be able to look over my code and give me any constructive criticism they
can think of?
#ifndef POINT_H
#define POINT_H
#include <iostream>
class Point {
friend bool operator==(Point&, Point&);
friend bool operator!=(Point&, Point&);
friend Point operator-(const Point& l, const Point& r);
friend Point operator+(const Point& l, const Point& r);
friend Point operator*(const Point& l, double r);
friend Point operator/(const Point& l, double r);
public:
Point() : _x(0), _y(0) { };
Point(double x, double y) : _x(x), _y(y) { };
void X(double x) { _x = x; } double X() const { return _x; }
void Y(double y) { _y = y; } double Y() const { return _y; }
Point& operator+=(const Point& p);
Point& operator-=(const Point& p);
double dot(const Point&) const;
double norm(void) const;
Point normal(void) const;
Point proj(const Point&) const;
private:
double _x, _y;
};
std:stream& operator<<(std:stream&, const Point&);
#endif
#include <math.h>
#include "Point.h"
std:stream& operator<<(std:stream& os, const Point& s) {
os << "(" << s.X() << "," << s.Y() << ")";
return os;
}
bool operator==(Point& p1, Point& p2) {
return (p1._x == p2._x && p1._y == p2._y);
}
bool operator!=(Point& p1, Point& p2) {
return (p1._x != p2._x || p1._y != p2._y);
}
Point operator+(const Point& l, const Point& r) {
Point p = l;
p += r;
return p;
}
Point operator-(const Point& l, const Point& r) {
Point p = l;
p -= r;
return p;
}
Point operator*(const Point &l, double r) {
Point p = l;
p._x *= r;
p._y *= r;
return p;
}
Point operator/(const Point &l, double r) {
Point p = l;
p._x /= r;
p._y /= r;
return p;
}
Point& Point:perator+=(const Point& p) {
_x += p._x;
_y += p._y;
return *this;
}
Point& Point:perator-=(const Point& p) {
_x -= p._x;
_y -= p._y;
return *this;
}
double Point::dot(const Point& op) const {
return _x * op.X() + _y * op.Y();
}
double Point::norm(void) const {
return sqrt(_x * _x + _y * _y);
}
Point Point::normal(void) const {
return *this / norm();
}
Point Point:roj(const Point& u) const {
return (*this * u.dot(*this)) / (norm() * norm());
}
Many thanks,
Darren Grant
I'm learning C++, and just wrote a 2D point class. Would some kind soul
be able to look over my code and give me any constructive criticism they
can think of?
#ifndef POINT_H
#define POINT_H
#include <iostream>
class Point {
friend bool operator==(Point&, Point&);
friend bool operator!=(Point&, Point&);
friend Point operator-(const Point& l, const Point& r);
friend Point operator+(const Point& l, const Point& r);
friend Point operator*(const Point& l, double r);
friend Point operator/(const Point& l, double r);
public:
Point() : _x(0), _y(0) { };
Point(double x, double y) : _x(x), _y(y) { };
void X(double x) { _x = x; } double X() const { return _x; }
void Y(double y) { _y = y; } double Y() const { return _y; }
Point& operator+=(const Point& p);
Point& operator-=(const Point& p);
double dot(const Point&) const;
double norm(void) const;
Point normal(void) const;
Point proj(const Point&) const;
private:
double _x, _y;
};
std:stream& operator<<(std:stream&, const Point&);
#endif
#include <math.h>
#include "Point.h"
std:stream& operator<<(std:stream& os, const Point& s) {
os << "(" << s.X() << "," << s.Y() << ")";
return os;
}
bool operator==(Point& p1, Point& p2) {
return (p1._x == p2._x && p1._y == p2._y);
}
bool operator!=(Point& p1, Point& p2) {
return (p1._x != p2._x || p1._y != p2._y);
}
Point operator+(const Point& l, const Point& r) {
Point p = l;
p += r;
return p;
}
Point operator-(const Point& l, const Point& r) {
Point p = l;
p -= r;
return p;
}
Point operator*(const Point &l, double r) {
Point p = l;
p._x *= r;
p._y *= r;
return p;
}
Point operator/(const Point &l, double r) {
Point p = l;
p._x /= r;
p._y /= r;
return p;
}
Point& Point:perator+=(const Point& p) {
_x += p._x;
_y += p._y;
return *this;
}
Point& Point:perator-=(const Point& p) {
_x -= p._x;
_y -= p._y;
return *this;
}
double Point::dot(const Point& op) const {
return _x * op.X() + _y * op.Y();
}
double Point::norm(void) const {
return sqrt(_x * _x + _y * _y);
}
Point Point::normal(void) const {
return *this / norm();
}
Point Point:roj(const Point& u) const {
return (*this * u.dot(*this)) / (norm() * norm());
}
Many thanks,
Darren Grant