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

实验4 类与数组、指针

时间:2022-11-03 23:01:57浏览次数:55  
标签:const Matrix int double lines cols 实验 数组 指针

 1 #pragma once
 2 #include<iostream>
 3 using namespace std;
 4 class vectorInt{
 5     public:
 6     vectorInt(int n):size(n) {
 7         cout << "construct 1 called"<<endl;
 8          p = new int[n];
 9     }
10     vectorInt(int n,int e):size(n) {
11         cout << "construct 2 called"<<endl;
12          p = new int[n];
13          for(int i = 0; i < size; i++) p[i] = e;}
14     vectorInt(vectorInt &obj): size{obj.size} {
15         cout << "copy constructor called" <<endl;
16         p = new int[size];
17         for(int i = 0;i < size; i++)
18         p[i] = obj.p[i];
19     }
20     ~vectorInt() {cout<< "destructor called" << endl;
21     delete[] p;}
22     int get_szie() const {return size;}
23     int &at(int n){ return p[n]; }
24 
25     private:
26     int size;
27     int *p;
28 
29     friend void output(vectorInt &x);
30 };
31 
32 void output(vectorInt &x){
33     for(int i = 0; i < x.size; i++)
34     if(i!=x.size-1) cout << x.at(i) << ",";
35     else cout << x.at(i);
36     cout << endl;
37 }
vectorInt.hpp
 1 #include <iostream>
 2 #include "vectorInt.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     int n;
 8     cin >> n;
 9     
10     vectorInt x1(n);
11     for(auto i = 0; i < n; ++i)
12         x1.at(i) = i*i;
13 
14     output(x1);
15 
16     vectorInt x2(n, 42);
17     vectorInt x3(x2);
18 
19     output(x2);
20     output(x3);
21 
22     x2.at(0) = 77;
23 
24     output(x2);
25     output(x3);
26 }
27 
28 int main() {
29     test();
30 }
task5.cpp

task5

 

 

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

 

 

实验总结

1.task5中输出函数,参数中用引用类型,否则会多次调用复制构造函数

2.用指针进行数组赋值时,主要最后回到初始位置

 

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

相关文章

  • 88.合并两个有序数组
    给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。请你 合并 nums2 到 nums1 中,使合......
  • 实验四
    #pragma#include<iostream>usingstd::cout;usingstd::endl;classvectorInt{public:vectorInt(intn);vectorInt(intn,intvalue);......
  • 参数传递误用 指针相关
    https://mp.weixin.qq.com/s/VS7DZg9qb_QmqAKqm_zc-Q本文收集一些使用Go开发过程中非常容易踩坑的case,所有的case都有具体的代码示例,以及针对的代码修复方法,以避免大家再......
  • Java学习笔记day3--二维数组
    packageday4_array;importjavax.swing.plaf.synth.SynthFormattedTextFieldUI;//一维数组的元素仍然是一维数组,则构成了二维数组publicclassArrayDemension2{......
  • 实验三
    实验任务一#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidpr......
  • 实验3
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_s......
  • 实验二:逻辑回归算法实验
    |20大数据三班||学号201613334|  【实验目的】1.理解逻辑回归算法原理,掌握逻辑回归算法框架;2.理解逻辑回归的sigmoid函数;3.理解逻辑回归的损失函数;4.针对特定应......
  • 实验四
    实验任务五vectorInt.hpp#pragmaonce#include<iostream>#include<cassert>usingnamespacestd;classvectorInt{public:vectorInt(intn);vectorInt(......
  • 实验3
    task_11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,charte......
  • 实验三
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明void......