|
|
|
|
void Vecteur::toString(){
cout << getX() <<";" << getY() << endl;
}
Dans cet exemple je définis d'un coup l'opérateur d'écriture pour les sorties standards std::cout et std::cerr (de type std::ostream) et pour les fichiers (de type std::ofstream) grâce à un template. Les fonctions template doivent impérativement être intégralement dans le .hpp
#include <iostream>
class vecteur{
protected:
double x;
double y;
public:
vecteur(){}
vecteur(const double & x0,const double & y0):x(x0),y(y0){}
inline void setx(const double & x0){x = x0;}
inline void sety(const double & y0){y = y0;}
inline const double & getx() const {return x;}
inline const double & gety() const {return y;}
};
template <typename Tstream>
Tstream & operator << (Tstream & out,const vecteur & v){
out << v.getx() << ' ' << v.gety();
return out;
}
int main(){
vecteur v1(5.0,3.6);
std::cout << v1 << std::endl;
return 0;
}
Bonne chance |
Résultats pour [string C++] ::>>> bien Relou
Résultats pour [string C++] ::>>> bien Relou
Résultats pour [string C++] ::>>> bien Relou
Résultats pour [string C++] ::>>> bien Relou
Résultats pour [string C++] ::>>> bien Relou
Résultats pour [string C++] ::>>> bien Relou
Résultats pour [string C++] ::>>> bien Relou