10.20

 

C++完成数学中向量的封装,其中,用指针和动态申请支持向量长度的改变,使用浅克隆和深克隆复制向量类,比较这两种克隆方式的异同。

实验要求:

1.画出对应的类图;

2.提交源代码(用C++完成);

3.注意编程规范。

 

类图:

 

 

 

源代码:

 

1. 浅克隆:

#include <iostream>

using namespace std;usingnamespacestd;

//向量类

class Vector{classVector{

private:private:

    int *p;int*p;

    int len;intlen;

public:public:

    Vector(int len);Vector(intlen);

    Vector(const Vector &vector);  //拷贝构造函数Vector(constVector &vector); //拷贝构造函数

    ~Vector();

    int operator[](int i) const { return p[i]; }intoperator[](inti)const{returnp[i]; }

    int &operator[](int i){ return p[i]; }int&operator[](inti){returnp[i]; } }

    int length() const { return len; }intlength()const{returnlen; }

};

Vector::Vector(int len): len(len){Vector::Vector(intlen): len(len){

    p = (int*)calloc( len, sizeof(int) );p = (int*)calloc( len,sizeof(int) );

}

//拷贝构造函数

Vector::Vector(const Vector &vector){Vector::Vector(constVector &vector){

    this->len = vector.len;this->len = vector.len;

    this->p = vector.p;this->p = vector.p;

}

Vector::~Vector(){ free(p); }Vector::~Vector(){free(p); }

//打印

void display(const Vector &vector){voiddisplay(constVector &vector){

    int len = vector.length();intlen = vector.length();

    for(int i=0; i<len; i++){for(inti=0; i<len; i++){

        if(i == len-1){if(i == len-1){

            cout<<vector[i]<<endl;

        }else{}else{

            cout<<vector[i]<<", ";cout<<vector[i]<<", ";

        }

    }

}

int main(){intmain(){

    Vector v1(10);

    for(int i=0; i<10; i++){for(inti=0; i<10; i++){

        v1[i] = i;

    }

    Vector v2 = v1;

    v2[6] = 55;

    v2[2] = 20;

    cout<<"浅克隆:"<<endl;cout<<"浅克隆:"<<endl;

    cout<<"更改copyVector后vector的数据:"<<endl;cout<<"更改copyVector后vector的数据:"<<endl;

    display(v1);

    cout<<"更改copyVector后copyVector的数据:"<<endl;cout<<"更改copyVector后copyVector的数据:"<<endl;

    display(v2);

    return 0;return0;

}

 

 

 

 

 

 

2. 深克隆:

 

#include <iostream>

using namespace std;usingnamespacestd;

//向量类

class Vector{classVector{

private:private:

    int *p;int*p;

    int len;intlen;

public:public:

    Vector(int len);Vector(intlen);

    Vector(const Vector &vector);  //拷贝构造函数Vector(constVector &vector); //拷贝构造函数

    ~Vector();

    int operator[](int i) const { return p[i]; }intoperator[](inti)const{returnp[i]; }

    int &operator[](int i){ return p[i]; }int&operator[](inti){returnp[i]; } }

    int length() const { return len; }intlength()const{returnlen; }

};

Vector::Vector(int len): len(len){Vector::Vector(intlen): len(len){

    p = (int*)calloc( len, sizeof(int) );p = (int*)calloc( len,sizeof(int) );

}

//拷贝构造函数

Vector::Vector(const Vector &vector){Vector::Vector(constVector &vector){

    this->len = vector.len;this->len = vector.len;

    this->p =  (int*)calloc( this->len, sizeof(int) );this->p =  (int*)calloc(this->len,sizeof(int) );

    memcpy( this->p, vector.p, len * sizeof(int) );memcpy(this->p, vector.p, len *sizeof(int) );

}

Vector::~Vector(){ free(p); }Vector::~Vector(){free(p); }

//打印

void display(const Vector &vector){voiddisplay(constVector &vector){

    int len = vector.length();intlen = vector.length();

    for(int i=0; i<len; i++){for(inti=0; i<len; i++){

        if(i == len-1){if(i == len-1){

            cout<<vector[i]<<endl;

        }else{}else{

            cout<<vector[i]<<", ";cout<<vector[i]<<", ";

        }

    }

}

int main(){intmain(){

    Vector v1(10);

    for(int i=0; i<10; i++){for(inti=0; i<10; i++){

        v1[i] = i;

    }

    Vector v2 = v1;

    v2[6] = 55;

    v2[2] = 20;

    cout<<"深克隆:"<<endl;cout<<"深克隆:"<<endl;

    cout<<"更改copyVector后vector的数据:"<<endl;cout<<"更改copyVector后vector的数据:"<<endl;

    display(v1);

    cout<<"更改copyVector后copyVector的数据:"<<endl;cout<<"更改copyVector后copyVector的数据:"<<endl;

    display(v2);

    return 0;return0;

}

 

运行截图:

浅克隆:

 

深克隆:

 

 

 

 

posted on 2024-10-20 20:00  HA_wind  阅读(17)  评论(0编辑  收藏  举报

导航