Grazie a chi darà un'occhiata.
- Codice:
#include <iostream>
#include <string.h>
using namespace std;
class oggetto{
friend ostream& operator<<(ostream& os, const oggetto& o){return o.stampa(os);}
private:
int n;
char* testo;
public:
oggetto(){n=0; testo=new char[1]; strcpy(testo, "");}
oggetto(const int x, const char* c){n=x; testo=new char[strlen(c)+1]; strcpy(testo, c);}
bool operator==(const oggetto& o) const{return(o.n==n && !strcmp(o.testo, testo));}
const oggetto& operator=(const oggetto& o){
if(this!=&o){
n=o.n;
if(testo) delete [] testo;
testo=new char[strlen(o.testo)+1];
strcpy(testo, o.testo);
}
return *this;
}
~oggetto(){delete [] testo; n=0;}
ostream& stampa(ostream& os) const{os << n << " " << testo; return os;}
};
typedef oggetto T;
class vettore{
friend ostream& operator<<(ostream& os, const vettore& o){return o.stampa(os);}
private:
static const int N=4;
T vett[N];
int nelem;
public:
vettore(){nelem=0;}
bool ricerca(const T& e) const{
bool trovato=false;
for(int i=0; i<nelem; i++){
if(e==vett[i])
trovato=true;
break;
}
return trovato;
}
bool full() const{return nelem==N;}
const vettore& operator=(const vettore& v){
if(this!=&v){
for(int i=0; i<v.nelem; i++)
vett[i]=v.vett[i];
}
return *this;
}
bool insert(const T& e){
if(full()) return false;
vett[nelem]=e;
nelem++;
return true;
}
const vettore operator&(const vettore& v) const{
vettore ris;
for(int i=0; i<v.nelem; i++){
if(ricerca(v.vett[i]))
ris.insert(v.vett[i]);
}
return ris;
}
ostream& stampa(ostream& os) const{
for(int i=0; i<nelem; i++)
os << vett[i] << " ";
return os;
}
};
int main(){
oggetto o1(4, "ciao");
oggetto o2(5, "hola");
oggetto o3(6, "salut");
oggetto o4(7, "halo");
vettore v1,v2, v3;
v1.insert(o1);
v1.insert(o2);
v2.insert(o3);
v2.insert(o1);
v3=v1&v2; //l'intersezione dovrebbe contenere o1
cout << v3;