CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 07
Class Details You can’t initialize data members in class interface class roster { private: int numStudents; int maxStudents = 30; char** names; public: roster(); //and so on }; Can’t do this.
Source Code Organization You have options – As you’ve been proceeding Interface:.h file Implementation:.cpp file Very common class roster { private: int numStudents; char ** names; public: roster(); }; roster::roster () { //constructor code } roster.h roster.cpp
Source Code Organization You have options – All in.h file but interface still separate from implementation class roster { private: int numStudents; char ** names; public: roster(); }; roster::roster() { //stuff } roster.h
Source Code Organization You have options – inline in the interface – method implementations actually appear in the class interface. class roster { private: int numStudents; char ** names; public: roster() { //constructor code here } int getNumStudents () { return numStudents; } }; roster.h
Constant Data Members and Initialization constant data members initialized using member initialization syntax class roster { private: int numStudents; const int maxStudents; char ** names; public: roster(int nums, int max); //and so on }; roster::roster(int nums, 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: int numStudents; const int maxStudents; char ** names; public: roster(int nums, int max); //and so on }; roster::roster(int nums, 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
?