CS 162 Intro to Programming II Operator Overloading 1 - - PowerPoint PPT Presentation

cs 162 intro to programming ii
SMART_READER_LITE
LIVE PREVIEW

CS 162 Intro to Programming II Operator Overloading 1 - - PowerPoint PPT Presentation

CS 162 Intro to Programming II Operator Overloading 1 Operator Overloading One operator used for different opera;ons or ac;ons Why we need


slide-1
SLIDE 1

CS ¡162 ¡ Intro ¡to ¡Programming ¡II ¡

Operator ¡Overloading ¡

1 ¡

slide-2
SLIDE 2

Operator ¡Overloading ¡

  • One ¡operator ¡used ¡for ¡different ¡opera;ons ¡or ¡

ac;ons ¡ ¡

  • Why ¡we ¡need ¡it? ¡ ¡ ¡
  • How ¡it’s ¡used? ¡ ¡ ¡

2 ¡

slide-3
SLIDE 3

Automa;c ¡Type ¡Conversion ¡

  • When ¡you ¡overload ¡operators, ¡there ¡is ¡a ¡

subtle ¡automa;c ¡type ¡conversion ¡you ¡need ¡to ¡ be ¡aware ¡of ¡

  • We ¡will ¡illustrate ¡this ¡with ¡a ¡point ¡class, ¡which ¡

represents ¡a ¡2 ¡dimensional ¡point ¡

3 ¡

slide-4
SLIDE 4

Automa;c ¡Type ¡Conversion ¡

point.hpp ¡ ¡ #ifndef ¡POINT_HPP ¡ #define ¡POINT_HPP ¡ class ¡point ¡{ ¡ public: ¡ ¡point(int ¡x_value, ¡int ¡y_value) ¡: ¡x(x_value), ¡ ¡ ¡y(y_value) ¡{ ¡} ¡ ¡int ¡get_x() ¡const ¡{ ¡return ¡x; ¡} ¡ ¡int ¡get_y() ¡const ¡{ ¡return ¡y; ¡} ¡ private: ¡ ¡int ¡x; ¡ ¡int ¡y; ¡ }; ¡ ¡ const ¡point ¡operator ¡+(const ¡point& ¡p1, ¡const ¡point& ¡p2); ¡ #endif ¡ ¡

4 ¡

slide-5
SLIDE 5

Automa;c ¡Type ¡Conversion ¡

point.cpp ¡ ¡ #include ¡<iostream> ¡ #include ¡"point.hpp" ¡ ¡ const ¡point ¡operator ¡+(const ¡point& ¡p1, ¡const ¡point& ¡p2) ¡{ ¡ ¡return ¡point(p1.get_x() ¡+ ¡p2.get_x(), ¡ ¡ ¡p1.get_y() ¡+ ¡p2.get_y()); ¡ } ¡ ¡ int ¡main(int ¡argc, ¡char** ¡argv) ¡{ ¡ ¡point ¡p1(1,2); ¡ ¡point ¡p2(3,4); ¡ ¡point ¡p3 ¡= ¡p1+p2; ¡ ¡std::cout ¡<< ¡p3.get_x() ¡<< ¡" ¡" ¡<< ¡p3.get_y() ¡<< ¡std::endl; ¡ } ¡ ¡

¡

5 ¡

slide-6
SLIDE 6

Automa;c ¡Type ¡Conversion ¡

  • Now ¡change ¡the ¡main ¡func;on ¡to ¡look ¡like ¡the ¡

following: ¡

int ¡main(int ¡argc, ¡char** ¡argv) ¡{ ¡ ¡point ¡p1(1,2); ¡ ¡point ¡p2(3,4); ¡ ¡point ¡p3 ¡= ¡p1+10; ¡ ¡std::cout ¡<< ¡p3.get_x() ¡<< ¡" ¡" ¡<< ¡p3.get_y() ¡<< ¡std::endl; ¡ } ¡

The ¡code ¡won’t ¡compile. ¡Why? ¡The ¡operator ¡+ ¡only ¡ accepts ¡objects ¡of ¡type ¡point ¡and ¡10 ¡is ¡an ¡int. ¡

¡

6 ¡

slide-7
SLIDE 7

Automa;c ¡Type ¡Conversion ¡

  • Add ¡a ¡constructor ¡to ¡point.hpp ¡that ¡takes ¡a ¡single ¡int ¡as ¡an ¡argument ¡

¡ class ¡point ¡{ ¡ public: ¡ ¡point(int ¡x_value) ¡: ¡x(x_value), ¡y(0) ¡{ ¡} ¡ ¡point(int ¡x_value, ¡int ¡y_value) ¡: ¡x(x_value), ¡ ¡y(y_value) ¡{ ¡} ¡ ¡int ¡get_x() ¡const ¡{ ¡return ¡x; ¡} ¡ ¡int ¡get_y() ¡const ¡{ ¡return ¡y; ¡} ¡ private: ¡ ¡int ¡x; ¡ ¡int ¡y; ¡ }; ¡ const ¡point ¡operator ¡+(const ¡point& ¡p1, ¡const ¡point& ¡p2); ¡ ¡ ¡

7 ¡

slide-8
SLIDE 8

Automa;c ¡Type ¡Conversion ¡

  • If ¡you ¡now ¡try ¡to ¡compile ¡this ¡main ¡func;on, ¡it ¡

will ¡compile ¡just ¡fine! ¡

int ¡main(int ¡argc, ¡char** ¡argv) ¡{ ¡ ¡point ¡p1(1,2); ¡ ¡point ¡p2(3,4); ¡ ¡point ¡p3 ¡= ¡p1+10; ¡ ¡std::cout ¡<< ¡p3.get_x() ¡<< ¡" ¡" ¡<< ¡p3.get_y() ¡<< ¡std::endl; ¡ } ¡

  • It ¡will ¡output: ¡ ¡ ¡11 ¡2 ¡

8 ¡

slide-9
SLIDE 9

Automa;c ¡Type ¡Conversion ¡

  • C++ ¡will ¡automa;cally ¡convert ¡10 ¡into ¡an ¡
  • bject ¡of ¡type ¡point ¡
  • C++ ¡will ¡call ¡the ¡constructor ¡for ¡point ¡that ¡

takes ¡a ¡single ¡int ¡as ¡an ¡argument ¡

  • This ¡creates ¡a ¡point ¡with ¡x ¡= ¡10 ¡and ¡y ¡=0 ¡

9 ¡

slide-10
SLIDE 10

Operator ¡Overloading ¡

  • Non-­‑member ¡operator ¡overloading ¡

¡ /* ¡In ¡point.cpp ¡*/ ¡ const ¡point ¡operator ¡+(const ¡point& ¡p1, ¡const ¡point& ¡p2) ¡{ ¡ ¡return ¡point(p1.get_x() ¡+ ¡p2.get_x(), ¡p1.get_y() ¡+ ¡p2.get_y()); ¡ } ¡ ¡ /* ¡In ¡point.hpp ¡*/ ¡ class ¡point ¡{ ¡ ¡/* ¡etc. ¡*/ ¡ }; ¡

¡

const ¡point ¡operator ¡+(const ¡point& ¡p1, ¡const ¡point& ¡p2); ¡

10 ¡

slide-11
SLIDE 11

Operator ¡Overloading ¡

  • Member ¡operator ¡overloading ¡

¡ /* ¡In ¡point.cpp ¡*/ ¡ const ¡point ¡point::operator ¡+(const ¡point& ¡p2) ¡{ ¡ ¡return ¡point(x ¡+ ¡p2.get_x(), ¡y ¡+ ¡p2.get_y()); ¡ } ¡

¡

/* ¡In ¡point.hpp ¡*/ ¡ class ¡point ¡{ ¡ public: ¡ ¡/* ¡etc. ¡*/ ¡ ¡const ¡point ¡operator ¡+(const ¡point& ¡p2); ¡ ¡/* ¡etc. ¡*/ ¡ }; ¡

11 ¡

slide-12
SLIDE 12

Operator ¡Overloading ¡

  • Non-­‑member ¡operator ¡overloading ¡

Pros: ¡ Can ¡interchange ¡the ¡order ¡of ¡the ¡arguments ¡to ¡the ¡

  • perator ¡eg. ¡

p1+10 ¡or ¡ 10+p1 ¡

Cons: ¡ Can’t ¡access ¡member ¡variables ¡directly ¡and ¡ incurs ¡overhead ¡of ¡a ¡geher ¡func;on ¡call ¡

12 ¡

slide-13
SLIDE 13

Operator ¡Overloading ¡

  • Member ¡operator ¡overloading ¡

Pros: ¡

  • Can ¡access ¡member ¡variables ¡directly ¡

Cons: ¡

  • ¡Can’t ¡interchange ¡the ¡order ¡of ¡the ¡arguments ¡to ¡

the ¡operator ¡

– ¡p1+10 ¡is ¡allowed ¡because ¡p1 ¡is ¡the ¡calling ¡object ¡ and ¡10 ¡is ¡the ¡argument ¡ – ¡10 ¡+ ¡p1 ¡is ¡not ¡allowed ¡because ¡10 ¡is ¡not ¡of ¡type ¡ Point ¡

13 ¡

slide-14
SLIDE 14

Operator ¡Overloading ¡

  • Can ¡we ¡get ¡the ¡best ¡of ¡both ¡worlds? ¡
  • Yes, ¡using ¡overloading ¡as ¡a ¡friend ¡func;on ¡

14 ¡

slide-15
SLIDE 15

Friends ¡

  • Friends ¡of ¡a ¡class ¡have ¡access ¡to ¡private ¡

member ¡variables ¡and ¡member ¡func;ons ¡of ¡ that ¡class ¡

  • Two ¡types ¡of ¡friends: ¡

–Friend ¡func;ons ¡ –Friend ¡classes ¡

15 ¡

slide-16
SLIDE 16

Friends ¡

  • Overloading ¡as ¡a ¡friend ¡func;on ¡

¡

/* ¡In ¡point.hpp ¡*/ ¡ class ¡point ¡{ ¡ public: ¡ ¡point(int ¡x_value) ¡: ¡x(x_value), ¡y(0) ¡{ ¡} ¡ ¡point(int ¡x_value, ¡int ¡y_value) ¡: ¡x(x_value), ¡y(y_value) ¡{ ¡} ¡ ¡int ¡get_x() ¡const ¡{ ¡return ¡x; ¡} ¡ ¡int ¡get_y() ¡const ¡{ ¡return ¡y; ¡} ¡ ¡friend ¡const ¡point ¡operator ¡+(const ¡point& ¡p1, ¡const ¡point& ¡p2); ¡ private: ¡ ¡int ¡x; ¡ ¡int ¡y; ¡ }; ¡ ¡ ¡

16 ¡

slide-17
SLIDE 17

Friends ¡

  • Overloading ¡as ¡a ¡friend ¡func;on ¡

/* ¡In ¡point.cpp ¡*/ ¡ const ¡point ¡operator ¡+(const ¡point& ¡p1, ¡const ¡point& ¡p2) ¡ { ¡ ¡return ¡point(p1.x ¡+ ¡p2.get_x(), ¡p1.y ¡+ ¡p2.get_y()); ¡ } ¡ Note: ¡the ¡operator ¡is ¡outside ¡the ¡class ¡and ¡has ¡access ¡to ¡ member ¡variables ¡x ¡and ¡y ¡ ¡

17 ¡

slide-18
SLIDE 18

Friends ¡

  • You ¡can ¡also ¡declare ¡a ¡class ¡A ¡to ¡be ¡a ¡friend ¡of ¡

class ¡B ¡

  • This ¡means ¡A ¡has ¡access ¡to ¡the ¡member ¡

variables ¡and ¡member ¡func;ons ¡of ¡B ¡

18 ¡

slide-19
SLIDE 19

Friends ¡

class ¡A; ¡ ¡ class ¡B ¡ { ¡ public: ¡ ¡// ¡etc. ¡ ¡friend ¡class ¡A; ¡ }; ¡ ¡ class ¡A ¡ { ¡ ¡// ¡etc. ¡ }; ¡ ¡

19 ¡

slide-20
SLIDE 20

Advanced ¡Operator ¡Overloading ¡

  • You ¡can ¡also ¡overload ¡operators ¡such ¡as: ¡

<< ¡ >> ¡ = ¡ [ ¡] ¡

  • But ¡be ¡warned… ¡ ¡ ¡

20 ¡

slide-21
SLIDE 21

Overloading ¡<< ¡

  • The ¡inser;on ¡operator ¡<< ¡is ¡typical ¡used ¡as ¡

follows: ¡ std::cout ¡<< ¡“Hello ¡world” ¡<< ¡std::endl; ¡

  • If ¡we ¡rewrite ¡this ¡in ¡our ¡operator ¡“func;on” ¡

nota;on ¡we ¡get: ¡ <<(std::cout, ¡“Hello ¡world”) ¡ ¡

21 ¡

slide-22
SLIDE 22

Overloading ¡<< ¡

  • What ¡if ¡we ¡replace ¡the ¡second ¡parameter ¡with ¡

a ¡point ¡object ¡p1 ¡ <<(std::cout, ¡p1) ¡ ¡

  • This ¡will ¡cause ¡a ¡compile ¡error ¡unless ¡we ¡
  • verload ¡the ¡<< ¡operator ¡
  • This ¡makes ¡it ¡easy ¡to ¡print ¡out ¡point ¡objects! ¡

22 ¡

slide-23
SLIDE 23

Overloading ¡<< ¡

/* ¡In ¡point.hpp ¡*/ ¡ class ¡point ¡{ ¡ public: ¡ ¡/* ¡etc. ¡*/ ¡ ¡friend ¡std::ostream& ¡operator ¡<<(std::ostream& ¡ ¡output_stream, ¡const ¡point& ¡p); ¡ ¡ private: ¡ ¡/* ¡etc. ¡*/ ¡ }; ¡ ¡

23 ¡

slide-24
SLIDE 24

Overloading ¡<< ¡

/* ¡In ¡point.cpp ¡*/ ¡ std::ostream& ¡operator ¡<<(std::ostream& ¡output_stream, ¡ ¡const ¡point& ¡p) ¡{ ¡ ¡std::cout ¡<< ¡"x ¡= ¡" ¡<< ¡p.x ¡<< ¡", ¡y ¡= ¡" ¡<< ¡p.y ¡<< ¡ ¡std::endl; ¡ ¡return ¡output_stream; ¡ } ¡ ¡ int ¡main(int ¡argc, ¡char** ¡argv) ¡{ ¡ ¡point ¡p1(1,2); ¡ ¡point ¡p2(3,4); ¡ ¡point ¡p3 ¡= ¡p1+p2; ¡ ¡std::cout ¡<< ¡p3 ¡<< ¡std::endl; ¡ } ¡

Prints-­‑ ¡x ¡= ¡4 ¡, ¡y ¡= ¡6 ¡

¡

24 ¡

slide-25
SLIDE 25

Overloading ¡<< ¡

  • Note: ¡The ¡overloaded ¡operator ¡<< ¡returns ¡a ¡

reference ¡of ¡type ¡ostream. ¡Why? ¡

¡

  • Allows ¡you ¡to ¡chain ¡<< ¡statements ¡eg. ¡

std::cout ¡<< ¡p3 ¡<< ¡“and ¡more ¡stuff ¡“ ¡<< ¡std::endl; ¡

  • Think ¡of ¡the ¡above ¡like ¡the ¡following: ¡

(((std::cout ¡<< ¡p3) ¡<< ¡“and ¡more ¡stuff ¡“) ¡<< ¡std::endl) ¡

  • All ¡40 ¡operators ¡that ¡can ¡be ¡overloaded ¡ ¡

¡

25 ¡

slide-26
SLIDE 26

Operator ¡Overloading ¡

  • One ¡operator ¡used ¡for ¡different ¡opera;ons ¡or ¡

ac;ons ¡ ¡

  • Why ¡we ¡need ¡it? ¡ ¡ ¡
  • How ¡it’s ¡used? ¡ ¡
  • New ¡access-­‑ ¡friend ¡

¡ ¡ ¡

26 ¡