運算子多載 operator overloading - csie.ntu.edu.twr95116/ca200/slide/c3_overloading.pdf ·...

40
講師:洪安 多載 Overloading 1

Upload: lamminh

Post on 04-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • Overloading

    1

  • ()

    function overloading ?

    operator overloading function overloading

    ()

    2

  • Function Overloading

    3

    (Signature)

    Animal::Animal(int a, const char * cat = Elephant){ age = a; strcpy(category,cat);}

    Animal ob1(16);

    Animal ob2(18,Dog);

  • 4

    print()

    print()

    3_Print.cpp

  • ()

    ( + = )

    5

    class frac {} ;int main() {

    int x=5, y =3, z ; z = x + y ; // =+int a[10], b[10], c[10]; c = a + b ; // ???frac f1(3,5), f2(2, 5), f3 ;f3 = f1 + f2 ; // 3/5 + 2/5 = 1, ?return 0;

    }

    += frac

  • 6

    int main() {

    frac f1(3,5), f2(2, 5) , f3 ;

    f3.set(f1.add(f2)) ; // f3 = f1 + f2 ;

    if (f1.great_equal(f2)) // f1 >= f2

    cout = f2 ;

    cout

  • 7

    +, -, *, /, =, >, , =,

  • 8

    C++

  • 9

    C++

  • 10

    C++

  • 11

    class Coord {

    int x, y ;

    } ;

    int main() {

    Coord o1(10,10), o2(5,3), o3 ;

    o3 = o1 + o2; // o3 (15, 13)

    o3.print() ; //later, you can use cout

  • 12

    += (1/4)class Coord {

    int x, y ;

    } ;

    int main() {

    Coord o1(10,10), o2(5,3), o3 ;

    o3.set(o1.add(o2)); // o3 = o1 + o2 ;

    o3.print() ; //later, you can use cout

  • 13

    += (2/4)class Coord {

    int x, int y;

    public:

    Coord(){}

    Coord(int a, int b){x=a; y=b;}

    Coord add(Coord c) {

    Coord temp ;

    temp.x = x + c.x ;

    temp.y = y + c.y ;

    return temp ;

    }

    void set(Coord c) {

    x = c.x; y=c.y;

    }

    void print(){cout

  • 14

    3_NotYet.cpp

    #include

    using namespace std;

    class Coord {

    int x,y;

    public:

    Coord();

    Coord(int, int);

    void print();

    } ;

    Coord::Coord(int a,int b)

    {

    x=a;

    y=b;

    }

    C++

  • 15

    += (3/4)

    class Coord {

    Coord operator+(Coord c) { //add()

    Coord temp ;

    temp.x = x + c.x ;

    temp.y = y + c.y ;

    return temp ;

    }

    void operator=(Coord c) { // set()

    x = c.x; y=c.y;

    }

    } ;

    void main() {

    Coord o1(10,10), o2(5,3), o3 ;

    // o3.set(o1.add(o2));

    o3.operator=(o1.operator+(o2));

    }

    C++

  • += (4/4)

    16

    class Coord {

    Coord operator+(Coord c) { //add()

    coord temp ;

    temp.x = x + c.x ;

    temp.y = y + c.y ;

    return temp ;

    }

    void operator=(Coord c) { // set()

    x = c.x; y=c.y;

    }

    } ;

    void main() {

    Coord o1(10,10), o2(5,3), o3 ;

    // o3.set(o1.add(o2));

    // o3.operator=(o1.operator+(o2);

    o3 = o1 + o2 ;

    }

    C++

  • 17

    o3 = o1 + o2 ;

    o3.operator=(o1.operator+(o2)) ;

    o3 = o1.operator+(o2) ;

    C++

  • 18

    (operator function)

    class {// overload X operatorX() { }

    };

    C++

  • 19

    classclass Coord {

    Coord operator+(Coord c) ;

    void operator=(Coord c) ;

    } ;

    Coord Coord::operator+(Coord c) {.}

    void Coord::operator=(Coord c) {}

    C++

  • 20

    (3_OpOverload.cpp)

    int main() {

    Coord o1(10,10), o2(5,3), o3 ;

    o3=o1+o2; // o3 = o1 + o2 ;

    o3.print() ; //later, you can use cout

  • Call by reference

    class Coord {

    Coord operator+(const Coord& c) ;

    void operator=(const Coord& c) ;

    } ;

    Coord Coord::operator+(const Coord& c) {}

    void Coord::operator=(const Coord& c) {}

    21

    ?

    C++

    operator+temp, operator=, Coord cconst,

  • 22

    Call by reference (3_reference.cpp)

    reference

    int main() {

    Coord o1(10,10), o2(5,3), o3;

    o3=o1+o2; // o3 = o1 + o2 ;

    o3.print(); //later, you can use cout

  • 23

    o4 = o1+o2+o3;

    o4.operator=(o1+o2+o3);

    o4.operator=(o1.operator+(o2+o3));

    o4.operator=(o1.operator+(o2.operator+(o3)));

    o1=o2=o3;

    o1.operator=(o2=o3);

    o1.operator=(o2.operator=(o3));

    C++

  • 24

    Coord operator+(Coord c) { //add()Coord temp ;temp.x = x + c.x ; temp.y = y + c.y ;return temp ;

    }

    o1.operator+(o2+o3);

    void operator=(Coord c) { // set()x = c.x; y=c.y;

    }

    o1.operator=(o2=o3);

    Coord Coord::operator=(Coord c) {

    x = c.x;

    y=c.y;

    return *this;

    }

  • 25

    (3_ContAdd.cpp)

    int main() {

    Coord o1(10,10), o2(5,3), o3(3,2),o4,o5 ;

    o5=o4=o1+o2+o3; // o3 = o1 + o2 ;

    o5.print() ; //later, you can use cout

  • 26

    (Binary Operators)

    : +, -, *, /, =, +=, -=,.,

    : >, =,

  • 27

    (1/2)

    class Coord {

    int x, y ;

    public:

    Coord() {x = 0 ; y=0 ; }

    Coord(int i, int j) { x=i; y=j;}

    bool operator==(const Coord& ob2) ;

    };

    int main() { Coord o1(10,10); o2(5,3) ;

    if (o1 == o2) cout

  • 28

    (2/2)

    bool Coord::operator==(const Coord& ob2) {

    return (this->x==ob2.x) && (this->y==ob2.y);

    }

    C++

  • 29

    == (3_Logic.cpp)

    int main() {

    Coord o1(10,10), o2(3,5), o3;

    if (o1==o2)cout

  • 30

    ++, --, +, -

    int main() {

    Coord o1(10, 10), o2 ;o2 = ++o1 ; o1.print() ; // ++, prefixo2 = o1++ ; o1.print() ; //++, postfixreturn 0;

    }

    o1.operator++()

    C++

  • 31

    prefixpostfix ++class Coord {

    int x, int y;

    public:

    Coord(){}

    Coord(int a, int b){x=a; y=b;}

    Coord& operator++() { // prefix ++

    x++; y++ ; return *this;

    }

    Coord operator++(int) {//postfix ++

    Coord temp = *this ;

    x++; y++ ;

    return temp ;

    }

    }

    ++o1 ;

    C++

    ++--,

    Q: void operator++()?

    o1++ ;

  • 32

    Operator ++ ++d1

    d1.operator++()

    Date &operator++()

    d1++

    d1.operator++(0)

    Date operator++(int)

    C++

  • 33

    ++ (3_AddAdd.cpp)

    int main() {Coord o1(10,10),o2;

    cout

  • 34

    o3 = o1+o2 ; o3 = o1.operator+(o2) ;

    o3 = o1+5 ; o3 = o1.operator+(5) ;

    o3 = 10 + o2 ; o3 = 10.operator+(o2);

    ?????

    !

    C++

  • 35

    #include

    class Coord {

    int x, y ;

    } ;

    Coord operator+(Coord ob1, Coord ob2) {

    return Coord(ob1.x+ob2.x, ob1.y+ob2.y) ;

    }

    void main() {

    Coord o1(10,10), o2(5, 3), o3;

    o3 = o1 + o2 ;

    }

    // o3 = operator+(o1, o2) ;

    operator+(Coord, Coord)Q1:class Coord?Q2:x, y ?

    friend Coord operator+(Coord ob1, Coord ob2) ;

    C++

  • 36

    10+o2class Coord{

    int x, y ;

    public:

    Coord(){x=0;y=0;}

    Coord(int a){x=a;y=a;}

    Coord(int a, int b){x=a;y=b;}

    friend Coord operator+(Coord ob1, Coord ob2);

    void print(){cout

  • operator+() 12

    1

    2

    37

  • >

    > ob1

    38

  • 3_OutputOverloading.cpp print()