C++程序设计实验三 类和对象Ⅱ

动态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++程序设计实验三 类和对象Ⅱ的更多相关文章
- 20155229实验三 《Java面向对象程序设计实验三 敏捷开发与XP实践 》实验报告
实验题目 1.在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能. 2.下载搭档实验二的Complex代 ...
- .NET程序设计实验三
实验三 Windows 应用程序开发 一.实验目的 1. 掌握窗口控件的使用方法: 2. 掌握Windows 的编程基础. 二.实验要求 根据要求,编写 C#程序,并将程序代码和运行结果写入实验报告 ...
- Java程序设计 实验三
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:李海空 学号:20135329 成绩: 指 ...
- 【C++ 实验5 类和对象】
1. #include <iostream> #include <vector> #include <string> using namespace std; // ...
- POJ C++程序设计 编程作业—类和对象 编程题 #2
编程题 #2 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序的输出 ...
- c++实验3类和对象
实 验 3: part 1:验证 part 2:graph #include <iostream> #include "graph.h" using namespac ...
- POJ C++程序设计 编程作业—类和对象 编程题#3
编程题 #3 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序的输出 ...
- POJ C++程序设计 编程作业—类和对象 编程题#1
编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序输出的结 ...
- C++ 实验3 类和对象
Part 2 #ifndef GRAPH_H #define GRAPH_H class Graph { public: Graph(char ch, int n); void draw(); pri ...
- Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?
什么是面向对象程序设计? 我们称为OOP(Object Oriented Programming) 就是非结构化的程序设计 要使用类和对象的方法来进行编程 什么是类,什么是对象 类就是封装了属性和 ...
随机推荐
- day07-Vue04
Vue04 12.Vue2 脚手架模块化开发 目前开发模式的问题: 开发效率低 不够规范 维护和升级,可读性比较差 12.1基本介绍 官网地址 什么是Vue Cli脚手架 12.2环境配置,搭建项目 ...
- windows使用管理员权限安装软件
安装步骤 系统搜索 cmd 点击右键,使用管理者方式运行 输入用户名密码 成功以管理员身份运行 cd 到软件存储的目录 输入软件执行文件名, 按回车键,成功开始安装
- 图文并茂quasar2.6+vue3+ts+vite创建项目并引入mockjs,mockjs 拦截ajax请求的原理是什么,quasar为什么要使用boot?
每天都要开心(▽)哇: 首先呢,我们来创建项目 执行下面命令,开始创建项目啦 $ npm i -g @quasar/cli $ npm init quasar 下面是我的选项,仅供参考哇 √ What ...
- centos7笔记本使用iptables服务,将笔记本模拟成为出口路由器 PPPOE拨号+NAT+端口映射
郑州洪水,闲置在家,捣鼓捣鼓 centos7笔记本使用iptables服务,将笔记本模拟成为出口路由器 PPPOE拨号+NAT+端口映射 环境: 1.笔记本单网口,无法做路由网关,手里有个闲置的USB ...
- 结对项目总结 -- 基于Qt开发的win10桌面应用
担任角色 在这次结对项目中,由于采用了我的个人项目作为参考,所以我继续担任后端开发的角色. 开发环境 前端采用Qt Creator4.13.2 (Community) 后端采用C++ 如何复用个人项目 ...
- Zabbix“专家坐诊”第180期问答汇总
问题一 Q:老师,请教个问题,zabbix通过自动发现扫描网段,然后添加主机,有没有什么办法区分路由器或者交换机类型的方法,这样才能把交换机模板或者路由器模板挂给对应的主机A:不多的话, 批量加2次模 ...
- immutable.js学习笔记(四)----- OrederMap
- 基于遗传算法的地图四色原理绘图上色的Python代码
本文介绍利用Python语言,实现基于遗传算法(GA)的地图四色原理着色操作. 1 任务需求 首先,我们来明确一下本文所需实现的需求. 现有一个由多个小图斑组成的矢量图层,如下图所示. ...
- spring-cloud03-consul
官网的安装说明https://learn.hashicorp.com/tutorials/consul/get-started-install 1.下载安装 环境:阿里云服务器,consul1.9.5 ...
- windwos提权漏洞CVE-2023-21746复现(LocalPotato)
0x01 漏洞原理 LocalPotato攻击是一种针对本地认证的NTLM反射攻击. Windows NTLM 在进行身份验证时存在漏洞,允许拥有低权限的本地攻 击者通过运行特制程序将权限提升至 SY ...