Presentation is loading. Please wait.

Presentation is loading. Please wait.

Code Organization Classes

Similar presentations


Presentation on theme: "Code Organization Classes"— Presentation transcript:

1 Code Organization Classes
CSCE 121

2 Recall Definitions can only happen once.
We can separate functions into different files. #include related header file We will do something similar with Classes!

3 Recall Method / member function definitions can be separated
from function declaration. class Student { string name; // ... public: string getName (); void setName (string _name); }; string Student::getName () { return name; } Consequently, they can be placed into separate files. Or we can leave the function declaration inside the class, but define (or implement) them outside. To do this, we need to use the scope resolution operator. Notice that the return type still goes at the very beginning, and that the actual name of this function is Student::getName(); This distinguishes from other functions of the same name. And also notice that since we specified that we are editing a member function of our class, we can directly access its private data members.

4 For classes Filename (.cpp and either .h or .hpp) should be name of the class. Start with a Capital Letter Header File Class definitions Method / member function declarations Class File Class method / member function definitions. Use scope resolution operator Header guards are REALLY important here. These header files contain definitions!

5 Class in Separate Files
Student.h #ifndef STUDENT_H #define STUDENT_H class Student { string name; int id; public: Student (string name, int id); string getName (); void setName (string name); }; #endif Student.cpp #include "Student.h" Student::Student (string name, int id): name(name), id(id) {} string Student::getName () { return name; } void Student::setName(string name) { this->name = name; Or we can leave the function declaration inside the class, but define (or implement) them outside. To do this, we need to use the scope resolution operator. Notice that the return type still goes at the very beginning, and that the actual name of this function is Student::getName(); This distinguishes from other functions of the same name. And also notice that since we specified that we are editing a member function of our class, we can directly access its private data members.


Download ppt "Code Organization Classes"

Similar presentations


Ads by Google