Chapter 6. Overloading Operator

Embed Size (px)

Citation preview

  • 8/17/2019 Chapter 6. Overloading Operator

    1/27

    Chapter 5. OVERLOADING OPERATOR

    Object-oriented programs use polymorphism to carry out the sameoperation in a manner customized to the object. Withoutpolymorphism, you would have to use a separate module or

    method name for a method that multiplies two numbers and onethat multiplies three numbers. Just as your blender can produce juice regardless of whether you insert two fruits or three vegetables,using a polymorphic, object-oriented multiplication function callwill produce a correct product whether you send it two integers or

    three floatingpoint numbers. This is how the English languageworks; you understand words based on their context.

    Paris Saclay Minh Quang NGUYEN 1

  • 8/17/2019 Chapter 6. Overloading Operator

    2/27

    Chapter 5. OVERLOADING OPERATOR

    Chapter 5. OVERLOADING OPERATOR

    Minh Quang NGUYEN

    Hanoi National University of Education

    September 2015

    Paris Saclay Minh Quang NGUYEN 2

  • 8/17/2019 Chapter 6. Overloading Operator

    3/27

    Chapter 5. OVERLOADING OPERATOR

    Paris Saclay Minh Quang NGUYEN 3

  • 8/17/2019 Chapter 6. Overloading Operator

    4/27

    Chapter 5. OVERLOADING OPERATOR

    Understanding polymorphism

    What is polymophism?

     Polymorphism is the object-oriented program feature thatallows the same operation to be carried out differentlydepending on the object.

     When you write C++ (and other object-oriented) programs,you can send the same message to different objects anddifferent types of objects.

     Example: there are two members of the   Employee  class,  clerk

    and   driver. You can display the ID number of each memberwith the following statements:

    cout

  • 8/17/2019 Chapter 6. Overloading Operator

    5/27

    Chapter 5. OVERLOADING OPERATOR

    Understanding polymorphism

    What is polymophism?

     Polymorphism is the object-oriented program feature thatallows the same operation to be carried out differentlydepending on the object.

     When you write C++ (and other object-oriented) programs,you can send the same message to different objects anddifferent types of objects.

     Example: there are two members of the   Employee  class,  clerk

    and   driver. You can display the ID number of each memberwith the following statements:

    cout

  • 8/17/2019 Chapter 6. Overloading Operator

    6/27

    Chapter 5. OVERLOADING OPERATOR

    Understanding polymorphism

    What is polymophism?

     Polymorphism is the object-oriented program feature thatallows the same operation to be carried out differentlydepending on the object.

     When you write C++ (and other object-oriented) programs,you can send the same message to different objects anddifferent types of objects.

     Example: there are two members of the   Employee  class,  clerk

    and   driver. You can display the ID number of each memberwith the following statements:

    cout

  • 8/17/2019 Chapter 6. Overloading Operator

    7/27

    Chapter 5. OVERLOADING OPERATOR

    Understanding polymorphism

    What is polymophism?

     Polymorphism is the object-oriented program feature thatallows the same operation to be carried out differentlydepending on the object.

     When you write C++ (and other object-oriented) programs,you can send the same message to different objects anddifferent types of objects.

     Example: there are two members of the   Employee  class,  clerk

    and   driver. You can display the ID number of each memberwith the following statements:

    cout

  • 8/17/2019 Chapter 6. Overloading Operator

    8/27

    Chapter 5. OVERLOADING OPERATOR

    Ad hoc polymorphysm: overloading functions

    Why overloading functions?   In many computer programming languages, each function

    used in a program must have a unique name.  For example, if you want a function to display a value’s square,

    you could create a function that squares integers, a function

    that squares floats, and a function that squares doubles.void squareInteger(int x)

    {

    cout

  • 8/17/2019 Chapter 6. Overloading Operator

    9/27

    Chapter 5. OVERLOADING OPERATOR

    Ad hoc polymorphysm: overloading functions

    Why overloading functions?   In many computer programming languages, each function

    used in a program must have a unique name.  For example, if you want a function to display a value’s square,

    you could create a function that squares integers, a function

    that squares floats, and a function that squares doubles.void squareInteger(int x)

    {

    cout

  • 8/17/2019 Chapter 6. Overloading Operator

    10/27

    Chapter 5. OVERLOADING OPERATOR

    Ad hoc polymorphysm: overloading functions

    Why overloading functions?   In many computer programming languages, each function

    used in a program must have a unique name.  For example, if you want a function to display a value’s square,

    you could create a function that squares integers, a function

    that squares floats, and a function that squares doubles.void squareInteger(int x)

    {

    cout

  • 8/17/2019 Chapter 6. Overloading Operator

    11/27

    Chapter 5. OVERLOADING OPERATOR

    Ad hoc polymorphysm: overloading functions

    What is polymorphism?

     C++ allows you to reuse, or overload, function names.

     When you overload a function, you create multiple functionswith the same name but with different parameter lists.

     For example, each function that squares a value can bear thename square(). If you want three versions of the function, youmust still write three versions of square() - one that accepts anint, one that accepts a double, and one that accepts a float.

    Paris Saclay Minh Quang NGUYEN 11

    Ch te 5 OVERLOADING OPERATOR

  • 8/17/2019 Chapter 6. Overloading Operator

    12/27

    Chapter 5. OVERLOADING OPERATOR

    Ad hoc polymorphysm: overloading functions

    What is polymorphism?

     C++ allows you to reuse, or overload, function names.

     When you overload a function, you create multiple functionswith the same name but with different parameter lists.

     For example, each function that squares a value can bear thename square(). If you want three versions of the function, youmust still write three versions of square() - one that accepts anint, one that accepts a double, and one that accepts a float.

    Paris Saclay Minh Quang NGUYEN 12

    Chapter 5 OVERLOADING OPERATOR

  • 8/17/2019 Chapter 6. Overloading Operator

    13/27

    Chapter 5. OVERLOADING OPERATOR

    Ad hoc polymorphysm: overloading functions

    What is polymorphism?

     C++ allows you to reuse, or overload, function names.

     When you overload a function, you create multiple functionswith the same name but with different parameter lists.

     For example, each function that squares a value can bear thename square(). If you want three versions of the function, youmust still write three versions of square() - one that accepts anint, one that accepts a double, and one that accepts a float.

    Paris Saclay Minh Quang NGUYEN 13

    Chapter 5 OVERLOADING OPERATOR

  • 8/17/2019 Chapter 6. Overloading Operator

    14/27

    Chapter 5. OVERLOADING OPERATOR

    Ad hoc polymorphysm: overloading functions

    What is polymorphism?

     C++ allows you to reuse, or overload, function names.

     When you overload a function, you create multiple functionswith the same name but with different parameter lists.

     For example, each function that squares a value can bear thename square(). If you want three versions of the function, youmust still write three versions of square() - one that accepts anint, one that accepts a double, and one that accepts a float.

    Paris Saclay Minh Quang NGUYEN 14

    Chapter 5 OVERLOADING OPERATOR

  • 8/17/2019 Chapter 6. Overloading Operator

    15/27

    Chapter 5. OVERLOADING OPERATOR

    Ad hoc polymorphysm: overloading functions

    Example

    Three functions do the same works.

    void squareInteger(int x)

    {

    cout

  • 8/17/2019 Chapter 6. Overloading Operator

    16/27

    Chapter 5. OVERLOADING OPERATOR

    Ad hoc polymorphysm: overloading functions

    Example

    Three functions do the same works.

    void square(int x)

    {

    cout

  • 8/17/2019 Chapter 6. Overloading Operator

    17/27

    p

    Ad hoc polymorphysm: overloading functions

    Overloading: avoiding ambiguity

      It must be ensured that the compiler can tell which functionto call by making sure each overloaded function has adifferent parameter list.void someFunction(int a) and  someFunction(int b)Which version of a function is used?

    Paris Saclay Minh Quang NGUYEN 17

    Chapter 5. OVERLOADING OPERATOR

  • 8/17/2019 Chapter 6. Overloading Operator

    18/27

    Ad hoc polymorphysm: overloading functions

    Overloading: avoiding ambiguity

      It must be ensured that the compiler can tell which functionto call by making sure each overloaded function has adifferent parameter list.void someFunction(int a) and  someFunction(int b)Which version of a function is used?

    Paris Saclay Minh Quang NGUYEN 18

    Chapter 5. OVERLOADING OPERATOR

  • 8/17/2019 Chapter 6. Overloading Operator

    19/27

    Ad hoc polymorphysm: overloading functions

    Overloading: avoiding ambiguity

      It must be ensured that the compiler can tell which functionto call by making sure each overloaded function has adifferent parameter list.void someFunction(int a) and  someFunction(int b)Which version of a function is used?

    Paris Saclay Minh Quang NGUYEN 19

    Chapter 5. OVERLOADING OPERATOR

  • 8/17/2019 Chapter 6. Overloading Operator

    20/27

    Ad hoc polymorphysm: overloading functions

    Overloading: avoiding ambiguity

      It must be ensured that the compiler can tell which functionto call by making sure each overloaded function has adifferent parameter list.void someFunction(int a) and  someFunction(int b)Which version of a function is used?

    Paris Saclay Minh Quang NGUYEN 20

    Chapter 5. OVERLOADING OPERATOR

  • 8/17/2019 Chapter 6. Overloading Operator

    21/27

    Ad hoc polymorphysm: overloading functions

    Overloading an operator

    When two objects are added using the + operator, the objects donot have to be the same type on both sides of the operator. Forexample, you can add an integer and a double with an expressionsuch as 5 + 7.84.Structure:

    class class_name{

    return_type operator symbol (parameter);

    };

    //implementation

    return_type class_name::operator symbol (parameter){

    return_type x;

    //...

    return x;

    }Paris Saclay Minh Quang NGUYEN 21

    Chapter 5. OVERLOADING OPERATOR

  • 8/17/2019 Chapter 6. Overloading Operator

    22/27

    Ad hoc polymorphysm: overloading functions

    Example

    class complex{

    private:

    int i, r;

    public:complex operator+(int);

    };

    //implementation

    complex complex::operator+(int p)

    {

    complex x;

    x.i = i;

    x.r = r + p;

    return x;

    }

    You do itWrite operator + to add 2complex numbers.

    Paris Saclay Minh Quang NGUYEN 22

    Chapter 5. OVERLOADING OPERATOR

  • 8/17/2019 Chapter 6. Overloading Operator

    23/27

    Ad hoc polymorphysm: overloading functions

    Example

    class complex{

    private:

    int i, r;

    public:complex operator+(int);

    };

    //implementation

    complex complex::operator+(int p)

    {

    complex x;

    x.i = i;

    x.r = r + p;

    return x;

    }

    You do itWrite operator + to add 2complex numbers.

    Paris Saclay Minh Quang NGUYEN 23

    Chapter 5. OVERLOADING OPERATOR

    Ad h l h l d f

  • 8/17/2019 Chapter 6. Overloading Operator

    24/27

    Ad hoc polymorphysm: overloading functions

    Overloading output

      The  

  • 8/17/2019 Chapter 6. Overloading Operator

    25/27

    Ad hoc polymorphysm: overloading functions

    Overloading output

      The  

  • 8/17/2019 Chapter 6. Overloading Operator

    26/27

    Ad hoc polymorphysm: overloading functions

    Overloading output

      The  

  • 8/17/2019 Chapter 6. Overloading Operator

    27/27

    Ad hoc polymorphysm: overloading functions

    Example (cont.)

    Derive class 1: Triangle

    class Triangle: public Shape{

    public:

    float area ()

    {

    return width * height / 2;}

    };

    main() function

    int main (){

    Rectangle rect;

    Triangle tri;

    rect.set_data (5,3);

    tri.set_data (2,5);cout