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

实验三-类与数组、指针

时间:2023-11-05 13:11:23浏览次数:33  
标签:const vectorPoint Point int void 实验 数组 include 指针

point.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 using std::cout;
 5 using std::endl;
 6 
 7 class Point {
 8 public:
 9     Point(int x0 = 0, int y0 = 0);
10     ~Point() = default;
11 
12     int get_x() const;
13     int get_y() const;
14     void show() const;
15     void move(int new_x, int new_y);
16 
17 private:
18     int x, y;
19 };
20 
21 Point::Point(int x0, int y0): x{x0}, y{y0} {   
22 }
23 
24 int Point::get_x() const {
25     return x;
26 }
27 
28 int Point::get_y() const {
29     return y;
30 }
31 
32 void Point::show() const {
33     cout << "(" << x << ", " << y << ")" << endl;
34 }
35 
36 void Point::move(int new_x, int new_y) {
37     x = new_x;
38     y = new_y;
39 }
View Code

 

task1.cpp

 1 #include <iostream>
 2 #include "point.hpp"
 3 #include <vector>
 4 
 5 using std::vector;
 6 using std::cin;
 7 
 8 void output(const vector<Point> &v) {
 9     for(auto &t: v)
10         t.show();
11 }
12 
13 void test() {
14     int n;
15     cout << "输入动态Point数组类对象中元素个数: ";
16     cin >> n;
17 
18     vector<Point> x(n);
19     cout << "x对象中所有点坐标信息: " << endl;
20     output(x); 
21 
22     vector<Point> y(x);  
23     cout << "\nx对象中所有点坐标信息: " << endl;
24     output(y);
25     
26     cout << "\n更新x对象  " << endl;
27     x.at(0).move(30, 50);       
28     x.push_back(Point(2, 2));   
29 
30     cout << "\nx对象中所有点坐标信息: " << endl;
31     output(x);          
32     cout << "\ny对象中所有点坐标信息: " << endl; 
33     output(y);           
34 }
35 
36 int main() {
37     test();
38 }
View Code

 

截图

 

问题1:对x对象进行更新时,基于 vector<Point> 对象x创建的对象y是否发生变化? 不发生变化
问题2:标准库模板类vector在复制一个动态数组对象时,实现的是深复制还是浅复制? 深复制
Point.hpp
 1 #pragma once
 2 
 3 #include <iostream>
 4 using std::cout;
 5 using std::endl;
 6 
 7 class Point {
 8 public:
 9     Point(int x0 = 0, int y0 = 0);
10     ~Point() = default;
11 
12     int get_x() const;
13     int get_y() const;
14     void show() const;
15     void move(int new_x, int new_y);
16 
17 private:
18     int x, y;
19 };
20 
21 Point::Point(int x0, int y0): x{x0}, y{y0} {   
22 }
23 
24 int Point::get_x() const {
25     return x;
26 }
27 
28 int Point::get_y() const {
29     return y;
30 }
31 
32 void Point::show() const {
33     cout << "(" << x << ", " << y << ")" << endl;
34 }
35 
36 void Point::move(int new_x, int new_y) {
37     x = new_x;
38     y = new_y;
39 }
View Code

 


vectorPoint.hpp
 1 #pragma once
 2 
 3 #include "point.hpp"
 4 #include <cassert>
 5 #include <iostream>
 6 
 7 class vectorPoint{
 8 public:
 9     vectorPoint(int n);
10     ~vectorPoint();
11     
12     int get_size() const;           
13     Point& at(int index);           
14     Point& at(int index) const;     
15 
16 private:
17     int size; 
18     Point *ptr;
19 };
20 
21 vectorPoint::vectorPoint(int n) : size{n} {
22     ptr = new Point[n];
23 }
24 
25 vectorPoint::~vectorPoint() {
26     delete[] ptr;
27 }
28 
29 int vectorPoint::get_size() const {
30     return size;
31 }
32 
33 Point& vectorPoint::at(int index) {
34     assert(index >= 0 && index < size);  
35     return ptr[index];
36 }
37 
38 Point& vectorPoint::at(int index) const {
39     assert(index >= 0 && index < size);  
40     return ptr[index];
41 }
View Code

 

task2.cpp
 1 #include "vectorPoint.hpp"
 2 #include <iostream>
 3 
 4 void output(const vectorPoint &v) {
 5     for(auto i = 0; i < v.get_size(); ++i)
 6         v.at(i).show();
 7 }
 8 
 9 void test() {
10     using namespace std;
11 
12     int n;
13     cout << "输入vectorPoint对象中元素个数: ";
14     cin >> n;
15 
16     vectorPoint x(n);
17     cout << "x对象中所有点坐标信息: " << endl;
18     output(x); 
19 
20     vectorPoint y(x);
21     cout << "\ny对象中所有点坐标信息: " << endl;
22     output(y); 
23 
24     cout << "\n更新x对象中点坐标信息 " << endl;
25     x.at(0).move(30, 50);
26     x.at(1).move(-1, -1);
27 
28     cout << "x对象中所有点坐标信息: " << endl;
29     output(x); 
30 
31     cout << "\ny对象中所有点坐标信息: " << endl;
32     output(y); 
33 }
34 
35 int main() {
36     test();
37 }
View Code

 

截图

 


问题1:观察更新对象x后,基于 vectorPoint 对象x创建的对象y是否发生变化? 发生变化
问题2:编译器为vectorPoint类创建的默认复制构造函数,在复制一个动态数组对象时,实现 的是深复制还是浅复制? 浅复制
问题3:在类vectorPoint内部,手动增加的以下复制构造函数声明和定义,实现的是浅复制还 是深复制? 浅复制
Point.hpp
 1 #pragma once
 2 
 3 #include <iostream>
 4 using std::cout;
 5 using std::endl;
 6 
 7 class Point {
 8 public:
 9     Point(int x0 = 0, int y0 = 0);
10     ~Point() = default;
11 
12     int get_x() const;
13     int get_y() const;
14     void show() const;
15     void move(int new_x, int new_y);
16 
17 private:
18     int x, y;
19 };
20 
21 Point::Point(int x0, int y0): x{x0}, y{y0} {   
22 }
23 
24 int Point::get_x() const {
25     return x;
26 }
27 
28 int Point::get_y() const {
29     return y;
30 }
31 
32 void Point::show() const {
33     cout << "(" << x << ", " << y << ")" << endl;
34 }
35 
36 void Point::move(int new_x, int new_y) {
37     x = new_x;
38     y = new_y;
39 }
View Code

 


vectorPoint.hpp
 1 #pragma once
 2 
 3 #include "point.hpp"
 4 #include <cassert>
 5 #include <iostream>
 6 
 7 class vectorPoint{
 8 public:
 9     vectorPoint(int n);
10     vectorPoint(const vectorPoint &vp);
11     ~vectorPoint();
12     
13     int get_size() const;           
14     Point& at(int index);           
15     Point& at(int index) const;     
16 
17 private:
18     int size; 
19     Point *ptr;
20 };
21 
22 vectorPoint::vectorPoint(int n) : size{n} {
23     ptr = new Point[n];
24 }
25 
26 vectorPoint::vectorPoint(const vectorPoint &vp): size{vp.size}, ptr{new Point[size]} {
27     for(auto i = 0; i < size; ++i)
28         ptr[i] = vp.ptr[i];
29 }
30 
31 vectorPoint::~vectorPoint() {
32     delete[] ptr;
33 }
34 
35 int vectorPoint::get_size() const {
36     return size;
37 }
38 
39 Point& vectorPoint::at(int index) {
40     assert(index >= 0 && index < size);  
41     return ptr[index];
42 }
43 
44 Point& vectorPoint::at(int index) const {
45     assert(index >= 0 && index < size);  
46     return ptr[index];
47 }
View Code

 

task3.cpp
 1 #include "vectorPoint.hpp"
 2 #include <iostream>
 3 
 4 void output(const vectorPoint &v) {
 5     for(auto i = 0; i < v.get_size(); ++i)
 6         v.at(i).show();
 7 }
 8 
 9 void test() {
10     using namespace std;
11 
12     int n;
13     cout << "输入vectorPoint对象中元素个数: ";
14     cin >> n;
15 
16     vectorPoint x(n);
17     cout << "x对象中所有点坐标信息: " << endl;
18     output(x); 
19 
20     vectorPoint y(x);
21     cout << "\ny对象中所有点坐标信息: " << endl;
22     output(y); 
23 
24     cout << "\n更新x对象中点坐标信息 " << endl;
25     x.at(0).move(30, 50);
26     x.at(1).move(-1, -1);
27 
28     cout << "x对象中所有点坐标信息: " << endl;
29     output(x); 
30 
31     cout << "\ny对象中所有点坐标信息: " << endl;
32     output(y); 
33 }
34 
35 int main() {
36     test();
37 }
View Code

 

截图

 


问题1:观察更新对象x后,基于 vectorPoint 对象x创建的对象y是否发生变化? 发生变化
问题2:这个vectorPoint 类的实现中,复制构造函数实现的是深复制还是浅复制? 浅复制
问题3:基于实验任务2和3,总结当类的成员中包含指针域成员时深复制与浅复制的区别。 深复制和浅复制的区别在于对指针所指向的内存空间的处理方式。浅复制是将指针的值复制给新对象,这样新对象和原对象在同一块内存空间。而深复制则会为新对象分配一块新的内存空间,互不影响。
task4_1.cpp
 1 #include <iostream>
 2 using namespace std;
 3 
 4 
 5 void swap1(int &rx, int &ry);    
 6 void swap2(int *px, int *py);    
 7 void print(int x, int y);        
 8 
 9 void test() {
10     int x = 3, y = 4;
11 
12     print(x, y);
13     swap1(x, y);        
14     print(x, y);
15 
16     cout << endl;
17 
18     x = 3, y = 4;
19     print(x, y);
20     swap2(&x, &y);        
21     print(x, y);
22 }
23 
24 int main() {
25     test();
26 }
27 
28 void swap1(int &rx, int &ry) {
29     int t;
30 
31     t = rx;  rx = ry;  ry = t;
32 }
33 
34 void swap2(int *px, int *py) {
35     int t;
36 
37     t = *px;  *px = *py;  *py = t;
38 }
39 
40 void print(int x, int y) {
41     std::cout << "x = " << x << ", y = " << y << "\n";
42 }
View Code

 

截图

 


task4_2.cpp
 1 #include <iostream>
 2 #include <typeinfo>
 3 using namespace std;
 4 
 5 int main() {
 6     int a;
 7     int &ra = a;
 8     ra = 4;
 9 
10     int *pa = &a;
11     *pa = 5;
12 
13     cout << "&a = " << hex << &a << endl;
14     cout << "&ra = " << hex << &ra << endl;
15     cout << "&pa = " << hex << &pa << "\n\n";
16     
17     cout << "a = " << a << endl;
18     cout << "ra = " << a << endl;
19     cout << "pa = " << hex << pa << endl;
20     
21     cout << "*pa = " << *pa << "\n\n";
22 
23     cout << "type a: " << typeid(a).name() << endl;
24     cout << "type ra: " << typeid(ra).name() << endl;
25     cout << "type pa: " << typeid(pa).name() << endl;
26 }
View Code

 

截图

 


task4_3.cpp
 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 template<typename T>
 7 void output(const T &x) {
 8     for(auto i: x)  
 9         std::cout << i << ", ";
10     std::cout << "\b\b \n";
11 }
12 
13 template<typename T>
14 void square1(T &x) {
15     for(auto i: x) 
16         i *= i;
17 }
18 
19 template<typename T>
20 void square2(T &x) {
21     for(auto &i: x)  
22         i *= i;
23 }
24 
25 void test1() {
26     vector<int> x {1, 2, 3, 4, 5};
27 
28     cout << "动态int型数组对象x内的元素值: ";
29     output(x);
30 
31     cout << "调用函数square1() " << endl;
32     square1(x);
33 
34     cout << "动态int型数组对象x内的元素值: ";
35     output(x);
36 }
37 
38 void test2() {
39     vector<int> x {1, 2, 3, 4, 5};
40 
41     cout << "动态int型数组对象x内的元素值: ";
42     output(x);
43 
44     cout << "调用函数square2() " << endl;
45     square2(x);
46 
47     cout << "动态int型数组对象x内的元素值: ";
48     output(x);
49 }
50 
51 int main() {
52     cout << "测试1: " << endl;
53     test1();
54 
55     cout << "\n测试2: " << endl;
56     test2();
57 }
View Code

 

截图

 


用文字总结引用类型、指针类型的区别 指针可以被重新赋值为另一个地址,而引用一旦被初始化就不能被改变。指针可以为空,即指向空地址,而引用必须引用一个有效的对象。指针可以指向另一个指针,而引用只能引用一个对象。指针可以独立存在,而引用不能独立存在。指针可以指向任意类型的数据,而引用只能引用其所指向类型的数据。
vectorInt.hpp
 1 #ifndef VECTORINT
 2 #define VECTORINT
 3 
 4 #include<iostream>
 5 #include<cassert>
 6 
 7 using namespace std;
 8 
 9 class vectorInt {
10     
11     public:
12         vectorInt(int n);
13         vectorInt(int n,int value);
14         vectorInt(const vectorInt &vc);
15         int get_size() const;
16         ~vectorInt();
17         int &at(int index);
18         int &at(int index) const;
19                  
20     private:
21         int size;
22         int *p;
23 };
24 
25 vectorInt::vectorInt(int n) {
26     size=n;
27     p=new int[n];  
28     
29 }
30 
31 vectorInt::vectorInt(int n,int value) {
32     size=n;
33     p=new int[n];
34     for(int i=0; i<size; i++) {
35         p[i]=value;
36     }
37    
38 }
39 
40 vectorInt::vectorInt(const vectorInt &vc) {
41     size=vc.size;
42     p=new int[size];
43     for(int i=0; i<size; ++i) {
44         p[i]=vc.p[i];
45     }
46     
47 }
48 
49 vectorInt::~vectorInt() {
50     delete[] p;
51    
52 }
53 
54 int vectorInt::get_size() const 
55 {
56      return size;
57 }
58 
59 int &vectorInt::at(int index) {
60     assert(index >= 0 && index < size);
61     return p[index];
62 }
63 
64 int &vectorInt::at(int index) const 
65 {
66      assert(index>=0 && index < size);
67      return p[index];
68 }
69 
70 #endif
View Code

 

task5.cpp
 1 #include "vectorInt.hpp"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::cin;
 6 using std::endl;
 7 
 8 void output(const vectorInt &vi) {
 9     for(auto i = 0; i <=vi.get_size(); ++i)
10         cout << vi.at(i) << ", ";
11     
12 }
13 
14 void test() {
15     int n;
16     cout << "输入vectorInt对象中元素个数: ";
17     cin >> n;
18 
19     vectorInt x1(n);    
20     for(auto i = 0; i < n; ++i)
21         x1.at(i) = i*i;
22     cout << "vectorInt对象x1: ";
23     output(x1);
24 
25     vectorInt x2(n, 42); 
26     cout << "vectorInt对象x2: ";
27     output(x2);
28     vectorInt x3(x2);    
29     cout << "vectorInt对象x3: ";
30     output(x3);
31 
32     cout << "更新vectorInt对象x2\n";
33     x2.at(0) = 77;
34     x2.at(1) = -999;
35 
36     cout << "vectorInt对象x2: ";
37     output(x2);
38     cout << "vectorInt对象x3: ";
39     output(x3);
40 }
41 
42 int main() {
43     test();
44 }
View Code

 

截图

 

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

 

task6.cpp
 1 #include <iostream>
 2 #include "Matrix.hpp"
 3 
 4 using namespace std;
 5 
 6 const int N1 = 3;
 7 const int N2 = 2;
 8 
 9 void output(const Matrix &m, int index) {
10     for(auto j = 0; j < m.get_cols(); ++j)
11         cout << m.at(index, j) << ", ";
12     cout << "\b\b \n";
13 }
14 
15 void test() {
16 
17     double x[N1*N2] = {2,4, 5, 7, 8, 9};
18 
19     Matrix m1(N1, N2);      
20     m1.set(x);              
21     cout << "矩阵对象m1: " << endl;
22     m1.print();            
23     cout << "矩阵对象m1第0行是: " << endl;
24     output(m1, 0);
25     cout << endl;
26 
27     Matrix m2(N2, N1);
28     m2.set(x);
29     cout << "矩阵对象m2: " << endl;
30     m2.print();         
31     cout << "矩阵对象m2第0行是: " << endl;
32     output(m2, 0);
33     cout << endl;
34 
35     Matrix m3(m2);      
36     m3.set(0, 0, 999);  
37     cout << "矩阵对象m3:" << endl;
38     m3.print();
39     cout << endl;
40 
41     Matrix m4(2);       
42     m4.set(x);          
43     cout << "矩阵对象m4:" << endl;
44     m4.print();
45 }
46 
47 int main() {
48     test();
49 }
View Code

 

截图

 

   

标签:const,vectorPoint,Point,int,void,实验,数组,include,指针
From: https://www.cnblogs.com/chenxiaolong202083290491/p/17810425.html

相关文章

  • 实验2 类和对象_基础编程2
    实验任务1demo1.dev方法一#ifndefT_H#defineT_H#include<iostream>#include<string>usingnamespacestd;//类T的声明classT{public:T(intx=0,inty=0);//带有默认形值的构造函数T(constT&t);//复制构造函数T(T&&......
  • 查找数组中元素
    查找数组中元素任务详情输入一个固定长度的数组,并输入一个要查找的数,给出能不能检索到的伪代码并测试伪代码fact赋值为0输入长度为8的数组num输入想检索的数searchi赋值为0如果i不超过7{判断num[i]是否等于search等于则fact赋值为1并结束循环i赋值为i+1}如果fact为1......
  • 面试必刷TOP101:21、旋转数组的最小数字
    题目题解二分法:importjava.util.ArrayList;publicclassSolution{publicintminNumberInRotateArray(int[]array){//特殊情况判断if(array.length==0){return0;}//左右指针ijinti=0,j=array.......
  • 实验3
    实验1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(intn);10......
  • Java小白学习记录--------常见的一维数组遍历方法
    一维数组:for循环遍历:int[]myArray={1,2,3,4,5};for(inti=0;i<myArray.length;i++){System.out.println("myArray["+i+"]="+myArray[i]);//输出数组中的每个元素} for-each循环遍历数组(增强for循环遍历)int[]myArray={1,2,3,4,5};......
  • linux shell中 创建数组及数组的基本操作
     001、创建数组a、方法1[root@pc1test01]#ay1=(100200"abc""xyz")##创建数组 b、方法2[root@pc1test01]#ay2[0]=100[root@pc1test01]#ay2[1]=200[root@pc1test01]#ay2[3]="mn" 002、访问数组的全部元素[root@pc1test01]#ay1=(100......
  • 实验3 类与数组、指针
    实验任务1point.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;voidshow()const;voidmov......
  • 【教3妹学编程-算法题】使数组变美的最小增量运算数
    2哥 :3妹,脸上的豆豆好了没呢。3妹:好啦,现在已经没啦2哥 :跟你说很快就会消下去的,还不信~既然你的容颜和心情都如此美丽,那我们就再做一道关于美丽的题吧。3妹:切,2哥就会取笑我,伤心时让我做题,开心时也让我做题! 1题目: 给你一个下标从0开始、长度为n的整数数组nums,和一个整......
  • 数组的练习专项(接上节的数组的详解)
     练习1:求和需求:定义一个数组,存储1,2,3,4,5遍历数组得到每一个元素,求数组里面所有的数据和代码示例:/*定义一个数组,存储1,2,3,4,5遍历数组得到每一个元素,求数组里面所有的数据和*///分析://1.定义一个数组,并添加数据1,2,3,4,5int[]arr={1,2,3,4,5};//求和变量int......
  • 数组的练习专项(接上节的数组的详解)
     练习1:求和需求:定义一个数组,存储1,2,3,4,5遍历数组得到每一个元素,求数组里面所有的数据和代码示例:/*定义一个数组,存储1,2,3,4,5遍历数组得到每一个元素,求数组里面所有的数据和*///分析://1.定义一个数组,并添加数据1,2,3,4,5int[]arr={1,2,3,4,5};//求和变量int......