SlideShare une entreprise Scribd logo
1  sur  24
C++ OOP :: Operator Overloading 06/10/2009 1 Hadziq Fabroyir - Informatics ITS
Function Signatures A function signature is what the compiler and linker use to identify a function. In C , functions are identified only by their name In C++ , a function’s signature includes its name, parameters, and (for member functions) const.   It does NOT include the return type. 06/10/2009 Hadziq Fabroyir - Informatics ITS 2
Ex: C++ swap( ) Function We still need separate functions, but they can all have the same name. For Examples: void swap (int& a, int& b); void swap (double& a, double& b); void swap (struct bob& a, struct bob& b); 06/10/2009 Hadziq Fabroyir - Informatics ITS 3
Operator Overloading Overview Many C++ operator are already overloaded for primitive types.  Examples: +     -     *     /     <<     >> It is often convenient for our classes to imitate the operations available on primitive types (e.g., +  or  - ). Then we can use the same concise notation for manipulating our objects. 06/10/2009 Hadziq Fabroyir - Informatics ITS 4
Ex: Complex Number Class class Complex {public: 		Complex (int real = 0, int imagine = 0); 		int getReal ( ) const; 		int getImagine ( ) const; 		void setReal (int n); 		void setImagine (int d); private: 		int real; 		int imagine; }; 06/10/2009 Hadziq Fabroyir - Informatics ITS 5
Using Complex Class It makes sense to want to perform mathematical operations with Complex objects.         Complex C1 (3, 5), C2 (5, 9), C3; 		C3 = C1 + C2;     // addition 		C2 = C3 * C1;      // subtraction 		C1 = -C2;            // negation 06/10/2009 Hadziq Fabroyir - Informatics ITS 6
Operators Are Really Functions For user-defined types, when you use an operator, you are making a function call. Consider the expression:  C2 + C1 This is translated into a function call. The name of the function is “operator+” The call is: C2.operator+(C1); 06/10/2009 Hadziq Fabroyir - Informatics ITS 7
Declaring operator+As a Member Function class Complex { 	public: 	     const Complex      	  operator+ (const Complex &operand) const; 	… }; Note all of the const’s! 06/10/2009 Hadziq Fabroyir - Informatics ITS 8
operator+ Implementation const Complex  Complex :: operator+ (const Complex &operand) const  { 	Complex sum; // accessor and mutators not required 	sum.imagine = imagine + operand.imagine; // but preferred 	sum.setReal( getReal( ) + operand.getReal ( ) );  	return sum; } 06/10/2009 Hadziq Fabroyir - Informatics ITS 9
Using operator+ We can now write C3 = C2 + C1; We can also use cascading operators. C4 = C3 + C2 + C1; And we can write C3 = C2 + 7; But  C3 = 7 + C2 is a compiler error.  (Why?) 06/10/2009 Hadziq Fabroyir - Informatics ITS 10
operator+ As aNon-member, Non-friend const Complex     operator+ (const Complex &lhs,      // extra parameter                       const Complex &rhs)     // not const {   // must use accessors and mutators 	Complex sum; 	sum.setImagine (lhs.getImagine( ) 				+ rhs.getImagine( ) ); 	sum.setReal (lhs.getReal ( ) + rhs.getReal( ) ); 	return sum; }  // is now commutative 06/10/2009 Hadziq Fabroyir - Informatics ITS 11
Printing Objects Each object should be responsible for printing itself. This guarantees objects are always printed the same way. It allows us to write intuitive output code: 	Complex C5 (5, 3);cout << C5 << endl; 06/10/2009 Hadziq Fabroyir - Informatics ITS 12
Operator<< The insertion operator << is a function and can (and should) be overloaded. We can do operator>>, too. <<  is a binary operator. The left-hand operand is of type ostream& Therefore, operator<< cannot be a member function.  It must be a non-member. 06/10/2009 Hadziq Fabroyir - Informatics ITS 13
operator<< ostream&  operator<< (ostream& out,  const Complex& c) { 	out << c.getReal( ); 	int imagine = c.getImagine( ); 	out << (imagine < 0 ? “ - ” : “ + ” )  	out << imagine << “i”; 	return out; } 06/10/2009 Hadziq Fabroyir - Informatics ITS 14
Operator<<Returns Type ‘ostream &’ Why?  So we can write statements such as cout << C5 << “is a complex number” OR cout << C3 << endl << C2 << endl; <<  associates from left to right. 06/10/2009 Hadziq Fabroyir - Informatics ITS 15
Overloading Unary Operators 	Complex C1(4, 5), C2; 	C2 = -C1; is an example of a unary operator (minus). We can and should overload this operator as a member function. 06/10/2009 Hadziq Fabroyir - Informatics ITS 16
Unary operator- const Complex Complex :: operator- ( ) const { 	Complex x; 	x.real = -real; 	x.imagine = imagine; 	return x; } 06/10/2009 Hadziq Fabroyir - Informatics ITS 17
Overloading  = Remember that assignment performs a memberwise (shallow) copy by default. This is not sufficient when a data member is dynamically allocated. =must be overloaded to do a deep copy. 06/10/2009 Hadziq Fabroyir - Informatics ITS 18
Restrictions Most of operators can be overloaded. You can’t make up your own operators. You can’t overload operators for primitive types (like int). You can’t change the precedence of an operator. You can’t change the associativity of an operator. 06/10/2009 Hadziq Fabroyir - Informatics ITS 19
Converting between Types Cast operator Convert objects into built-in types or other objects  Conversion operator must be a non-static member function. Cannot be a friend function Do not specify return type For user-defined class A 	A::operator char *() const;     // A to char 	A::operator int() const;        //A to int 	A::operator otherClass() const; //A to otherClass 	When compiler sees (char *) s it calls  		s.operator char*() 06/10/2009 Hadziq Fabroyir - Informatics ITS 20
Good Programming Practices Overload operators so that they mimic the behavior of primitive data types. Overloaded binary arithmetic operators should return const objects by value be written as non-member functions when appropriate to allow commutativity be written as non-friend functions (if data member accessors are available) Overload unary operators as member functions. Always overload << Always overload  =  for objects with dynamic data members. 06/10/2009 Hadziq Fabroyir - Informatics ITS 21
Another Example Vectors in the Plane Suppose we want to implement vectors in 2D and the operations involving them.  06/10/2009 Hadziq Fabroyir - Informatics ITS 22
For your practice … Exercise Vector2D Class Properties:  double X, double Y Method (Mutators): (try)  all possible operators that can be applied on Vector 2D (define methods of)  the remains operation that can’t be overloaded Lab Session for lower order (Senin, 15.00-17.00) Lab Session for upper order (Senin, 19.00-21.00) Please provide: 	the softcopy(send it by email – deadline Sunday 23:59) 	the hardcopy(bring it when attending the lab session) 06/10/2009 Hadziq Fabroyir - Informatics ITS 23
☺~ Next: C++ OOP :: Inheritance ~☺ [ 24 ] Hadziq Fabroyir - Informatics ITS 06/10/2009

Contenu connexe

Tendances

Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversionsAmogh Kalyanshetti
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type ConversionsRokonuzzaman Rony
 
Operator overloading in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++BalajiGovindan5
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingDustin Chase
 
C++ overloading
C++ overloadingC++ overloading
C++ overloadingsanya6900
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingabhay singh
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloadingHaresh Jaiswal
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingBurhan Ahmed
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++gourav kottawar
 

Tendances (20)

Lecture5
Lecture5Lecture5
Lecture5
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
operator overloading in C++
operator overloading in C++operator overloading in C++
operator overloading in C++
 
Operator overloading in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 

En vedette

#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop InheritanceHadziq Fabroyir
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritanceshatha00
 
Oop inheritance
Oop inheritanceOop inheritance
Oop inheritanceZubair CH
 
OOP Chapter 8 : Inheritance
OOP Chapter 8 : InheritanceOOP Chapter 8 : Inheritance
OOP Chapter 8 : InheritanceAtit Patumvan
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Ameen Sha'arawi
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class StructureHadziq Fabroyir
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programmingAshita Agrawal
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 

En vedette (16)

#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Oop inheritance
Oop inheritanceOop inheritance
Oop inheritance
 
OOP Chapter 8 : Inheritance
OOP Chapter 8 : InheritanceOOP Chapter 8 : Inheritance
OOP Chapter 8 : Inheritance
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Interesting Concept of Object Oriented Programming
Interesting Concept of Object Oriented Programming Interesting Concept of Object Oriented Programming
Interesting Concept of Object Oriented Programming
 
OOP Inheritance
OOP InheritanceOOP Inheritance
OOP Inheritance
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similaire à #OOP_D_ITS - 5th - C++ Oop Operator Overloading

C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloadingRai University
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++ppd1961
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaingzindadili
 
Gude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerGude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerApache Traffic Server
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfesuEthopi
 
overloading in C++
overloading in C++overloading in C++
overloading in C++Prof Ansari
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfstudy material
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functionsAlisha Korpal
 
#OOP_D_ITS - 9th - Template
#OOP_D_ITS - 9th - Template#OOP_D_ITS - 9th - Template
#OOP_D_ITS - 9th - TemplateHadziq Fabroyir
 
operator overloading
operator overloadingoperator overloading
operator overloadingNishant Joshi
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Introduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoIntroduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoMark John Lado, MIT
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_iiNico Ludwig
 

Similaire à #OOP_D_ITS - 5th - C++ Oop Operator Overloading (20)

C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Gude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerGude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic Server
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
Overloading
OverloadingOverloading
Overloading
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
#OOP_D_ITS - 9th - Template
#OOP_D_ITS - 9th - Template#OOP_D_ITS - 9th - Template
#OOP_D_ITS - 9th - Template
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Introduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoIntroduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John Lado
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Inline function
Inline functionInline function
Inline function
 
(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
08 -functions
08  -functions08  -functions
08 -functions
 

Plus de Hadziq Fabroyir

An Immersive Map Exploration System Using Handheld Device
An Immersive Map Exploration System Using Handheld DeviceAn Immersive Map Exploration System Using Handheld Device
An Immersive Map Exploration System Using Handheld DeviceHadziq Fabroyir
 
在不同尺度遙現系統中具空間感知特性的使用者介面開發
在不同尺度遙現系統中具空間感知特性的使用者介面開發在不同尺度遙現系統中具空間感知特性的使用者介面開發
在不同尺度遙現系統中具空間感知特性的使用者介面開發Hadziq Fabroyir
 
NTUST Course Selection (Revision: Fall 2016)
NTUST Course Selection (Revision: Fall 2016)NTUST Course Selection (Revision: Fall 2016)
NTUST Course Selection (Revision: Fall 2016)Hadziq Fabroyir
 
律法保護的五件事
律法保護的五件事律法保護的五件事
律法保護的五件事Hadziq Fabroyir
 
Pelajaran 5 第五課 • Telepon 給打電話
Pelajaran 5 第五課 • Telepon 給打電話Pelajaran 5 第五課 • Telepon 給打電話
Pelajaran 5 第五課 • Telepon 給打電話Hadziq Fabroyir
 
Pelajaran 4 第四課 • Belanja 買東西
Pelajaran 4 第四課 • Belanja 買東西Pelajaran 4 第四課 • Belanja 買東西
Pelajaran 4 第四課 • Belanja 買東西Hadziq Fabroyir
 
Pelajaran 3 第三課 • Transportasi 交通
Pelajaran 3 第三課 • Transportasi 交通Pelajaran 3 第三課 • Transportasi 交通
Pelajaran 3 第三課 • Transportasi 交通Hadziq Fabroyir
 
Pelajaran 2 第二課 • Di Restoran 在餐廳
Pelajaran 2 第二課 • Di Restoran 在餐廳Pelajaran 2 第二課 • Di Restoran 在餐廳
Pelajaran 2 第二課 • Di Restoran 在餐廳Hadziq Fabroyir
 
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹Pelajaran 1 第一課 • Perkenalan Diri 自我介紹
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹Hadziq Fabroyir
 
Living in Taiwan for Dummies
Living in Taiwan for DummiesLiving in Taiwan for Dummies
Living in Taiwan for DummiesHadziq Fabroyir
 
How to Select Course at NTUST
How to Select Course at NTUSTHow to Select Course at NTUST
How to Select Course at NTUSTHadziq Fabroyir
 
NTUST-IMSA • International Students Orientation
NTUST-IMSA • International Students OrientationNTUST-IMSA • International Students Orientation
NTUST-IMSA • International Students OrientationHadziq Fabroyir
 
NTUST Course Selection - How to
NTUST Course Selection - How toNTUST Course Selection - How to
NTUST Course Selection - How toHadziq Fabroyir
 
#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class DiagramHadziq Fabroyir
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting StartedHadziq Fabroyir
 
#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And ReferencesHadziq Fabroyir
 
#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++Hadziq Fabroyir
 
#OOP_D_ITS - 1st - Introduction To Object Oriented Programming
#OOP_D_ITS - 1st - Introduction To Object Oriented Programming#OOP_D_ITS - 1st - Introduction To Object Oriented Programming
#OOP_D_ITS - 1st - Introduction To Object Oriented ProgrammingHadziq Fabroyir
 

Plus de Hadziq Fabroyir (20)

An Immersive Map Exploration System Using Handheld Device
An Immersive Map Exploration System Using Handheld DeviceAn Immersive Map Exploration System Using Handheld Device
An Immersive Map Exploration System Using Handheld Device
 
在不同尺度遙現系統中具空間感知特性的使用者介面開發
在不同尺度遙現系統中具空間感知特性的使用者介面開發在不同尺度遙現系統中具空間感知特性的使用者介面開發
在不同尺度遙現系統中具空間感知特性的使用者介面開發
 
NTUST Course Selection (Revision: Fall 2016)
NTUST Course Selection (Revision: Fall 2016)NTUST Course Selection (Revision: Fall 2016)
NTUST Course Selection (Revision: Fall 2016)
 
律法保護的五件事
律法保護的五件事律法保護的五件事
律法保護的五件事
 
Pelajaran 5 第五課 • Telepon 給打電話
Pelajaran 5 第五課 • Telepon 給打電話Pelajaran 5 第五課 • Telepon 給打電話
Pelajaran 5 第五課 • Telepon 給打電話
 
Pelajaran 4 第四課 • Belanja 買東西
Pelajaran 4 第四課 • Belanja 買東西Pelajaran 4 第四課 • Belanja 買東西
Pelajaran 4 第四課 • Belanja 買東西
 
Pelajaran 3 第三課 • Transportasi 交通
Pelajaran 3 第三課 • Transportasi 交通Pelajaran 3 第三課 • Transportasi 交通
Pelajaran 3 第三課 • Transportasi 交通
 
Pelajaran 2 第二課 • Di Restoran 在餐廳
Pelajaran 2 第二課 • Di Restoran 在餐廳Pelajaran 2 第二課 • Di Restoran 在餐廳
Pelajaran 2 第二課 • Di Restoran 在餐廳
 
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹Pelajaran 1 第一課 • Perkenalan Diri 自我介紹
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹
 
Living in Taiwan for Dummies
Living in Taiwan for DummiesLiving in Taiwan for Dummies
Living in Taiwan for Dummies
 
How to Select Course at NTUST
How to Select Course at NTUSTHow to Select Course at NTUST
How to Select Course at NTUST
 
NTUST-IMSA • International Students Orientation
NTUST-IMSA • International Students OrientationNTUST-IMSA • International Students Orientation
NTUST-IMSA • International Students Orientation
 
NTUST Course Selection - How to
NTUST Course Selection - How toNTUST Course Selection - How to
NTUST Course Selection - How to
 
Brain Battle Online
Brain Battle OnlineBrain Battle Online
Brain Battle Online
 
Manajemen Waktu
Manajemen WaktuManajemen Waktu
Manajemen Waktu
 
#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started
 
#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References
 
#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++
 
#OOP_D_ITS - 1st - Introduction To Object Oriented Programming
#OOP_D_ITS - 1st - Introduction To Object Oriented Programming#OOP_D_ITS - 1st - Introduction To Object Oriented Programming
#OOP_D_ITS - 1st - Introduction To Object Oriented Programming
 

Dernier

Models Call Girls In Hyderabad 9630942363 Hyderabad Call Girl & Hyderabad Esc...
Models Call Girls In Hyderabad 9630942363 Hyderabad Call Girl & Hyderabad Esc...Models Call Girls In Hyderabad 9630942363 Hyderabad Call Girl & Hyderabad Esc...
Models Call Girls In Hyderabad 9630942363 Hyderabad Call Girl & Hyderabad Esc...GENUINE ESCORT AGENCY
 
Russian Call Girls Service Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...
Russian Call Girls Service  Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...Russian Call Girls Service  Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...
Russian Call Girls Service Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...parulsinha
 
Top Rated Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...
Top Rated  Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...Top Rated  Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...
Top Rated Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...chandars293
 
Call Girls Mysore Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Mysore Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Mysore Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Mysore Just Call 8250077686 Top Class Call Girl Service AvailableDipal Arora
 
Premium Bangalore Call Girls Jigani Dail 6378878445 Escort Service For Hot Ma...
Premium Bangalore Call Girls Jigani Dail 6378878445 Escort Service For Hot Ma...Premium Bangalore Call Girls Jigani Dail 6378878445 Escort Service For Hot Ma...
Premium Bangalore Call Girls Jigani Dail 6378878445 Escort Service For Hot Ma...tanya dube
 
Call Girls Madurai Just Call 9630942363 Top Class Call Girl Service Available
Call Girls Madurai Just Call 9630942363 Top Class Call Girl Service AvailableCall Girls Madurai Just Call 9630942363 Top Class Call Girl Service Available
Call Girls Madurai Just Call 9630942363 Top Class Call Girl Service AvailableGENUINE ESCORT AGENCY
 
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...adilkhan87451
 
Most Beautiful Call Girl in Bangalore Contact on Whatsapp
Most Beautiful Call Girl in Bangalore Contact on WhatsappMost Beautiful Call Girl in Bangalore Contact on Whatsapp
Most Beautiful Call Girl in Bangalore Contact on WhatsappInaaya Sharma
 
Call Girls Hyderabad Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Hyderabad Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Hyderabad Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Hyderabad Just Call 8250077686 Top Class Call Girl Service AvailableDipal Arora
 
Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...
Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...
Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...parulsinha
 
Jogeshwari ! Call Girls Service Mumbai - 450+ Call Girl Cash Payment 90042684...
Jogeshwari ! Call Girls Service Mumbai - 450+ Call Girl Cash Payment 90042684...Jogeshwari ! Call Girls Service Mumbai - 450+ Call Girl Cash Payment 90042684...
Jogeshwari ! Call Girls Service Mumbai - 450+ Call Girl Cash Payment 90042684...Anamika Rawat
 
Saket * Call Girls in Delhi - Phone 9711199012 Escorts Service at 6k to 50k a...
Saket * Call Girls in Delhi - Phone 9711199012 Escorts Service at 6k to 50k a...Saket * Call Girls in Delhi - Phone 9711199012 Escorts Service at 6k to 50k a...
Saket * Call Girls in Delhi - Phone 9711199012 Escorts Service at 6k to 50k a...BhumiSaxena1
 
Call Girls Amritsar Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Amritsar Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Amritsar Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Amritsar Just Call 8250077686 Top Class Call Girl Service AvailableDipal Arora
 
Independent Call Girls Service Mohali Sector 116 | 6367187148 | Call Girl Ser...
Independent Call Girls Service Mohali Sector 116 | 6367187148 | Call Girl Ser...Independent Call Girls Service Mohali Sector 116 | 6367187148 | Call Girl Ser...
Independent Call Girls Service Mohali Sector 116 | 6367187148 | Call Girl Ser...karishmasinghjnh
 
Premium Call Girls In Jaipur {8445551418} ❤️VVIP SEEMA Call Girl in Jaipur Ra...
Premium Call Girls In Jaipur {8445551418} ❤️VVIP SEEMA Call Girl in Jaipur Ra...Premium Call Girls In Jaipur {8445551418} ❤️VVIP SEEMA Call Girl in Jaipur Ra...
Premium Call Girls In Jaipur {8445551418} ❤️VVIP SEEMA Call Girl in Jaipur Ra...parulsinha
 
Call Girl In Pune 👉 Just CALL ME: 9352988975 💋 Call Out Call Both With High p...
Call Girl In Pune 👉 Just CALL ME: 9352988975 💋 Call Out Call Both With High p...Call Girl In Pune 👉 Just CALL ME: 9352988975 💋 Call Out Call Both With High p...
Call Girl In Pune 👉 Just CALL ME: 9352988975 💋 Call Out Call Both With High p...chetankumar9855
 
Andheri East ) Call Girls in Mumbai Phone No 9004268417 Elite Escort Service ...
Andheri East ) Call Girls in Mumbai Phone No 9004268417 Elite Escort Service ...Andheri East ) Call Girls in Mumbai Phone No 9004268417 Elite Escort Service ...
Andheri East ) Call Girls in Mumbai Phone No 9004268417 Elite Escort Service ...Anamika Rawat
 
Top Rated Pune Call Girls (DIPAL) ⟟ 8250077686 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls (DIPAL) ⟟ 8250077686 ⟟ Call Me For Genuine Sex Serv...Top Rated Pune Call Girls (DIPAL) ⟟ 8250077686 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls (DIPAL) ⟟ 8250077686 ⟟ Call Me For Genuine Sex Serv...Dipal Arora
 
Call Girls Jaipur Just Call 9521753030 Top Class Call Girl Service Available
Call Girls Jaipur Just Call 9521753030 Top Class Call Girl Service AvailableCall Girls Jaipur Just Call 9521753030 Top Class Call Girl Service Available
Call Girls Jaipur Just Call 9521753030 Top Class Call Girl Service AvailableJanvi Singh
 

Dernier (20)

Models Call Girls In Hyderabad 9630942363 Hyderabad Call Girl & Hyderabad Esc...
Models Call Girls In Hyderabad 9630942363 Hyderabad Call Girl & Hyderabad Esc...Models Call Girls In Hyderabad 9630942363 Hyderabad Call Girl & Hyderabad Esc...
Models Call Girls In Hyderabad 9630942363 Hyderabad Call Girl & Hyderabad Esc...
 
Russian Call Girls Service Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...
Russian Call Girls Service  Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...Russian Call Girls Service  Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...
Russian Call Girls Service Jaipur {8445551418} ❤️PALLAVI VIP Jaipur Call Gir...
 
Top Rated Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...
Top Rated  Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...Top Rated  Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...
Top Rated Hyderabad Call Girls Erragadda ⟟ 9332606886 ⟟ Call Me For Genuine ...
 
Call Girls Mysore Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Mysore Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Mysore Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Mysore Just Call 8250077686 Top Class Call Girl Service Available
 
Premium Bangalore Call Girls Jigani Dail 6378878445 Escort Service For Hot Ma...
Premium Bangalore Call Girls Jigani Dail 6378878445 Escort Service For Hot Ma...Premium Bangalore Call Girls Jigani Dail 6378878445 Escort Service For Hot Ma...
Premium Bangalore Call Girls Jigani Dail 6378878445 Escort Service For Hot Ma...
 
Call Girls Madurai Just Call 9630942363 Top Class Call Girl Service Available
Call Girls Madurai Just Call 9630942363 Top Class Call Girl Service AvailableCall Girls Madurai Just Call 9630942363 Top Class Call Girl Service Available
Call Girls Madurai Just Call 9630942363 Top Class Call Girl Service Available
 
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...
Russian Call Girls Lucknow Just Call 👉👉7877925207 Top Class Call Girl Service...
 
Most Beautiful Call Girl in Bangalore Contact on Whatsapp
Most Beautiful Call Girl in Bangalore Contact on WhatsappMost Beautiful Call Girl in Bangalore Contact on Whatsapp
Most Beautiful Call Girl in Bangalore Contact on Whatsapp
 
Call Girls Hyderabad Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Hyderabad Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Hyderabad Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Hyderabad Just Call 8250077686 Top Class Call Girl Service Available
 
Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...
Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...
Independent Call Girls In Jaipur { 8445551418 } ✔ ANIKA MEHTA ✔ Get High Prof...
 
Jogeshwari ! Call Girls Service Mumbai - 450+ Call Girl Cash Payment 90042684...
Jogeshwari ! Call Girls Service Mumbai - 450+ Call Girl Cash Payment 90042684...Jogeshwari ! Call Girls Service Mumbai - 450+ Call Girl Cash Payment 90042684...
Jogeshwari ! Call Girls Service Mumbai - 450+ Call Girl Cash Payment 90042684...
 
Saket * Call Girls in Delhi - Phone 9711199012 Escorts Service at 6k to 50k a...
Saket * Call Girls in Delhi - Phone 9711199012 Escorts Service at 6k to 50k a...Saket * Call Girls in Delhi - Phone 9711199012 Escorts Service at 6k to 50k a...
Saket * Call Girls in Delhi - Phone 9711199012 Escorts Service at 6k to 50k a...
 
Call Girls Amritsar Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Amritsar Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Amritsar Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Amritsar Just Call 8250077686 Top Class Call Girl Service Available
 
Independent Call Girls Service Mohali Sector 116 | 6367187148 | Call Girl Ser...
Independent Call Girls Service Mohali Sector 116 | 6367187148 | Call Girl Ser...Independent Call Girls Service Mohali Sector 116 | 6367187148 | Call Girl Ser...
Independent Call Girls Service Mohali Sector 116 | 6367187148 | Call Girl Ser...
 
Premium Call Girls In Jaipur {8445551418} ❤️VVIP SEEMA Call Girl in Jaipur Ra...
Premium Call Girls In Jaipur {8445551418} ❤️VVIP SEEMA Call Girl in Jaipur Ra...Premium Call Girls In Jaipur {8445551418} ❤️VVIP SEEMA Call Girl in Jaipur Ra...
Premium Call Girls In Jaipur {8445551418} ❤️VVIP SEEMA Call Girl in Jaipur Ra...
 
Call Girls in Gagan Vihar (delhi) call me [🔝 9953056974 🔝] escort service 24X7
Call Girls in Gagan Vihar (delhi) call me [🔝  9953056974 🔝] escort service 24X7Call Girls in Gagan Vihar (delhi) call me [🔝  9953056974 🔝] escort service 24X7
Call Girls in Gagan Vihar (delhi) call me [🔝 9953056974 🔝] escort service 24X7
 
Call Girl In Pune 👉 Just CALL ME: 9352988975 💋 Call Out Call Both With High p...
Call Girl In Pune 👉 Just CALL ME: 9352988975 💋 Call Out Call Both With High p...Call Girl In Pune 👉 Just CALL ME: 9352988975 💋 Call Out Call Both With High p...
Call Girl In Pune 👉 Just CALL ME: 9352988975 💋 Call Out Call Both With High p...
 
Andheri East ) Call Girls in Mumbai Phone No 9004268417 Elite Escort Service ...
Andheri East ) Call Girls in Mumbai Phone No 9004268417 Elite Escort Service ...Andheri East ) Call Girls in Mumbai Phone No 9004268417 Elite Escort Service ...
Andheri East ) Call Girls in Mumbai Phone No 9004268417 Elite Escort Service ...
 
Top Rated Pune Call Girls (DIPAL) ⟟ 8250077686 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls (DIPAL) ⟟ 8250077686 ⟟ Call Me For Genuine Sex Serv...Top Rated Pune Call Girls (DIPAL) ⟟ 8250077686 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls (DIPAL) ⟟ 8250077686 ⟟ Call Me For Genuine Sex Serv...
 
Call Girls Jaipur Just Call 9521753030 Top Class Call Girl Service Available
Call Girls Jaipur Just Call 9521753030 Top Class Call Girl Service AvailableCall Girls Jaipur Just Call 9521753030 Top Class Call Girl Service Available
Call Girls Jaipur Just Call 9521753030 Top Class Call Girl Service Available
 

#OOP_D_ITS - 5th - C++ Oop Operator Overloading

  • 1. C++ OOP :: Operator Overloading 06/10/2009 1 Hadziq Fabroyir - Informatics ITS
  • 2. Function Signatures A function signature is what the compiler and linker use to identify a function. In C , functions are identified only by their name In C++ , a function’s signature includes its name, parameters, and (for member functions) const. It does NOT include the return type. 06/10/2009 Hadziq Fabroyir - Informatics ITS 2
  • 3. Ex: C++ swap( ) Function We still need separate functions, but they can all have the same name. For Examples: void swap (int& a, int& b); void swap (double& a, double& b); void swap (struct bob& a, struct bob& b); 06/10/2009 Hadziq Fabroyir - Informatics ITS 3
  • 4. Operator Overloading Overview Many C++ operator are already overloaded for primitive types. Examples: + - * / << >> It is often convenient for our classes to imitate the operations available on primitive types (e.g., + or - ). Then we can use the same concise notation for manipulating our objects. 06/10/2009 Hadziq Fabroyir - Informatics ITS 4
  • 5. Ex: Complex Number Class class Complex {public: Complex (int real = 0, int imagine = 0); int getReal ( ) const; int getImagine ( ) const; void setReal (int n); void setImagine (int d); private: int real; int imagine; }; 06/10/2009 Hadziq Fabroyir - Informatics ITS 5
  • 6. Using Complex Class It makes sense to want to perform mathematical operations with Complex objects. Complex C1 (3, 5), C2 (5, 9), C3; C3 = C1 + C2; // addition C2 = C3 * C1; // subtraction C1 = -C2; // negation 06/10/2009 Hadziq Fabroyir - Informatics ITS 6
  • 7. Operators Are Really Functions For user-defined types, when you use an operator, you are making a function call. Consider the expression: C2 + C1 This is translated into a function call. The name of the function is “operator+” The call is: C2.operator+(C1); 06/10/2009 Hadziq Fabroyir - Informatics ITS 7
  • 8. Declaring operator+As a Member Function class Complex { public: const Complex operator+ (const Complex &operand) const; … }; Note all of the const’s! 06/10/2009 Hadziq Fabroyir - Informatics ITS 8
  • 9. operator+ Implementation const Complex Complex :: operator+ (const Complex &operand) const { Complex sum; // accessor and mutators not required sum.imagine = imagine + operand.imagine; // but preferred sum.setReal( getReal( ) + operand.getReal ( ) ); return sum; } 06/10/2009 Hadziq Fabroyir - Informatics ITS 9
  • 10. Using operator+ We can now write C3 = C2 + C1; We can also use cascading operators. C4 = C3 + C2 + C1; And we can write C3 = C2 + 7; But C3 = 7 + C2 is a compiler error. (Why?) 06/10/2009 Hadziq Fabroyir - Informatics ITS 10
  • 11. operator+ As aNon-member, Non-friend const Complex operator+ (const Complex &lhs, // extra parameter const Complex &rhs) // not const { // must use accessors and mutators Complex sum; sum.setImagine (lhs.getImagine( ) + rhs.getImagine( ) ); sum.setReal (lhs.getReal ( ) + rhs.getReal( ) ); return sum; } // is now commutative 06/10/2009 Hadziq Fabroyir - Informatics ITS 11
  • 12. Printing Objects Each object should be responsible for printing itself. This guarantees objects are always printed the same way. It allows us to write intuitive output code: Complex C5 (5, 3);cout << C5 << endl; 06/10/2009 Hadziq Fabroyir - Informatics ITS 12
  • 13. Operator<< The insertion operator << is a function and can (and should) be overloaded. We can do operator>>, too. << is a binary operator. The left-hand operand is of type ostream& Therefore, operator<< cannot be a member function. It must be a non-member. 06/10/2009 Hadziq Fabroyir - Informatics ITS 13
  • 14. operator<< ostream& operator<< (ostream& out, const Complex& c) { out << c.getReal( ); int imagine = c.getImagine( ); out << (imagine < 0 ? “ - ” : “ + ” ) out << imagine << “i”; return out; } 06/10/2009 Hadziq Fabroyir - Informatics ITS 14
  • 15. Operator<<Returns Type ‘ostream &’ Why? So we can write statements such as cout << C5 << “is a complex number” OR cout << C3 << endl << C2 << endl; << associates from left to right. 06/10/2009 Hadziq Fabroyir - Informatics ITS 15
  • 16. Overloading Unary Operators Complex C1(4, 5), C2; C2 = -C1; is an example of a unary operator (minus). We can and should overload this operator as a member function. 06/10/2009 Hadziq Fabroyir - Informatics ITS 16
  • 17. Unary operator- const Complex Complex :: operator- ( ) const { Complex x; x.real = -real; x.imagine = imagine; return x; } 06/10/2009 Hadziq Fabroyir - Informatics ITS 17
  • 18. Overloading = Remember that assignment performs a memberwise (shallow) copy by default. This is not sufficient when a data member is dynamically allocated. =must be overloaded to do a deep copy. 06/10/2009 Hadziq Fabroyir - Informatics ITS 18
  • 19. Restrictions Most of operators can be overloaded. You can’t make up your own operators. You can’t overload operators for primitive types (like int). You can’t change the precedence of an operator. You can’t change the associativity of an operator. 06/10/2009 Hadziq Fabroyir - Informatics ITS 19
  • 20. Converting between Types Cast operator Convert objects into built-in types or other objects Conversion operator must be a non-static member function. Cannot be a friend function Do not specify return type For user-defined class A A::operator char *() const; // A to char A::operator int() const; //A to int A::operator otherClass() const; //A to otherClass When compiler sees (char *) s it calls s.operator char*() 06/10/2009 Hadziq Fabroyir - Informatics ITS 20
  • 21. Good Programming Practices Overload operators so that they mimic the behavior of primitive data types. Overloaded binary arithmetic operators should return const objects by value be written as non-member functions when appropriate to allow commutativity be written as non-friend functions (if data member accessors are available) Overload unary operators as member functions. Always overload << Always overload = for objects with dynamic data members. 06/10/2009 Hadziq Fabroyir - Informatics ITS 21
  • 22. Another Example Vectors in the Plane Suppose we want to implement vectors in 2D and the operations involving them. 06/10/2009 Hadziq Fabroyir - Informatics ITS 22
  • 23. For your practice … Exercise Vector2D Class Properties: double X, double Y Method (Mutators): (try) all possible operators that can be applied on Vector 2D (define methods of) the remains operation that can’t be overloaded Lab Session for lower order (Senin, 15.00-17.00) Lab Session for upper order (Senin, 19.00-21.00) Please provide: the softcopy(send it by email – deadline Sunday 23:59) the hardcopy(bring it when attending the lab session) 06/10/2009 Hadziq Fabroyir - Informatics ITS 23
  • 24. ☺~ Next: C++ OOP :: Inheritance ~☺ [ 24 ] Hadziq Fabroyir - Informatics ITS 06/10/2009

Notes de l'éditeur

  1. All but . .* ?: ::Good Programming Practices:Overload operators so that they mimic the behavior of primitive data types.Overloaded binary arithmetic operators shouldreturn const objects by valuebe written as non-member functions when appropriate to allow commutativitybe written as non-friend functions (if data member accessors are available)Overload unary operators as member functions.Always overload <<Always overload = for objects with dynamic data members.