1 / 10

Structs versus Classes

Structs versus Classes. Similarities. Both can have public and private sections. Similarities. Both can have public and private sections Both can hold member variables. Similarities. Both can have public and private sections Both can hold member variables Both can hold member functions.

zahur
Download Presentation

Structs versus Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structs versus Classes

  2. Similarities Both can have public and private sections

  3. Similarities Both can have public and private sections Both can hold member variables

  4. Similarities Both can have public and private sections Both can hold member variables Both can hold member functions

  5. Differences Struct members default to public

  6. Differences Struct members default to public Class members default to private

  7. Equivalent Examples struct boot { float m_size; float m_heelheight; }; • struct boot • { • public: • float m_size; • float m_heelheight; • }; • class boot • { • public: • float m_size; • float m_heelheight; • };

  8. Equivalent Examples class table { private: intm_numLegs; float m_topArea; public: void fold(); }; • class table • { • intm_numLegs; • float m_topArea; • public: • void fold(); • }; • struct table • { • void fold(); • private: • intm_numLegs; • float m_topArea; • };

  9. Traditionally Classes are used when you need an object that has functionality Structs are used when you need an object that is composed entirely of public variables This is a hold-over from the C programming language when there were no classes and structs could not provide functionality

  10. End of Session

More Related