1 / 17

Operator Overloading

Operator Overloading. BCA Sem III K.I.R.A.S. What is operator overloading?. Operator overloading is a form of polymorphism i.e performing many actions with a single operator

rianne
Download Presentation

Operator Overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operator Overloading BCA Sem III K.I.R.A.S

  2. What is operator overloading? • Operator overloading is a form of polymorphism i.e performing many actions with a single operator • The operator keyword declares a function specifying what operator-symbol means when applied to instances of a class. This gives the operator more than one meaning, or "overloads" it. The compiler distinguishes between the different meanings of an operator by examining the types of its operands • One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention. C++ enables u to build operators that implement unary and binary operations on objects of classes. THIS FEATURE IS CALLED OPERATOR OVERLOADING ,and with this u can add member functions to the class to implement the overloaded operators

  3. What is operator overloading? • An operator is overloaded by writing a non-static member function definition or a global function definition except that the function name becomes the keyword operator followed by the symbol for the operation being overloaded. • Operator overloading means it is simply another way for you to make a function call. The difference is that the arguments for this function don’t appear inside parentheses, but instead they surround or are next to characters you’ve always thought of as immutable operators. • operator overloading is a specific case of polymorphism in which some or all of operators like +, =, or == have different implementations depending on the types of their arguments.

  4. What is operator overloading? • There are two differences between the use of an operator and an ordinary function call. The syntax is different; an operator is often “called” by placing it between or sometimes after the arguments. The second difference is that the compiler determines which “function” to call. For instance, if you are using the operator + with floating-point arguments, the compiler “calls” the function to perform floating-point addition (this “call” is typically the act of inserting in-line code, or a floating-point-processor instruction). If you use operator + with a floating-point number and an integer, the compiler “calls” a special function to turn the int into a float, and then “calls” the floating-point addition code. But in C++, it’s possible to define new operators that work with classes. This definition is just like an ordinary function definition except that the name of the function consists of the keyword operator followed by the operator. That’s the only difference, and it becomes a function like any other function, which the compiler calls when it sees the appropriate pattern.

  5. What is operator overloading • C++ tries to make the user-defined data types behave in much the same way as the built-in data types. • In built-in data types we have: • c=a+b //a,b and c are of type ‘int’. • We can also have in C++: • object1=object2+object3; • C++ has the ability to provide the operators with a special meaning for a data type. This mechanism is known as operatoroverloading. • The concept of operator overloading can also be applied to data conversions. C++ offers automatic conversion of primitive data types. For ex: x =a + b, the compiler implicitly converts the result to floating point representation and then assigns it to the float variable “ x” Use of operator overloading : • Extending the capability of operators to operate on user defined data • Data conversion

  6. Operator Overloading Syntax • Syntax is: Examples: operator+ operator- operator* operator/ Returntype operator symbol(arg-list) --- operator is a keyword --- symbol is one of C++ operator symbols (+, -, =, etc..)

  7. Operator Overloading Overview • Use operators with objects (operator overloading) • Clearer than function calls for certain classes • Examples of operator overloading: firststring + secondstring might concatenate two string objects myDate++ might increment a Date object a * b might multiply two Number objects

  8. 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. Operator functions must be either member functions or friend functions. • vector operator+(vector); • vector operator-(); • friend vector operator+(vector,vector); • friend vector operator-(vector); • int operator==(vector); • friend int operator==(vector,vector); • friend void operator-(space &s);

  9. Why Operator Overloading int i, j, k; // integers float m, n, p; // floats k = i + j; // integer addition and assignment p = m + n; // floating addition and assignment The compiler overloads the + operator for built-in integer and float types by default, producing integer addition with i+j, and floating addition with m+n.

  10. Implementing Operator Overloading • Two ways: 1)Implemented as member functions 2)Implemented as non-member or Friend functions (the operator function may need to be declared as a friend if it requires access to private data ) • Expression object1@object2 translates into a function call • object1.operator@(object2), if this function is defined within class object1 • operator@(object1,object2), if this function is defined outside the class object1

  11. c = a.operator+ (b); Implementing Operator Overloading • Defined as a member function class Complex { ... public: ... Complex operator +(const Complex &op) { double real = _real + op._real, imag = _imag + op._imag; return(Complex(real, imag)); } ... }; c = a+b;

  12. c = operator+ (a, b); Implementing Operator Overloading • Defined as a non-member function class Complex { ... public: ... double real() { return _real; } //need access functions double imag() { return _imag; } ... }; c = a+b; Complex operator +(Complex &op1, Complex &op2) { double real = op1.real() + op2.real(), imag = op1.imag() + op2.imag(); return(Complex(real, imag)); }

  13. c = operator+ (a, b); Implementing Operator Overloading • Defined as a friend function class Complex { ... public: ... friend Complex operator +( const Complex &, const Complex & ); ... }; c = a+b; Complex operator +(Complex &op1, Complex &op2) { double real = op1._real + op2._real, imag = op1._imag + op2._imag; return(Complex(real, imag)); }

  14. A Complex Number Class class Complex { private: int real; int imagine; public: Complex (int real = 0, int imagine = 0); int getReal ( ) const; int getImagine ( ) const; void setReal (int n); void setImagine (int d); }; • 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

  15. 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);

  16. Commonly Overloaded Operators • Unary ++, --, (increment, decrement) ( ), (function call) [ ], (subscript) +, - (sign) • Binary +, -, *, /, % (arithmetic) =, +=, -+, *=, /=, %= (assignment) &, |, ^, ^=, &=, |= (bitwise) ==, !=, >, <, >=, <= (relational) ||, && (logical) <<, >> (shift)

  17. Restrictions • Not all operators can be overloaded. • You can’t make up your own operators. • The order of precedence cannot be changed for overloaded operators. • Default arguments may not be used with overloaded operators • Overloading must be explicit, i.e. overloading + does not imply += is overloaded • Badly overloaded operators can confuse a programmer

More Related