Exforsys
+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

operator overloading???

This is a discussion on operator overloading??? within the C and C++ forums, part of the Programming Talk category; what is the difference between overloading '=' operator and using = in normal...... what is he significance of operator overloading???...

  1. #1
    Manyamanas is offline Member Array
    Join Date
    Dec 2010
    Answers
    32

    operator overloading???

    what is the difference between overloading '=' operator and using = in normal......
    what is he significance of operator overloading???


  2. #2
    Halbert's Avatar
    Halbert is offline Junior Member Array
    Join Date
    Jan 2011
    Location
    USA
    Answers
    4
    Hello guys....!
    Operator overload allows you to define a mathematical operator to operate on types other than numeric.Can someone tell me in very simple code that what exactly is operator overloading and what is it mainly used for...i read a couple of articles and all explained it using multidimensional arrays and vectors and i am beginner so really could'nt get it very clearly .


  3. #3
    techvinny is offline Moderator Array
    Join Date
    Dec 2010
    Answers
    56
    hi halbert...operator overloading is widely used to perform evaluations the way you want to use it...
    simplest example is the use of left shift (<<) operator and right shift (>>) operator by cout (cout<<..) and in (in>>...) for standard IO operations...
    another example is the use of + operator by string to concenate two strings...

    likewise, you can have your own meaning defined for the operator...

    HTH!!!


  4. #4
    Akash Thukral is offline Junior Member Array
    Join Date
    Apr 2011
    Answers
    1
    What is overloadimg of binary operator


  5. #5
    sudha06 is offline Junior Member Array
    Join Date
    Mar 2011
    Answers
    15
    In C++ the overloading principle applies not only to functions, but to operators too. That is, the meaning of operators can be extended from built-in types to user-defined types. In this way a programmer can provide his or her own operator to a class by overloading the built-in operator to perform some specific computation when the operator is used with objects of that class. One question may arise here: is this really useful in real world implementations? Some programmers consider that overloading is not useful most of the time. This and the fact that overloading makes the language more complicated is the main reason why operator overloading is banned in Java.

    Even if overloading adds complexity to the language it can provide a lot of syntactic sugar, and code written by a programmer using operator overloading can be easy, but sometimes misleading, to read. We can use operator overloading easily without knowing all the implementation's complexities.


  6. #6
    sofiarunner's Avatar
    sofiarunner is offline Junior Member Array
    Join Date
    Mar 2011
    Location
    NY
    Answers
    15
    Operator overloading is a great feature of Object Oriented Programing Language. Operator overloading enables us to use operator not only with numeric but also on alphabets.


  7. #7
    Sudheer Kethavarapu is offline Junior Member Array
    Join Date
    May 2011
    Answers
    5
    hi guys, first let me tel u what is an operator..
    OPERATOR is a symbol that performs operations(eg addition,substraction, etc..) only on variables and numbers.
    Eg: int a,b,c;
    c=a+b;

    Allowing the operators to perform on objects(instance of a class) also, such feature is allowed in c++ and it is called operator overloading.

    Eg: WITHOUT USING OPERATOR OVERLOADING
    class complex //a combination of real and imaginary number eg: 2+3i
    {
    int real,img;
    public:
    complex add(complex x,complex y)
    {
    complex result;
    result.real=x.real+y.real;
    result.img=x.img+y.img;
    return result;
    }
    void display()
    {
    cout<<endl<<"The complex number is"<<real<<img<0?"+i":"-i"<<img;
    }
    };
    void main()
    {
    complex x,y,r;
    x.real=x.img=2;
    y.real=y.img=3;
    r.real=add(x,y);
    r.display();
    getch();
    }

    Eg: WITH USING OPERATOR OVERLOADING
    class complex //a combination of real and imaginary number eg: 2+3i
    {
    int real,img;
    public:
    complex operator+(complex x,complex y) //here + is overloaded to complex number addition
    {
    complex result;
    result.real=x.real+y.real;
    result.img=x.img+y.img;
    return result;
    }
    void display()
    {
    cout<<endl<<"The complex number is"<<real<<img<0?"+i":"-i"<<img;
    }
    };
    void main()
    {
    complex x,y,r;
    x.real=x.img=2;
    y.real=y.img=3;
    r.real=x+y; // u can simply cal like this no need of function name. we canot aply a operator on object but we achieved with the help of
    operator overloading.
    r.display();
    getch();
    }


    Finaly , u noticed the difference, there is no change in the function code the only difference is function name declaration and calling the function.

    another important feature of operator overloading is we can apply the function(which adds two objects) to any number of objects.
    eg:
    complex x,y,z,r;

    r=add(x,y); //correct
    r=add(x,y,z); //incorrect because we wrote the code for only two object addition


    r=x+y; //correct
    r=x+y+z; //is also correct. although we wrote the code for two objects only.




    yours.
    sudheer


  8. #8
    sofiarunner's Avatar
    sofiarunner is offline Junior Member Array
    Join Date
    Mar 2011
    Location
    NY
    Answers
    15
    Sudher thanks for such a detailed explanation


  9. #9
    sofiarunner's Avatar
    sofiarunner is offline Junior Member Array
    Join Date
    Mar 2011
    Location
    NY
    Answers
    15
    what is the difference between urinary and binary operated over loading


  10. #10
    sofiarunner's Avatar
    sofiarunner is offline Junior Member Array
    Join Date
    Mar 2011
    Location
    NY
    Answers
    15
    any one please answer the question


    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...