1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using namespace std;
class Vector{
    public:
        Vector();
        Vector(const Vector &origin);
        ~Vector();
        Vector &operator=(const Vector &origin);
        double &operator[](int i);
        Vector operator+(Vector &second);
        Vector operator*(double multiple);
    private:
        int dim;        //dimension of Vector
        double *ele;    //head of element array

};


double &Vector::operator[](int i){
    if(i>=0 && i<dim)
        return *(ele+i);
    cout<<"out of range";
    *(ele+dim)=0.0;
    return *(ele+dim);//return 0
}


Vector Vector::operator+(Vector &second){
    if(dim!=second.getDimension()){
        Vector temp;
        cout<<"the dimensions are not the same."<<endl;
        return temp;
    }
    else{
        Vector temp(dim);
        for(int i=0;i<dim;++i){
            temp[i]=*(ele+i)+second[i];
        }
        return temp;

    }
}


Vector Vector::operator*(double multiple){
    Vector temp(dim);
    for(int i=0;i<dim;++i){
        temp[i]=*(ele+i)*multiple;
    }
    return temp;
}