Sunteți pe pagina 1din 11

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<string>
using namespace std;
class Student
{
char* nume;
int varsta;
string facultate;
int nrNote;
int* note;
public:
//Constructori
//constructor default(fara parametrii)- datele de tip pointeri le initializam
cu NULL
//datele de tip numeric cu 0, iar cele de tip string cu " "
Student()
{
this->nume = new char[strlen("Nimeni") + 1];//+1 reprezinta
terminatorul pentru sirurile de caractere(Char*) ...."\0"
strcpy(this->nume, "Nimeni");
this->varsta = 0;
this->facultate = " ";
this->nrNote = 0;
this->note = NULL;
}
//constructorii cu toti parametrii
Student(const char* nume, int varsta, string facultate, int nrNote, int*
note)
{
this->nume = new char[strlen(nume) + 1];//+1 reprezinta terminatorul
pentru sirurile de caractere(Char*) ...."\0"
strcpy(this->nume, nume);
this->varsta = varsta;
this->facultate = facultate;
this->nrNote = nrNote;
this->note = new int[this->nrNote];//aici alocam spatiu pt vector
for (int i = 0; i < this->nrNote; i++)
this->note[i] = note[i];
}

//Constructor de copiere=> are aceeasi forma ca si ala cu toti parametrii


Student(const Student& s)
{
this->nume = new char[strlen("Constructor copiere") + 1];//+1
reprezinta terminatorul pentru sirurile de caractere(Char*) ...."\0"
strcpy(this->nume, "Constructor copiere");
this->varsta = s.varsta;
this->facultate = s.facultate;
this->nrNote = s.nrNote;
this->note = new int[this->nrNote];//aici alocam spatiu pt vector
for (int i = 0; i < this->nrNote; i++)
this->note[i] = s.note[i];
}
//Operator = => se compune destructor + constructor de copiere + return *this
Student& operator=(const Student& s)
{
if (this->nume != NULL)
delete[]this->nume;
if (this->note != NULL)
delete[]this->note;
this->nume = new char[strlen("Operator =") + 1];//+1 reprezinta
terminatorul pentru sirurile de caractere(Char*) ...."\0"
strcpy(this->nume, "Operator =");
this->varsta = s.varsta;
this->facultate = s.facultate;
this->nrNote = s.nrNote;
this->note = new int[this->nrNote];//aici alocam spatiu pt vector
for (int i = 0; i < this->nrNote; i++)
this->note[i] = s.note[i];
return *this;
}
//Sfarsit Constructor
//Getteri -> sunt functii accesor care ne permit sa afisam valorile datelor
din zona privata pe ecran
//ei au mereu tipul variabilei pe care dorim sa o returnam( pt nume -> char*
getNUme())
//in paranteza nu punem nimic
//in interior avem doar return this->numele_variabilei;
char* getNume()
{
return this->nume;
}

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

string getFacultate()
{
return this->facultate;
}
int* getnote()
{
return this->note;
}
int getNrNote()
{
return this->nrNote;
}
//Sfarsit Getteri

//Setteri
//setter-ul este la fel ca si getter-ul(o functie accesor), dar el doar
modifica, nu returneaza nimic
//tipul unui setter este mereu VOID!!
//in paranteza trecem parametru/variabila a carui valoare dorim sa o
schimbam/modificam
void setNume(const char* numeNou)
{
if (this->nume != NULL)
delete[]this->nume;
if (strlen(numeNou) > 2)
{
this->nume = new char[strlen(numeNou) + 1];//+1 reprezinta
terminatorul pentru sirurile de caractere(Char*) ...."\0"
strcpy(this->nume, numeNou);
}
else
{
this->nume = new char[strlen("Nume gresit") + 1];//+1 reprezinta
terminatorul pentru sirurile de caractere(Char*) ...."\0"
strcpy(this->nume, "Nume gresit");

}
void setVarsta(int varstaNoua)
{
this->varsta = varstaNoua;
}
void setFacultate(string facultateNoua)
{
this->facultate = facultateNoua;
}
void setNote(int nrNoteNoi, int* noteNoi)
{
if (nrNoteNoi > 0)
{
this->nrNote = nrNoteNoi;
delete[]this->note;
this->note = new int[this->nrNote];
for (int i = 0; i < this->nrNote; i++)
{
if (noteNoi[i] > 0 && noteNoi[i] <= 10)
{
this->note[i] = noteNoi[i];
}
else
{
this->note[i] = 1;
}
}
}
else
{
this->nrNote = 0;
this->note = NULL;
}
}
//Sfarsit Setteri
//metode
//are tipul valori/varibilei pe care vrei o sa returnezi si asupra careia
vrei sa aplici metoda
int sumaNote()
{
int suma = 0;
for (int i = 0; i < this->nrNote; i++)//punem numarul de care depinde
vectorul
{
suma = suma + this->note[i]; // e acelasi lucru cu suma +=this-
>note[i]
}
return suma;
}
float medieNote()
{
float suma = 0;
for (int i = 0; i < this->nrNote; i++)//punem numarul de care depinde
vectorul
{
suma = suma + this->note[i]; // e acelasi lucru cu suma +=this-
>note[i]
}
return suma / this->nrNote;
}
bool areRestanta()
{
bool restanta = 0;// presupun ca studentul nu are restante => 0
inseamna false
for (int i = 0; i < this->nrNote; i++)
{
if (this->note[i] < 5)
{
restanta = 1;
}
}
return restanta;
}
bool areBursa()
{
int suma = 0;
float medie = 0;
float pragMedie = 8.5;
bool restanta = 0;
bool bursa = 0;
for (int i = 0; i < this->nrNote; i++)
{
suma = suma + this->note[i];
if (this->note[i] < 5)
{
restanta = 1;
}
}
medie = suma / this->nrNote;
if (medie >= 8.5 && restanta == 0)
{
bursa = 1;
}
return bursa;
}
void adaugaNota(int nota)
{

//pasul 1 - punem in variabila noua, numarul de note


int nr = this->nrNote;// am pus in nr valoarea lui this->nrNote
//pasul 2 - cream un vector pe baza noii variabile -> care la noi e
nr(in cazul asta)
int* vect = new int[nr];
//pasul 3 - punem valorile in vectorul nou
//folosim for
for (int i = 0; i < nr; i++)
{
vect[i] = this->note[i];
}
//pasul 4 - stergem vectorul cu this(this->note)
delete[]this->note;
//pasul 5- marim numarul de note
this->nrNote = this->nrNote + 1;
//pasul 6 - alocam memorie pt vectorul din obiect
this->note = new int[this->nrNote];
//pasul 7 - punem valorile din vectorul auxiliar(vect) in this->note
for (int i = 0; i < nr; i++)
{
this->note[i] = vect[i];
}
//pasul 8 - completam spatiile libere din vector cu numarul din
antet(din paranteza)
for (int i = nr; i < this->nrNote; i++)
{
if (nota > 1 && nota <= 10)
{
this->note[i] = nota;
}
else
{
this->note[i] = 1;
}

}
//sfarsti metode

//destructor
~Student() // daca nu avem elemente de tip pointer(alea cu *) =>
~nume_clasei(){}
{
if (this->nume != NULL)
delete[]this->nume;
if (this->note != NULL)
delete[]this->note;
}
//sfarsit destructor
friend ostream& operator<<(ostream& out, Student& s)
{
out << s.nume << " " << s.varsta << " " << s.facultate << " " <<
s.nrNote << " ";
for (int i = 0; i < s.nrNote; i++)
out << s.note[i] << " ";
return out;
}

//operator >>
friend istream& operator>>(istream& in, Student& s)
{
delete[]s.nume;
delete[]s.note;
char aux[100];//aici punem numele pe care vrem sa il adaugam in obiect
cout << "Introduceti numele: ";
in >> aux;
if (strlen(aux) > 2)
{
s.nume = new char[strlen(aux) + 1];
strcpy(s.nume, aux);
}
else
{
s.nume = new char[strlen("Nume necunoscut") + 1];
strcpy(s.nume, "Nume necunoscut");
}

cout << "Introduceti varsta: ";


in >> s.varsta;
if (s.varsta <= 18 || s.varsta > 45)
{
s.varsta = 18;
}
cout << "Introduceti facultate: ";
in >> s.facultate;
if (s.facultate.length() < 3)
s.facultate = "Facultate necunoscuta";
cout << "Introduceti numar note: ";
in >> s.nrNote;
if (s.nrNote > 0)
{

s.note = new int[s.nrNote];


cout << "Introduceti note: ";
for (int i = 0; i < s.nrNote; i++)
{
in >> s.note[i];
if (s.note[i] < 1)
{
s.note[i] = 1;
}
if (s.note[i] > 10)
{
s.note[i] = 10;
}

}
}
else
{
s.nrNote = 0;
s.note = NULL;
}
return in;
}
//operator index - []
//este folosit pentru a indica/ a afisa/a modifica valoarea de pe o anumita
pozitie din vector
Student operator +=(int valoare)
{
this->varsta += valoare;// this->varsta = this->varsta +valoare
return *this;

}
Student operator +(int valoare)
{
Student stud = *this;
stud.nrNote = this->nrNote + 1;
if (stud.note != NULL)
delete[]stud.note;
stud.note = new int[stud.nrNote];
for (int i = 0; i < this->nrNote; i++)
{
stud.note[i] = this->note[i];
}
for (int i = this->nrNote; i < stud.nrNote; i++)
{
stud.note[i] = valoare;
}
return stud;
}
Student operator +(Student &s)//adunam notele la 2 studenti
{
Student stud = *this;
stud.nrNote = this->nrNote + s.nrNote;
if (stud.note != NULL)
delete[]stud.note;
stud.note = new int[stud.nrNote];
for (int i = 0; i < this->nrNote; i++)
{
stud.note[i] = this->note[i];
}
for (int i = this->nrNote; i < stud.nrNote; i++)
{
stud.note[i] = s.note[i-this->nrNote];
}
return stud;
}
int &operator[](int index)
{
int x = -1;
if (index >= 0 && index < this->nrNote)
{
return this->note[index];
}
else
{
return x;
}
}
//operator functie- ()
int operator()()
{
return this->varsta;
}
//operatorul cast - la int, la float( la tot)
explicit operator char* ()
{
return this->nume;
}
explicit operator string()
{
return this->facultate;
}
explicit operator int()
{
return this->nrNote;
}
explicit operator int*()
{

return this->note;
}
//operator ->
Student* operator->()
{
this->varsta =25;
return this - varsta;

}
//operatori de tip bool !,<,>, ==
bool operator!()
{
bool restanta = 0;
for (int i = 0; i < this->nrNote; i++)
{
if (this->note[i] < 5)
{
restanta = 1;
}
}
return restanta;
}
bool operator <(Student& s)
{
float sumaS1 = 0;
float sumaS2 = 0;
for (int i = 0; i < this->nrNote; i++)
{
sumaS1 = sumaS1 + this->note[i];
}
for (int i = 0; i < s.nrNote; i++)
{
sumaS2 = sumaS2 + s.note[i];
}
return (sumaS1 / this->nrNote) < (sumaS2 / s.nrNote);
}
bool operator >(Student& s)
{
return this->nrNote > s.nrNote;
}
bool operator ==(Student& s)
{
return this->varsta == s.varsta;
}

};

void main()
{
int note[] = { 9,9,10 };
//cum cream obiecte?=> le cream folosind numele clasei
Student s;//aici am creat un student pe baza constructorului fara parametrii
Student s1("Irina", 18, "CSIE", 3, note);//aici am creat un student pe baza
constructorului cu toti parametrii
//cout << "--------GETTERI--------" << endl;
////cum apelam getteri=> cout<<numele_obiectului.numele_getterului();
//cout << "Nume student s: " << s.getNume() << endl;
//cout << "Varsta student s: " << s.getVarsta() << endl;
//cout << "Facultate student s: " << s.getFacultate() << endl;
//cout << endl;
//cout << "Nume student s1: " << s1.getNume() << endl;
//cout << "Varsta student s1: " << s1.getVarsta() << endl;
//cout << "Facultate student s1: " << s1.getFacultate() << endl;
//cout << "--------sfarsit GETTERI--------" << endl;
//cout << endl << endl;
//cout << "--------SETTERI--------" << endl;
////cum apelam un setter =>> ca la getter dar ca nu mai punem cout
//s.setNume("Viorel");
//s.setVarsta(22);
//s.setFacultate("Poli");
//cout << "Nume student s: " << s.getNume() << endl;
//cout << "Varsta student s: " << s.getVarsta() << endl;
//cout << "Facultate student s: " << s.getFacultate() << endl;
//cout << "--------sfarsit SETTERI--------" << endl;

//cout << "--------CONSTRUCTOR DE COPIERE--------" << endl;


////il folosim pentru a crea obiecte noi pe baza unora deja existente
//Student s2 = s1;
//cout << "Nume student s2: " << s2.getNume() << endl;
//cout << "--------sfarsit CONSTRUCTOR DE COPIERE--------" << endl;
//cout << "--------OPERATOR =--------" << endl;
////atribuie valorile unui obiect la altul
//Student s3;
//s3 = s1;
//cout << "Nume student s3: " << s3.getNume() << endl;
//cout << "--------sfarsit OPERATOR =--------" << endl;
//cout << "--------OPERATOR <<--------" << endl;
//cout << s1 << endl;
//cout << "--------sfarsit OPERATOR <<--------" << endl;
cout << s1.getNrNote() << endl;
cout << s1.getnote() << endl;//aici afisam adresa
for (int i = 0; i < s1.getNrNote(); i++)
{
cout << s1.getnote()[i] << " "; // asa afisam corect notele
}
cout << endl;
Student stud = s1;
int noteNoi2[] = { 10,10,8,10 };
s1.setNote(4, noteNoi2);
cout << s1.getNrNote() << endl;
cout << s1.getnote() << endl;//aici afisam adresa
for (int i = 0; i < s1.getNrNote(); i++)
{
cout << s1.getnote()[i] << " "; // asa afisam corect notele
}
cout << endl;
/*s1.setNume("An");
cout << s1.getNume() << endl;*/
cout << s1.sumaNote() << endl;
cout << s1.medieNote() << endl;
if (s1.areRestanta())
{
cout << "Studentul s1 are restanta." << endl;
}
else
{
cout << "Studentul s1 nu are restanta." << endl;
}
if (s1.areBursa())
{
cout << "Studentul s1 are bursa." << endl;
}
else
{
cout << "Studentul s1 nu are bursa." << endl;
}
s1.adaugaNota(10);
cout << s1 << endl;
/*Student s12;
cin >> s12;
cout << s12 << endl;*/
cout << "--------OPERATORI --------" << endl;
cout << s1[2] << endl;//asa apelam operatorul index
int notaNoua;
/*cout << "Introduceti noua nota pe care doriti sa o puneti in vector: ";
cin >> notaNoua;
if(notaNoua <1)
{
notaNoua = 1;
}
else if (notaNoua > 10)
{
notaNoua = 10;
}
s1[2] = notaNoua;*/
cout << s1[2] << endl;//asa apelam operatorul index
cout << s1<< endl;
s1->setVarsta(70);
s1 += 20;
cout << s1 << endl;
cout << s1() << endl;//asa apelam operatorul functie
cout << (char*)s1 << endl;// operator cast pt char*
cout << (string)s1 << endl;// operator cast pt string
cout << (int)s1 << endl;// operator cast pt int
cout << (int*)s1 << endl;// operator cast pt int*
cout << s1.getVarsta() << endl;
cout << s1.getVarsta() << endl;

cout << s1 << endl;//asa apelam operatorul functie


cout << stud << endl;//asa apelam operatorul functie
if (!s1)
{
cout << "Studentul are restanta!" << endl;
}
else
{
cout << "Studentul nu are restanta!" << endl;

}
if (s1 < stud)
{
cout << "Studentul stud are media mai mare decat Studentul s1!" <<
endl;
}
else
{
cout << "Studentul stud nu are media mai mare decat Studentul s1!" <<
endl;

}
if (s1 > stud)
{
cout << "Studentul s1 are mai multe note decat Studentul stud!" <<
endl;
}
else
{
cout << "Studentul stud are mai multe note decat Studentul s1!" <<
endl;

}
if (s1 == stud)
{
cout << "Studentul s1 are aceeasi varsta ca Studentul stud!" << endl;
}
else
{
cout << "Studentul s1 nu are aceeasi varsta ca Studentul stud!" <<
endl;

}
s1 = s1 + 50;
cout << s1 << endl;
s1 = s1 + stud;
cout << s1 << endl;
cout << "--------sfarsit OPERATORI --------" << endl;
}
//de completat cu getter si setter pentru pointer

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