Documente Academic
Documente Profesional
Documente Cultură
Elementele definite vor fi punctate doar dacă sunt complete și au fost testate în main().
Recapitulare, exemplu cu angajat
#include <iostream>
#include <string>
using namespace std;
class Persoana {
protected:
string nume;
int varsta;
float salariu;
public:
Persoana() {
this->nume = "Test1";
this->varsta = 25;
this->salariu = 2500;
}
class IMarireSalariu {
public:
virtual void maresteSalariu(int procent) = 0;
virtual float getSalariu()=0;
};
public:
Angajat() :Persoana() {
this->vechime = 10;
}
//overriding
void afisare() override {
cout << "Nume " << this->nume << endl;
cout << "Varsta " << this->varsta << endl;
cout << "Salariu " << this->salariu << endl;
cout << "Vechime " << this->vechime << endl;
}
void getInfoPers() {
this->afisare();
}
float getSalariu() {
return this->salariu;
}
Intern() {
this->durata = 3;
}
Intern(int durata,string nume, int varsta, float
salariu) :Persoana(nume,varsta, salariu) {
this->durata=durata;
}
void getInfoPers() {
this->afisare();
}
float getSalariu() {
return this->salariu;
}
};
class Echipa {
private:
int nrPersoane;
Persoana** persoane;
public:
Echipa() {
this->nrPersoane = 0;
this->persoane = NULL;
}
~Echipa() {
if (this->persoane != NULL) {
delete[] this->persoane;
}
}
void afisare() {
cout << "Numar persoane " << this->nrPersoane << endl;
for (int i = 0; i < this->nrPersoane; i++) {
this->persoane[i]->afisare();
}
}
};
void main() {
Persoana* p;
//p->afisare();
Angajat a1;
a1.afisare();
a1.afisare("Afisez inca o data");
Angajat a2(5,"Test2", 26, 3000.0f);
a2.getInfoPers();
Intern i1;
i1.afisare();
i1.getInfoPers();
//upcasting
Persoana* persoane[3] = { &a1,&a2,&i1 };
//downcasting
cout << ((Angajat*)persoane[1])->getSalariu();
IMarireSalariu* lista[3];
lista[0] = &a1;
lista[1] = &a2;
lista[2] = new Intern(1,"Test3",22,456.0f);
public:
Masina() :serie(1) {
this->marca = "Dacia";
this->anFabricatie = 2012;
this->tipCaroserie = "berlina";
this->pret = 1234.5;
this->nrModele = 2;
this->modele = new string[nrModele];
for (int i = 0; i < this->nrModele; i++) {
this->modele[i] = "Model " + to_string(i + 200);
}
this->nrMasini++;
}
string getMarca() {
return this->marca;
}
int getAnFabricatie() {
return this->anFabricatie;
}
void setAnFabricatie(int anFabricatieNou) {
this->anFabricatie = anFabricatieNou;
}
string* getModele() {
return this->modele;
}
float getPret() {
return this->pret;
}
int getSerie() {
return this->serie;
}
~Masina() {
if (this->modele != NULL) {
delete[] this->modele;
}
Masina::nrMasini--;
}
if (a.modele != NULL) {
delete[] a.modele;
}
a.modele = new string[a.nrModele];
for (int i = 0; i < a.nrModele; i++) {
cout << "Dati modelul " << i + 1 << ": ";
in >> a.modele[i];
}
return in;
}
//supraincarcare operator +
//este folosit pentru a aduna o variabila de tip float cu un obiect
Masina
//folosit pentru a face operatii de forma: m1+250.0f
Masina operator+(float nr) {
Masina copie = *this;
copie.pret += nr;
return copie;
}
return *this;
}
return copie;
}
void afisare() {
cout << "Serie = " << this->serie << endl;
cout << "Nr masini = " << this->nrMasini << endl;
cout << "Marca = " << this->marca << endl;
cout << "An fabricatie = " << this->anFabricatie << endl;
cout << "Tip caroserie = " << this->tipCaroserie << endl;
cout << "Pret = " << this->pret << endl;
cout << "Nr modele = " << this->nrModele << endl;
for (int i = 0; i < this->nrModele; i++) {
cout << this->modele[i] << endl;
}
}
};
int main() {
Masina m1;
Masina m2;
m1.afisare();
cout << "\n=============================\n";
float a = 500.0f;
a -= m1;
cout << "Operator -= " << a;
m1 += 200;
return 0;
}
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
class Examen
{
private:
const int idExamen;
protected:
string denumire;
int nota;
string data;
int subiecte;
float* punctajPeSubiect;
public:
static int contor;
Examen() :idExamen(contor++)
{
this->denumire = "";
this->nota = 1;
this->data = "01/06/2020";
this->subiecte = 0;
this->punctajPeSubiect = NULL;
}
Examen(string denumire, int nota, string data, int subiecte, float*
punctajPeSubiect) :idExamen(contor++)
{
if (denumire.size() > 2)
{
this->denumire = denumire;
}
if (nota < 1)
this->nota = 1;
if (nota > 10)
this->nota = 10;
else
this->nota = nota;
if (data.size() == 8)
this->data = data;
this->subiecte = subiecte;
this->punctajPeSubiect = new float[this->subiecte];
for (int i = 0; i < this->subiecte; i++)
{
this->punctajPeSubiect[i] = punctajPeSubiect[i];
}
}
Examen(const Examen& e) :idExamen(e.idExamen)
{
if (e.denumire.size() > 2)
{
this->denumire = e.denumire;
}
if (e.nota < 1)
this->nota = 1;
if (e.nota > 10)
this->nota = 10;
else
this->nota = e.nota;
if (e.data.size() == 8)
this->data = e.data;
this->subiecte = e.subiecte;
this->punctajPeSubiect = new float[this->subiecte];
for (int i = 0; i < this->subiecte; i++)
{
this->punctajPeSubiect[i] = e.punctajPeSubiect[i];
}
}
Examen& operator =(const Examen& e)
{
if (this != &e)//evitarea auto-asignarii
{
if (this->punctajPeSubiect != NULL)
delete[]this->punctajPeSubiect;
if (e.denumire.size() > 2)
{
this->denumire = e.denumire;
}
if (e.nota < 1)
this->nota = 1;
if (e.nota > 10)
this->nota = 10;
else
this->nota = e.nota;
if (e.data.size() == 8)
this->data = e.data;
this->subiecte = e.subiecte;
this->punctajPeSubiect = new float[this->subiecte];
for (int i = 0; i < this->subiecte; i++)
{
this->punctajPeSubiect[i] =
e.punctajPeSubiect[i];
}
return *this;
}
}
~Examen()
{
if (this->punctajPeSubiect != NULL)
delete[]this->punctajPeSubiect;
cout << "S-a apelat destructorul" << endl;
}
friend ostream& operator<<(ostream& out, const Examen& e)
{
out << "Examenul "<<e.idExamen << " la disciplina " <<
e.denumire << " a avut nota " << e.nota << ", in data de " << e.data << "
Punctajele sunt: ";
for (int i = 0; i < e.subiecte; i++)
{
out << e.punctajPeSubiect[i] << " ";
}
out << endl;
return out;
}
explicit operator string()
{
return this->denumire;
}
bool operator >(int prag)
{
return this->nota > prag;
}
string getDenumire()
{
return this->denumire;
}
int getNrSubiecte()
{
return this->subiecte;
}
float totalPunctaj()
{
float total = 0;
for (int i = 0; i < this->subiecte; i++)
{
total = total + punctajPeSubiect[i];
}
return total;
}
float SubiectCuPunctajulCelMaiMare()
{
float maxim = this->punctajPeSubiect[0];
for (int i = 0; i < this->subiecte; i++)
{
if (maxim < this->punctajPeSubiect[i])
maxim = this->punctajPeSubiect[i];
}
return maxim;
}
void salveaza_in_fisier(ofstream& out)
{
}
void restaureaza_din_fisier(ifstream& in)
{
if (punctajPeSubiect != NULL)
delete[]punctajPeSubiect;
int dimensiuneDenumire = 0;
in.read((char*)&dimensiuneDenumire, sizeof(int));
in.read((char*)¬a, sizeof(int));
int dimData = 0;
in.read((char*)&dimData, sizeof(int));
char* aux_data = new char[dimData + 1];
in.read(aux_data, dimData + 1);
data = aux_data;
in.read((char*)&subiecte, sizeof(int));
this->punctajPeSubiect = new float[this->subiecte];
for (int i = 0; i < subiecte; i++)
{
in.read((char*)&punctajPeSubiect[i], sizeof(int));
}
}
};
int Examen::contor = 1;
class Student
{
string nume;
Examen** vectorExamene;
int nrExamene;
public:
Student()
{
this->nume = "";
this->vectorExamene = NULL;// Examen* joaca rolul lui int
=> Examen** ar fi un int*
this->nrExamene = 0;
}
Student(string nume, int nrExamene, Examen** vectorExamene)
{
this->nume = nume;
this->nrExamene = nrExamene;
this->vectorExamene = new Examen * [this->nrExamene];
for (int i = 0; i < this->nrExamene; i++)
{
this->vectorExamene[i] = vectorExamene[i];
}
}
Student(const Student& s)
{
this->nume = s.nume;
this->nrExamene = s.nrExamene;
this->vectorExamene = new Examen * [this->nrExamene];
for (int i = 0; i < this->nrExamene; i++)
{
this->vectorExamene[i] = s.vectorExamene[i];
}
}
Student& operator =(const Student& s)
{
if (this->vectorExamene != NULL)
delete[] this->vectorExamene;
this->nume = s.nume;
this->nrExamene = s.nrExamene;
this->vectorExamene = new Examen * [this->nrExamene];
for (int i = 0; i < this->nrExamene; i++)
{
this->vectorExamene[i] = s.vectorExamene[i];
}
return *this;
}
friend ostream& operator<<(ostream& out, Student& s)
{
out << s.nume << endl;
out << s.nrExamene << endl;
for (int i = 0; i < s.nrExamene; i++)
{
out << *s.vectorExamene[i] << endl;
}
return out;
}
int getnrExamene()
{
return this->nrExamene;
}
Examen** getvectorExamene()
{
return this->vectorExamene;
}
~Student()
{
if (this->vectorExamene != NULL)
delete[] this->vectorExamene;
}
Student& operator +=(Examen& e)
{
Student temp = *this;
this->vectorExamene = new Examen * [this->nrExamene + 1];
for (int i = 0; i < this->nrExamene; i++)
{
this->vectorExamene[i] = temp.vectorExamene[i];
}
this->vectorExamene[this->nrExamene] = &e;
this->nrExamene++;
return *this;
}
Student& operator -=(Examen& e)
{
Student temp = *this;
this->vectorExamene = new Examen * [this->nrExamene];
int i = 0;
int j = 0;
while ((i < this->nrExamene) && (j < this->nrExamene))
{
if (temp.vectorExamene[i]->getDenumire() !=
e.getDenumire())
{
this->vectorExamene[j] =
temp.vectorExamene[i];
j++;
}
i++;
}
this->nrExamene = j;
return *this;
}
};
void main()
{
float punctaj[] = { 1.5,3,3.5,2 };
Examen e1;
cout << e1 << endl;
Examen e2("POO", 9, "18/06/2020", 4, punctaj);
Examen e12("SDD", 9, "18/06/2020", 4, punctaj);
Examen e13("PAW", 9, "18/06/2020", 4, punctaj);
Examen e14("PEAG", 9, "18/06/2020", 4, punctaj);
cout << e2 << endl;
Examen e3 = e2;
cout << e3 << endl;
e1 = e2;
cout << e1 << endl;
string numeExamen = (string)e2;
cout << numeExamen << endl;
if (e2 > 5)
{
cout << "Studentul a promovat examenul!" << endl;
}
else
{
cout << "Studentul nu a promovat examenul!" << endl;
}
cout << e2.getDenumire() << endl;
cout << e2.getNrSubiecte() << endl;
cout << e2.totalPunctaj() << endl;
cout << e2.SubiectCuPunctajulCelMaiMare() << endl;
string fisierBinar;
cout << "Introduceti nuemele fisierului binar: ";
cin >> fisierBinar;
fisierBinar = fisierBinar + ".bin";
ofstream outBinaryProiect(fisierBinar, ios::out | ios::binary);
if (outBinaryProiect.is_open()) {
e2.salveaza_in_fisier(outBinaryProiect);
cout << "e2 a fost scris in fisierul binar.\n";
outBinaryProiect.close();
}
cout << "\n\nCitire si afisare din fisier binar: \n";
Examen e6;
cout << e6 << endl;
ifstream inBinaryProiect(fisierBinar, ios::in | ios::binary);
if (inBinaryProiect.is_open()) {
e6.restaureaza_din_fisier(inBinaryProiect);
cout << "\n\ne6 citit din fisierul binar: \n" << e6 <<
endl;
inBinaryProiect.close();
}
cout << "\n\n Student\n\n";
Examen e7;
Examen** lista = new Examen * [3];
lista[0] = &e14;
lista[1] = &e12;
lista[2] = &e13;
Student s1;
Student s2("Andrei", 3, lista);
Student s3 = s2;
cout << "Constructor de copiere: " << s3 << endl;
s1 = s2;
cout << "Op= : " << s1 << endl;
cout << s2 << endl;
s2 += e6;
cout << s2 << endl;
s2 -= e7;
cout << s2 << endl;
s2 -= e14;
cout << s2 << endl;
BAFTA!