(15C++ for homework)Vector-ND
标签(空格分隔): 程序设计实验 c++
本人学院
description
Please create a class Vector.(N-dimension, N >= 2)
The declaration of Vector is in ‘Vector.h’
before programing, you should revise the knowledge about constructer and destructer.
note:
// constructer, print out "construct a vector called vector_name.\n"
Vector(string, int, int[]);
// copy constructer, print out "copy a vector called vector_name.\n"
Vector(const Vector &otherVec);
// destructer, print out "release memory from a vector called vector_name.\n"
~Vector();
// check two vector is whether the same or not
// if same name but the value is not, print like "same name, different value.\n"
// other conditions are:
//"same name, same value.\n" "different name, same value.\n" "different name, different value".
isEqual();
// set the name of the vector
void setName(string);
// display the vecter in the format "vecter_name(1, 2..N)\n"
void print();
读题
Vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include <string>
using namespace std;
class Vector {
public:
Vector() {
name = "";
dimension = 0;
param = NULL;
}
Vector(string, int, int[]);
Vector(const Vector &otherVec);
~Vector();
void isEqual(const Vector &);
void setName(string);
void print();
private:
string name;
int dimension, *param;
};
#endif
my answer
Vector.cpp
#include<iostream>
#include<string>
#include"Vector.h"
using namespace std;
Vector::Vector(string n, int d, int p[]) {
name = n;
dimension = d;
param = p;
cout << "construct a vector called " << name << "." <<endl;
}
Vector::Vector(const Vector &otherVec) {
name = otherVec.name;
dimension = otherVec.dimension;
param = otherVec.param;
cout << "copy a vector called " << name << "." << endl;
}
Vector::~Vector() {
cout << "release memory from a vector called "<< name << "." <<endl;
}
void Vector::isEqual(const Vector &otherVec) {
bool isEqualName = true, isEqualValue = true;
if (name != otherVec.name) isEqualName = false;
if (dimension != otherVec.dimension) {
isEqualValue = false;
} else {
for (int i = 0; i < dimension; i++) {
if (param[i] != otherVec.param[i]) {
isEqualValue = false;
break;
}
}
}
if (isEqualName) {
cout << "same name,";
} else {
cout << "different name,";
}
if (isEqualValue) {
cout << " same value." << endl;
} else {
cout << " different value." << endl;
}
}
void Vector::setName(string n) {
name = n;
}
void Vector::print() {
cout << name <<"(";
for (int i = 0; i < dimension - 1; i++) {
cout << param[i] << ", ";
}
cout << param[dimension - 1] << ")" <<endl;
}
the standard answer
Vector.cpp
#include "Vector.h"
#include <iostream>
#include <string>
using namespace std;
Vector::Vector(string n, int a, int b[]) {
name = n;
dimension = a;
param = new int[a+1];
for (int i = 0; i < a; i++)
param[i] = b[i];
cout << "construct a vector called " << name << ".\n";
}
Vector::Vector(const Vector &otherVec) {
name = otherVec.name;
dimension = otherVec.dimension;
param = new int[dimension+1];
for (int i = 0; i < dimension; i++)
param[i] = otherVec.param[i];
cout << "copy a vector called " << name << ".\n";
}
Vector::~Vector() {
delete [] param;
cout << "release memory from a vector called " << name << ".\n";
}
void Vector::isEqual(const Vector &otherVec) {
if (name == otherVec.name) {
cout << "same name, ";
} else {
cout << "different name, ";
}
if (dimension != otherVec.dimension) {
cout << "different value.\n";
return;
}
for (int i = 0; i < dimension; i++) {
if (param[i] != otherVec.param[i]) {
cout << "different value.\n";
return;
}
}
cout << "same value.\n";
}
void Vector::setName(string newName) {
name = newName;
}
void Vector::print() {
cout << name << "(";
for (int i = 0; i < dimension-1; i++)
cout << param[i] << ", ";
cout << param[dimension-1] << ")\n";
}
反馈
我直接将传进来的数组地址赋值给指针param
而答案则是用new新开辟内存,复制存放传进来的数据数组
在析构函数中delete
在copy函数中,我也是直接param = otherVec.param
这样做..在修改当下对象时会修改到当初的otherVec.
所以还是应该用遍历复制的方法.