实验四 类与数组、指针

task5.

 1 #pragma once
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 class vectorInt {
 5 public:
 6     vectorInt(int n);
 7     vectorInt(int n,int value);
 8     vectorInt(const vectorInt& v0);
 9     ~vectorInt();
10     int &at(int n);
11     int get_size();
12     friend void output(vectorInt& v);
13 private:
14     int size;
15     int* p;
16 };
17 vectorInt::vectorInt(int n0) {
18     cout << "constructor 1 called.\n";
19     size = n0;
20     p = new int[n0];
21 }
22 vectorInt::vectorInt(int n0,int value) {
23     cout << "constructor 2 called.\n";
24     size = n0;
25     p = new int[n0];
26     for (int i = 0; i < n0; i++) 
27         p[i] = value;
28 }
29 vectorInt::vectorInt(const vectorInt& v0) {
30     cout << "copy constructor called.\n";
31     size = v0.size;
32     p = new int[size];
33     for (int i = 0; i < size; i++)
34         p[i] = v0.p[i];
35 }
36 vectorInt::~vectorInt() {
37     cout << "destructor called.\n";
38 }
39 int &vectorInt::at(int n) {
40     assert(n >= 0 && n < size);
41     return p[n];
42 }
43 int vectorInt::get_size() {
44     return size;
45 }
46 void output(vectorInt& v) {
47     for (int i = 0; i < v.size; i++)
48         cout << v.at(i) << " ";
49     cout << endl;
50 }

 task6

 1 #pragma once
 2 #include <iostream>
 3 
 4 using std::cout;
 5 
 6 using std::endl;
 7 
 8 class Matrix {
 9 
10 public:
11     Matrix(int n); 
12 
13     Matrix(int n, int m); 
14 
15     Matrix(const Matrix& X); 
16 
17     ~Matrix() { delete[] p; };
18 
19     void set(const double* pvalue); 
20 
21         void set(int i, int j, int value); 
22 
23     double& at(int i, int j); 
24 
25     double at(int i, int j) const; 
26 
27     int get_lines() const; 
28 
29     int get_cols() const;
30 
31     void print() const; 
32 
33 private:
34     int lines; 
35 
36     int cols; 
37 
38     double* p; 
39 
40 };
41 Matrix::Matrix(int n) {
42     lines = n;
43     cols = n;
44     p = new double[n*n];
45 }
46 Matrix::Matrix(int n, int m) {
47     lines = n;
48     cols = m;
49     p = new double[n*m];
50 }
51 Matrix::Matrix(const Matrix& X) {
52     lines = X.lines;
53     cols = X.cols;
54     p = new double[lines*cols];
55     for (int i = 0; i < lines * cols; i++)
56         p[i] = X.p[i];
57 }
58 void Matrix::set(const double* pvalue) {
59     for (int i = 0; i < lines * cols; i++)
60         p[i] = pvalue[i];
61 }
62 void Matrix::set(int i, int j, int value) {
63     p[i * lines + j ] = value;
64 }
65 double& Matrix::at(int i, int j) {
66     return p[i * lines + j];
67 }
68 double Matrix::at(int i, int j) const{
69     return p[i * lines + j];
70 }
71 int Matrix::get_lines() const {
72     return lines;
73 }
74 int Matrix::get_cols() const {
75     return cols;
76 }
77 void Matrix::print() const {
78     for (int i = 0; i < lines; i++) {
79         for (int j = 0; j < cols; j++) {
80             cout << p[i * cols + j] << ",";
81         }
82         cout << "\b \n";
83     }
84 }

 

posted @ 2022-11-02 18:04  一路走好真君  阅读(41)  评论(0)    收藏  举报