100 likes | 221 Views
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();
E N D
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: intnumStudents; intmaxStudents = 30; char** names; public: roster(); //and so on };
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 }
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 }
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; } };
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?
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
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
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