Operator Overloading

 Operator Overloading

Introduction-

  •  Operator overloading allows you to redefine the way operator works for user-defined types only (objects, structures). It cannot be used for built-in types (int, float, char etc.). Two operators = and & are already overloaded by default in C++. For example: To copy objects of same class, you can directly use = operator


Method of overloading-

  •  Function Overloading
  •  If any class have multiple functions with same names but different parameters then they are said to be overloaded. Function overloading allows you to use the same name for different functions, to perform, either same or different functions in the same class.
  •  Function overloading is usually used to enhance the readability of the program. If you have to perform one single operation but with different number or types of arguments, then you can simply overload the function.
                        int sum (int x, int y)
                    {
                        cout << x+y;
                    }
                        int sum(int x, int y, int z)
                    {
                        cout << x+y+z;
                    }

Overloading unary and binary operators-

  •  an unary operator is used, it works with one operand, therefore with the user defined data types, the operand becomes the caller and hence no arguments are required.
  •  Take a look at the following unary operator overloading example, in this case the unary operators increment (++) and decrement (–):

                    #include<iostream>
                    using namespace std;

                    //Increment and decrement overloading
                    class Inc {
                            private:
                                int count ;
                            public:
                                Inc() {
                                     //Default constructor
                                    count = 0 ;
                          }
                          Inc(int C) {
                                    // Constructor with Argument
                                    count = C ;
                        }
                        Inc operator ++ () {
                                    // Operator Function Definition
                                    return Inc(++count);
                        }
                        Inc operator -- () {
                                    // Operator Function Definition
                                    return Inc(--count);
                        }
                        void display(void) {
                                    cout << count << endl ;
                        }
            };

            void main(void) {
                Inc a, b(4), c, d, e(1), f(4);

                cout << "Before using the operator ++()\n";
                cout << "a = ";
                a.display();
                cout << "b = ";
                b.display();
                ++a;
                b++;
                cout << "After using the operator ++()\n";
                cout << "a = ";
                a.display();
                cout << "b = ";
                b.display();
                c = ++a;
                d = b++;
                cout << "Result prefix (on a) and postfix (on
        b)\n";
                cout << "c = ";
                c.display();
                cout << "d = ";
                d.display();
                cout << "Before using the operator --()\n";
                cout << "e = ";
                e.display();
                cout << "f = ";
                f.display();
                --e;
                f--;
                cout << "After using the operator --()\n";
                cout << "e = ";
                e.display();
                cout << "f = ";
                f.display();
       }

Manipulation of strings using operators-

 C++ Manipulation of Strings. Manipulating of strings in C++ by operator overloading using character arrays, pointers and string functions. There are no operators for manipulating the strings. There are no direct operator that could act upon the strings or manipulate the strings.

                #include<iostream>
                using namespace std;
                int main ()
                {
                string First = "This is First String and ";
                string Second = "This is Second String.";
                string Third = First + Second;
                cout << Third;
                return 0;
                }

  Rules for overloading  operators-

  •  In C++, following are the general rules for operator overloading-
  1.  Only built-in operators can be overloaded. New operators can not be created.
  2. Arity of the operators cannot be changed.
  3.  Precedence and associativity of the operators cannot be changed.
  4. Overloaded operators cannot have default arguments except the function call operator () which can have default arguments.
  5. Operators cannot be overloaded for built in types only. At least one operand must be used defined type.
  6.  Assignment (=), subscript ([]), function call (“()”), and member selection (->) operators must be defined as member functions
  7. Except the operators specified in point 6, all other operators can be either member functions or a non member functions.
  8. Some operators like (assignment)=, (address)& and comma (,) are by default overloaded.

Post a Comment

Previous Post Next Post