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

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Shape.h Shape Point Circle Cylinder // SHAPE.H
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types.
. Plab – Tirgul 8 I/O streams Example: string class.
Lecture 4 OOP Course. 4. Operators Using constructors: String int main(){ String s1(“My String”); String s2(s1); String s3; s3=s1; } int main(){ String.
Class Array template The array class defined in last week manipulate array of integer If we need to define class of array for float, double data type,
1 Overloading Lesson #8 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
1 Chapter 8 Composition. 2 Outlines 8.1 Composition versus Inheritance 8.2 Using Composition 8.3 Constructing and Destroying Composed Classes 8.4 Combining.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
Copyright  Hannu Laine C++-programming Part 4: Operator overloading.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Finding Memory Leaks. The new operator and operator new When allocating memory using new, e.g. –Student *ps = new Student(“Yossi Cohen”); –int *pi = new.
Copyright  Hannu Laine C++-programming Part 9 Hannu Laine.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
OPERATOR OVERLOADING WEEK 4-5 CHAPTER 19. class Money {private:int lira; int kurus; public: Money() {}; Money(int l, int k) { lira=l+ k/100; kurus=k%100;
Operator Overloading. Introduction Computer is calculating machine. It calculates the data provided to it. It performs the various operations on data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 5 1.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
C++ Programming: chapter 3 - class 2015, Spring Pusan National University Ki-Joune Li 1.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
Exercises on Polymorphism and Operator Overloading TCP1201: 8.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
第三章細部檢視類別 3-1 指定物件 3-2 傳遞物件給函數 3-3 從函數中傳回物件 3-4 簡介夥伴函數.
1 C++ Classes and Data Structures Course link…..
Andy Wang Object Oriented Programming in C++ COP 3330
Yan Shi CS/SE 2630 Lecture Notes
Overloading C++ supports the concept of overloading Two main types
Strings: C-strings vs. Strings as Objects
Overloading Operator MySting Example
A First C++ Class – a Circle
Reserved Words.
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Dynamic Memory Allocation Reference Variables
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
C++ Constructor Insanity CSE 333 Spring 2018
Phần 2: Ngôn ngữ lập trình C++
Strings: C-strings vs. Strings as Objects
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Introduction to Programming
Pointers & Functions.
C++ Programming: chapter 3 - class
C++ Programming: chapter 4 – operator overloading
References, const and classes
COP 3330 Object-oriented Programming in C++
Pointers & Functions.
Review of Function Overloading
Presentation transcript:

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