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

49 618 0
Object oriented programming with C++ - Session 4 Operator Overloading potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4 Object Oriented Programming with C++/ Session 4/ 2 of 49 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/ 3 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/ 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/ 5 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/ 6 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/ 7 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 syntax obj3 = obj1 + obj2; instead of, obj3.addobjects(obj1,obj2); Object Oriented Programming with C++/ Session 4/ 8 of 49 The Operator function ■ Operator function: Contains actual instructions to overload an operator. The operator that has to be overloaded follows the keyword "operator". return_type operator op(argument list); where op is the symbol for the operator that is being overloaded. Object Oriented Programming with C++/ Session 4/ 9 of 49 Unary Operators ■ Unary operators have only one operand. increment operator ++, decrement operator , and unary minus operator. ■ The increment and decrement operators can be used as either prefix or postfix operations. class Sample{ private: int counter; public: Sample() {counter =0;} void operator++() {++counter;} }; Object Oriented Programming with C++/ Session 4/ 10 of 49 Unary Operators (Contd.) void main() { Sample obj1; obj1++; //increments counter to 1 ++obj1; //increments counter to 2 } • If an operator function is found in the class specifier, the statement to increment the object obj1++;gets converted, by the compiler to the following: obj1.operator++(); [...]... changes to the object that is being copied Object Oriented Programming with C++/ Session 4/ 31 of 49 Copy Constructor (Contd.) s Copy constructor is called in three contexts: • when an object of a class is initialised to another of the same class • when an object is passed as an argument to a function • when a function returns an object Object Oriented Programming with C++/ Session 4/ 32 of 49 Copy Constructor... s1 object still points to it Object Oriented Programming with C++/ Session 4/ 27 of 49 Assignment Operator (Contd.) • Solution: String& String: :operator= (String& s) { delete str; int length = strlen(s.str); str = new char[length+1]; strcpy(str,s.str); return(*this); } • By including this function in the program the error message will not appear Object Oriented Programming with C++/ Session 4/ 28 of 49 ... nameless object is initialised to the same values as this object Object Oriented Programming with C++/ Session 4/ 22 of 49 Comparison Operators s Comparison and logical operators are binary operators that need two objects to be compared The comparison operators that can be overloaded include =, ==, and != int String: :operator> (String ss) { return(strcmp(str,ss.str) > 0)); } Object Oriented Programming. .. return temp; //return temp object } s Now we are able to perform addition of objects with a statement, obj3 = obj1 + obj2; //objects of class Sample Object Oriented Programming with C++/ Session 4/ 18 of 49 Binary Arithmetic Operators (Contd.) s The operator + can access two objects • object on the left side of the operator, obj1, is the one that will invoke the function • object on the right hand side,... Object Oriented Programming with C++/ Session 4/ 34 of 49 A typical class s When a class X has a data member of pointer type, the class should have a constructor, assignment operator function, copy constructor and destructor class X{ X(some_value); //constructor X(const X&); //copy constructor X& operator= (const X&); //assignment ~X(); //destructor }; Object Oriented Programming with C++/ Session 4/ ...Unary Operators (Contd.) s A similar function to decrement the object can also be included in the class as: void operator () { counter;} • Can be invoked with the statement obj1; or obj ; s With the overloaded increment and decrement operators, the operator function is executed first, regardless of whether the operator is postfix or prefix Object Oriented Programming with C++/ Session 4/ 11 of 49 Unary... operand (object obj1 ) is accessed directly since this is the object invoking the function • Right hand operand is accessed as the function's argument as a.counter s Also possible to do obj4 = obj3 + obj2 + obj1; Possible because return type of the + operator function is an object of type Sample Object Oriented Programming with C++/ Session 4/ 19 of 49 Overloaded + Operator for strings String String: :operator+ (String... "to C++" ; String s3; s3 = s1 + s2; Object Oriented Programming with C++/ Session 4/ 20 of 49 Compound Assignment Operators void Sample: :operator+ =(Sample a) { counter += a.counter; //addition } • No need for a temporary object The object, whose data member counter is changed, is the object that invokes the function • The function also needs no return value because the result of the assignment operator. .. Session 4/ 12 of 49 Using nameless temporary object s Another way is to create a nameless temporary object and return it class Sample{ private: int counter; public: Sample() //constructor with no argument {counter = 0;} Sample(int c) //constructor with one argument {counter = c;} Sample operator+ +(); }; Object Oriented Programming with C++/ Session 4/ 13 of 49 Using nameless temporary object (Contd.)... with C++/ Session 4/ 23 of 49 Assignment Operator Overloading s s Default assignment operator simply copies the source object to the destination object byte by byte If data members contain pointers and have been allocated memory using the new operator class String{ private: char *str; public: String(char *s = "") { int length = strlen(s); str = new char[length+1]; strcpy(str,s); } Object Oriented Programming . Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4 Object Oriented Programming with C++/ Session 4/ 2 of 49 Session. types • between Objects of different Classes ■ Identify operators that cannot be overloaded Object Oriented Programming with C++/ Session 4/ 4 of 49 Operator Overloading ■ It

Ngày đăng: 23/03/2014, 04:21

Từ khóa liên quan

Mục lục

  • Operator Overloading

  • Session Objectives

  • Session Objectives (Contd.)

  • Slide 4

  • Operator Overloading(Contd.)

  • Points to note

  • Advantage

  • The Operator function

  • Unary Operators

  • Unary Operators (Contd.)

  • Slide 11

  • Slide 12

  • Using nameless temporary object

  • Using nameless temporary object (Contd.)

  • Using the this pointer

  • Problems with post and prefix

  • Binary Operators

  • Binary Arithmetic Operators

  • Binary Arithmetic Operators (Contd.)

  • Overloaded + Operator for strings

Tài liệu cùng người dùng

Tài liệu liên quan