objects inheritance wrapup class descrip5on of a data
play

Objects/Inheritance Wrapup Class : descrip5on of a data type - PowerPoint PPT Presentation

Objects/Inheritance Wrapup Class : descrip5on of a data type that can contain fields (variables) and methods (func5ons) Think of a class as a


  1. Objects/Inheritance ¡Wrapup ¡

  2. • Class : ¡descrip5on ¡of ¡a ¡data ¡type ¡that ¡can ¡ contain ¡fields ¡(variables) ¡and ¡methods ¡ (func5ons) ¡ – Think ¡of ¡a ¡class ¡as ¡a ¡template ¡for ¡crea5ng ¡objects. ¡ • Object : ¡a ¡par5cular ¡instance ¡of ¡a ¡class. ¡ point ¡is ¡the ¡class. ¡ class ¡point ¡{ ¡… ¡}; ¡ p1 ¡and ¡p2 ¡are ¡objects ¡ point ¡p1, ¡p2; ¡ of ¡the ¡point ¡class. ¡

  3. • When ¡a ¡class ¡is ¡a ¡par5cular ¡kind ¡of ¡another ¡ class, ¡use ¡ inheritance . ¡ class ¡X ¡{ ¡void ¡f(); ¡}; ¡ class ¡Y ¡: ¡public ¡X ¡{ ¡void ¡g(); ¡}; ¡ void ¡X::f() ¡{ ¡cout ¡<< ¡"Base ¡f"; ¡} ¡ void ¡Y::g() ¡{ ¡cout ¡<< ¡"Derived ¡g"; ¡} ¡ ¡ Prints ¡"Base ¡f" ¡ X ¡ex; ¡Y ¡why; ¡ ex.f(); ¡ Prints ¡"Base ¡f" ¡ why.f(); ¡ Prints ¡"Derived ¡g" ¡ why.g(); ¡

  4. • A ¡derived ¡class ¡is ¡allowed ¡to ¡ override ¡methods ¡ in ¡the ¡base ¡class. ¡ class ¡X ¡{ ¡void ¡f(); ¡}; ¡ class ¡Y ¡: ¡public ¡X ¡{ ¡ void ¡f(); ¡}; ¡ void ¡X::f() ¡{ ¡cout ¡<< ¡"Base ¡f"; ¡} ¡ void ¡Y::f() ¡{ ¡cout ¡<< ¡"Derived ¡f"; ¡} ¡ ¡ X ¡ex; ¡Y ¡why; ¡ Prints ¡"Base ¡f" ¡ ex.f(); ¡ why.f(); ¡ Prints ¡"Derived ¡f" ¡

  5. • If ¡a ¡derived ¡class ¡overrides ¡a ¡method, ¡the ¡ overridden ¡method ¡code ¡can ¡s5ll ¡call ¡the ¡base ¡ class ¡version ¡of ¡the ¡method ¡if ¡needed. ¡ class ¡X ¡{ ¡void ¡f(); ¡}; ¡ class ¡Y ¡: ¡public ¡X ¡{ ¡ void ¡f(); ¡}; ¡ void ¡X::f() ¡{ ¡cout ¡<< ¡"Base ¡f"; ¡} ¡ void ¡Y::f() ¡{ ¡ X::f(); ¡ cout ¡<< ¡"Derived ¡f"; ¡} ¡ ¡ Prints ¡"Base ¡f" ¡ X ¡ex; ¡Y ¡why; ¡ ex.f(); ¡ Prints ¡"Base ¡f ¡Derived ¡f" ¡ why.f(); ¡

  6. • Some5mes ¡a ¡class ¡needs ¡access ¡to ¡"itself" ¡as ¡a ¡ stand-­‑alone ¡object: ¡ ¡ class ¡X ¡{ ¡void ¡f(); ¡}; ¡ ¡ void ¡g(const ¡X ¡& ¡ex) ¡{ ¡… ¡} ¡ ¡ void ¡X::f() ¡{ ¡ ¡ ¡// ¡how ¡can ¡I ¡call ¡g ¡on ¡myself? ¡ } ¡

  7. • Every ¡object ¡has ¡a ¡special ¡variable ¡called ¡ this ¡ that ¡is ¡available ¡to ¡be ¡used ¡inside ¡any ¡method ¡ in ¡the ¡class. ¡ • this ¡is ¡always ¡a ¡pointer ¡to ¡the ¡object ¡itself. ¡ • In ¡other ¡words, ¡for ¡a ¡class ¡X, ¡the ¡data ¡type ¡of ¡ this ¡is ¡ X* . ¡

  8. • Some5mes ¡a ¡class ¡needs ¡access ¡to ¡"itself" ¡as ¡a ¡ stand-­‑alone ¡object: ¡ ¡ class ¡X ¡{ ¡void ¡f(); ¡}; ¡ ¡ void ¡g(const ¡X ¡& ¡ex) ¡{ ¡… ¡} ¡ ¡ void ¡X::f() ¡{ ¡ ¡ ¡ g(*this); ¡ } ¡

  9. • We ¡know ¡that ¡the ¡keyword ¡const ¡declares ¡ that ¡a ¡func5on ¡will ¡not ¡change ¡an ¡argument: ¡ ¡ void ¡g(const ¡vector<int> ¡& ¡vec) ¡{ ¡… ¡} ¡ ¡

  10. • We ¡know ¡that ¡the ¡keyword ¡const ¡declares ¡ that ¡a ¡func5on ¡will ¡not ¡change ¡an ¡argument: ¡ ¡ void ¡g(const ¡vector<int> ¡& ¡vec) ¡{ ¡… ¡} ¡ ¡ • This ¡const ¡keyword ¡can ¡also ¡be ¡used ¡with ¡a ¡ class's ¡methods ¡to ¡declare ¡that ¡the ¡method ¡ will ¡not ¡change ¡any ¡of ¡the ¡object's ¡fields. ¡ ¡

  11. class ¡point ¡{ ¡ ¡ ¡public: ¡ ¡ ¡ ¡ ¡ ¡int ¡get_x(); ¡ ¡ ¡ ¡ ¡int ¡get_y(); ¡ ¡ ¡private: ¡ ¡ ¡ ¡ ¡int ¡x, ¡y; ¡ }; ¡ int ¡point::get_x() ¡{ ¡ ¡ ¡return ¡x; ¡ } ¡ int ¡point::get_y() ¡{ ¡ ¡ ¡return ¡y; ¡ } ¡ ¡ ¡

  12. class ¡point ¡{ ¡ ¡ ¡public: ¡ ¡ ¡ ¡ ¡ ¡int ¡get_x() ¡ const ; ¡ ¡ ¡ ¡ ¡int ¡get_y() ¡ const ; ¡ ¡ ¡private: ¡ ¡ ¡ ¡ ¡int ¡x, ¡y; ¡ }; ¡ int ¡point::get_x() ¡ const ¡{ ¡ ¡ ¡return ¡x; ¡ } ¡ int ¡point::get_y() ¡ const ¡{ ¡ ¡ ¡return ¡y; ¡ } ¡ ¡ ¡

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