12438_12 Operator Overloading

download 12438_12 Operator Overloading

of 13

Transcript of 12438_12 Operator Overloading

  • 7/29/2019 12438_12 Operator Overloading

    1/13

    Operator Overloading

  • 7/29/2019 12438_12 Operator Overloading

    2/13

    Introduction It is one of the important features of C++

    language Compile time polymorphism.

    Using overloading feature, we can add two user

    defined data types such as, objects, with thesame syntax, just as basic data types.

    We can overload almost all the C++ operators

    except the following:

    Class member access operators( ., .*)

    Scope resolution operator (::)

    Size operator (sizeof)

    Conditional operator (?:)

  • 7/29/2019 12438_12 Operator Overloading

    3/13

    Defining Operator Overloading

    It is done with the help of a special function,called operator function, which describes the

    special task to an operator.

    General form:return typeclassname :: operator op (argulist)

    {

    function body //task defined}

    where return typetype of value returned by the specified

    operation & op operator being overloaded.

    operator op function name

  • 7/29/2019 12438_12 Operator Overloading

    4/13

    Steps :

    Create a class that defines the data type i.e.

    to be used in the overloading operation.

    Declare the operator function operator op() inthe public part of the class.

    Note: It may be either a member function or a

    friend function.

    Define the operator function to implement the

    required operations.

  • 7/29/2019 12438_12 Operator Overloading

    5/13

    Rules for overloadingoperators Only existing operators can be overloaded. New

    operators cannot be created.

    At least one operand (user defined type).

    Cannot change the basic meaning of the operator.

    Follow the syntax rules of the original operators.

    Some operators cannot be overloaded.

    Cannot use friend function to overload certain operators.

    When using binary operators overloaded through amember function, the left hand operand must be an

    object of the relevant class.

    Binary arithmetic operators must explicitly return a value.

  • 7/29/2019 12438_12 Operator Overloading

    6/13

    Not used / overloaded

    Operators that cannot Where a friend

    be overloaded function cant be used

    Sizeof = (assignment)

    . (membership operator) ( ) (function call)

    .* (pointer to member operator) [ ] (subscripting)

    :: (class member access)

    ?:

  • 7/29/2019 12438_12 Operator Overloading

    7/13

    Overloading of Unary Operators

    ++ & -- Operator

    Unary Operator

    >> &

  • 7/29/2019 12438_12 Operator Overloading

    8/13

    Overloading of Binary Operators

    Arithmetic operators (+, - , *, /)

    Relational Operators (, ==, =, !=)

  • 7/29/2019 12438_12 Operator Overloading

    9/13

    Program: - Arithmetic Operatorclass complex

    { float x,y;

    public:complex() { } int main()

    complex(float real, float img) {

    { x = real; y = img;} complex c1, c2, c3;

    complex operator + (complex); c1 = complex(2.5, 3.5);

    void display(void); } ; c2 = complex(1.5, 2.5);

    complex complex :: operator + (complex c) c3 = c1 + c2;

    { complex temp; cout

  • 7/29/2019 12438_12 Operator Overloading

    10/13

    Explain coding

    complex complex :: operator + (complex c)

    { complex temp;

    temp.x = x + c.x;

    temp.y = y + c.y;

    return (temp);}

    Features: -1. it receives only one complex type argumentexplicitly

    2. It returns a complex type value.

    3. It is a member function ofcomplex.

  • 7/29/2019 12438_12 Operator Overloading

    11/13

    Overloading Binary OperatorsUsing Friends

    Friend functions may be used in the place of

    member functions for overloading a binary

    operator.

    Difference : - a friend function requires two

    arguments to be explicitly passed to it, while

    a member function requires only one.

  • 7/29/2019 12438_12 Operator Overloading

    12/13

    Using Friend Function

    Replace the member function declaration by the friend

    function declaration.

    friend complex operator + (complex, complex);

    Redefine the operator function as follows: complex operator + (complex a, complex b)

    {

    return complex ((a.x + b.x), (a.y + b.y));

    }

    In this case, the statement c3 = c1 + c2; is equivalent to

    c3 = operator + (c1, c2);

  • 7/29/2019 12438_12 Operator Overloading

    13/13

    Thank You!!!