1 / 10

CSE 2341 Honors

CSE 2341 Honors. Professor Mark Fontenot Southern Methodist University Note Set 07. Class Details. You can’t initialize data members in class interface. Can’t do this. class roster { private: int numStudents ; int maxStudents = 30; char** names; public: roster();

jennis
Download Presentation

CSE 2341 Honors

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. CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 07

  2. Class Details • You can’t initialize data members in class interface Can’t do this. class roster { private: intnumStudents; intmaxStudents = 30; char** names; public: roster(); //and so on };

  3. Source Code Organization • You have options • As you’ve been proceeding • Interface: .h file • Implementation: .cpp file • Very common roster.h class roster { private: intnumStudents; char ** names; public: roster(); }; roster.cpp roster::roster () { //constructor code }

  4. Source Code Organization • You have options • All in .h file • but interface still separate from implementation roster.h class roster { private: intnumStudents; char ** names; public: roster(); }; roster::roster() { //stuff }

  5. Source Code Organization roster.h • You have options • inline in the interface • method implementations actually appear in the class interface. class roster { private: intnumStudents; char ** names; public: roster() { //constructor code here } intgetNumStudents () { return numStudents; } };

  6. Constant Data Members and Initialization • constant data members initialized using member initialization syntax class roster { private: intnumStudents; const intmaxStudents; char ** names; public: roster(intnums, int max); //and so on }; roster::roster(intnums, int max) { numStudents = nums; maxStudents = max; } What’s wrong here?

  7. Constant Data Members and Initialization • constant data members initialized using member initialization syntax class roster { private: intnumStudents; const intmaxStudents; char ** names; public: roster(intnums, int max); //and so on }; roster::roster(intnums, int max) : maxStudents(max) { numStudents = nums; maxStudents = max; } Executed before the object is fully created; const-ness has not attached yet to variables

  8. Class Relationships • Several types of relationships between two different classes • Major types: • Composition • one classis composed of other class objects and perhaps primitives • Inheritance • one class inherits all of the data and functionality of another class

  9. Composition • An object can be made up of other objects class Computer { private: Motherboard mBoard; Processor proc; Ram ram; public: computer ();}; These aren’t primitives; they are object types

  10. ?

More Related