combining objects a class can use another class as a
play

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


  1. Combining ¡objects ¡

  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. ¡

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

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

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

  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. ¡ ¡

  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. ¡

  8. • 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 ¡

  9. • 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. ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend