hi everybody,
to create a c++ class i can do the following
1. first way:
Personnage.cpp
#include <iostream>
using namespace std;
class Personnage
{
public:
Personnage();
~Personnage();
int getVie();
protected:
private:
int vie;
};
in the file “main.cpp”
i can write
#include <iostream>
#include "Personnage.cpp"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
Personnage david;
return 0;
}
2. But it is also possible
to create a header file that i will include in the main like this :
Personnage.h
#ifndef PERSONNAGE_H
#define PERSONNAGE_H
class Personnage
{
public:
Personnage();
~Personnage();
int getVie();
protected:
private:
int vie;
};
#endif
Personnage.cpp
#include<iostream>
#include<string>
#include "Personnage.h"
using namespace std ;
Personnage::Personnage():vie(2)
{
//
}
Personnage::~Personnage()
{
}
int Personnage::getVie(){
return vie ;
}
and finally in the main.cpp
#include <iostream>
#include "Personnage.h"
using namespace std;
/** \brief
*
* \param
* \param
* \return
*
*/
int main()
{
cout << "Hello world!" << endl;
Personnage david;
//cout<<david.getVie()<<endl;
return 0;
}
so i want to know what is the best way to create a c++ class is it good to include directly a sourcefile.cpp which contains the definition of the class ? thanks
Edit: please use @ tags around code sections; Andre
↧