C++ Programming: chapter 4 – operator overloading

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.

Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
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,
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
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,
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
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.
Copyright  Hannu Laine C++-programming Part 9 Hannu Laine.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
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.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
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.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
C++ Programming: chapter 4 – operator overloading 2014, Spring Pusan National University Ki-Joune Li 1.
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
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
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
Random Number Generation
Phần 2: Ngôn ngữ lập trình C++
Strings: C-strings vs. Strings as Objects
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
C++ Programming: chapter 3 - class
References, const and classes
Pass by Reference.
CSC 270 – Survey of Programming Languages
Introduction to Programming
COP 3330 Object-oriented Programming in C++
C++ Programming: chapter 8 – Templates
Pointers & Functions.
C++ Programming: chapter 9 – Exception Handling
C++ Constructor Insanity CSE 333 Spring 2019
Presentation transcript:

C++ Programming: chapter 4 – operator overloading 2018, Spring Pusan National University Ki-Joune Li http://lik.pnu.kr

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

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

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

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

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

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

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

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

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

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

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