Documente Academic
Documente Profesional
Documente Cultură
#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];
}
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)
{
}
//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");
}
}
}
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;
}
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