C++程序运行
#include<iostream>using namespace std;
class Vector {
private:
int dimension; double* data;
public:
Vector();
Vector(int a[]);
Vector(const Vector &v);
~Vector();
void Display();
int GetDimension();
double SetAt(double newdata,int index);
double GetAt(int index);
Vector Add(const Vector &ob2 );
Vector Sub(const Vector &ob2 );
int Mult(const Vector &ob2 );
};
Vector::Vector() //无参构造函数,实现把向量所有元素初始化为0
{ dimension=4;
for(int i=0;i<4;i++) data[i]=0;
} Vector::Vector(int a[]) //有参构造函数,实现用一个n维数组初始化n维向量
{
dimension=sizeof(a);
int i;
for(i=0;i<dimension;i++) data[i]=a[i];
}
Vector::Vector(const Vector &v) //拷贝构造函数
{
dimension=v.dimension;
for(int i=0;i<v.dimension;i++) data[i]=v.data[i];
}
Vector::~Vector() //析构函数
{}
void Vector::Display() //输出数据成员函数
{
cout<<'('; for(int i=0;i<dimension;i++) cout<<data[i]<<','; cout<<')';
}
int Vector::GetDimension() //获取向量对象的维数
{
return sizeof(data);
}
double Vector::SetAt(double newdata,int index) //实现给向量的第index+1个元素赋值
{
data[index]=newdata; return 0;
}
double Vector::GetAt(int index) //实现读取向量的第index+1个元素的值
{
return data[index];
}
Vector Vector::Add(const Vector &ob2) //实现向量加、减、乘运算的成员函数
{
Vector temp;
temp.dimension=dimension;
for(int i;i<dimension;i++) temp.data[i]=data[i]+ob2.data[i];
return temp;
}
Vector Vector::Sub(const Vector &ob2)
{
Vector temp;
temp.dimension=dimension;
for(int i;
i<dimension;i++) temp.data[i]=data[i]-ob2.data[i];
return temp;
}
int Vector::Mult(const Vector &ob2)
{
int i;
double sum=0;
for(i=0;i<dimension;i++) sum+=data[i]*ob2.data[i];
return int(sum);
}
int main()
{
Vector v1;
double data=0;
int a[4]={1,2,3,4};
Vector v2(a);
Vector v3(v2);
Vector v4,v5;
cout<<"lv1:"<<v1.GetDimension()<<endl;
v1.Display();
cout<<"lv2:"<<v2.GetDimension()<<endl;
v2.Display();
cout<<"lv3:"<<v3.GetDimension()<<endl;
v3.Display();
v1.SetAt(3,2);
v1.Display();
cout<<v1.GetAt(2)<<endl;
v1.Display();
v4=v1.Add(v2);
cout<<"v1+v2="<<endl;
v4.Display();
v5=v1.Sub(v2);
cout<<"v1-v2="<<endl;
v5.Display();
int sum;
sum=v1.Mult(v2);
cout<<"v1*v2="<<sum;
return 0;
}
谁帮我运行一下这个程序