Advertisement
Guest User

Classe Chaine - Travail réalisé par NCIBI Ali

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