TD 14 C++: Entrées/Sorties, Smart Pointers

Rappels


Exercice 1: file I/O

  1. Écrivez un programme 1.1.cc (avec un main()) qui compte le nombre de lignes d'un fichier texte, dont le nom sera donné en argument du programme. Il affichera ce nombre dans stdout.
    Indices: ifstream et getline pour lire un fichier (cherchez sur cppreference.com), int main(int argc, char** argv) pour récupérer les arguments du programme.

    Testez votre code:
    rm 1.tar.gz; wget --no-cache http://fabien.viger.free.fr/cpp/td14/1.tar.gz
    tar xf 1.tar.gz
    make 1.1
    RENDU: 1.1.cc

  2. De même, écrivez un programme 1.2.cc qui affiche (dans stdout) les N premières lignes d'un fichier texte. Le programme prendra le nom du fichier en 1er argument et le nombre N en 2ème argument.

    Test: make 1.2
    RENDU: 1.2.cc

  3. De même, écrivez un programme 1.3.cc qui réduit un fichier donné à ses N dernières lignes.
    Exemple pour N=3: un fichier contenant:
    Hello
    World
    This is a
    simple

    text file

    ne contiendra, après ce programme, plus que:
    simple

    text file

    Attention! votre code ne devra pas prendre plus de mémoire que le contenu des N dernières lignes!
    Le test le verifiera.

    Test: make 1.3
    RENDU: 1.3.cc


Exercice 2: Smart pointers

  1. Implémentez ComputeMedianObj() qui est déclaré dans 2.1.h (copié ci-dessous) dans un fichier 2.1.cc, sans utiliser le mot-clé delete.
    Ne modifiez pas 2.1.h et n'implémentez pas les constructeurs/destructeurs de MyObj.
    class MyObj {
     public:
      explicit MyObj(int seed);  // Implemented in 2.1.test.cc
      ~MyObj();                  // Implemented in 2.1.test.cc
    
      // NOT default- and copy-constructible!
      MyObj() = delete;
      MyObj(const MyObj&) = delete;
    
      int Value() const { return value_; }
     private:
      const int value_;
    };
    
    // This function, which you must implement, should do the following:
    // - Construct N instances of MyObj, with seed=0...N-1.
    // - Sort them by Value()
    // - Return the "median" value, i.e. the Value() of the N/2-th MyObj in the
    //   sorted list.
    // - [IMPORTANT!] Destroy all instances of MyObj before returning.
    int ComputeMedianMyObj(int N);
    Testez votre code:
    rm 2.tar.gz; wget --no-cache http://fabien.viger.free.fr/cpp/td14/2.tar.gz
    tar xf 2.tar.gz
    make 2.1
    RENDU: 2.1.cc


Exercice 3: Factory

  1. Complétez 3.1.h (version initiale copiée ci-dessous) et écrivez l'implémentation dans un fichier 3.1.cc.
    class Hat {
     public:
      Hat() {  ++num_hats_global_; }
      ~Hat() { --num_hats_global_; }
      static int NumHatsGlobal() { return num_hats_global_; }
     private:
      static int num_hats_global_;
    };
    
    class HatFactory {
     public:
      // The factory constructs a new hat, and gives me a pointer to the
      // newly constructed object. The caller does not own the Hat.
      Hat* NewHat();
    
      // All hats ever constructed are destructed when the factory is destructed.
      ~HatFactory();
    
     private:
      // TODO
    };
    Testez votre code:
    rm 3.tar.gz; wget --no-cache http://fabien.viger.free.fr/cpp/td14/3.tar.gz
    tar xf 3.tar.gz
    make 3.1
    RENDU: 3.1.h et 3.1.cc