Untitled

 avatar
unknown
c_cpp
4 years ago
1.6 kB
7
Indexable
#include <iostream>
#define CAPACITY 50

using namespace std;

class StaticContainer {
public:
	StaticContainer() {
		usedValues = 0;
	}
	int getUsedValues() {
		return usedValues;
	}
	int count(int value); //Funzione conteggio
	int remove(int value); //Funzione cancella
	bool removeOne(int value); //Funzione cancella uno
	void add(int value); //Funzione inserisci
	int checkElement(int value); //Necessaria per eseguire funzione +=
	void operator += (StaticContainer istance);
	int getElements(int i); //Fondamentale per eseguire la inline operator +
private:
	int elements[CAPACITY];
	int usedValues;
};

//Sovraccarico gli operatori
inline StaticContainer operator + (StaticContainer firstIstance, StaticContainer secondIstance) {
	StaticContainer istance = StaticContainer();
	int i = 0;
	int j = 0;
	while (i < firstIstance.getUsedValues() || j < secondIstance.getUsedValues()) { //Fino all' ultimo elemento utilizzato per ogni contenitore
		/*E' importantissimo rompere il ciclo quando arrivo alla fine per evitare che in un contenitore ho 11 elementi e nell' altro 10 e si provi a 
		sommare l' 11 elemento del contenitore con un elemento non inizalizzato del secondo*/
		istance.add(firstIstance.getElements(i) + secondIstance.getElements(j));
		i++;
		j++;
		if (i == firstIstance.getUsedValues() || j == secondIstance.getUsedValues())
			break;
	}
	return istance;
}
inline ostream& operator << (ostream& out, StaticContainer& istance) {
	for (int i = 0; i < istance.getUsedValues(); i++)
		out << istance.getElements(i) << " ";
	return out;
}

Editor is loading...