C++运算符重载求两个集合的并集,使用new和delete运行出错
//以下是程序代码//请在这段代码的基础上修改,如果您有好的代码,请务必分享
#include <iostream>
using namespace std;
const int SIZE=100;
class Set
{
private:
int *element;
int count;
public:
Set();
void Addmember(int *p,int n);
bool Ismember(int n);
Set operator+(Set &s);
void Show();
int Getcount();
~Set();
};
Set::Set()
{
element=new int [SIZE];
count=0;
}
Set::~Set()
{
delete[] element;
}
void Set::Addmember(int *p,int n)
{
element=p;
count=n;
}
bool Set::Ismember(int n)
{
for(int i=0;i<count;i++)
if(element[i]==n)
return true;
return false;
}
Set Set::operator+ (Set &s)
{
Set A;
for(int i=0;i<this->count;i++)
A.element[i]=this->element[i];
for(int j=0;j<s.count;j++)
if(!this->Ismember(s.element[j]))
A.element[i++]=s.element[j];
A.count=i;
// A.Show();
return A;
}
void Set::Show()
{
cout<<"{ ";
for(int i=0;i<count-1;i++)
cout<<element[i]<<" , ";
cout<<element[i]<<" }"<<endl;
}
int Set::Getcount()
{
return count;
}
int main ()
{
int a[6]={1,2,3,4,5,6};
int b[5]={2,5,1,7,0};
Set set1,set2,set3;
set1.Addmember(a,6);
set2.Addmember(b,5);
cout<<"set1=";
set1.Show();
cout<<"set2=";
set2.Show();
set3=set1+set2;
cout<<"set3=";
set3.Show();
return 0;
}