#include<iostream>
using namespace std;
int i=0;
class CVector
{
private:
float* p_val;
int size;
public:
CVector(float *,int s);
CVector(const CVector &v);
~CVector();
CVector& operator = (const CVector &v); //赋值运算符重载
float& operator[] (int ix); //下标运算符重载
void display();
};
CVector::CVector(float *p,int s)
{
size=s;
p_val=new float[size];
for(i;i<size;i++)
p_val[i]=p[i];
}
CVector::CVector(const CVector &v)
{
size=v.size;
p_val=new float[v.size];
for(i=0;i<size;i++)
p_val[i]=v.p_val[i];
}
CVector:: ~CVector()
{
delete []p_val;
}
CVector & CVector::operator =(const CVector &v)
{
delete []p_val;
size=v.size;
p_val=new float[v.size];
for(i=0;i<size;i++)
p_val[i]=v.p_val[i];
return *this;
}
float & CVector:: operator [] (int ix)
{
if(ix>=0 && ix<size)
return p_val[ix-1];
else
exit(1);
}
void CVector::display()
{
for(i=0;i<size;i++)
cout<<p_val[i]<<" ";
cout<<endl;
}
int main()
{
float a[5]={1,2,3,4,5},b[5]={6,7,8,9,10};
CVector c1(a,5),c2(c1),c3(b,5);
cout<<"对象c1: ";
c1.display();
cout<<"对象c2:";
c2.display();
cout<<"c2=c3后,对象c2: ";
c2=c3;
c2.display(); // 这里为什么输出的是乱码?如下图
cout<<"c1[3]= ";
cout<<c1[3]<<endl;
return 0;
}