Zaibon

carte.cpp

Oct 17th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include "Carte.h"
  5.  
  6.  
  7.  
  8. //constructeur par default
  9. Carte::Carte(){
  10.     nom[0] = 0;
  11.     serie = NULL;
  12.     numero = 0;
  13.     caraScore.setNom("");
  14.     caraScore.setScore(0);
  15. }
  16. //constructeur d'initilisation
  17. Carte::Carte(const char* n,const char* nomS,const int num,const CaracteristiqueScore cs){
  18.     setNom(n);
  19.     serie = NULL;
  20.     setSerie(nomS);
  21.     setNumero(num);
  22.     setCaracteristiqueScore(cs);
  23. }
  24. Carte::Carte(const char* n,const char* nomS,const int num,const char* cNom,const int cScore){
  25.     setNom(n);
  26.     serie = NULL;
  27.     setSerie(nomS);
  28.     setNumero(num);
  29.     caraScore.setNom(cNom);
  30.     caraScore.setScore(cScore);
  31. }
  32. //constructeur de copy
  33. Carte::Carte(const Carte& c){
  34.     setNom(c.nom);
  35.     serie = NULL;
  36.     setSerie(c.serie);
  37.     setNumero(c.numero);
  38.     setCaracteristiqueScore(c.caraScore);
  39. }
  40.  
  41. //destructeur
  42. Carte::~Carte(){
  43. //  s << "apprele de desctructeur pour " << nom << endll;
  44.     if(serie != NULL)
  45.         delete serie;
  46. }
  47.  
  48.  
  49. //Surgarche des opérateur
  50. Carte& Carte::operator=(const Carte& c){
  51.     setNom(c.nom);
  52.     serie = NULL;
  53.     setSerie(c.serie);
  54.     setNumero(c.numero);
  55.     setCaracteristiqueScore(c.caraScore);
  56.     return (*this);
  57. }
  58. Carte Carte::operator+(int add){
  59.     Carte c(*this);
  60.     c.caraScore.setScore(c.caraScore.getScore()+add);
  61.     return c;
  62. }
  63.  
  64. Carte Carte::operator-(int less){
  65.     Carte c(*this);
  66.     c.caraScore.setScore(c.caraScore.getScore()-less);
  67.     return c;
  68. }
  69. int  Carte::operator-(Carte c){
  70.     int result = 0;
  71.     Carte carte(*this);
  72.     if(carte.caraScore.getNom() != c.caraScore.getNom() ) return -1;
  73.     return carte.caraScore.getScore() - c.caraScore.getScore();
  74. }
  75.  
  76. std::ostream& operator<<(std::ostream& s,const Carte& c){
  77. /*  s << "Nom : " << c.getNom(); << endll;
  78.     s << "Serie : " << c.getSerie() << endll;
  79.     s << "Numero : " << c.getNumero() << endll;
  80.     s << "Caracteristique : " << c.caraScore.getNom() << endll;
  81.     s << "Score : " << c.caraScore.getScore() << endll;
  82. */
  83.     c.affiche(s);
  84.     return s;
  85. }
  86.  
  87. bool Carte::operator<(const Carte c){
  88.     Carte carte(*this);
  89.     if(strcmp(carte.caraScore.getNom(),c.caraScore.getNom()) != 0 ) return false;
  90.     if(carte.caraScore.getScore() < c.caraScore.getScore() ) return true;
  91.     return false;
  92. }
  93.  
  94. bool Carte::operator>(const Carte c){
  95.     Carte carte(*this);
  96.     if(strcmp(carte.caraScore.getNom(),c.caraScore.getNom()) != 0 ) return false;
  97.     if(carte.caraScore.getScore() > c.caraScore.getScore() ) return true;
  98.     return false;
  99. }
  100.  
  101. Carte Carte::operator++(){//préfixé
  102.     (*this).caraScore.setScore((*this).caraScore.getScore() + 5);
  103.     return (*this);
  104. }
  105.  
  106. Carte Carte::operator++(int){//postfixé
  107.     Carte c(*this);
  108.     (*this).caraScore.setScore(c.caraScore.getScore() + 5);
  109.     return c;
  110. }
  111. //getter - setter
  112. void Carte::setNom(const char* n){
  113.     if(n){
  114.         strcpy(nom,n);
  115.     }else{
  116.         strcpy(nom,"");
  117.     }
  118. }
  119. const char* Carte::getNom() const{
  120.     return nom;
  121. }
  122.  
  123. void Carte::setSerie(const char* s){
  124.     if(serie) delete serie;
  125.     if(s){
  126.         serie = new char[strlen(s)+1];
  127.         strcpy(serie,s);
  128.     }else{
  129.         serie = NULL;
  130.     }
  131. }
  132. char* Carte::getSerie() const{
  133.     return serie;
  134. }
  135.  
  136. void Carte::setNumero(int n){
  137.     numero = n;
  138. }
  139. int Carte::getNumero() const{
  140.     return numero;
  141. }
  142.  
  143. void Carte::setCaracteristiqueScore(const CaracteristiqueScore cs){
  144.     caraScore.setNom(cs.getNom());
  145.     caraScore.setScore(cs.getScore());
  146. }
  147.  
  148. CaracteristiqueScore Carte::getCaracteristiqueScore()const {
  149.     return caraScore;
  150. }
  151.  
  152. //methode
  153. void Carte::affiche(std::ostream& s) const {
  154.     s << "Nom : " << nom << std::endl;
  155.     if(serie == NULL){
  156.         s << "serie : Inconnue " << std::endl;
  157.     }else{
  158.         s << "Serie : " << serie << std::endl;
  159.     }
  160.     s << "Numero : " << numero << std::endl;
  161.     caraScore.affiche();
  162. }
Advertisement
Add Comment
Please, Sign In to add comment