130 likes | 232 Views
C++ Programming: chapter 4 – operator overloading. 2014, Spring Pusan National University Ki-Joune Li http://isel.cs.pnu.edu/~lik. #include <iostream> using namespace std; class Point{ private: float X; float Y; public: Point(float a): X(a),Y(a) {};
E N D
C++ Programming: chapter 4 – operator overloading 2014, Spring Pusan National University Ki-Joune Li http://isel.cs.pnu.edu/~lik
#include <iostream> 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; } 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; } Point::Point(const Point & a) { X=a.X; Y=a.Y; } operator overloading – 방법 1
#include <iostream> 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; } 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; } Point::Point(const Point & a) { X=a.X; Y=a.Y; } operator overloading – 방법 2
#include <iostream> 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; } const Point Point::operator+(const Point& b) { Point temp; temp.X=X+a.X; temp.Y=Y+b.Y; return temp; } Point::Point(const Point & a) { X=a.X; Y=a.Y; } operator overloading – 방법 3
#include <iostream> 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) • 주의 • =, [ ], ->, ( ) 등의연산자는 반드시 방법 3으로만 가능. • 연산자의 우선순위는 원래의 연산자의 우선순위와 동일 operator overloading – 비교
#include <iostream> 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;} constPoint operator+(constPoint&a, constPoint& 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; } constPoint operator+(constPoint& a, constPoint& b) { Point temp; temp.setX(a.getX()+b.getX()); temp.setY(a.getY()+b.getY()); return temp; } Point::Point(const Point & a) { X=a.X; Y=a.Y; } Const
class Location{ private: Point loc; public: Point& getLoc() { return loc;} void printLocation() {loc.printPoint();} }; #include <iostream> 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: const Point& getLoc() { return loc;} void printLocation() {loc.printPoint();} }; Const and Reference
Point Point::operator=(const Point& rhs) X=rhs.X; Y=rhs.Y; return *this; }; #include <iostream> 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++; } 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; }; ++, Assignment =
#include <iostream> 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); }; <, >, ==
#include <iostream> 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; } ->
#include <iostream> using namespace std; class PointSet{#include <iostream> 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? [ ], ( )
#include <iostream>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); voidoperator 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);} new, delete operators
#include <iostream>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 << “input x=“; istream >> p.X; cout << “input y=“; istream >> p.Y; return istream; } ostream& operator<<(const ostream& istreamPoint, const Point& p) { cout << “x=“ <<p.X << “y=“ <<p.Y << endl; return ostream; } <<, >> operators