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) 就是非结构化的程序设计 要使用类和对象的方法来进行编程 什么是类,什么是对象 类就是封装了属性和 ...
随机推荐
- CH9434-MCU代码移植,芯片使用详细说明(附Linux开发资料链接)
简介 CH9434是一款SPI转四串口转接芯片,提供四组全双工的9线异步串口,用于单片机/嵌入式/安卓系统扩展异步串口.提供25路GPIO,以及支持RS485收发控制引脚TNOW.本篇基于STM32F ...
- [机器学习] 特征选择笔记4-使用SelectFromModel特征选择
特征选择 代码下载 本文主要介绍sklearn中进行特征选择的方法. sklearn.feature_selection模块中的类可用于样本集的特征选择/降维,以提高估计量的准确性得分或提高其在超高维 ...
- 基于云基础设施快速部署 RocketMQ 5.0 集群
本文作者:蔡高扬,Apache RocketMQ Committer, 阿里云智能技术专家. 背景 上图左侧为 RocketMQ 4.x版本集群,属于非切换架构.NameServer 作为无状态节点可 ...
- Codeforces Round #846 (Div. 2) A-E
比赛链接 A 题意 给 \(n\) 个正整数,找到三个数,使得他们的和为奇数,输出他们的下标. 题解 知识点:贪心. 找到三个奇数或者一个奇数两个偶数即可,其他情况无解. 时间复杂度 \(O(n)\) ...
- mysql 简单查询
查询特定列SELECT ename,birthday FROM emp; 查询所有的select*from emp; 给列起别名select ename AS 姓名, salary AS 工资 FRO ...
- LTC2440串行SPI通讯时序
LTC2440 简介 我们使用4-wire SPI接口 按照时序图上的描述,SDO是在SCLK的下降沿更新数据,那么FPGA接收端就应该在上升沿采集数据. 实际测试发现SDO数据相对于SCLK延迟了6 ...
- 使用ProPerties集合存储数据-Properties集合中的方法store
使用ProPerties集合存储数据 java.util.Properties`继承于Hashtable,来表示一个持久的属性集.它使用键值结构存储数据每个键及其对应值都是一个字符串.该类也被许多Ja ...
- 数据结构-详解优先队列的二叉堆(最大堆)原理、实现和应用-C和Python
一.堆的基础 1.1 优先队列和堆 优先队列(Priority Queue):特殊的"队列",取出元素顺序是按元素优先权(关键字)大小,而非元素进入队列的先后顺序. 若采用数组或链 ...
- Dubbo 入门系列之基于 Dubbo API 开发微服务应用
目标 从零上手开发基于 Dubbo 的微服务 难度 低 环境要求 系统:Windows.Linux.MacOS JDK 8 及以上(推荐使用 JDK17) Git IntelliJ IDEA(可选) ...
- mysql查询逗号,分隔的多个id连表查询
先来几个SQL看看效果 SELECT i.*,n.*FROM iLEFT JOIN n on FIND_IN_SET(n.id, i.n_id)GROUP BY n.id效果图: 想把信息都放一行里面 ...