You are on page 1of 4

#include <iostream>

#include <fstream>
#include <string.h>
#include <map>
using namespace std;

class Calc {
private:
float pret;
public:
int idCalc;
char marca[30];

Calc() :idCalc(1000), marca("Noname"), pret(2000) {

Calc(int idCalc, char marca[30], float pret):idCalc(idCalc),pret(pret) {


strcpy(this->marca, marca);

}
friend ostream &operator << (ostream &out, const Calc &c) {
out << endl << "Id-ul calc: " << c.idCalc << " Marca: " << c.marca << "
si pretul: " << c.pret;
return out;

}
bool operator >(const Calc &c) {
return this->pret > c.pret;
}

friend fstream &operator<<(fstream &f, const Calc &c) {


f << c.idCalc;
f << " ";
f << c.marca;
f << " ";
f << c.pret;
f << endl;

return f;
}
friend fstream &operator >> (fstream &f, Calc &c) {
f >> c.idCalc;
f >> c.marca;
f >> c.pret;
return f;
}

~Calc() {

};

class Pers {
private:
int idPers;
public:
char nume[30];
Calc * pCalc;
int idCalc;
Pers() :idPers(100), nume("Anonim"), pCalc(NULL), idCalc(0) {

Pers(int idPers, char nume[30]) :idPers(idPers), pCalc(NULL), idCalc(0) {


strcpy(this->nume, nume);
}

Pers &operator <<(Calc &c) {


this->idCalc = c.idCalc;
this->pCalc = &c; //referinta la un calculator
return *this;
}

friend ostream &operator << (ostream &out, const Pers &p) {


out << endl << "Id Pers: " << p.idPers << " nume " << p.nume;
if (p.pCalc != NULL)
out << *p.pCalc; //* pt redirect obiectului,nu a adresei
else
out << " nu are asociat un calculator";
return out;
}
bool operator>(const Calc &c) {
if (this->pCalc == NULL)
return false;
return *this->pCalc > c;
}
bool operator>(const Pers &p) {
if (this->pCalc == NULL)
return false;
if (p.pCalc == NULL)
return true;
return *this->pCalc > *p.pCalc;
}

friend fstream &operator<<(fstream &f, const Pers &p) {


f << p.idPers;
f << " ";
f << p.nume;
f << " ";
f << p.idCalc;
f << endl;

return f;
}

friend fstream &operator >> (fstream &f, Pers &p) {


f >> p.idPers;
f >> p.nume;
f >> p.idCalc;
p.pCalc = NULL;

return f;
}

~Pers() {
}

};

void main() {
Pers p1(123, "Popescu"), p2, p3, p4;
Calc c1(1001, "Fujitsu", 3200), c2(1002, "Acer", 3500), c3, c4;

p1 << c1;
p2 << c2;
cout << c1;

cout << p1 << p2;

p4.pCalc = new Calc(1004, "Dell", 2500);


if (p1 > c2)
cout << endl << p1.nume << " are un calculator mai scump decat " <<
c2.marca << endl;
else
cout<<endl<<p1.nume<<" are un calculator mai ieftin decat " << c2.marca
<< endl;

if (p1 > p2)


cout << endl << p1.nume << " are un calculator mai scump decat " <<
p2.nume << endl;
else
cout << endl << p1.nume << " are un calculator mai ieftin decat " <<
p2.nume<< endl;

fstream calcOut("calc.Out", ios::out);


fstream persOut("pers.Out", ios::out);

calcOut << c1;


calcOut << c2;
persOut << p1;
persOut << p2;

calcOut.close();
persOut.close();

fstream calcInp("calc.Out", ios::in);


calcInp >> c3;
calcInp >> c4;
cout << c3;
cout << c4;
calcInp.close();

map<int, Calc> reteaCalc;


reteaCalc.insert(pair<int, Calc>(c3.idCalc, c3));
reteaCalc.insert(pair<int, Calc>(c4.idCalc, c4));

fstream persInp("pers.Out", ios::in);


persInp >> p3;
persInp >> p4;
cout << p3;
calcInp.close();

map<int, Calc>::iterator it = reteaCalc.find(p3.idCalc);


p3.pCalc = &(it->second);
cout << p3;
system("pause");
}

You might also like