I was wondering about header files. When creating one with a class, do you just put the class in without the class function members, or do you include it all. Meaning, when you create the class with all its members, would you put the full functions into the header file also, or would you just put the function names in the header file.
Ex.
class Date
{
private:
int month;
int day;
long year;
public:
int getdate
{
function info
}
int removedate
{
function info
}
};
or should it be as such:
class Date
{
private:
int month;
int day;
long year;
public:
int getdate{}
int removedate{}
};
And the file would be called, of course <date.h>
I am just curious how to create one after I am done with my program. Wanting to see how to clean it up.
#ifndef DATE_H_INCLUDED // header must have an include guard
#define DATE_H_INCLUDED
#include <iosfwd> // include other headers to the extent required
class Date
{
public:
enum month_t { JAN=1, FEB /* , .....*/, DEC };
Date() ;
Date( int day, month_t month, int year ) ;
Date& advance_by( int days ) ;
// declare other public member functions
private:
int day ;
month_t month ;
int year ;
bool is_valid() ; // checks if the class invariant holds
};
// declare non-member functions (with external linkage)
// which are part of the class interface
std::ostream& operator<< ( std::ostream& stm, Date dt ) ;
booloperator== ( Date a, Date b ) ;
// etc.
#endif // DATE_H_INCLUDED
#include "date.h" // include the header for date
#include <iostream> // // include other headers required for the imlementation
// give internal linkage to entities that are not declared in the header
// for example, implementation helper functions:
staticbool is_leap_year( int year )
{
// ....
}
staticint num_days_in_month( Date::month_t month, bool leap = false )
{
// ...
}
// etc
// define constructors and other member functions of Date
Date& Date::advance_by( int days )
{
// ....
}
// etc.
// define, with external linkage, non-member functions declared in the header
booloperator== ( Date a, Date b )
{
// ...
}
// etc.
It's legal to do it either way. But unless you have a really good reason to put the full method definitions inside the header, it's better to do it the way JLBorges shows - the method declarations in the header, and the implementation in a separate source file.
The main difference between the two is function inlining and compile time.
Putting function bodies in the header means the function can be inlined, but also means that making any change to the function body requires that every single source file which includes the header needs to be recompiled.
Compile time might not seem like it'd be much of an issue... and it isn't for small "print out a date" school programs. But in real programs that consist of dozens or even hundreds of different files, it becomes quite significant.
So yeah... basically what MikeyBoy said. Typically keep the function bodies in a separate source file. Although that's not a strict "always do this" rule. It's a judgement call.