operator Overloading & Data Conversion

download operator Overloading & Data Conversion

of 39

Transcript of operator Overloading & Data Conversion

  • 7/27/2019 operator Overloading & Data Conversion

    1/39

    C++ permits us to add two variables of user-defined types with the same syntax that is applied

    to the basic types.C++ has the ability to provide

    special meaning for a data type. The mechanism of giving such a special meaning to an operator isknown as operator overloading

  • 7/27/2019 operator Overloading & Data Conversion

    2/39

    The operator overloading feature is one of

    the method realizing Polymorphism .Operator overloading allows to provideadditional meaning to already existingoperator.Operator overloading extends the semanticsof an operator without changing its syntax.Precedence and associativity remains samefor overloaded operators.Overloaded operator should not change itsoriginal meaning.

  • 7/27/2019 operator Overloading & Data Conversion

    3/39

    Operator can be unary or binary operator.

  • 7/27/2019 operator Overloading & Data Conversion

    4/39

    Operators that cannot be overloaded

    . .* :: ?:

    Operators that cannot be overloaded

    Cannot create new operators .

    Operators must be overloaded explicitly .

    Overloading + and = does not overload +=Operator ?: cannot be overloaded.

  • 7/27/2019 operator Overloading & Data Conversion

    5/39

    When an operator is overloadedWe cannot change its syntaxThe grammatical rules that govern its use

    such as : No of operandsPrecedence andAssociativity

    For example: the multiplication operator will enjoy higher precedence than

    the addition operator.

  • 7/27/2019 operator Overloading & Data Conversion

    6/39

    return type is the type of value returned bythe specified operation.op is the operation being overloaded.

    Operator function must be either member-function or friend function.

    return type classname:: operator(op-arglist){

    functon-body}

  • 7/27/2019 operator Overloading & Data Conversion

    7/39

    Friend Function(ff) Member Function(mf)1) A ff will have only one 1) A mf function will haveArgument for unary no arguments for unaryoperators. operators.2) A ff will have two 2)A mf will have only one

    argument for argument for binary binary operators. operators.

    because object used to

    invoke the mf is passedimplicitly therefore isavailable for mf

    This is not case withfriend function.Arguments may bepassed either byvalue or reference.

  • 7/27/2019 operator Overloading & Data Conversion

    8/39

    vector operator+(vector); //vector addition

    vector operator-(); // unary minus

    friend vector operator+(vector,vector) ;//vector additn

    friend vector operator-(vector); // unary minus

    int operator==(vector); // comparison

    friend int operator==(vector,vector);//comparison

    vector is a data type of class

  • 7/27/2019 operator Overloading & Data Conversion

    9/39

    Create a class that defines the data type that isto be used in overloading operation.

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

    It may be either friend function or memberfunction.

    Define the operator function to implement therequired operations.

  • 7/27/2019 operator Overloading & Data Conversion

    10/39

    A minus operator when used as unary, takes justone operand.Minus operator changes the sign of an operandwhen applied to basic data item.

    Now we will see how to overload this operator sothat it can be applied to an object in much thesame way as applied to an int or float.

    The unary minus when applied to an objectshould change the sign of each data items.

  • 7/27/2019 operator Overloading & Data Conversion

    11/39

    #includeclass space{

    int x,y,z;public:

    void getdata(int a,int b,int c);void display();void operator-(); // overload unary minus

    };void space :: getdata(int a,int b,int c){

    x=a;y=b;z=c;

    }void space :: display(){

    cout

  • 7/27/2019 operator Overloading & Data Conversion

    12/39

    It is possible to overload a unary minusoperator using friend functions follows:

    friend void operator-(space &s); //declarationfriend void operator-(space &s) //definition{

    s.x = -s.x;s.y = -s.y;

    s.z = -s.z;}

  • 7/27/2019 operator Overloading & Data Conversion

    13/39

    Overloading Binary Operators: How toadd two complex numbers

    A statement like:C=sum(A , B); // functional notation

    was used. the fnal notation can be replaced by anatural looking expression:

    C=A+B; //arithmetic notation.

    By overloading the + operator using an operator+()function.

  • 7/27/2019 operator Overloading & Data Conversion

    14/39

    #includeclass complex{

    float x;float y;

    public:complex();complex(float real,float imag){

    x=real;y=imag;

    }complex operator+(complex);void display();

    };complex complex :: operator+(complex c){

    complex temp;temp.x= x + c.x;temp.y= y + c.y;return(temp);

    }void complex :: display(){ cout

  • 7/27/2019 operator Overloading & Data Conversion

    15/39

    #include class Exforsys

    {private:int x;int y;public:Exforsys() //Constructor{ x=0; y=0; }Exforsys(int a,int b) //Constructor{ x=a; y=b; }

    void getvalue( ) // Member Function for Inputting values{cout > x;cout > y;

    }

  • 7/27/2019 operator Overloading & Data Conversion

    16/39

    void displayvalue( ) //Member Function{cout

  • 7/27/2019 operator Overloading & Data Conversion

    17/39

    void main( ){

    Exforsys e1,e2,e3; //Objects e1, e2, e3 createdcout

  • 7/27/2019 operator Overloading & Data Conversion

    18/39

    #include class Example{private:int count;public:Example() : count(0) //Constructor{ }Example(int c) : count(c) // Parametrized Constructor{ }void getcount(){cout

  • 7/27/2019 operator Overloading & Data Conversion

    19/39

    Example operator ++(Example);};

    Example Example :: operator ++ (void)//unary operator overloading for ++ operator defined

    {int c;c = ++count;

    return Example(c);

    }

    Int main(){Example c1(100), c2,c3;c3 = ++c1;

    c3.getcount();C3 = ++c2;c3.getcount();return 0;

    }

  • 7/27/2019 operator Overloading & Data Conversion

    20/39

  • 7/27/2019 operator Overloading & Data Conversion

    21/39

    Classes and ObjectsOperator Overloading and Friend Functions// OPERATOR: an operator function for vector addition#include class vector {public:vector(float xx=0, float yy=0){ x = xx; y = yy;}void printvec()const;

    void getvec(float &xx, float &yy)const{ xx = x; yy = y;}private:float x, y;};

  • 7/27/2019 operator Overloading & Data Conversion

    22/39

    Use operator overloading to improvereadability Avoid excessive or inconsistent usageFormat Write function definition as normal Function name is keyword operator followed by

    the symbol for the operator being overloaded. operator+ would be used to overload the

    addition operator ( + )

  • 7/27/2019 operator Overloading & Data Conversion

    23/39

    #include class Index{private:int value;public:Index() : value(0) //Constructor{ }Index(int c) : value(c) // Parametrized Constructor{ }int getindex(){return value;

    }

  • 7/27/2019 operator Overloading & Data Conversion

    24/39

    Index operator++(){

    value = value+1;return Index(value);

    }};Void main(){Index idx1, idx2;

    Cout

  • 7/27/2019 operator Overloading & Data Conversion

    25/39

    When we return a nameless temporary object,

    there must be a parameterized constructor inthe class definition.Index(int value){

    value = val;}

  • 7/27/2019 operator Overloading & Data Conversion

    26/39

    The prefix notation causes a variable to be

    updated before its value is used.++XThe postfix notation causes it to be updatedafter its value is used.

    X++But in our program the statements

    Idx1 = idx2++; & idx1 = ++idx2;

    have same effect.

  • 7/27/2019 operator Overloading & Data Conversion

    27/39

    C++ provides additional syntax to express

    and distinguish between prefix and postfixoverloaded operator function.A new syntax for indicating postfix operatoroverloading is used

    operator ++ ( int )

  • 7/27/2019 operator Overloading & Data Conversion

    28/39

    #include class Index{private:int value;public:Index() : value(0) //Constructor{ }Index(int c) : value(c) // Parametrized Constructor{ }int getindex(){return value;

    }

  • 7/27/2019 operator Overloading & Data Conversion

    29/39

    Index operator++ (){return Index(++value);

    }Index operator++ ( int ){return Index(value++);

    }

    };Void main(){Index idx1(2), idx2(2), idx3, idx4;

    Cout

  • 7/27/2019 operator Overloading & Data Conversion

    30/39

  • 7/27/2019 operator Overloading & Data Conversion

    31/39

    Representing the same data in multiple forms

    involves the conversion of data from oneform to another. For exampleDegree = Radian

    Or

    Radian = DegreeIn C++, implicit conversion is achieved byoverloading the assignment operator.

    Var1 = Var2

    Obj1 = Obj2 + Obj3This is applicable for basic data types anduser defined data types.

  • 7/27/2019 operator Overloading & Data Conversion

    32/39

    Example

    int I;float F;F = I; // Conversion from integer to floatI = F; // Conversion from float to integer

    The compiler calls the special routine toconvert these values.This feature of compiler is known as implicit

    type conversion.

  • 7/27/2019 operator Overloading & Data Conversion

    33/39

    Explicit conversion using typecast operator

    F = (float) I;Or

    F = float (I);

  • 7/27/2019 operator Overloading & Data Conversion

    34/39

    User can not rely on the compiler to perform

    conversion from user defined to primitivedata types and vise-versa.

    Basic type to user defined conversionConstructor(Basic Type){// steps for converting basic type to

    // object attributes}

  • 7/27/2019 operator Overloading & Data Conversion

    35/39

    User defined type to Basic type conversion:

    achieved through operator function, whichmust be defined as overloaded basic datatype with no argument.operator BasicType()

    {// steps for converting object attributes// to basic data types

    }

  • 7/27/2019 operator Overloading & Data Conversion

    36/39

    #include class Meter{private:float length;public:Meter() //Constructor{length = 0.0 ;}Meter(float initlength) //Parametrized Constructor{length = initlength / 100.0 ; // cm to meter}operator float(){float lengthCms;lengthCms = length * 100.0 ; // meter to cmreturn (lengthCms);

    }

  • 7/27/2019 operator Overloading & Data Conversion

    37/39

    Void getlength(){

    cout>length;

    }Void showlength(){

    cout

  • 7/27/2019 operator Overloading & Data Conversion

    38/39

    Void main(){

    Meter meter1;float length 1;cout> length1;meter1 = length1; // basic to class typemeter1.showlength();

    Meter meter2;float length2;meter2.getlength();length2 = meter2; // class type to basiccout

  • 7/27/2019 operator Overloading & Data Conversion

    39/39