defining the member functions of a class inside a header function is BAD! other than the fact that it's bad form to put executable code in header files anyway, if this header was included in multiple .cpp files, you would get a "function redefinition" error by the linker.
the correct way to do this is to have a header (.h) file that contains the class declaration, and a .cpp file (usually of the same name) that defines the methods. using the author's examples:
Code:
/************* NiceClass.h ************/
#ifndef NiceClass_h
#define NiceClass_h
class NiceClass
{
private: // Can't be accessed by anyone but the class.
int NiceNumber; // A number.
char NiceLetter; // A letter.
public:
NiceClass(); // This is the all important constructor. NO RETURN TYPE!
void setNiceNumber( int ); // This sets the value of the number.
void setNiceLetter( char ); // This sets the value of the letter.
int getNiceNumber( void ); // This returns the value of the number.
char getNiceLetter( void ); // THis returns the value of the letter.
}; // END!
#endif
Code:
/************* NiceClass.cpp ************/
#include "NiceClass.h"
NiceClass::NiceClass()
{
NiceNumber = 0;
NiceLetter = 'a';
}
void NiceClass::setNiceNumber( int userNumber )
{
NiceNumber = userNumber;
}
void NiceClass::setNiceLetter( char userLetter )
{
NiceLetter = userLetter;
}
int NiceClass::getNiceNumber( void )
{
return NiceNumber;
}
char NiceClass::getNiceLetter( void )
{
return NiceLetter;
}