C++: Correction Gestionnaire de documents 2

 

Voici la solution proposée par LECUCQ Delphin et BLIN Yann (Master 1 IUP, 2004-05):
 
#include <iostream>
#include <map>
#include <string>
#include <stdlib.h>
 
// ABSTRAIT
class Document {
  public:
  Document(string _name);
  string getName();
  virtual void open() = 0;
  protected:
  string name;
};
 
class DocumentPDF: public Document {
  public:
  DocumentPDF(string _name);
  virtual void open();
};
 
class DocumentTXT: public Document {
  public:
  DocumentTXT(string _name);
  virtual void open();
};
 
class Gestionnaire {
  public:
  void addDoc(const char* code, Document* doc);
  Document* find(const char* code);
  private:
  map<string, Document*> liste;
};
 
// Implementation des classes
Document::Document(string _name):name(_name) {}
string Document::getName() {return name;}
 
DocumentTXT::DocumentTXT(string _name):Document(_name) {}
void DocumentTXT::open() {system(("gvim "+getName()).c_str());}
 
DocumentPDF::DocumentPDF(string _name):Document(_name) {}
void DocumentPDF::open() {system(("gv "+getName()).c_str());}
 
void Gestionnaire::addDoc(const char* code, Document* doc) {liste[code]=doc;}
Document* Gestionnaire::find(const char* code) {return liste[code];}
 
// LE MAIN
main() {
  Gestionnaire g;
  DocumentPDF d1("/home/filieres/master1iup/lecucqde/Downloads/cours-cpp.pdf");
  DocumentTXT d2("/home/filieres/master1iup/lecucqde/Downloads/Morrowind.txt");
 
  g.addDoc("X16", &d1);
  g.addDoc("D14", &d2);
 
  g.find("X16")->open();
  g.find("D14")->open();
}