C++ Programming: chapter 4 – operator overloading 2014, Spring Pusan National University Ki-Joune Li 1
PNU operator overloading – 방법 1 #include using namespace std; class Point{ private: float X; float Y; public: Point(float a): X(a),Y(a) {}; Point(float a, float b):X(a),Y(b) {}; Point(const Point& a); printValues() { cout << “(“<<X<<“, “<<Y<<“)\n”; } friend const Point operator+(const Point&a, const Point& b); }; int main() { Point myPoint(1.0); Point yourPoint(2.0); (myPoint+yourPoint).printValues(); return 0; } Point::Point(const Point & a) { X=a.X; Y=a.Y; } const Point operator+(const Point& a, const Point& b) { Point temp; temp.X=a.X+b.X; temp.Y=a.Y+b.Y; return temp; }
PNU operator overloading – 방법 2 #include using namespace std; class Point{ private: float X; float Y; public: Point(float a): X(a),Y(a) {}; Point(float a, float b):X(a),Y(b) {}; Point(const Point& a); float getX() { return X;} float getY() { return Y;} float setX(float x) {X=x;} float setY(float y) {Y=y;} printValues() { cout << “(“<<X<<“, “<<Y<<“)\n”; } }; const Point operator+(const Point&a, const Point& b); int main() { Point myPoint(1.0); Point yourPoint(2.0); (myPoint+yourPoint).printValues(); return 0; } Point::Point(const Point & a) { X=a.X; Y=a.Y; } const Point operator+(const Point& a, const Point& b) { Point temp; temp.setX(a.getX()+b.getX()); temp.setY(a.getY()+b.getY()); return temp; }
PNU operator overloading – 방법 3 #include using namespace std; class Point{ private: float X; float Y; public: Point(float a): X(a),Y(a) {}; Point(float a, float b):X(a),Y(b) {}; Point(const Point& a); printValues() { cout << “(“<<X<<“, “<<Y<<“)\n”; } const Point operator+(const Point& b); }; int main() { Point myPoint(1.0); Point yourPoint(2.0); (myPoint+yourPoint).printValues(); return 0; } Point::Point(const Point & a) { X=a.X; Y=a.Y; } const Point Point::operator+(const Point& b) { Point temp; temp.X=X+a.X; temp.Y=Y+b.Y; return temp; }
PNU operator overloading – 비교 5 #include using namespace std; class Point{ … }; int main() { Point myPoint(1.0); Point yourPoint(2.0); (myPoint+yourPoint).printValues(); (myPoint+5.7).printValues(); (5.7+myPoint).printValues(); return 0; } Member Function 으로의 Overloading ( 방법 3) vs 일반 Function 으로의 Overloading( 방법 1 또는 2) 주의 1.=, [ ], ->, ( ) 등의 연산자는 반드시 방법 3 으로만 가능. 2. 연산자의 우선순위는 원래의 연산자의 우선순위와 동일
PNU Const 6 #include using namespace std; class Point{ private: float X; float Y; public: Point(float a): X(a),Y(a) {}; Point(float a, float b):X(a),Y(b) {}; Point(const Point& a); float getX() const { return X;} float getY() const { return Y;} float setX(float x) {X=x;} float setY(float y) {Y=y;} const Point operator+(const Point&a, const Point& b); printValues() { cout << “(“<<X<<“, “<<Y<<“)\n”; } }; int main() { Point myPoint(1.0); Point yourPoint(2.0); (myPoint+yourPoint).printValues(); (myPoint+yourPoint).setX(5.0); // incorrect return 0; } Point::Point(const Point & a) { X=a.X; Y=a.Y; } const Point operator+(const Point& a, const Point& b) { Point temp; temp.setX(a.getX()+b.getX()); temp.setY(a.getY()+b.getY()); return temp; }
PNU Const and Reference 7 #include using namespace std; class Point{ private: float X; float Y; public: printPoint() { cout << “(“<<X<<“, “<<Y<<“)\n”; } void inputPoint(float a, float b) {X=a;Y=b;} }; int main() { Location myLocation; (myLocation.getLoc()).inputPoint(1.0,2.0); myLocation.printLocation(); return 0; } class Location{ private: Point loc; public: Point& getLoc() { return loc;} void printLocation() {loc.printPoint();} }; class Location{ private: Point loc; public: Point getLoc() { return loc;} void printLocation() {loc.printPoint();} }; class Location{ private: Point loc; public: const Point& getLoc() { return loc;} void printLocation() {loc.printPoint();} };
PNU ++, Assignment = 8 #include using namespace std; class Point{ private: float X; float Y; public: Point(float x, float y):X(x),Y(y) {} Point operator=(const Point&); Point operator++(); Point operator++(int); }; int main() { Point p(1.0,2.0); Point q, r; r=q=p; q=++p; r=q++; } Point Point::operator=(const Point& rhs) X=rhs.X; Y=rhs.Y; return *this; }; this: pointer to the object itself. Point Point::operator++() X++; Y++; return *this; }; Point Point::operator++(int nothing) Point temp=*this; X++; Y++; return temp; };
PNU, == 9 #include using namespace std; class Point{ private: float X; float Y; public: Point(float x, float y):X(x),Y(y) {} friend bool operator<(const Point&); friend bool operator==(const Point&); }; int main() { Point p(1.0,2.0); Point q(2.1,1.1); if(p>q) cout << “p is greater than q\n”; else cout << “p is NOT greater than q\n”; } bool Point::operator<(const Point& a, const Point& b) return (a.X*a.X+a.Y*a.Y)<(b.X*b.X+b.Y*b.Y); };
PNU -> 10 #include using namespace std; class Point{ public: float X; float Y; Point(float x, float y):X(x),Y(y) {} Point *operator->() {return this;} friend bool operator==(const Point&); }; int main() { Point p; p->X=10.1; p.Y=20.2; }
PNU [ ], ( ) 11 #include using namespace std; class PointSet{ private: Point *p; int n; public: PointSet(int, float); Point operator[](int i) { return p[i];} }; int main() { PointSet ps(10,0.0); Point q=ps[5]; q.printPoint(); ps[6].setX(10.0); ps[6].setY(20.0); ps[6].printPoint(); } PointSet::PointSet(int i,float x) { n=i; p=new Point[n]; for(int k=0;k<n;k++) { p[k].setX(x); p[k].setY(x); } } Is it correct?
PNU new, delete operators 12 #include using namespace std; class Point { float X, Y; public: Point() {} Point(float px, float py) {X = px; Y = py; } void *operator new(size_t size); void operator delete(void *p); }; int main() { Point *p; try { p = new Point (10.0, 20.0); } catch (bad_alloc xa) { cout << "Allocation error for p1.\n"; return 1; } delete p; return 0; } void *Point::operator new(size_t size) { void *p; cout<<“overloaded new operator\n"; p = malloc(size); if(!p) { bad_alloc ba; throw ba; } return p; } void Point::operator delete(void *p) { cout << "overloaded delete operator\n"; free(p); }
PNU > operators 13 #include using namespace std; class Point { float X, Y; public: friend istream& operator>>(istream&, const Point&); friend ostream& operator<<(ostream&, const Point&); }; int main() { Point p,q; cin >> p >> q; cout << p << q; return 0; } istream& operator>>(const istream& istreamPoint, const Point& p) { cout > p.X; cout > p.Y; return istream; } ostream& operator<<(const ostream& istreamPoint, const Point& p) { cout << “x=“ <<p.X << “y=“ <<p.Y << endl; return ostream; }