动态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();
}

运行测试截图

C++程序设计实验三 类和对象Ⅱ的更多相关文章

  1. 20155229实验三 《Java面向对象程序设计实验三 敏捷开发与XP实践 》实验报告

    实验题目 1.在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能. 2.下载搭档实验二的Complex代 ...

  2. .NET程序设计实验三

    实验三  Windows 应用程序开发 一.实验目的 1. 掌握窗口控件的使用方法: 2. 掌握Windows 的编程基础. 二.实验要求 根据要求,编写 C#程序,并将程序代码和运行结果写入实验报告 ...

  3. Java程序设计 实验三

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计   班级:1353       姓名:李海空  学号:20135329 成绩:             指 ...

  4. 【C++ 实验5 类和对象】

    1. #include <iostream> #include <vector> #include <string> using namespace std; // ...

  5. POJ C++程序设计 编程作业—类和对象 编程题 #2

    编程题 #2 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序的输出 ...

  6. c++实验3类和对象

     实 验 3: part 1:验证 part 2:graph #include <iostream> #include "graph.h" using namespac ...

  7. POJ C++程序设计 编程作业—类和对象 编程题#3

    编程题 #3 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序的输出 ...

  8. POJ C++程序设计 编程作业—类和对象 编程题#1

    编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序输出的结 ...

  9. C++ 实验3 类和对象

    Part 2 #ifndef GRAPH_H #define GRAPH_H class Graph { public: Graph(char ch, int n); void draw(); pri ...

  10. Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?

    什么是面向对象程序设计? 我们称为OOP(Object  Oriented  Programming) 就是非结构化的程序设计 要使用类和对象的方法来进行编程 什么是类,什么是对象 类就是封装了属性和 ...

随机推荐

  1. RSA中用到的推导,笔记持续更新

    1.同余式组求p和q 已知条件: 推导过程: 根据上述已知条件,以及同余式性质,我们可以得到如下: c1e2 = (2p + 3q)e1*e2 mod N c2e1 = (5p + 7q)e1*e2  ...

  2. Service层和Dao层的一些自我理解(╥╯^╰╥)(╥╯^╰╥)(学了这么久,这玩意儿似懂非懂的)

    学习java已经有很长时间了,但由于是在学校学的,基础不怎么扎实. 这几个月系统的学习,弥补了很多的缺陷,虽然大多数时间都在弄算法(咳咳),我前面的博客有写 如果有认真看过我代码的朋友会发现,我其实英 ...

  3. Spark详解(03) - Spark3.0.0运行环境安装

    Spark详解(03) - Spark3.0.0运行环境安装 Spark运行模式 Spark常见部署模式: Local模式:在本地部署单个Spark服务 所谓的Local模式,就是不需要其他任何节点资 ...

  4. 10分钟做好 Bootstrap Blazor 的表格组件导出 Excel/Word/Html/Pdf

    上篇: Bootstrap Blazor 实战 通用导入导出服务(Table组件) 1.新建工程 新建工程b14table dotnet new blazorserver -o b14table 将项 ...

  5. [C++标准模板库:自修教程与参考手册]关于auto_ptr

    什么是auto_ptr指针 auto_ptr是这样一种指针:它是"它所指的对象"的拥有者,所以,当身为对象拥有者的auto_ptr指针被摧毁时,该对象也会被摧毁,auto_ptr要 ...

  6. UVA12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

    简要题意 这道题就是要你维护一个学生成绩管理系统. 代码实现 程序设计 为了方便输出,我们定义了 println 函数: void println(string s){ cout<<s&l ...

  7. [NOIP2017 普及组]跳房子 【题解】

    题目背景 NOIP2017 普及组 T4 题目描述 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一. 跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画 \(n ...

  8. 使用 NineData 高效编写 SQL

    SQL 是 Structured Query Language 的缩写,中文翻译为"结构化查询语言".它是关系型数据库的标准语言,所有的关系型数据库管理系统(RDBMS),比如 M ...

  9. SkyWalking安装及SkyApm运行(.net和java)

    一.部署SkyWalking 1.下载SkyWalking包(版本为6.6.0) 下载地址为:http://skywalking.apache.org/downloads/ 2.修改数据库配置 默认为 ...

  10. 【学习笔记】QT从入门到实战完整版(基础控件)(4)

    添加资源 将存有资源文件的文件夹放到工程目录中,如文件夹名称为 "Image",里面有 Luffy.png 文件. 在 Qt Creator 中右击工程选择[添加新文件]. 在弹出 ...