11.8
今天完成了武老师的实验6
@startuml
class Vector {
- size: int
- data: double*
- name: string
- Vector(size: int, name: string)
- Vector(const Vector& other)
- ~Vector()
- setValue(index: int, value: double): void
- getValue(index: int): double
- getSize(): int
- getName(): string
- setName(name: string): void
- display(): void
- clone(): Vector*
- deepClone(): Vector*
- operator=(const Vector& other): Vector&
}
class VectorPrototype {
- clone(): Vector*
- deepClone(): Vector*
}
Vector --|> VectorPrototype
@enduml
2.源代码
include
include
include
using namespace std;
// 向量原型基类
class VectorPrototype {
public:
virtual VectorPrototype* clone() = 0;
virtual VectorPrototype* deepClone() = 0;
virtual ~VectorPrototype() {}
};
// 向量类
class Vector : public VectorPrototype {
private:
int size; // 向量长度
double* data; // 向量数据指针
string name; // 向量名称
public:
// 构造函数
Vector(int size = 0, const string& name = "Unnamed") : size(size), name(name) {
cout << "构造函数被调用 - " << name << endl;
if (size > 0) {
data = new double[size];
// 初始化为0
for (int i = 0; i < size; i++) {
data[i] = 0.0;
}
} else {
data = nullptr;
}
}
// 拷贝构造函数(深拷贝)
Vector(const Vector& other) : size(other.size), name(other.name + "_copy") {
cout << "拷贝构造函数被调用 - " << name << endl;
if (size > 0) {
data = new double[size];
memcpy(data, other.data, size * sizeof(double));
} else {
data = nullptr;
}
}
// 析构函数
~Vector() {
cout << "析构函数被调用 - " << name << endl;
delete[] data;
}
// 赋值运算符重载(深拷贝)
Vector& operator=(const Vector& other) {
cout << "赋值运算符被调用 - " << name << " = " << other.name << endl;
if (this != &other) {
delete[] data;
size = other.size;
name = other.name + "_assigned";
if (size > 0) {
data = new double[size];
memcpy(data, other.data, size * sizeof(double));
} else {
data = nullptr;
}
}
return *this;
}
// 设置向量值
void setValue(int index, double value) {
if (index >= 0 && index < size) {
data[index] = value;
}
}
// 获取向量值
double getValue(int index) const {
if (index >= 0 && index < size) {
return data[index];
}
return 0.0;
}
// 获取向量大小
int getSize() const {
return size;
}
// 获取向量名称
string getName() const {
return name;
}
// 设置向量名称
void setName(const string& newName) {
name = newName;
}
// 显示向量内容
void display() const {
cout << name << " [" << size << "]: ";
cout << "[";
for (int i = 0; i < size; i++) {
cout << data[i];
if (i < size - 1) cout << ", ";
}
cout << "]" << endl;
}
// 浅克隆 - 只复制指针,不复制数据
Vector* clone() override {
cout << "执行浅克隆 - " << name << endl;
Vector* newVector = new Vector();
newVector->size = this->size;
newVector->data = this->data; // 共享数据指针
newVector->name = this->name + "_shallow";
return newVector;
}
// 深克隆 - 复制指针和数据
Vector* deepClone() override {
cout << "执行深克隆 - " << name << endl;
Vector* newVector = new Vector();
newVector->size = this->size;
newVector->name = this->name + "_deep";
if (this->size > 0) {
newVector->data = new double[this->size];
memcpy(newVector->data, this->data, this->size * sizeof(double));
} else {
newVector->data = nullptr;
}
return newVector;
}
// 修改数据指针指向的内容(用于测试浅克隆的问

浙公网安备 33010602011771号