Vous êtes sur la page 1sur 3

#include<iostream>

#include<conio.h>
using namespace std;
class Florarie {
private:
char* nume;
int nrTipuriFlori;
float*preturi_tipuri_flori;
const int id;
static int nr;
public:

Florarie() :id(nr++)
{
this->nume = new char[strlen("Anonim") + 1];
strcpy(this->nume, "Anonim");
this->nrTipuriFlori = 0;
this->preturi_tipuri_flori = new float[this->nrTipuriFlori];
for (int i = 0; i < this->nrTipuriFlori; i++)
{
this->preturi_tipuri_flori[i] = preturi_tipuri_flori[i];
}
}
Florarie(const char* nume, float* preturi, int nr1):id(nr++)
{
this->nume = new char[strlen(nume) + 1];
strcpy(this->nume, nume);
this->nrTipuriFlori = nr1;
this->preturi_tipuri_flori = new float[this->nrTipuriFlori];
for (int i = 0; i < this->nrTipuriFlori; i++)
{
this->preturi_tipuri_flori[i] = preturi[i];
}
}
Florarie(const Florarie& f1):id(nr++)
{
this->nume = new char[strlen(f1.nume)+ 1];
strcpy(this->nume, f1.nume);
this->nrTipuriFlori = f1.nrTipuriFlori;
this->preturi_tipuri_flori = new float[f1.nrTipuriFlori];
for (int i = 0; i < f1.nrTipuriFlori; i++)
{
this->preturi_tipuri_flori[i] = f1.preturi_tipuri_flori[i];
}
}
Florarie& operator=(const Florarie&f1)
{
if (this->nume != NULL)
{
delete[]this->nume;
}
if (this->preturi_tipuri_flori != NULL)
{
delete[]this->preturi_tipuri_flori;
}
this->nume = new char[strlen(f1.nume) + 1];
strcpy(this->nume, f1.nume);
this->nrTipuriFlori = f1.nrTipuriFlori;
this->preturi_tipuri_flori = new float[f1.nrTipuriFlori];
for (int i = 0; i < f1.nrTipuriFlori; i++)
{
this->preturi_tipuri_flori[i] = f1.preturi_tipuri_flori[i];
}
return *this;
}
int getNrTipuriFlori()
{
return this->nrTipuriFlori;
}
const char* getNume()
{
return this->nume;
}
const int getId()
{
return this->id;
}
float getMaximumPrice()
{
float max = -99999;
for (int i = 0; i < this->nrTipuriFlori; i++)
{
if (this->preturi_tipuri_flori[i] > max)max = this-
>preturi_tipuri_flori[i];
}
return max;
}
void setPreturi(float* pret, int nr)
{
if (this->preturi_tipuri_flori != NULL)
{
delete[]this->preturi_tipuri_flori;
}
this->nrTipuriFlori = nr;
this->preturi_tipuri_flori = new float[nr];
for (int i = 0; i < this->nrTipuriFlori; i++)
{
this->preturi_tipuri_flori[i] = pret[i];
}
}
Florarie& operator++(int)
{
Florarie copie = *this;
float* vector = new float[this->nrTipuriFlori + 1];
for (int i = 0; i < this->nrTipuriFlori; i++)
{
vector[i] = this->preturi_tipuri_flori[i];
}
vector[this->nrTipuriFlori] = 10;
this->nrTipuriFlori++;
if (this->preturi_tipuri_flori != NULL)
{
delete[]preturi_tipuri_flori;
}
this->preturi_tipuri_flori = vector;
return copie;

}
~Florarie()
{

if (this->nume != NULL)
{
delete[]this->nume;
}
if (this->preturi_tipuri_flori != NULL)
{
delete[]this->preturi_tipuri_flori;
}
}
friend ostream& operator<<(ostream& out, Florarie& f)
{
out << "Floararia " << f.nume << " vinde " << f.nrTipuriFlori << "
tipurii de flori cu preturile: ";
for (int i = 0; i < f.nrTipuriFlori; i++)
{
out << f.preturi_tipuri_flori[i] << " ; ";
}
return out;
}
};
int Florarie::nr = 0;
void main()
{
Florarie f1;
cout << f1.getNrTipuriFlori() << endl;
float preturi[] = { 7.6,23.6,16.3 };
Florarie f2("Floris", preturi, 3);
cout << f2.getNume() << endl;
cout << f1.getId() << endl;
Florarie f3 = f2;
cout << "f2#######" << f2 << endl;
cout<<"f3######" << f3 << endl;
cout << f3.getMaximumPrice() << endl;
float preturi2[] = { 11.2,44.6,7.3,21.9 };
f2.setPreturi(preturi2, 4);
cout << f2.getMaximumPrice() << endl;
cout << f3.getMaximumPrice() << endl;
cout << f2 << endl;
f1 = f2;
cout << f1.getMaximumPrice() << endl;
Florarie* pf2 = new Florarie("Flora", preturi2, 4);
cout << "PF3333333333333333333" << (*pf2) << endl;
delete pf2;
Florarie f5 = (*pf2)++;
cout << pf2->getNrTipuriFlori() <<"pt pf2"<< " " <<"pt f5"<<" "<<
f5.getNrTipuriFlori() << endl;
getch();
}

Vous aimerez peut-être aussi