Documente Academic
Documente Profesional
Documente Cultură
#include <string>
#include <fstream>
using namespace std;
}
};
class student
{
private:
const int id;
int* note;
int nrNote;
char* grupa;
int anulNasterii = 2002;
static string universitate;
public:
int matricol;
string nume;
student() : id(1)
{
nume = "Anonim";
matricol = 0;
grupa = nullptr;
note = nullptr;
nrNote = 0;
}
student(int id, string nume, int matricol, int* note, int nrNote) : id(id)
{
this->nume = nume;
if (matricol > 0)
{
this->matricol = matricol;
}
else
{
this->matricol = 0;
}
grupa = nullptr;
if (note != nullptr && nrNote > 0)
{
this->note = new int[nrNote];
for (int i = 0; i < nrNote; i++)
{
this->note[i] = note[i];
}
this->nrNote = nrNote;
}
else
{
this->note = nullptr;
this->nrNote = 0;
}
}
virtual ~student()
{
if (note != nullptr)
{
delete[] note;
}
if (grupa != nullptr)
{
delete[] grupa;
}
}
nume = s.nume;
matricol = s.matricol;
delete[] grupa;
if (s.grupa != nullptr)
{
int len = strlen(s.grupa) + 1;
grupa = new char[len];
strcpy_s(grupa, len, s.grupa);
}
else
{
grupa = nullptr;
}
anulNasterii = s.anulNasterii;
if (s.note != nullptr && s.nrNote > 0)
{
nrNote = s.nrNote;
note = new int[s.nrNote];
for (int i = 0; i < s.nrNote; i++)
{
note[i] = s.note[i];
}
}
else
{
note = nullptr;
nrNote = 0;
}
return *this;
}
void serializare()
{
ofstream f("student.bin", ios::binary);
int length = nume.length() + 1;
f.write((char*)&length, sizeof(length));
f.write(nume.c_str(), length);
f.write((char*)&matricol, sizeof(matricol));
f.write((char*)&anulNasterii, sizeof(anulNasterii));
f.write(grupa, (long long)strlen(grupa) + 1);
f.write((char*)&nrNote, sizeof(nrNote));
for (int i = 0; i < nrNote; i++)
{
f.write((char*)¬e[i], sizeof(note[i]));
}
f.close();
}
void deserializare()
{
ifstream f("student.bin", ios::binary);
int length = 0;
f.read((char*)&length, sizeof(length));
char* aux = new char[length];
f.read(aux, length);
nume = aux;
f.read((char*)&matricol, sizeof(matricol));
f.read((char*)&anulNasterii, sizeof(anulNasterii));
string buffer = "";
char c = 0;
while ((c = f.get()) != 0)
{
buffer += c;
}
delete[] grupa;
grupa = new char[buffer.length() + 1];
strcpy_s(grupa, buffer.length() + 1, buffer.c_str());
f.read((char*)&nrNote, sizeof(nrNote));
delete[] note;
note = new int[nrNote];
for (int i = 0; i < nrNote; i++)
{
f.read((char*)¬e[i], sizeof(note[i]));
}
f.close();
}
void metoda()
{
cout << "student" << endl;
}
int getAnulNasterii()
{
return anulNasterii;
}
string getNume();
int* getNote()
{
if (note != nullptr)
{
int* copie = new int[nrNote];
for (int i = 0; i < nrNote; i++)
{
copie[i] = note[i];
}
return copie;
}
else
{
return nullptr;
}
}
int getNrNote()
{
return nrNote;
}
bool operator!()
{
return nrNote > 0;
}
student operator++()
{
this->anulNasterii++;
return *this;
}
student operator++(int i)
{
student copie = *this;
this->anulNasterii++;
return copie;
}
string operator()()
{
return nume;
}
string student::getNume()
{
return nume;
}
char buffer[100];
//in.clear();
//in.ignore(2);
in >> ws;
in.getline(buffer, 99);
s.grupa = new char[strlen(buffer) + 1];
strcpy_s(s.grupa, strlen(buffer) + 1, buffer);
cout << "Anul nasterii: ";
in >> s.anulNasterii;
if (s.note != nullptr)
{
delete[] s.note;
}
cout << "Numar note: ";
in >> s.nrNote;
if (s.nrNote > 0)
{
s.note = new int[s.nrNote];
for (int i = 0; i < s.nrNote; i++)
{
cout << "nota[" << i << "] = ";
in >> s.note[i];
}
}
else
{
s.nrNote = 0;
s.note = nullptr;
}
return in;
}
class voluntar
{
private:
int nrZile;
int* oreZilnice;
public:
voluntar()
{
nrZile = 0;
oreZilnice = nullptr;
}
~voluntar()
{
delete[] oreZilnice;
}
voluntar(const voluntar& v)
{
if (v.nrZile > 0 && v.oreZilnice != nullptr)
{
nrZile = v.nrZile;
oreZilnice = new int[nrZile];
for (int i = 0; i < nrZile; i++)
{
oreZilnice[i] = v.oreZilnice[i];
}
}
else
{
nrZile = 0;
oreZilnice = nullptr;
}
}
void metoda()
{
cout << "voluntar" << endl;
}
};
public:
student_voluntar()
{
organizatie = nullptr;
}
~student_voluntar()
{
if (organizatie != nullptr)
{
delete[] organizatie;
}
}
void metoda()
{
student::metoda();
voluntar::metoda();
}
return in;
}
int main()
{
int note[] = { 10, 8, 9 };
student s1;
s1.setNume("Ionel Popescu");
cout << s1.getNume() << endl;
s1.schimba_grupa("1133");
student* ps = &s1;
cout << (*ps).getNume() << endl;
ps->matricol = 123;
cout << ps->matricol << endl;
s3.setNume("Adrian Pop");
cout << s3.getNume() << endl;
note[1] = 12;
int* x = s3.getNote();
if (x != nullptr)
{
for (int i = 0; i < s3.getNrNote(); i++)
{
cout << x[i] << " ";
}
}
cout << endl;
student s4;
s4 = s3;
student::setUniversitate("Politehnica");
student studenti[] = { s1, s3 };
float result = student::medieSerie(studenti, 2);
cout << result << endl;
if (!s4)
{
cout << "are note" << endl;
}
else
{
cout << "nu are note" << endl;
}
student s5 = ++s4;
student s6 = s4++;
student s7 = s4 + 3;
student s8 = 4 + s4;
cout << s5;
student s9;
//cin >> s9;
//cout << s9;
student s10;
student_voluntar sv1;
s10 = sv1;
student* pstud = &sv1;
try
{
sv3[0];
sv3 + 2;
sv3.schimba_grupa(nullptr);
}
catch (null_exception& e)
{
cout << "NULL -> " << e.what();
}
catch (exception& e)
{
cout << e.what();
}
catch (int x)
{
cout << "cod eroare: " << x;
}
catch (...)
{
cout << "alta eroare";
}
ofstream f;
f.open("student.txt");
f << s4;
f.close();
ifstream g("student.txt");
student s12;
g >> s12;
g.close();
ifstream k("student_voluntar.txt");
student_voluntar sv5, sv6;
k >> sv5 >> sv6;
k.close();
s4.schimba_grupa("1133");
s4.serializare();
student s14;
s14.deserializare();
voluntar v1;
int ore[] = { 5, 3, 2, 1 };
voluntar v2(5, ore);
voluntar v3 = v2;
v1 = v2;
sv6.student::metoda();
sv6.metoda();
student* grupa[3];
grupa[0] = new student(5, "Gigel", 123, vector, 4);
grupa[1] = new student_voluntar(6, "Ionel", 456, vector, 4, "SiSC");
grupa[2] = new student(7, "Vasile", 458, vector, 4);
delete grupa[0];
delete grupa[1];
delete grupa[2];
}