Sunteți pe pagina 1din 12

/*CONSTRUCTOR FARA PARAMETRI => nu returneaza nimic

constructorul poarta numele clasei


rol : alocare zona de memorie pt variab dinamice +initializare*/

Student()
{
cout << "\n Apel constructor fara parametri:";

//initializare obiect THIS cu valori default


strcpy(this->nume, "Anonim");
this->varsta = 18;
this->nrNote = 0;
this->note = NULL;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Proiect() : cod(0) /*lista de initializatori; echiv lui this->cod=0; OBLIGATORIU pt atribute constante
sa se afle in lista de initializatori*/, nrAngajati(0), nrEtape(0), costEtape(0) //optionale
{
cout << "\nApel constructor fara param:";
}

//CONSTRUCTOR CU PARAMETRI

Proiect(int cod, int nrAngajati, int nrEtape, int* costEtape) :cod(cod) //echiv cu this->cod=cod
{

cout << "\nApel constructor cu param:";


this->nrAngajati = nrAngajati; //primul e atribut, al doilea parametru
this->nrEtape = nrEtape;

//DO NOT
//this->costEtape=costEtape; ->shallow copy;
if (costEtape != NULL) // verif daca avem pt ce aloca zona de memorie
{
//aloca zona de mem
this->costEtape = new int[this->nrEtape]; //atribut alocat dinamic

//copiere elem cu elem


for (int i = 0; i < this->nrEtape; i++)
this->costEtape[i] = costEtape[i];
}
else
this->costEtape = NULL;
}

//CONSTRUCTOR DE COPIERE
Proiect(const Proiect& p): cod(p.cod) //primeste inglobate intr-un obiect, nu fiecare in parte;
se pune const, in gen, de fiecare data cand avem transfer prin referinta si nu vrem sa modif obiectul
//pt a fi siguri ca nu
modif val ob prim prin ref

{
cout << "\nApel constructor de copiere";
this->nrAngajati =p. nrAngajati;
this->nrEtape = p.nrEtape;

if (p.costEtape != NULL) // verif daca avem pt ce aloca zona de memorie


{
//aloca zona de mem
this->costEtape = new int[this->nrEtape]; //atribut alocat dinamic

//copiere elem cu elem


for (int i = 0; i < this->nrEtape; i++)
this->costEtape[i] = p.costEtape[i];
}
else
this->costEtape = NULL;
}

// SET
//modifica varsta, nu returneaza nimic
void setVarsta(int varstaNoua)
{
//adaugare de validari
if(varstaNoua >=18)
this->varsta = varstaNoua;

//modifica nume
void setNume(char* numeNou)
{
strcpy(this->nume, numeNou);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setNote(int* note, int nrNote)//e pe void pt ca seteaza, nu returneaza
{
delete[]this->note; //pt a evita memory leaks
if (note != NULL)
{
this->nrNote = nrNote;
this->note = new int[this->nrNote];
for (int i = 0; i < this->nrNote; i++)
this->note[i] = note[i];
}

}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

static int getVarstaMaxima() {


return Student::VARSTA_MAXIMA;
}

//GET
//folosit pt a consulta, nu returneaza nimic

int getVarsta()
{
return this->varsta;
}

char* getNume()
{
return this->nume;
}

//DESTRUCTOR,
~Proiect() singura functionalitate= sa dezaloce zona de mem
{
cout << "\nApel destructor!";
//dezalocare zona de memorie alocata dinamic
if (this->costEtape != NULL)
delete[] this->costEtape;
}

OPERATOR =

Proiect& operator=(const Proiect& p)


{
//combinatie intre destructor si constructor de copiere
//obiectul this(p1) este deja construit => verific daca:

cout << "\nApel operator=";


if (this->costEtape != NULL)
delete[] this->costEtape;

//copiem codul din constr de copiere

this->nrAngajati = p.nrAngajati;
this->nrEtape = p.nrEtape;

if (p.costEtape != NULL) // verif daca avem pt ce aloca zona de memorie


{
//aloca zona de mem
this->costEtape = new int[this->nrEtape]; //atribut alocat dinamic

//copiere elem cu elem


for (int i = 0; i < this->nrEtape; i++)
this->costEtape[i] = p.costEtape[i];
}
else
this->costEtape = NULL;

return *this;

OPERATORUL<<

ostream& operator<<(ostream& out, const Proiect& p)


{
//copie metoda afisare

out << "\n\nCod:" << p.cod;


out << "\nNr angajati:" << p.nrAngajati;
if (p.costEtape != NULL)
{
out << "\nCost etape:";
for (int i = 0; i < p.nrEtape; i++)
out << p.costEtape[i] << " ";
}
else
out << "\nFara etape!" << endl;
return out;
}
OPERATORUL >>

istream& operator>>(istream& in, Proiect& p) // PRIMESTE OBIECTUL p PRIN REFERINTA , pt ca


obiectul p se modifica la citire!!!!!(si vreau sa se modif in urma citirii
{
cout << "\nIntroduceti nr angajati:";
in >> p.nrAngajati;
cout << "\nIntroduceti nr etape:";
in >> p.nrEtape;
//pt a nu avea memory leaks
if (p.costEtape != NULL)
delete[]p.costEtape;
p.costEtape = new int[p.nrEtape];
for (int i = 0; i < p.nrEtape; i++)
{
cout << "c[" << i << "]=";
in >> p.costEtape[i];
}
return in;
}

OPERATORUL >=

bool operator >=(Proiect p)


{
if (this->nrAngajati >= p.nrAngajati)
return true;
return false;
}

OPERATORUL++

//forma de pre-incrementare

ap3 = ++ap1; //ap1.operator++(); //mai intai se implem o anumit variab(ap3), urmand ca


aceasta variab sa ia valoarea ap1
cout << ap3 << ap1;

Apartament operator++()
{
this->etaj++;
return *this;
}

//forma de post-incrementare

ap3 = ap1++; //ap1.operator++(10); //ap3 va lua valoarea lui ap1, si apoi il marim pe ap1
cout << ap3 << ap1;

Apartament operator++(int)
{
Apartament copie = *this;
this->etaj++;
return copie;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//preincrementare
Student operator++() {
this->varsta += 1;
return *this;
}

//postincrementare
Student operator++(int) {
Student copie = *this;
this->varsta += 1;
return copie;
}

OPERATORUL [] INDEX

float operator[](int indice)


{
if (indice < this->nrCamere && indice >= 0)
return this->suprafata[indice];
else
throw exception("Indice invalid");
}

main:

try
{
float suprafataCamera = ap1[1]; //ap1.operator[](1)
cout << "\n" << suprafataCamera;
}

catch (exception ex)


{
cout << "\n" << ex.what();
}

try
{
float suprafataCamera2 = ap1[5];
cout << "\n" << suprafataCamera2;
}

catch (exception ex)


{
cout << "\n" << ex.what();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int& operator[](int index) {


if (index >= 0 && index <= this->nrNote)
return this->note[index];
else
throw new exception();
}

OPERATORUL +=

Apartament operator+=(string numeToAdd)


{
this->proprietar += "-";
this->proprietar += numeToAdd;
return *this;
}

main

// ap2 = ap1 + "Gigel nou";


ap2 += "Gigel nou"; //ap2.operator+=("Gigel nou");
cout << ap2;
OPERATORUL FUNCTIE

explicit operator float() //se implem cast ul la float singurul care nu primeste semnul grafic
//nu prim nimic ca param pt ca suntem in clasa si
primeste automat this-ul, singurul de care are nevoie
//adaugarea cuvantului explicit, forteaza utilizarea cast-ului
doar atunci cand e explicit
{
int s = 0;//suma
for (int i = 0; i < this->nrNote; i++)
s += this->note[i];
if (this->nrNote != 0)
return (float)s / this->nrNote;
return 0;

MAIN:

int nrnote = s2(5, 7);


//apel explicit
s2.operator() (5, 7);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//operatorul la float
explicit operator float() {
return this->getMedie();
}

explicit operator int() {


return this->varsta;
}

OPERATORUL CAST

int operator()(int li, int ls)


{
int nr = 0;
for (int i = 0; i < this->nrNote; i++)
if (this->note >= li && this->note[i] <= ls)
nr++;
return nr;}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
explicit operator float() //se implem cast ul la float singurul care nu primeste semnul grafic
//nu prim nimic ca param pt ca suntem in clasa si
primeste automat this-ul, singurul de care are nevoie
//adaugarea cuvantului explicit, forteaza utilizarea cast-ului
doar atunci cand e explicit
{
int s = 0;//suma
for (int i = 0; i < this->nrNote; i++)
s += this->note[i];
if (this->nrNote != 0)
return (float)s / this->nrNote;
return 0;

main:

//OPERATORUL CAST, op unar, se implem in clasa


//apel cast explicit

delete ps1; // dezaloca spatiul ocupat de ps1 – implementati apelul pentru stergere
float medie = (float)s1;//forteaza s1 la float

//apel cast implicit


//float medie2 = s2;
cout << "\n" << medie; //afiseaza 6
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int operator()(int min, int max) {


int contor = 0;
for (int i = 0; i < this->nrNote; i++)
if (this->note[i] >= min && this->note[i] <= max)
contor += 1;
return contor;
}
OPERATORUL +

Student operator+(Student& stud, int nota) {

Student rezultat = stud;

//adaugam o nota noua la lista existenta


//NU ASA
//this->note[this->nrNote] = nota;

int* listaNoua = new int[rezultat.getNrNote() + 1];


for (int i = 0; i < rezultat.getNrNote(); i++)
listaNoua[i] = rezultat.note[i];
listaNoua[rezultat.nrNote] = nota;

if (rezultat.note != NULL)
delete[] rezultat.note;

rezultat.note = listaNoua;
rezultat.nrNote += 1;

return rezultat;

Student operator+(int nota, Student& stud) {


return stud + nota;
}

OPERATOR - =

void operator-=(int nrNoteDeSters) {


if (nrNoteDeSters <= this->nrNote) {
int* listaNoua = new int[this->nrNote - nrNoteDeSters];
for (int i = 0; i < this->nrNote - nrNoteDeSters; i++)
listaNoua[i] = this->note[i];
if (this->note != NULL)
delete[] this->note;
this->note = listaNoua;
this->nrNote -= nrNoteDeSters;
}
else
throw new exception();
}

OPERATORUL = =

bool operator==(Student stud) {


//const char* test1 = "Gigel";
//const char* test2 = "gigel";
//if (strcmp(test1, test2) == 0)
// cout << endl << " gigel == Gigel";
//else
// cout << endl << "Sunt diferite";
if (this->nume.compare(stud.nume) == 0)
return true;
else
return false;

OPERATOR >

bool operator>(Student stud1, Student stud2) {


if(stud1.getMedie() > stud2.getMedie())
return true;
else
return false;
}

OPERATORUL !

bool operator!(Student stud) {


bool esteRestantier = false;
for(int i=0;i<stud.getNrNote();i++)
if (stud.getNota(i) < 5) {
esteRestantier = true;
break;
}
return esteRestantier;
}

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