tailieunhanh - Object oriented programming with C++ - Session 4 Operator Overloading

Describe Operator Overloading Unary operators Binary operators Binary arithmetic operators Compound assignment operators Comparison operators Describe overloading of the Assignment Operator Describe Copy Constructors | Operator Overloading Session 4 Session Objectives Describe Operator Overloading Unary operators Binary operators Binary arithmetic operators Compound assignment operators Comparison operators Describe overloading of the Assignment Operator Describe Copy Constructors Object Oriented Programming with C++/ Session 4/ of 49 Session Objectives (Contd.) Describe conversion functions which help in conversion from Basic types to User-Defined types from User-Defined types to Basic types between Objects of different Classes Identify operators that cannot be overloaded Object Oriented Programming with C++/ Session 4/ of 49 Operator Overloading It is the ability to associate an existing operator with a member function and use it with objects of its class as its operands Expressions with operators like +, -, >, +=, ==, etc. can be used only on basic data types like int and float Operator overloading allows statements like , if (obj1>obj2){ . . .} where obj1 and obj2 are objects of a class. | Operator Overloading Session 4 Session Objectives Describe Operator Overloading Unary operators Binary operators Binary arithmetic operators Compound assignment operators Comparison operators Describe overloading of the Assignment Operator Describe Copy Constructors Object Oriented Programming with C++/ Session 4/ of 49 Session Objectives (Contd.) Describe conversion functions which help in conversion from Basic types to User-Defined types from User-Defined types to Basic types between Objects of different Classes Identify operators that cannot be overloaded Object Oriented Programming with C++/ Session 4/ of 49 Operator Overloading It is the ability to associate an existing operator with a member function and use it with objects of its class as its operands Expressions with operators like +, -, >, +=, ==, etc. can be used only on basic data types like int and float Operator overloading allows statements like , if (obj1>obj2){ . . .} where obj1 and obj2 are objects of a class. Object Oriented Programming with C++/ Session 4/ of 49 Operator Overloading(Contd.) Operation of comparing the objects can be defined in a member function and associated with the comparison operator. Compiler can distinguish between overloaded operators by examining the data type of its operators. Operator overloading is one form of polymorphism - operational polymorphism. Object Oriented Programming with C++/ Session 4/ of 49 Points to note Overloading cannot alter the basic function of an operator, nor change its place in the order of precedence that is already defined in the language. ++ (increment) and --(decrement) can be used only as unary operators. an overloaded + operator can be used to multiply two objects but this would make your code unreadable. Object Oriented Programming with C++/ Session 4/ of 49 Advantage Makes programs easier to read and debug. Easy to understand that two objects are being added and the result assigned to a third object, if you use the .