Header FilesWhen you should make, and use, your own header files.
/* * Header file * Put in this a definition of the class, * and possibly the simplest, one-line functions * as "inline" */ /* * prevent multiple definitions * by defining a unique symbol */ #ifndef DATAPOINT_HH #define DATAPOINT_HH class DataPoint { public: DataPoint(double x,double y, double z); inline double getX() const { return x; } double distance(DataPoint p); private: double x,y,e; }; #endif //DATAPOINT_HH
#include "DataPoint.hh" #include <cmath> /* * implementation of the method declared in the header */ double DataPoint::distance(DataPoint p) { return sqrt(pow(x-p.x,2),pow((y-p.y)/e,2)); }
/* * this program uses the DataPoint class */ // in the source, you only need to include the header #include "DataPoint.hh" #include <iostream> int main() { using namespace std; DataPoint p1; DataPoint p2; cout << p1.distance(p2) <<endl; } < Container String | Trail Index | References and Pointers > |