首页 > 其他分享 >实验4 类与数组、指针

实验4 类与数组、指针

时间:2022-11-05 21:15:16浏览次数:37  
标签:const Matrix int lines cols 实验 数组 vectorInt 指针

实验任务5

 1 #pragma once
 2 #include<iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 class vectorInt {
 8 public:
 9     //构造函数和析构函数
10     vectorInt(int n);
11     vectorInt(int n, int value);
12     vectorInt(const vectorInt& X);
13     ~vectorInt();  //~vectorInt()=default;  默认合成的析构函数 
14 
15     //其它成员函数
16     int& at(int i);
17     int get_size();
18 
19     //友元函数
20     friend void output(const vectorInt& X);
21 
22 private:
23     int size;
24     int* p;
25 };
26 
27 vectorInt::vectorInt(int n) :size{ n } {
28     cout << "constructor 1 called.\n";
29     p = new int[size];
30 }
31 
32 vectorInt::vectorInt(int n, int value) :size{ n }
33 {
34     cout << "constuctot 2 called" << endl;
35     p = new int[size];
36     for (int i = 0; i < n; ++i)
37         p[i] = value;
38 }
39 
40 vectorInt::vectorInt(const vectorInt& X) :size{ X.size }
41 {
42     cout << "copy constuctot called" << endl;
43     p = new int[X.size];
44     for (auto i = 0; i < X.size; ++i)
45         p[i] = X.p[i];
46 }
47 
48 vectorInt::~vectorInt() {
49     cout << "destructor called.\n";
50     delete[]p;
51 }
52 
53 int& vectorInt::at(int i) {
54     return p[i];
55 }
56 
57 int vectorInt::get_size() {
58     return size;
59 }
60 
61 void output(const vectorInt& X) {
62     for (auto i = 0; i < X.size; i++)
63         cout << X.p[i] << " ";
64     cout << endl;
65 }
vectorInt.hpp
 1 #include <iostream>
 2 #include "vectorInt.hpp"
 3 void test() {
 4     using namespace std;
 5     int n;
 6     cin >> n;
 7     vectorInt x1(n);
 8     for (auto i = 0; i < n; ++i)
 9         x1.at(i) = i * i;
10     output(x1);
11     vectorInt x2(n, 42);
12     vectorInt x3(x2);
13     output(x2);
14     output(x3);
15     x2.at(0) = 77;
16     output(x2);
17     output(x3);
18 }
19 int main() {
20     test();
21 }
task5.cpp

实验测试截图:

 

 

实验任务6

 1 #pragma once
 2 #include <iostream>
 3 using std::cout;
 4 using std::endl;
 5 class Matrix {
 6 public:
 7     Matrix(int n); // 构造函数,构造一个n*n的矩阵
 8     Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵
 9     Matrix(const Matrix& X); // 复制构造函数,使用已有的矩阵X构造
10     ~Matrix(); //析构函数
11     void set(const double* pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值
12     void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value
13     double& at(int i, int j); //返回矩阵第i行第j列元素的引用
14     double at(int i, int j) const; // 返回矩阵第i行第j列元素的值
15     int get_lines() const; //返回矩阵行数
16     int get_cols() const; //返回矩列数
17     void print() const; // 按行打印输出矩阵
18 private:
19     int lines; // 矩阵行数
20     int cols; // 矩阵列数
21     double* p; // 指向存放矩阵数据的内存块的首地址
22 };
23 // 类Matrix的实现:
24 
25 Matrix::Matrix(int n) :lines(n), cols(n) {
26     p = new double[n * n];
27 }
28 
29 Matrix::Matrix(int n, int m) :lines(n), cols(m){
30     p = new double[n * m];
31 }
32 
33 Matrix::Matrix(const Matrix& X) :lines(X.lines), cols(X.cols) {
34     p = new double[X.lines * X.cols];
35     for (auto i = 0; i < X.lines * X.cols; i++) {
36         p[i] = X.p[i];
37     }
38 }
39 
40 Matrix::~Matrix() {
41     delete[]p;
42 }
43 
44 void Matrix::set(const double* pvalue) {
45     for (int i = 0; i < lines * cols; i++) {
46         p[i] = pvalue[i];
47     }
48 }
49 
50 void Matrix::set(int i, int j, int value) {
51     p[i * cols + j] = value;
52 }
53 
54 double& Matrix::at(int i, int j) {
55     return p[i * cols + j];
56 }
57 
58 double Matrix::at(int i, int j)const {
59     return p[i * cols + j];
60 }
61 
62 int Matrix::get_lines()const {
63     return lines;
64 }
65 
66 int Matrix::get_cols()const {
67     return cols;
68 }
69 
70 void Matrix::print()const {
71     for (auto i = 0; i < lines * cols; i++) {
72         cout << p[i] << " ";
73         if ((i+1) % cols == 0) cout << '\n';
74     }
75 }
matrix.hpp
 1 #include <iostream>
 2 #include "matrix.hpp"
 3 void test() {
 4     using namespace std;
 5     double x[] = { 1, 2, 3, 4, 5, 6 };
 6     Matrix m1(3, 2); // 创建一个3×2的矩阵
 7     m1.set(x); // 用一维数组x的值按行为矩阵m1赋值
 8     m1.print(); // 打印矩阵m1的值
 9     cout << "the first line is: " << endl;
10     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; // 输出矩阵m1第1行两个元素的值
11         cout << endl;
12     Matrix m2(2, 3);
13     m2.set(x);
14     m2.print();
15     cout << "the first line is: " << endl;
16     cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
17     cout << endl;
18     Matrix m3(m2); // 用矩阵m2构造新的矩阵m3
19     m3.set(0, 0, 999); // 将矩阵m3第0行第0列元素值设为999
20     m3.print();
21 }
22 int main() {
23     test();
24 }
task6.cpp

测试截图:

 

标签:const,Matrix,int,lines,cols,实验,数组,vectorInt,指针
From: https://www.cnblogs.com/Terrence0403/p/16861280.html

相关文章

  • 实验三
    实验任务1:#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//*函数声明*......
  • 实验4
    //vectorInt.hpp#include<iostream>usingnamespacestd;classvectorInt{private:intsize;int*data;public:vectorInt(......
  • solidity数组的使用
    长度固定的数组uint[]定义的数组,可以使用引用查看索引位置的数值,使用.length得到数组的长度byte定义的数组,将字符串以十六进制形式保存,不能使用.length。byte默认值为0......
  • 实验二:逻辑回归算法实验
    【实验目的】1.理解逻辑回归算法原理,掌握逻辑回归算法框架;2.理解逻辑回归的sigmoid函数;3.理解逻辑回归的损失函数;4.针对特定应用场景及数据,能应用逻辑回归算法解决实际分......
  • 指针和引用
    c++11新增了“右值引用”,这里的引用特指“左值引用”。(1)定义:引用是为变量另起一个名字,它和这个变量实质上是同一个东西。指针是一个变量,它存储的是一个地址,指向内存的一......
  • 实验二:逻辑回归算法实验
    |20大数据三班|实验二:逻辑回归算法实验-作业-20级大数据3班机器学习-班级博客-博客园(cnblogs.com)||学号|201613328|实验二:逻辑回归算法实验-胡辛原-博客......
  • 实验四
    #include<iostream>usingnamespacestd;classvectorint{public:vectorint(intl=0){size=l;cout<<"constructor1called\n"<<endl;......
  • vue如何过滤出一个数组中不包含另一个数组的数据
    data里面定义测试数组arr1:[1,3,5,7,11,0],arr2:[1,11],arrres:[]然后过滤一下for(letoneofthis.arr1){if(this.arr2.indexOf(one)==-1)......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopam......
  • 实验3 C语言控制语句应用编程
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces......