动态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. 线性构造treap

    数据结构 线性构造treap treap的线性构造可以使复杂度锐减到\(O(n)\),很优秀 treap的本质就是小根堆+二叉搜索树,即保证val满足搜索树的同时,维护rad的小根堆. 现在我们先把数 ...

  2. P1605迷宫——题解

    展开 题目背景 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右四种方式,每次 ...

  3. C語言成績分析系統

    C語言成績分析系統,可以實現七個功能.(使用的編譯器是 code::blocks) 主要實現對於學生信息的輸入 顯示輸入學生的信息 根據期末成績來進行排名. 查找某個學生的信息 刪除某個學生的信息 修 ...

  4. angular--连接服务获取数据并展示到页面加载结束禁用按钮-分页加载-管道使用

  5. webpack动态配置多静态资源路径,动态配置多上线路径,配置less,多种图片引用方式----"webpack": "^4.41.6",

    1.项目场景是有两个静态资源目录,一个用于开发,一个用于发布,上线多个版本,打包多个版本后,也要部署到同一个服务器的同一个端口下. 根据我自己的摸索,我搞出来了下面的配置,自感觉还蛮好用的 先看我的c ...

  6. 日常JS数据各种操作方法总结~~欢迎大家留言板补充哦~~

    需求情景一: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  7. RestTemplate的超全讲解(全)转

    RestTemplate的超全讲解(全)转 https://blog.csdn.net/weixin_47872288/article/details/121842374 (81条消息) RestTe ...

  8. VUE10 计算属性

    1 Vue计算属性简介 1)定义:我们需要一个属性,但是这个属性不存在,但是可以通过已有的属性计算得来,那么就可以定义一个计算属性. 2)原理:底层借助了Object.defineproperty方法 ...

  9. Vue08 数据代理

    1 说明 所谓"数据代理",是指 通过一个对象代理对另一个对象的属性进行读或写操作. 2 简单示例 2.1 代码 let obj = {x:100}; let obj2 = {y: ...

  10. @Slf4j -- lombok.extern.slf4j.Slf4j;

    @Log4j:注解在类上:为类提供一个 属性名为log 的 log4j 日志对像 package com.atguigu.springcloud.controller; import com.atgu ...