Advertisement
Guest User

Classe Chaine - Travail réalisé par NCIBI Ali

a guest
Feb 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. class Chaine
  6. {
  7. public:
  8.  
  9.     //Constructeur avec paramètre " une chaine " , sert aussi comme opérateur de conversion "any string" => Chaine Objet
  10.     Chaine(const char * ch);
  11.  
  12.     //Surcharge du constructeur avec paramètre " longueur de la chaine "
  13.     Chaine(const int x);
  14.  
  15.     //Surcharge de l'opérateur =
  16.     Chaine operator=(const Chaine &object);
  17.  
  18.     //Surcharge de l'opérateur +
  19.     Chaine operator+(const Chaine &object);
  20.  
  21.     //Surcharge de l'opérateur +=
  22.     Chaine operator+=(const Chaine &object);
  23.    
  24.     //Surcharge de l'opérateur << par une fonction globale
  25.     friend std::ostream& operator<<(std::ostream& out, const Chaine &object);  
  26.  
  27.     //Destructeur
  28.     /*~Chaine();*/
  29.  
  30. private:
  31.     char * theString;
  32.     int length;
  33.  
  34. };
  35.  
  36.  
  37. //Définition du constructeur à paramètre chaine de caractère
  38. Chaine::Chaine(const char * ch)
  39. {
  40.  
  41.     length = strlen(ch);
  42.  
  43.     theString = new char[length];
  44.  
  45.     for (int j = 0;j < length;j++)
  46.     {
  47.         theString[j] = ch[j];
  48.     }
  49. }
  50.  
  51. //Définition du constructeur à paramètre int longueur de la chaine
  52. Chaine::Chaine(const int x)
  53. {
  54.     theString = new char[x];
  55.     length = x;
  56. }
  57.  
  58. /*
  59. Je l'ai commenté car ça donne des exceptions face auxquelles je ne pouvais rien ! :(
  60. //Définition du destructeur
  61. Chaine::~Chaine()
  62. {
  63.     delete[] theString;
  64. }
  65. */
  66.  
  67. //Définition de la surcharge de l'opérateur =
  68. Chaine Chaine::operator=(const Chaine &object)
  69. {
  70.     if (this != &object)
  71.     {
  72.         theString = object.theString;
  73.         this->length = object.length;
  74.     }
  75.     return *this;
  76. }
  77.  
  78. //Définition de la surcharge de l'opérateur +
  79. Chaine Chaine::operator+(const Chaine &object)
  80. {
  81.     Chaine newObject(object.length + length);
  82.     for (int i = 0;i<length;i++)
  83.         newObject.theString[i] = theString[i];
  84.     for (int j = 0; j<object.length;j++)
  85.         newObject.theString[j + length] = object.theString[j];
  86.  
  87.     return newObject;
  88. }
  89.  
  90. //Définition de la surcharge de l'opérateur += ( en utilisant '+' et '=' déjà définis )
  91. Chaine Chaine::operator+=(const Chaine &object)
  92. {
  93.     *this = *this + object;
  94.     return *this;
  95. }
  96.  
  97. //Définition de la surcharge de l'opérateur << (comme fonction non membre)
  98. //En effet << est un opérateur binaire : opérande1 << opérande2
  99. //Avec opérande1 : cout, opérande2 : chaine
  100. std::ostream& operator<<(std::ostream &cout, const Chaine &object)
  101. {
  102.     for(int i=0;i<object.length;i++)
  103.         cout << object.theString[i];
  104.        
  105.     return cout;
  106. }
  107.  
  108. int main() {
  109.    
  110.     //Ch1 = SALUT
  111.     Chaine Ch1("SALUT");
  112.     //Ch3, vide de longueur 100
  113.     Chaine Ch3(100);
  114.     //Ch2 = BON
  115.     Chaine Ch2 = "BON";
  116.     //Ch3 = Monsieur
  117.     Ch3 = "Monsieur";
  118.     //Ch1 = BONJOUR
  119.     Ch1 = Ch2 + "JOUR";
  120.     //Ch2 = ,cher,
  121.     Ch2 = ",cher,";
  122.     //Ch1 = Bonjour,cher,
  123.     Ch1 += Ch2;
  124.     //Ch1 = Bonjour,cher,Monsieur
  125.     Ch1 += Ch3;
  126.     cout << Ch1;
  127.    
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement