class VectorBase
{
int len;
public:
VectorBase(int len) :len(len){}
int length()const { return len; }
virtual double getElement(int i) const = 0;
virtual double sum() const = 0;
void show()const
{
cout << "(";
for (int i = 0; i < length() - 1;i++)
{
cout << getElement(i) << ",";
}
cout << getElement(length()-1) << ")" << endl;
}
};
class Vector :public VectorBase
{
double* val;
public:
Vector(int len, double v[] = NULL) :VectorBase(len)
{
val = new double[len];
for (int i = 0; i < len;i++)
{
val[i] = (v == NULL ? 0.0 : v[i]);
}
}
~Vector(){ delete[] val; }
double getElement(int index)const { return val[index]; }
double sum()const
{
double s = 0.0;
for (int i = 0; i < length();i++)
{
s += val[i];
}
return s;
}
};
class ZeroVector :public VectorBase
{
public:
ZeroVector(int len) :VectorBase(len){}
double getElement(int index)const { return 0.0; }
double sum()const { return 0.0; }
};
int _tmain()
{
VectorBase* v;
double d[] = { 1, 2, 3, 4, 5 };
v = new Vector(5, d);
v->show();
delete v;
v = new ZeroVector(6);
v->show();
delete v;
return 0;
}