动态int型数组类Vector_int的定义实现源码(vector_int.hpp)

#include <iostream>
#include <cassert> using namespace std; class Vector_int
{
public:
Vector_int(int n, int value = 0);
Vector_int(const Vector_int& x); // 复制构造函数
~Vector_int();
int& at(int index); // 返回下标为index的元素引用
private:
int size; // 动态数组的大小
int* p;
}; Vector_int::Vector_int(int n, int value): size(n)
{
cout << "dynamic create vector..." << endl;
p = new int[size];
for (int i = 0; i < size; i++)
{
p[i] = value;
}
} Vector_int::Vector_int(const Vector_int& x) : size(x.size)
{
p = new int[size];
for (auto i = 0; i < size; ++i) // 通过for循环实现对p指向的内存空间的数据复制
p[i] = x.p[i];
} Vector_int::~Vector_int()
{
cout << "deleting..." << endl;
delete[] p;
}
int& Vector_int::at(int index)
{
assert(index >= 0 && index < size);
return p[index];
}

测试类Vector_int的代码(文件task4.cpp)

#include <iostream>
#include "Vector_int.hpp"
int main()
{
using namespace std;
int n;
cout << "input size: ";
cin >> n;
Vector_int t(n);
cout << t.at(0) << endl;
Vector_int x(n, 6);
cout << x.at(0) << endl;
Vector_int y(x);
cout << y.at(0) << endl;
x.at(0) = 999;
cout << x.at(0) << endl;
cout << y.at(0) << endl;
}

运行测试结果截图

类Matrix的定义和实现完整代码(Matrix.hpp)

#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <cassert> 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::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 (auto i = 0; i < lines * cols; i++)
p[i] = X.p[i];
} Matrix::~Matrix()
{
delete[] p;
} void Matrix::set(const double* pvalue)
{
int i = 0;
while (pvalue + i && i < lines * cols)
{
p[i] = pvalue[i];
i++;
}
}
void Matrix::set(int i, int j, int value)
{
assert(i < lines&& j < cols);
p[i * cols + j] = value;
}
double& Matrix::at(int i, int j)
{
double& m = p[i * cols + j];
return m;
}
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
{ for (auto i = 0; i < lines * cols; i++)
{
cout << p[i] << ", ";
if ((i + 1) % cols == 0)
cout << "\b\b " << endl;
} }
#endif

测试代码(task5.cpp)

#include <iostream>
#include "Matrix.hpp"
int main()
{
using namespace std;
double x[] = { 6, 4, 1, 3, 5, 7, 7, 1, 3, 5, 4, 2 };
Matrix m1(6, 2); // 创建一个6×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(4, 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();
}

运行测试截图

最新文章

  1. 开放api接口签名验证
  2. CodeForces 540
  3. [POJ2773]:Happy 2006
  4. 仿souhu页面设计
  5. Linux 各文件夹介绍
  6. CAD字体显示问号的解决办法
  7. 干货:结合Scikit-learn介绍几种常用的特征选择方法
  8. 浅谈mysql mvcc
  9. Oracle基础 exp/imp命令
  10. Java多线程--同步函数
  11. 关于Redis的常识(推荐)
  12. Invert Binary Tree——LeetCode
  13. eclipse重构详解(转)
  14. Spring-java代理技术总结
  15. 详解Mysql自动备份与恢复
  16. bzoj 4565 状压区间dp
  17. kernel笔记——库文件与系统调用
  18. CF209C Trails and Glades
  19. 理解JS深拷贝
  20. DataTabe使用Linq实现 Group

热门文章

  1. CVE-2022-32532 Apache Shiro 身份认证绕过
  2. [LeetCode]819. 最常见的单词
  3. Word 交叉引用 给参考文献、图片题注添加引用
  4. C# 线程查漏补缺
  5. 用if语句替换三元运算符-标准的switch语句
  6. Azure Artifacts--全平台的程序包管理仓库(支持nuget)
  7. vue3 | shallowReactive 、shallowRef、triggerRef
  8. 【CTO变形记】驱动力的选择
  9. Quartz帮助类
  10. 《自定义工作流配置,springboot集成activiti,前端vue,完整版审批单据》