Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming: chapter 4 – operator overloading

Similar presentations


Presentation on theme: "C++ Programming: chapter 4 – operator overloading"— Presentation transcript:

1 C++ Programming: chapter 4 – operator overloading
2018, Spring Pusan National University Ki-Joune Li

2 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); printValues() { cout << “(“<<X<<“, “<<Y<<“)\n”; } friend Point operator+(const Point&a, const Point& b); };  int main() {     Point myPoint(1.0);  Point yourPoint(2.0); (myPoint+yourPoint).printValues();   return 0;  } 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; }

3 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); 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”; } };  Point operator+(const Point&a, const Point& b); int main() {     Point myPoint(1.0);  Point yourPoint(2.0); (myPoint+yourPoint).printValues();   return 0;  } 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; }

4 operator overloading – 방법 3
#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”; } Point operator+(const Point& b); };  int main() {     Point myPoint(1.0);  Point yourPoint(2.0); (myPoint+yourPoint).printValues();   return 0;  } 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; }

5 operator overloading – 비교
#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으로만 가능. 연산자의 우선순위는 원래의 연산자의 우선순위와 동일

6 Const #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;} 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 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; }

7 Reference as a return object
#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: Point getLoc() { return loc;} void printLocation() {loc.printPoint();} };

8 ++, Assignment = 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; };

9 <, >, == #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); };

10 -> #include <iostream> using namespace std; class Point{
public:    float X; float Y; Point(float x, float y):X(x),Y(y) {} friend bool operator==(const Point&); };  int main() {     Location myLocation; myLocation->X=10.1; myLocation->Y=20.2; } class Location{ public: Point loc; Point *operator->{return &loc;} Point& getLoc() { return loc;} void printLocation() {loc.printPoint();} };

11 [ ], ( ) #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?

12 new, delete operators #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); 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); }

13 <<, >> 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; }


Download ppt "C++ Programming: chapter 4 – operator overloading"

Similar presentations


Ads by Google