如题,请各位帮忙,简单简洁!
#include <iostream>
using namespace std;
class a{
public:
a(){
size=0;
value=0;
}
a(int s,int v){
size=s;
value=v;
}
int operator [](int index){
return value;
}
int operator =(int i1){
//i=i1;
//return i;
value=i1;
return value;
}
void display(){
}
private:
int size;
int value;
};
void main(){
a a1(10,3),a2(12,4),a3;
a3=11;
cout<<a3[3];
}
这是自己写的不完整的代码
private: int* value; a(int s,int v){ size=s; value= new int[s](v); } ~a(){ delete[] value; value = 0; }
我写的类,,应该符合你的需要
/////set.h
#ifndef IOSTREAM #define IOSTREAM
#include<iostream>
#endif
#pragma once typedef int INT;
class MySet { public: MySet():containable(0),used(0),data(NULL) {} MySet(MySet & r) { this->containable=r.used; this->used=r.used;
int i=0; this->data=new INT [used]; for (i=0;i<r.used;i++) this->data[i]=r.data[i]; } MySet(INT *Array) { int i; for (i=0;*(Array+i)!='\0';i++) { put_back(*(Array+i)); } } MySet(INT *Array ,int start,int end) { int i; for (i=0;*(Array+i)!='\0';i++) { put_back(*(Array+i)); } if (i>end) std::cout<<"输入的数据有误"<<std::endl; else { for (int k=0;k<=i;k++) { put_back(*(Array+k)); } } } virtual ~MySet() { if (data!=NULL) delete data; }
MySet& operator+(const MySet & r); //并 MySet& operator-(const MySet & r); //差
MySet& operator=(const MySet &r); bool put_back(const INT &d); bool put_array(const INT *d,int length); bool remove(const INT& d); bool remove(const int start,const int end); bool removeall();
bool insert(const INT & d,int index); bool insert(const INT *d,int length,int index);
friend std::ostream& operator<<(std::ostream &out,MySet & r) { for (int i=0;i<r.used;i++) std::cout<<*(r.data+i)<<" ";
return out; } INT GetData(int index);
private: int containable; int used; INT *data;
};
////set.cpp
#include"Set.h" MySet temp;
MySet& MySet::operator+(const MySet & r) //并 { for (int i=0;i<used;i++) temp.put_back(data[i]); for (int i=0;i<r.used;i++) temp.put_back(r.data[i]);
return temp; } MySet& MySet::operator-(const MySet & r) //差 { for (int i=0;i<r.used;i++) temp.remove(r.data[i]);
return temp;
} MySet& MySet::operator=(const MySet& r) { bool b=this->removeall(); if (b==true) { this->containable=r.used; this->used=r.used;
int i=0; this->data=new INT [used]; for (i=0;i<r.used;i++) this->data[i]=r.data[i]; } return *this; } bool MySet::put_back(const INT &d) { if (used==containable) { INT *p=data; data=new INT [2*(used+1)]; for (int i=0;i<used;i++) { *(data+i)=*(p+i); } containable=2*(containable+1);
delete p; } *(data+used)=d; used++;
return true; } bool MySet::put_array(const INT *d,int length) { bool f=false; for (int i=0;i<length;i++) { f=put_back(*(d+i)); if (f==false) std::cout<<"put_array error\n"; } return true; } bool MySet::remove(const INT& d) { bool f=false;
int k=-1; for (int i=0;i<used;i++) if (d==data[i]) k=i; if (k!=-1) { while (k+1<used) { data[k]=data[k+1]; k++; } f=true; used--; } return f; } bool MySet::remove(const int start,const int end) { bool f=false; if (end>used) return false; else { int len=used-(end-start); INT *p=data; data=new INT [len];
for (int i=1;i<start;i++) *(data+i-1)=*(p+i-1); for (int i=end;i<=used;i++) *(data+i-1)=*(p+i-1);
delete p;
used=len; containable=len; f=true;
} return f; } bool MySet::removeall() { if (data!=NULL) delete data; data=NULL; used=containable=0; return true; } INT MySet::GetData(int index) { if (index<0||index>used) std::cout<<"index is not suit"<<std::endl; if (used!=0) { used--; return *(data+index-1); } else { std::cout<<"set is empty"; return INT(0); } } bool MySet::insert(const INT & d,int index) { bool f=false; if (index<1||index>used) { std::cout<<"insert error\n"; } if (used==containable) { INT *p=data; data=new INT [used+1]; containable=used+1; int i=0; for ( i=0;i<index-1;i++) { *(data+i)=*(p+i); } for (;i<used;i++) { *(data+i+1)=*(p+i); } data[index-1]=d; used+=1; delete p; f=true; } else { int i;
for (i=used-1;i>=index-1;i--) { data[i+1]=data[i]; } data[index-1]=d; used++; f=true; } return f; } bool MySet::insert(const INT *d,int length,int index) { bool f=false; for (int i=0;i<length;i++) { f=insert(*(d+i),index+i); if (f==false) return f; }
return true; }