Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _POINT_H
- #define _POINT_H
- #include <iostream>
- class Point {
- private:
- /**
- * x coord
- */
- double x;
- /**
- * coord y
- */
- double y;
- public:
- Point():x(0),y(0)
- {
- }
- Point(double x , double y);
- //copy constructor
- Point(const Point &p);
- ~Point();
- double getX();
- void setX(double _x);
- double getY();
- void setY(double _y);
- friend Point operator+(const Point &p1, const Point &p2);
- friend Point operator-(const Point &p1, const Point &p2);
- friend std::ostream &operator<<( std::ostream &output, const Point &p );
- };
- #endif
- #include <iostream>
- #include "Point.h"
- using namespace std;
- Point::Point(double _x, double _y)
- {
- x = _x;
- y = _y;
- }
- Point::Point(const Point &p){
- x = p.x;
- y = p.y;
- cout << "copy constructor" << endl;
- }
- Point::~Point()
- {
- }
- double Point::getX(){
- return x;
- }
- void Point::setX(double _x){
- x = _x;
- }
- double Point::getY(){
- return y;
- }
- void Point::setY(double _y){
- y = _y;
- }
- Point operator+(const Point &p1, const Point &p2)
- {
- return Point(p1.x + p2.x, p1.y + p2.y);
- }
- Point operator-(const Point &p1, const Point &p2)
- {
- return Point(p1.x - p2.x, p1.y - p2.y);
- }
- ostream& operator<<(ostream &output, const Point &p)
- {
- output << "Point coords x: " << p.x << " y: " << p.y;
- return output;
- }
- /* test pass object to function */
- void test_value(Point p)
- {
- p.setY(100);
- cout << p << endl;
- }
- void test_pointer(Point * p)
- {
- p->setY(1000);
- }
- void test_reference(Point & p)
- {
- p.setY(200);
- }
- int main()
- {
- /* operator overload */
- cout << endl << "OPERATOR OVERLOAD" << endl << endl;
- Point test_add = p1 + p2;
- cout << test_add << endl;
- Point test_subtract = p1 - p2;
- cout << test_subtract << endl;
- cout << test_subtract << endl;
- Point copy = test_add;
- cout << test_add << endl;
- test_value(test_add);
- cout << test_add << endl;
- Point* pointer = &test_add;
- test_pointer(pointer);
- cout << test_add << endl;
- test_reference(test_add);
- cout << test_add << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment