C++ : Operator overloading example

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

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.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Introduction to Programming Lecture 31. Operator Overloading.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator overloading Object Oriented Programming.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Recap: Interfaces A class is a logical abstraction, but an object has physical existence. access-specifier can be: public: Allow functions or data to be.
CS 11 C++ track: lecture 3 Today const and const -correctness operator overloading.
Lecture 5 : Inheritance & Polymorphism Acknowledgement : courtesy of Prof. Timothy Budd lecture slides.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
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.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading CS 3370 – C++ Chapter 14.
Lecture 5 : Inheritance Acknowledgement : courtesy of Prof. Timothy Budd lecture slides.
#define #include<iostream> using namespace std; #define GO
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Lecture 9: Operator Overloading
Introduction Rules Types Programs
Developed By : Ms. K. S. Kotecha
Polymorphism in C++ Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 21
Command Line Arguments
Functions and an Introduction to Recursion
School of EECS, Peking University
Pointers Psst… over there.
this Pointer Scope Resolution Operator Friend Function
group work #hifiTeam
Reserved Words.
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Zhen Jiang West Chester University
Random Number Generation
Operators Lecture 10 Fri, Feb 8, 2008.
Chapter 5 Function Basics
Name: Rubaisha Rajpoot
Operator Overloading; String and Array Objects
Introduction to Programming
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Anatomy of a Function Part 1
Code::Block vs Visual C++
Introduction to Programming
CS1201: Programming Language 2
Pass by Reference.
Operator Overloading.
Operator Overloading.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Functions and an Introduction to Recursion
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Eighth step for Learning C++ Programming
Pointers & Functions.
Anatomy of a Function Part 1
Introduction to Programming
Introduction to Functions
Presentation transcript:

C++ : Operator overloading example // operator_overloading.cpp #include <iostream> using namespace std; class Complex { private: double re, im; public : Complex( double r, double i ) : re(r), im(i) {} Complex operator+( Complex &other ); Complex operator-( Complex &other ); Complex operator*( Complex &other ); void Display( ) { if (im>0) cout << re << "+" << im <<"i" << endl; else if (im<0) cout << re << im <<"i" << endl; else if (im==0) cout << re << endl; } }; // Operator overloaded using a member function Complex Complex::operator+( Complex &other ) { return Complex( re + other.re, im + other.im ); Complex Complex::operator-( Complex &other ) { return Complex( re - other.re, im - other.im ); Complex Complex::operator*( Complex &other ) { return Complex( re * other.re - im * other.im , re*other.im + im*other.re ); int main() { Complex a = Complex( 1.2, 3.4 ); Complex b = Complex( 5.6, 7.8 ); Complex c = Complex( 0.0, 0.0 ); a.Display(); b.Display(); cout << endl; c = a + b; // c=a.operator+(b); c.Display(); c = a - b; // c=a.operator-(b); c = a * b; // c=a.operator*(b); } 결과: 1.2+3.4i 5.6+7.8i 6.8+11.2i -4.4-4.4i -19.8+28.4i

C++ : Operator overloading example (using friend) 2 C++ : Operator overloading example (using friend) // operator_overloading.cpp #include <iostream> using namespace std; class Complex { private: double re, im; public : Complex( double r, double i ) : re(r), im(i) {} friend Complex operator+(Complex&, Complex&); friend Complex operator-(Complex&, Complex&); friend Complex operator*(Complex&, Complex&); void Display( ) { if (im>0) cout << re << "+" << im <<"i" << endl; else if (im<0) cout << re << im <<"i" << endl; else if (im==0) cout << re << endl; } }; // Operator overloaded using a non-member function Complex operator+( Complex& i , Complex& other ) { return Complex( i.re + other.re, i.im + other.im ); Complex operator-( Complex& i , Complex& other ) { return Complex( i.re - other.re, i.im - other.im ); Complex operator*( Complex& i , Complex& other ) { return Complex( i.re * other.re - i.im * other.im , i.re*other.im + i.im*other.re ); int main() { Complex a = Complex( 1.2, 3.4 ); Complex b = Complex( 5.6, 7.8 ); Complex c = Complex( 0.0, 0.0 ); a.Display(); b.Display(); cout << endl; c = a + b; // c = operator+(a,b); c.Display(); c = a - b; // c = operator-(a,b); c = a * b; // c = operator*(a,b); } 결과: 1.2+3.4i 5.6+7.8i 6.8+11.2i -4.4-4.4i -19.8+28.4i

Operator Functions As Class Members v.s. As Non-member Functions Use this keyword to implicitly get argument Gets left operand for binary operators (like +) Leftmost object must be of same class as operator Non member functions Need parameters for both operands Can have object of different class than operator Must be a friend to access private or protected data