A class declaration would typically go into a header file say Example.h. The methods would typically get implemented in a separate file Example.cpp5.3.
To illustrate this I have repackaged the struct-using example in 5.2.1 in:
class declaration
class method implementation
class use
(Note: these are in the file system also in /shared/teaching/CSLL/2061/Classes) In the header file the class Person is defined:
class Person { public: int age; int height; void print(); void zero(); };
The methods are implemented in class method implementation, which has #include "Person.h"
to access the class
declaration:
#include <iostream>
#include "Person.h"
using namespace std;
void Person::zero() {
age = 0;
height = 0;
}
void Person::print() {
cout << " age " <<
age << '\n';
cout << " height " <<
height << '\n';
}
Note:
The code using the class is user
#include <iostream> #include "Person.h" using namespace std; int main() { Person fred; fred.age = 3; fred.height = 10; fred.print(); fred.zero(); fred.print(); }