Вы находитесь на странице: 1из 5

#include<iostream>

#include<stdio.h>
#include<fstream>
using namespace std;
class Apartament
{
private:
const int anConstructie;
char* localitate;
int pret;
int nrCamere;
int* suprCamere;
public:
Apartament() :anConstructie(0)
{
this->localitate = new char[strlen("Implicit") + 1];
strcpy(this->localitate, "Implicit");
this->pret = 0;
this->nrCamere = 0;
this->suprCamere = new int[nrCamere];
for (int i = 0; i < nrCamere; i++)
this->suprCamere[i] = suprCamere[i];
}
Apartament(const int anConstructie,char* localitate, int pret, int nrCam
ere, int* suprCamere) :anConstructie(anConstructie)
{
this->localitate = new char[strlen(localitate) + 1];
strcpy(this->localitate, localitate);
this->pret = pret;
this->nrCamere = nrCamere;
this->suprCamere = new int[nrCamere];
for (int i = 0; i < nrCamere; i++)
this->suprCamere[i] = suprCamere[i];
}
Apartament(Apartament&a) :anConstructie(anConstructie)
{
this->localitate = new char[strlen(a.localitate) + 1];
strcpy(this->localitate, a.localitate);
this->pret = a.pret;
this->nrCamere = a.nrCamere;
this->suprCamere = new int[a.nrCamere];
for (int i = 0; i < a.nrCamere; i++)
this->suprCamere[i] = a.suprCamere[i];
}
Apartament &operator=(Apartament &a)
{
if (this->localitate)
delete[] this->localitate;
this->localitate = new char[strlen(a.localitate) + 1];
strcpy(this->localitate, a.localitate);
this->pret = a.pret;
this->nrCamere = a.nrCamere;
if (this->suprCamere)
delete[]this->suprCamere;
this->suprCamere = new int[a.nrCamere];

for (int i = 0; i < a.nrCamere; i++)


this->suprCamere[i] = a.suprCamere[i];
return *this;
}
const int getAnConstructie()
{
return this->anConstructie;
}
void setLocalitate(char* localitate)
{
this->localitate = new char[strlen(localitate) + 1];
strcpy(this->localitate, localitate);
}
int* getVectorSupr()
{
for (int i = 0; i < nrCamere; i++)
return this->suprCamere;
}
void setSuprCamere(int* suprCamere,int n)
{
this->suprCamere = new int[n];
for (int i = 0; i < n; i++)
{
this->suprCamere[i] = suprCamere[i];
}
}
char* getLocalitate()
{
return this->localitate;
}
int suprafataTotala()
{
int total = 0;
for (int i = 0; i < nrCamere; i++)
total += suprCamere[i];
return total;
}
double suprMedie()
{
float suma = 0;
float medie;
for (int i = 0; i < nrCamere; i++)
suma += suprCamere[i];
medie = suma / nrCamere;
return medie;
}
operator double()
{
return this->suprMedie();
}
friend ostream &operator<<(ostream&, Apartament&);
friend istream &operator>>(istream&, Apartament&);
friend Apartament &operator+=(Apartament&, float);
friend Apartament &operator-=(Apartament&, float);
friend const Apartament operator++(Apartament&,int);
friend bool operator==(Apartament&,Apartament&);
friend bool operator<(Apartament&, Apartament&);

friend bool operator>(Apartament&, Apartament &);


friend ofstream &operator<<(ofstream&, Apartament&);
friend ifstream &operator>>(ifstream&, Apartament&);
};
ofstream &operator<<(ofstream& out, Apartament& a)
{
out.open("iesire.txt", ios::out);
if (out)
{
out << a.localitate << endl;
out << a.pret << endl;
out << a.nrCamere << endl;
out.close();
}
return out;
}
ifstream &operator>>(ifstream&in, Apartament&a)
{
in.open("intrare.txt", ios::in);
if (in)
{
in >> a.localitate;
in >> a.pret;
in >> a.nrCamere;
in.close();
}return in;
}
bool operator==(Apartament& a, Apartament& b)
{
if (a.pret == b.pret)
return 1;
else
return 0;
}
bool operator<(Apartament& a, Apartament&b)
{
if (a.pret < b.pret)
return 1;
else
return 0;
}
bool operator>(Apartament& a, Apartament&b)
{
if (a.pret > b.pret)
return 1;
else
return 0;
}
const Apartament operator++(Apartament&a, int )
{
Apartament aux = a;
a.anConstructie++;
return aux;
}
Apartament &operator+=(Apartament &a, float b)
{
a.pret += b;
return a;

}
Apartament &operator-=(Apartament&a, float b)
{
a.pret -= b;
return a;
}
ostream &operator<<(ostream&out, Apartament&a)
{
out << a.anConstructie<<endl;
out << a.localitate<<endl;
out << a.pret<<endl;
out << a.nrCamere<<endl;
out << a.suprCamere<<endl;
return out;
}
istream &operator>>(istream& in, Apartament&a)
{
in >> a.localitate;
in >> a.pret;
in >> a.nrCamere;
return in;
}
class ApartamentSocial :public Apartament
{
private:
char* locatar;
int chirie;
public:
ApartamentSocial() :Apartament()
{
this->locatar = new char[strlen("Anonim") + 1];
strcpy(this->locatar, "Anonim");
this->chirie = 0;
}
ApartamentSocial(const int anConstructie, char* localitate, int pret, in
t nrCamere, int* suprCamere, char* locatar, int chirie) :Apartament(anConstructi
e, localitate, pret, nrCamere, suprCamere)
{
this->locatar = new char[strlen(locatar) + 1];
strcpy(this->locatar, locatar);
this->chirie = chirie;
}
friend ostream& operator<<(ostream&, ApartamentSocial&);
friend istream& operator>>(istream&, ApartamentSocial&);
};
ostream& operator<<(ostream& out, ApartamentSocial&a)
{
out << a.locatar<<endl;
out << a.chirie<<endl;
return out;
}
istream& operator>>(istream& in, ApartamentSocial&a)
{
in >> a.locatar;
in >> a.chirie;
return in;
}
void main()
{

Apartament anConstructie();
int vectSupr1[] = { 200, 300, 400 };
int vectSupr2[] = { 100, 150, 180};
int vectSupr3[] = { 200, 220, 240 };
Apartament a1;
Apartament a2(1978, "Bucuresti", 28000,3, vectSupr1);
cout << a1.getAnConstructie()<<endl;
a2.setLocalitate("Cluj");
cout << a2.getVectorSupr()<<endl;
a2.setSuprCamere(vectSupr2, 3);
Apartament a3 = a2;
Apartament a4;
a4 = a3;
cout << a1 <<endl<< a2 << endl<<a3 <<endl<< a4<<endl;
a3 += 3000;
cout << a3;
cout << "Suprafata totala a apartamentului este: " << a3.suprafataTotala
() << endl;
if (a3 > a2) cout << "Apartamentul a3 este mai scump decat a2 " << endl
;
else if (a3 < a2) cout << "Apartamentul a2 este mai scump decat a3 " <<
endl;
else cout << "Apartamentele a3 si a2 au acelasi pret " << endl;
ofstream out("iesire.txt");
out << a4;
out.close();
ifstream in("intrare.txt", ios::in);
in >> a4;
cout << a4;
in.close();
cout << (double)a2<<endl;
ApartamentSocial as1(1999, "Bucuresti", 57000, 2, vectSupr3, "Popescu Io
n", 750);
cout << as1;
cin.get();
}