Sunteți pe pagina 1din 4

#include<iostream> #include<conio.

h> using namespace std; class Plante {private: char*nume; int* frunze; int flori; //seteri si geteri pentru atribute public: void set_nume(char* n) {nume=new char[strlen(n)+1]; strcpy(nume, n);} char* get_nume() {return nume;}

void set_frunze(int*f) { if(this->frunze) {delete[] this-> frunze;} frunze=new int[12]; for(int i=0; i<12; i++) this-> frunze[i]=f[i]; } int*get_frunze() {return frunze;}

void set_flori(int i) {this-> flori=i;} int get_flori() {return flori;} // constructorul fara parametri Plante() {this-> nume=NULL; this-> frunze=NULL; this-> flori=0; cout<<"S-a apelat constructorul fara parametri"<<endl; } // constructorul cu parametri Plante(char*n, int* r, int p) {this-> nume=new char[strlen(n)+1]; strcpy(this-> nume, n); this-> frunze=new int[12]; for(int i=0; i<12; i++) this-> frunze[i]=r[i]; this-> flori=p; cout<<"S-a apelat constructorul cu parametri"<<endl; } // constructorul de copiere Plante(Plante &a)

{this-> nume=new char[strlen(a.nume)+1]; strcpy(this-> nume, a.nume); this-> frunze=new int[12]; for(int i=0; i<12; i++) this-> frunze[i]=a.frunze[i]; this-> flori=a.flori; cout<<"S-a apelat constructorul de copiere"<<endl; } //destructorul ~Plante() { delete[] this-> nume; delete[] this-> frunze; cout<<"S-a apelat destructorul"<<endl; } //operatorul egal Plante &operator=(const Plante&a) { if(this-> nume) {delete[] this-> nume;} this-> nume=new char[strlen(a.nume)+1]; strcpy(nume, a.nume); if(frunze) { delete[] this-> frunze;} this-> frunze= new int[12]; for(int i=0; i<12; i++) frunze[i]=a.frunze[i]; this-> flori=a.flori; return *this;} // supraincarcarea operatorilor >> , << friend ostream &operator<<(ostream&iesire, Plante x); friend istream &operator>>(istream&intrare, Plante &x); friend int operator+(Plante &, int); friend int operator-(Plante &, int); friend int operator *(Plante&, int); friend class Plante_Flori; virtual void tip() {cout<<"tip necunoscut";} friend ofstream& operator<<(ofstream&, Plante &j); friend ifstream& operator>>(ifstream&, Plante&j); }; ostream &operator<<(ostream&iesire, Plante x) {iesire<<"Planta este denumita:"<<x.nume<<"; Numarul de flori este:"<<x.flori<<e ndl; return iesire; } istream &operator>>(istream&intrare, Plante &x) {cout<<"Nume:"; char nume[40]; intrare>>x.nume; x.set_nume(nume); intrare>>x.nume; cout<<"Flori"; intrare>>x.flori; return intrare; } int operator +(Plante &T, int m)

{ return T.flori+m; } int operator -(Plante &T, int m) { return T.flori-m; } int operator *(Plante&T, int m) { return T.flori*m; } //clasa abstracta class Perioada_inflorire{ virtual void luni()=0; virtual void saptamani()=0; friend class Perioada_inflorire; }; class Plante_Flori :public Plante, public Perioada_inflorire {private: int variabile; public: void luni(){cout<<"S-a introdus numarul de luni."<<endl;} void saptamani(){cout<<"Numarul de saptamani."<<endl;} //constructorul fara parametri Plante_Flori():Plante() {this-> variabile=0; cout<<"S-a apelat constructorul fara parametri, pentru clasa derivata"<< endl;} //constructorul cu parametri Plante_Flori(char*n, int*f, int i, int vr):Plante(n, f, i ) {this-> variabile=vr; cout<<"S-a apelat constructorul cu parametri, pentru clasa derivata"<<e ndl;} //destructorul ~Plante_Flori() {cout<<"S-a apelat destructorul, pentru clasa derivata"<<endl; } // constructorul de copiere Plante_Flori(const Plante_Flori &g) {this-> variabile=g.variabile; cout<<"S-a apelat constructor de copiere, pentru clasa derivata"<<endl; } //operatorul egal Plante_Flori operator=(const Plante_Flori&h) {if(this-> nume) {delete[] this-> nume;} if(frunze) { delete[] this-> frunze;} this-> variabile=h.variabile; return *this; } void tip() {cout<<"Flori";} // geteri si seteri pentru clasa derivata int get_variabile() {return this-> variabile;}

void set_variabile(int f) {this->variabile=f;} };

void main() { Plante (); int v[]={5}; Plante B ("Lalea", v, 1); Plante D ("Trandafir", v, 2); Plante E; Plante_Flori(); E=D; cout<<B; cout<<E; cout<<D; cin.get(); }

S-ar putea să vă placă și