You are on page 1of 3

#include <iostream>

#include<string>
using namespace std;

//clasa template
template<class T>
class Colectie
{
//vector alocat dinamic int/float/Student/...
private:
T* v; //T v[100] alocare statica. asta nu se seteaza pe null
//int* v; //alocare dinamica
int nr;//dimensiune vector

public:
//constructor fara parametrii, cu param, de copiere, destructor,
ostream(op<<)
Colectie()
{
this->v = NULL;
this->nr = 0;
}

Colectie(T*v, int nr) { //vectorul se mapeaza cand declar in main


this->nr = nr;
this->v = new T[this->nr];
for (int i = 0; i < this->nr; i++)
this->v[i] = v[i];
}

Colectie(const Colectie&c)
{
this->nr = c.nr;
this->v = new T[this->nr];
for (int i = 0; i < this->nr; i++)
this->v[i] = c.v[i];
}

Colectie& operator=(const Colectie&c)


{
delete[]this->v;
this->nr = c.nr;
this->v = new T[this->nr];
for (int i = 0; i < this->nr; i++)
this->v[i] = c.v[i];
return *this;
}

~Colectie()
{
delete[]this->v;
}

friend ostream& operator<<(ostream& out, const Colectie&c)


{
out << "\n colectia detine " << c.nr << " elemente";
for (int i = 0; i < c.nr; i++)
out << "\n " << c.v[i];
return out;
}

int sort_ASC()
{
T aux;
for(int i=0;i<this->nr;i++)
for(int j=i+1;j<this->nr;j++)
if (v[i] > v[j])
{
aux = v[i];
v[i] = v[j];
v[j] = aux;
}
}
};

class Erou {
private:
int varsta;
int health;
public:
Erou()
{
this->varsta = 0;
this->health = 0;
}

Erou(int n, int h)
{
this->varsta = n;
this->health = h;
}

Erou(const Erou&e)
{
this->varsta = e.varsta;
this->health = e.health;
}

Erou&operator=(const Erou&e)
{
this->varsta = e.varsta;
this->health = e.health;
return*this;
}

friend ostream&operator<<(ostream&out, const Erou&e)


{
out << "nume:" << e.varsta;
out << "health: " << e.health;
return out;
}

Erou&operator+=(const Erou&e)
{
this->varsta += e.varsta;
this->health += e.health;
return*this;
}

};

void main() {
//colectii ce stocheaza vectori de tip generic
//declara o colectie de int
//Colectie<int> colectieInt;
//cout << colectieInt;

int vector[] = { 10,20,50 };


//Colectie<int> colectieInt2(vector, 3);
//cout << colectieInt2;

//colectie de float
//Colectie<float> colectieFloat;

//o colectie de Student


//Colectie<Student> colectieStudenti;
//cout << colectieStudenti;
//colectieStudenti.sort_ASC(); //sortare generica if(v[i]>v[j])

//to do
//alegere o clasa cu 2 atribute
//implementati tot ce este nevoie
//astfel incat sa avem o colectie de ...
//de 10:
//Student s;
//colectieStudenti += s;

Erou e1;
//int vector2[] = { 10,20,40 };
//Colectie<Erou>colectieErou(vector2, 3);
//cout << colectieErou;
Colectie<Erou> colectieErou;
cout << colectieErou;

Erou e2(88, 100);


Erou vector2[] = { e1,e2 };
Colectie<Erou> colectieErou2(vector2, 100);

cout << colectieErou2;

colectieErou += e1;
}

You might also like