Combining objects A class can use another class as a - - PowerPoint PPT Presentation

combining objects a class can use another class as a
SMART_READER_LITE
LIVE PREVIEW

Combining objects A class can use another class as a - - PowerPoint PPT Presentation

Combining objects A class can use another class as a member variable (a field). This called object composi<on. Use this when you would


slide-1
SLIDE 1

Combining ¡objects ¡

slide-2
SLIDE 2
  • A ¡class ¡can ¡use ¡another ¡class ¡as ¡a ¡member ¡

variable ¡(a ¡field). ¡

  • This ¡called ¡object ¡composi<on. ¡
  • Use ¡this ¡when ¡you ¡would ¡say ¡"An ¡object ¡of ¡

class ¡A ¡has ¡an ¡object ¡of ¡class ¡B." ¡

– A ¡dog ¡has ¡an ¡owner. ¡ – A ¡car ¡has ¡an ¡engine. ¡ – A ¡student ¡has ¡an ¡advisor. ¡ – A ¡line ¡segment ¡has ¡a ¡star<ng ¡point ¡and ¡an ¡ending ¡

  • point. ¡
slide-3
SLIDE 3

class ¡person ¡{ ¡ ¡ ¡ ¡// ¡things ¡here ¡ }; ¡ ¡ class ¡dog ¡{ ¡ ¡ ¡public: ¡ ¡ ¡ ¡ ¡… ¡ ¡ ¡private: ¡ ¡ ¡ ¡ ¡person ¡owner; ¡ }; ¡

slide-4
SLIDE 4

class ¡point ¡{ ¡ ¡ ¡ ¡// ¡things ¡here ¡ }; ¡ ¡ class ¡line ¡{ ¡ ¡ ¡public: ¡ ¡ ¡ ¡ ¡… ¡ ¡ ¡private: ¡ ¡ ¡ ¡ ¡point ¡start, ¡end; ¡ }; ¡

slide-5
SLIDE 5

class ¡line ¡{ ¡ ¡ ¡public: ¡ ¡ ¡double ¡getLength() ¡{ ¡ ¡ ¡ ¡ ¡return ¡sqrt( ¡ ¡ ¡ ¡ ¡ ¡ ¡pow(start.getX() ¡– ¡end.getX(), ¡2) ¡+ ¡ ¡ ¡ ¡ ¡ ¡ ¡pow(start.getY() ¡– ¡end.getY(), ¡2)); ¡ ¡ ¡} ¡ ¡ ¡private: ¡ ¡ ¡ ¡ ¡point ¡start, ¡end; ¡ }; ¡

slide-6
SLIDE 6
  • Object ¡composi<on ¡is ¡also ¡known ¡as ¡a ¡"has-­‑a" ¡

rela<onship. ¡

  • A ¡different ¡kind ¡of ¡rela<onship ¡is ¡an ¡"is-­‑a" ¡

rela<onship. ¡

  • Use ¡this ¡rela<onship ¡to ¡express ¡when ¡a ¡class ¡

is ¡a ¡specific ¡kind ¡of ¡another ¡class. ¡

– A ¡poodle ¡is ¡a ¡specific ¡kind ¡of ¡dog. ¡ – A ¡racecar ¡is ¡a ¡specific ¡kind ¡of ¡car. ¡

  • This ¡concept ¡is ¡called ¡inheritance. ¡

¡

slide-7
SLIDE 7

Inheritance ¡(is-­‑a) ¡versus ¡composi<on ¡ (has-­‑a) ¡

  • Inheritance ¡expresses ¡that ¡one ¡class ¡can ¡do ¡

everything ¡anther ¡class ¡can ¡do, ¡plus ¡more: ¡

– A ¡racecar ¡is ¡just ¡a ¡car ¡that ¡can ¡also ¡drive ¡extra ¡fast ¡ around ¡a ¡race ¡track. ¡

  • Composi<on ¡expresses ¡that ¡one ¡class ¡is ¡a ¡

component ¡of ¡another ¡class: ¡

– An ¡engine ¡is ¡a ¡piece ¡of ¡a ¡car. ¡

slide-8
SLIDE 8
slide-9
SLIDE 9
  • When ¡a ¡derived ¡class ¡inherits ¡from ¡a ¡base ¡

class: ¡

– Inside ¡the ¡class, ¡the ¡derived ¡class ¡has ¡access ¡to ¡all ¡ the ¡public ¡and ¡protected ¡members ¡of ¡the ¡base ¡

  • class. ¡

– Inside ¡the ¡class, ¡the ¡derived ¡class ¡cannot ¡access ¡ private ¡members. ¡ – Outside ¡the ¡class, ¡the ¡derived ¡class ¡has ¡all ¡the ¡ same ¡public ¡members ¡as ¡the ¡base ¡class ¡has. ¡

  • except ¡constructors ¡
slide-10
SLIDE 10
  • Create ¡a ¡pig ¡struct ¡(one ¡field ¡called ¡energy). ¡
  • Create ¡a ¡bird ¡class. ¡

– A ¡bird ¡object ¡can ¡be ¡launched ¡at ¡a ¡big ¡and ¡ decreases ¡the ¡pig's ¡energy. ¡

  • Create ¡a ¡splitbird ¡class ¡that ¡inherits ¡from ¡bird. ¡

– A ¡splitbird ¡object ¡can ¡also ¡be ¡launched ¡at ¡two ¡pigs ¡

  • simultaneously. ¡