Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operator Overloading. Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined.

Similar presentations


Presentation on theme: "Operator Overloading. Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined."— Presentation transcript:

1 Operator Overloading

2 Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined data type. Notation :- All overloaded operators have the following declaration operator (Argument-List) Eg.) If we want to overload the ‘+’ operator for a class named Test then the declaration will look like :- Test operator+ (Test& op1, Test& op2);

3 Elements of the Declaration Let us focus on each element of the declaration –Return-Type :- Must be same as the nature of return-type of the operator being overloaded. Eg.) If we are overloading the >= operator which is a boolean-logic operator then the return type will be bool On the other hand if we are overloading the ‘+’ operator then the return type will be same as the operand type since addition is closed under the data- type it is being used for

4 Elements of the Declaration (cont.) –Arguments-List The basic fundamental to building the argument-list for an overloaded operator is to note the follwing –Whether the operator is unary or, binary –The context of use that it is being loaded for a.k.a the nature of the operands that it is being overloaded for Eg.) If we want to add an integer to an object of class Test and the operation will always be written as + then the argument-List will be (int& op1, Test& op2) NOTE :- The overloaded operator behaves like a function with its operands as the arguments.

5 Restrictions on Operator Overloading Cannot change –How operators act on built-in data types I.e., cannot change integer addition –Precedence of operator (order of evaluation) Use parentheses to force order-of-operations –Associativity (left-to-right or right-to-left) –Number of operands & is unitary, only acts on one operand Cannot create new operators Operators must be overloaded explicitly –Overloading + does not overload +=

6 What can be Overloaded

7 Converting between Types Casting –Traditionally, cast integers to floats, etc. –May need to convert between user-defined types Cast operator (conversion operator) –Convert from One class to another Class to built-in type ( int, char, etc.) –Must be non- static member function Cannot be friend –Do not specify return type Implicitly returns type to which you are converting

8 Converting between Types Example –Prototype A::operator char *() const; Casts class A to a temporary char * (char *)s calls s.operator char*() –Also A::operator int() const; A::operator OtherClass() const;

9 Converting between Types Casting can prevent need for overloading –Suppose class String can be cast to char * –cout << s; // s is a String Compiler implicitly converts s to char * Do not have to overload << –Compiler can only do 1 cast

10 Conversion Constructor Conversion constructor –Single-argument constructor –Turns objects of other types into class objects String s1(“hi”); Creates a String from a char *

11 1 // Fig. 8.8: string1.cpp 2 // Member function definitions for class String. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; 11 12 #include // C++ standard "new" operator 13 14 #include // strcpy and strcat prototypes 15 #include // exit prototype 16 17 #include "string1.h" // String class definition 18 19 // conversion constructor converts char * to String 20 String::String( const char *s ) 21 : length( strlen( s ) ) 22 { 23 cout << "Conversion constructor: " << s << '\n'; 24 setString( s ); // call utility function 25 26 } // end String conversion constructor

12 class Test { private : int X,Y; float Z; public : 180 // overloaded output operator 181 ostream &operator<<( ostream &output, const String &s ) 182 { 183 output<< s.getX()<<‘\n’; output<< s.getY() <<‘\n’; output<< s.getZ() <<‘\n’; 184 185 return output; // enables cascading 186 } // end function operator<< 188 189 // overloaded input operator 190 istream &operator>>( istream &input, String &s ) 191 { int a; float b; 192 input>>a; s.setX(a); input>>a; s. setY(a); input >>b; s. setZ(b); 193 return input; // enables cascading 199 } // end function operator>> };


Download ppt "Operator Overloading. Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined."

Similar presentations


Ads by Google