Exercitii Poo [PDF]

  • 0 0 0
  • Gefällt Ihnen dieses papier und der download? Sie können Ihre eigene PDF-Datei in wenigen Minuten kostenlos online veröffentlichen! Anmelden
Datei wird geladen, bitte warten...
Zitiervorschau

Seminar 6 recapitulare #include using namespace std; class Carte { private: char* titlu; char* autor; char gen[50]; unsigned int numar_pagini; double pret; const int id_carte; static unsigned int numar_carti; public: Carte() :id_carte(Carte::numar_carti++) { this->titlu = new char[strlen("Lipsa") + 1]; strcpy(this->titlu, "Lipsa"); this->autor = new char[strlen("Lipsa") + 1]; strcpy(this->autor, "Lipsa"); strcpy(this->gen, "Lipsa"); this->numar_pagini = 0; this->pret = 0; } Carte(char* titlu, char* autor, char gen[50], unsigned int numar_pagini, double pret) :id_carte(Carte::numar_carti++) { this->titlu = new char[strlen(titlu) + 1]; strcpy(this->titlu, titlu); this->autor = new char[strlen(autor) + 1]; strcpy(this->autor, autor); strcpy(this->gen, gen); this->numar_pagini = numar_pagini; this->pret = pret; } Carte(char* titlu, char* autor, char* gen, unsigned int nr_pagini = 10) :numar_pagini(nr_pagini), id_carte(Carte::numar_carti++) { this->titlu = new char[strlen(titlu) + 1]; strcpy(this->titlu, titlu);

this->autor = new char[strlen(autor) + 1]; strcpy(this->autor, autor); strcpy(this->gen, gen); this->pret = 0; } //constructor de copiere Carte(const Carte& carte) :id_carte(carte.id_carte) { this->titlu = new char[strlen(carte.titlu) + 1]; strcpy(this->titlu, carte.titlu); this->autor = new char[strlen(carte.autor) + 1]; strcpy(this->autor, carte.autor); strcpy(this->gen, carte.gen); this->numar_pagini = carte.numar_pagini; this->pret = carte.pret; } //supraincarcarea operatorului= Carte& operator=(const Carte& carte) { if (this->titlu != NULL){ delete[] this->titlu; } this->titlu = new char[strlen(carte.titlu) + 1]; strcpy(this->titlu, carte.titlu); if (this->autor != NULL){ delete[] this->autor; //evit memory leaks } this->autor = new char[strlen(carte.autor) + 1]; strcpy(this->autor, carte.autor); strcpy(this->gen, carte.gen); this->numar_pagini = carte.numar_pagini; this->pret = carte.pret; return *this; } ~Carte() { delete[] this->titlu; delete[] this->autor; }

void afisare(); char* getAutor() { return this->autor; } void setAutor(char* autor) { if (this->autor != NULL) { delete[] this->autor; } this->autor = new char[strlen(autor) + 1]; strcpy(this->autor, autor); } unsigned int getNrPagini() { return this->numar_pagini; } void setNrPagini(unsigned int numar_pagini) { this->numar_pagini = numar_pagini; } char* getGen() { return this->gen; } void setGen(char gen[50]) { strcpy(this->gen, gen); } int getIdCarte() { return this->id_carte; } static unsigned int getNumarCarti() { return Carte::numar_carti; } char* getTitlu() {

return this->titlu; } double getPret() { return this->pret; } friend istream& operator>>(istream& i, Carte& carte) { cout carte.pret; cout > carte.numar_pagini; return i; } Carte operator+(float pret) { Carte copie = *this; copie.pret = copie.pret + pret; return copie; } //operatorul de pre-decrementare

//scade nr de pag cu 1 Carte& operator--() { this->numar_pagini--; return *this; } //operatorul de post-decrementare Carte operator--(int) { Carte copie = *this; this->numar_pagini--; return copie; } }; ostream& operatornr_pagini; i++) sum += this->cuv_pe_pagina[i]; return sum; } ////////////SUPRAINCARCARE///////////////// Document operator+=(double x) { this-> dim += x; return *this; } Document operator-=(int i) { if(this->nr_pagini == 1) { this->nr_pagini; return *this; } else { this-> nr_pagini--; this-> cuv_pe_pagina[i-1] = 0; } return *this; } friend ostream& operator(istream& in, Document& d); }; int Document::nr_documente = 0; ostream& operator