sashomaga

C++

Feb 21st, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #ifndef _POINT_H
  2. #define _POINT_H
  3.  
  4. #include <iostream>
  5.  
  6. class Point {
  7.  
  8. private:
  9.  
  10.     /**
  11.      * x coord
  12.      */
  13.     double x;
  14.  
  15.     /**
  16.      * coord y
  17.      */
  18.     double y;
  19.  
  20. public:
  21.  
  22.  
  23.     Point():x(0),y(0)
  24.     {
  25.     }
  26.  
  27.     Point(double x , double y);
  28.  
  29.     //copy constructor
  30.     Point(const Point &p);
  31.  
  32.     ~Point();
  33.  
  34.     double getX();
  35.  
  36.     void setX(double _x);
  37.  
  38.     double getY();
  39.  
  40.     void setY(double _y);
  41.  
  42.     friend Point operator+(const Point &p1, const Point &p2);
  43.  
  44.     friend Point operator-(const Point &p1, const Point &p2);
  45.  
  46.     friend std::ostream &operator<<( std::ostream &output, const Point &p );
  47.  
  48.  
  49. };
  50. #endif
  51.  
  52. #include <iostream>
  53. #include "Point.h"
  54.  
  55. using namespace std;
  56.  
  57. Point::Point(double _x, double _y)
  58. {
  59.     x = _x;
  60.     y = _y;
  61. }
  62.  
  63. Point::Point(const Point &p){
  64.     x = p.x;
  65.     y = p.y;
  66.     cout << "copy constructor" << endl;
  67. }
  68.  
  69. Point::~Point()
  70. {
  71. }
  72.  
  73. double Point::getX(){
  74.     return x;
  75. }
  76.  
  77. void Point::setX(double _x){
  78.     x = _x;
  79. }
  80.  
  81. double Point::getY(){
  82.     return y;
  83. }
  84.  
  85. void Point::setY(double _y){
  86.     y = _y;
  87. }
  88.  
  89. Point operator+(const Point &p1, const Point &p2)
  90. {
  91.     return Point(p1.x + p2.x, p1.y + p2.y);
  92. }
  93.  
  94. Point operator-(const Point &p1, const Point &p2)
  95. {
  96.     return Point(p1.x - p2.x, p1.y - p2.y);
  97. }
  98.  
  99. ostream& operator<<(ostream &output, const Point &p)
  100. {
  101.   output << "Point coords x: " << p.x << " y: " << p.y;
  102.   return output;
  103. }
  104.  
  105. /* test pass object to function */
  106.  
  107. void test_value(Point p)
  108. {
  109.     p.setY(100);
  110.     cout << p << endl;
  111. }
  112.  
  113. void test_pointer(Point * p)
  114. {
  115.     p->setY(1000);
  116. }
  117.  
  118. void test_reference(Point & p)
  119. {
  120.     p.setY(200);
  121. }
  122.  
  123. int main()
  124. {
  125. /* operator overload */
  126.     cout << endl << "OPERATOR OVERLOAD" << endl << endl;
  127.     Point test_add = p1 + p2;
  128.     cout << test_add << endl;
  129.  
  130.     Point test_subtract = p1 - p2;
  131.     cout << test_subtract << endl;
  132.  
  133.     cout << test_subtract << endl;
  134.  
  135.     Point copy = test_add;
  136.  
  137.     cout << test_add << endl;
  138.     test_value(test_add);
  139.     cout << test_add << endl;
  140.  
  141.     Point* pointer = &test_add;
  142.     test_pointer(pointer);
  143.     cout << test_add << endl;
  144.  
  145.     test_reference(test_add);  
  146.     cout << test_add << endl;
  147.  
  148.     return 0;  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment