您的位置:首页 > 编程语言 > C语言/C++

面向对象->实验报告三(C++)

2021-11-07 02:01 609 查看

欢迎访问我的个人博客: xzajyjs.cn

task4

实验要求

main.cpp

#include <iostream>
#include "vector_int.hpp"
int main(){
using namespace std;
int x_len, y_len;
cout << "请输入x的长度:";
cin >> x_len;
Vector_int x(x_len); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项空间
x.show('x');
cout << "请输入y的长度:";
cin >> y_len;
Vector_int y(y_len, 6); // 创建一个动态大小的int型数组对象x,向系统申请n个int型数据项空间, 初始值均为6
y.show('y');
Vector_int z(y); // 用已经存在的对象y构造新的对象z(深拷贝的实现)
z.show('z');
z.at(0) = 999; // 通过at()方法访问对象y中索引为0的数据项

cout << "---以下是深拷贝的检验---\n";
y.show('y');
z.show('z');
return 0;
}

vector_int.hpp

#ifndef CPP_VECTOR_INT_HPP
#define CPP_VECTOR_INT_HPP
using namespace std;
class Vector_int{
private:
int size;
int *p;
public:
//Vector_int(int n);
Vector_int(int n, int value = 0);	// 在这里将默认值改为0,因此上一行省去
~Vector_int();
Vector_int(const Vector_int &_temp);
int &at(int wh);
void show(char which);
};
//Vector_int::Vector_int(int n):size(n) {
//    p = new int[size];
//    cout << "created..." << endl;
//}
Vector_int::Vector_int(int n, int value):size(n) {
p = new int[size];
for(int i = 0;i < size;++i)
p[i] = value;
cout << "created..." << endl;
}
Vector_int::~Vector_int() {
delete[] p;
cout << "deleted..." << endl;
}
// 深拷贝的实现
Vector_int::Vector_int(const Vector_int &_temp):size(_temp.size) {
p = new int[size];
for(int i = 0;i < _temp.size;++i)
p[i] = _temp.p[i];
}
int &Vector_int::at(int wh) {
assert(wh >= 0 && wh < size);
return p[wh];
}
void Vector_int::show(char which) {
cout << which << ":\t";
for(int i = 0; i < size; ++i)
cout << p[i] << "\t";
cout << endl;
}

#endif //CPP_VECTOR_INT_HPP

运行结果截图

小结

  • 相较于task2,每个数组成员是整形而不是自定义的Point类型。在各方面都要简单一些
  • 要注意深拷贝和默认的浅拷贝的含义的区别。在主函数中对深拷贝的效果进行了检验

task5

实验要求

main.cpp(原)

#include <iostream>
#include "matrix.hpp"
int main()
{
using namespace std;
double x[] = {1, 2, 3, 4, 5, 6};
Matrix m1(3, 2); // 创建一个3×2的矩阵
m1.set(x); // 用一维数组x的值按行为矩阵m1赋值
m1.print(); // 打印矩阵m1的值
cout << "the first line is: " << endl;
cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; // 输出矩阵m1第1行两个元素的值
cout << endl;
Matrix m2(2, 3);
m2.set(x);
m2.print();
cout << "the first line is: " << endl;
cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
cout << endl;
Matrix m3(m2); // 用矩阵m2构造新的矩阵m3
m3.set(0, 0, 999); // 将矩阵m3第0行第0列元素值设为999
m3.print();
}

main.cpp(改)

#include <iostream>
#include "Matrix.hpp"
int main()
{
using namespace std;
double x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Matrix m1(4, 3);
m1.set(x);
m1.print();
cout << "m1 has " << m1.get_lines() << " lines and " << m1.get_cols() << " cols.\n\n";
cout << "the third line is: " << endl;
cout << m1.at(2, 0) << " " << m1.at(2, 1) << " " << m1.at(2, 2) << endl;
cout << endl;

Matrix m2(3, 4);
m2.set(x);
m2.print();
cout << "the first line is: " << endl;
cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << " " << m2.at(0, 3) << endl;
cout << endl;

Matrix m3(m2);
m3.set(2, 3, 233);
m3.print();
cout << endl;

Matrix m4(3);
double temp[] = {3, 4, 5, 6, 7, 8, 9, 1, 2};
m4.set(temp);
m4.print();
}

Matrix.hpp

#ifndef CPP_MATRIX_HPP
#define CPP_MATRIX_HPP
using namespace std;
class Matrix
{
public:
Matrix(int n); // 构造函数,构造一个n*n的矩阵
Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵
Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造
~Matrix(); //析构函数
void set(const double *pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值
void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value
double &at(int i, int j); //返回矩阵第i行第j列元素的引用
double at(int i, int j) const; // 返回矩阵第i行第j列元素的值
int get_lines() const; //返回矩阵行数
int get_cols() const; //返回矩列数
void print() const; // 按行打印输出矩阵
private:
int lines; // 矩阵行数
int cols; // 矩阵列数
double *p; // 指向存放矩阵数据的内存块的首地址
};

Matrix::Matrix(int n):lines(n), cols(n) {
p = new double[lines*cols];
}

Matrix::Matrix(int n, int m):lines(n), cols(m) {
p = new double[lines*cols];
}

Matrix::Matrix(const Matrix &X):lines(X.lines), cols(X.cols) {  // 深拷贝的实现
p = new double[lines*cols];
for(int i = 0;i < lines*cols;++i)
p[i] = X.p[i];
}

Matrix::~Matrix() {
delete[] p;
}

void Matrix::set(const double *pvalue) {
for(int i = 0;i < lines*cols;++i)
p[i] = pvalue[i];
}

void Matrix::set(int i, int j, int value) {
p[i*cols+j] = value;
}

double &Matrix::at(int i, int j) {
return p[i*cols+j];
}

double Matrix::at(int i, int j) const {
return p[i*cols+j];
}

int Matrix::get_lines() const {
return lines;
}

int Matrix::get_cols() const {
return cols;
}

void Matrix::print() const {
int k = 0;
for(int i = 0;i < lines;++i){
for(int j = 0;j < cols;++j, ++k)
cout << p[k] << ", ";
cout << "\b\b";
cout << endl;
}
}

#endif //CPP_MATRIX_HPP

运行结果截图

小结

  • 考虑到函数的复杂度,以一维数组的方式来保存数据。
  • 练习了深拷贝
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: