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

实验三:类与数组、指针。

时间:2023-11-06 09:57:27浏览次数:26  
标签:index const vectorPoint Point int void 实验 数组 指针

实验任务1

 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 }
point.hpp
 1 #include <iostream>
 2 #include "point.hpp"
 3 #include <vector>
 4 
 5 using std::vector;
 6 using std::cin;
 7 
 8 //输出vector<Point>对象内所有点的坐标
 9 void output(const vector<Point>& v) {
10     for (auto& t : v)
11         t.show();
12 }
13 
14 void test() {
15     int n;
16     cin >> n;
17 
18     vector<Point> x(n);
19     cout << "x对象中所有点坐标信息:" << endl;
20     output(x);
21 
22     vector<Point> y(x); //基于vector<Point>对象x构建对象y
23     cout << "\nx对象中所有点坐标信息:" << endl;
24     output(y);
25 
26     cout << "\n更新x对象:" << endl;
27     x.at(0).move(30, 50);        //更新对象x内索引为0的点对象坐标
28     x.push_back(Point(2, 2));  //向x对象末尾添加一个点对象
29 
30     cout << "\nx对象中所有点坐标信息:" << endl;
31     output(x);
32     cout << "\ny对象中所有点坐标信息:" << endl;
33     output(y);
34 }
35 
36 int main() {
37     test();
38 }
task1.cpp

实验任务2

#pragma once
#include<iostream>
using std::cout;
using std::endl;

class Point {
public:
    Point(int x0 = 0, int y0 = 0);
    ~Point() = default;

    int get_x() const;
    int get_y() const;
    void show() const;
    void move(int new_x, int new_y);

private:
    int x, y;
};

Point::Point(int x0, int y0) : x{ x0 }, y{ y0 } {
}

int Point::get_x() const {
    return x;
}

int Point::get_y() const {
    return y;
}

void Point::show() const {
    cout << "(" << x << "," << y << ")" << endl;
}

void Point::move(int new_x, int new_y) {
    x = new_x;
    y = new_y;
}
point.hpp
#pragma once
#include"point.hpp"
#include<cassert>
#include<iostream>

class vectorPoint {
public:
    vectorPoint(int n);
    ~vectorPoint();

    int get_size() const; // 获得当前动态数组内元素个数
    Point& at(int index); // 返回下标为index的元素引用
    Point& at(int index) const; // 返回下标为index的元素const引用

private:
    int size; // 动态数组的大小
    Point* ptr;
};

vectorPoint::vectorPoint(int n) : size{ n } {
    ptr = new Point[n];
}

vectorPoint::~vectorPoint() {
    delete[] ptr;
}

int vectorPoint::get_size() const {
    return size;
}

Point&vectorPoint::at(int index) {
    assert(index >= 0 && index < size); // 宏,在测试模式下工作。如果不满足条件,则程序终止
    return ptr[index];
}

Point&vectorPoint::at(int index) const {
    assert(index >= 0 && index < size);
    return ptr[index];
}
vectorPoint.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 }
point.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);           // 返回下标为index的元素引用
15     Point& at(int index) const;     // 返回下标为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 }
vectorPoint.hpp
#include "vectorPoint.hpp"
#include <iostream>

// 输出vectorPoint对象内的所有数据
void output(const vectorPoint& v) {
    for (auto i = 0; i < v.get_size(); ++i)
        v.at(i).show();
}

// 测试vectorPoint类:构造对象、复制构造对象
void test() {
    using namespace std;

    int n;
    cout << "输入vectorPoint对象中元素个数: ";
    cin >> n;

    vectorPoint x(n);
    cout << "x对象中所有点坐标信息: " << endl;
    output(x);

    vectorPoint y(x);
    cout << "\ny对象中所有点坐标信息: " << endl;
    output(y);

    cout << "\n更新x对象中点坐标信息......" << endl;
    x.at(0).move(30, 50);
    x.at(1).move(-1, -1);

    cout << "x对象中所有点坐标信息: " << endl;
    output(x);

    cout << "\ny对象中所有点坐标信息: " << endl;
    output(y);
}

int main() {
    test();
}
task3.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 // 测试代码
10 void test() {
11     int x = 3, y = 4;
12 
13     print(x, y);
14     swap1(x, y);        // 函数调用,注意:引用作为形参时,实参形式
15     print(x, y);
16 
17     cout << endl;
18 
19     x = 3, y = 4;
20     print(x, y);
21     swap2(&x, &y);        // 函数调用,注意:指针作为形参时,实参形式
22     print(x, y);
23 }
24 
25 int main() {
26     test();
27 }
28 
29 // 函数定义:交换两个变量(引用变量作为形参)
30 void swap1(int& rx, int& ry) {
31     int t;
32 
33     t = rx;  rx = ry;  ry = t;
34 }
35 
36 // 函数定义:交换两个变量(指针变量作为形参)
37 void swap2(int* px, int* py) {
38     int t;
39 
40     t = *px;  *px = *py;  *py = t;
41 }
42 
43 // 函数定义:输出两个变量(普通变量作为形参)
44 void print(int x, int y) {
45     std::cout << "x = " << x << ", y = " << y << "\n";
46 }
task4_1.cpp
 1 #include <iostream>
 2 #include <typeinfo>
 3 using namespace std;
 4 
 5 int main() {
 6     int a;
 7 
 8     int& ra = a;
 9     ra = 4;
10 
11     int* pa = &a;
12     *pa = 5;
13 
14     // 以十六进制形式输出普通变量a, 引用变量ra,指针变量pa的地址
15     cout << "&a = " << hex << &a << endl;
16     cout << "&ra = " << hex << &ra << endl;
17     cout << "&pa = " << hex << &pa << "\n\n";
18 
19     // 输出普通变量a, 引用变量ra,指针变量pa的值
20     cout << "a = " << a << endl;
21     cout << "ra = " << a << endl;
22     cout << "pa = " << hex << pa << endl;
23 
24     // 输出指针变量pa指向的变量的值
25     cout << "*pa = " << *pa << "\n\n";
26 
27     // 输出普通变量a,引用变量ra, 指针变量pa的类型信息
28     cout << "type a: " << typeid(a).name() << endl;
29     cout << "type ra: " << typeid(ra).name() << endl;
30     cout << "type pa: " << typeid(pa).name() << endl;
31 }
task4_2.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) // i是普通类型
16         i *= i;
17 }
18 
19 template<typename T>
20 void square2(T &x) {
21     for(auto &i: x)  // i是引用类型
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 }
task4_3.cpp

 

 

 实验任务五

 1 #pragma once
 2 #include <iostream>
 3 #include <cassert>
 4 
 5 using namespace std;
 6 
 7 class vectorInt {
 8 public:
 9     vectorInt(int n);
10     vectorInt(int n, int value);
11     vectorInt(const vectorInt& vi);
12     ~vectorInt();
13 
14     int get_size() const;
15     int& at(int index);
16     int& at(int index) const;
17 
18 private:
19     int* ptr;
20     int size;
21 };
22 
23 vectorInt::vectorInt(int n) : size{ n } {
24     ptr = new int[n];
25     cout << "constructor vectorInt(int n) called." << endl;
26 }
27 
28 vectorInt::vectorInt(int n, int value) : size{ n }, ptr{ new int[n] } {
29     for (auto i = 0; i < size; i++)
30         ptr[i] = value;
31     cout << "constructor vectorInt(int n, int value) called." << endl;
32 }
33 
34 vectorInt::vectorInt(const vectorInt& vi) : size{ vi.size }, ptr{ new int[size] } {
35     for (auto i = 0; i < size; i++)
36         ptr[i] = vi.ptr[i];
37     cout << "copy constructor called." << endl;
38 }
39 
40 
41 vectorInt::~vectorInt() {
42     delete[] ptr;
43     cout << "destructor called" << endl;
44 }
45 
46 int vectorInt::get_size() const {
47     return size;
48 }
49 
50 int& vectorInt::at(int index) {
51     assert(index >= 0 && index < size);
52     return ptr[index];
53 }
54 
55 int& vectorInt::at(int index) const {
56     assert(index >= 0 && index < size);
57     return ptr[index];
58 }
vectorInt.hpp
 1 #include "vectorInt.hpp"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 using std::cin;
 7 
 8 //函数output()的定义:遍历输出vectorInt对象内的所有元素
 9 void output(const vectorInt& vi) {
10     for (auto i = 0; i <= vi.get_size(); i++)
11         cout << vi.at(i) << ",";
12 }
13 
14 void test() {
15     int n;
16     cout << "输入vectorInt对象中元素个数:";
17     cin >> n;
18 
19     vectorInt x1(n);  //构造动态int数组对象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);  // 构造动态int数组对象x1,包含n个元素,每个元素初始值为42
26     cout << "vectorInt对象x2:";
27     output(x2);
28     vectorInt x3(x2);   //使用x2构造x3
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对象x:";
37     output(x2);
38     cout << "vectorInt对象x3:";
39     output(x3);
40 }
41 
42 int main() {
43     test();
44 }
task5.cpp

 实验任务6

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

 

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

相关文章

  • 实验3
    task11#include<iostream>2#include"point.hpp"3#include<vector>45usingstd::vector;6usingstd::cin;78voidoutput(constvector<Point>&v){9for(auto&t:v)10t.show();11}12voidtest(){......
  • std::sort 传入成员函数指针报错的解决方案
    问题引入有一个类A,A的某个成员函数需要对A的某些变量进行std::sort,同时要调用A的另一个成员函数作为比较器。如代码所示:structA{vector<int>pos={0,4,2,5,3};boolcmp(intx,inty){returnpos[x]<pos[y];}voiddemo(){vector<int>a={2......
  • 实验8:适配器模式
    实验8:适配器模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解适配器模式的动机,掌握该模式的结构;2、能够利用适配器模式解决实际问题。 [实验任务一]:双向适配器实现一个双向适配器,使得猫可以学狗叫,狗可以学猫抓老鼠。实验要求:1. 画出对应的类图;2.提......
  • 实验9:桥接模式
    本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解桥接模式的动机,掌握该模式的结构;2、能够利用桥接模式解决实际问题。 [实验任务一]:两个维度的桥接模式用桥接模式实现在路上开车这个问题,其中,车可以是car或bus,路可以是水泥路或沥青路。实验要求:1. 画出对应......
  • 实验7:单例模式
    实验7:单例模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解单例模式的动机,掌握该模式的结构;2、能够利用单列模式解决实际问题。 [实验任务一]:学号的单一仿照课堂的身份证的例子,实现每个同学仅有一个学号这一问题。实验要求:1. 画出对应的类图;2.提交......
  • 实验6:原型模式
    实验6:原型模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解原型模式的动机,掌握该模式的结构;2、能够利用原型模式解决实际问题。 [实验任务一]:向量的原型用C++完成数学中向量的封装,其中,用指针和动态申请支持向量长度的改变,使用浅克隆和深克隆复制向量类,比......
  • 力扣905 按奇偶排序数组 C++ 双指针+一次遍历
    905.按奇偶排序数组classSolution{public:vector<int>sortArrayByParity(vector<int>&nums){inti=0,j=nums.size()-1;while(i<nums.size()-1&&i<j){while(i<j&&(nums[i]%2==0))i++;......
  • C++_14_常量指针—this指针 - 重写版
    常量指针—this指针this指针:成员函数一般都会拥有一个常量指针(this),指向调用函数的对象,储存的是改对象的首地址(注意:静态成员函数是没有this指针的)//标准写法classbook{public:book(){this->price=0.0;this->title=NULL;}private:doubleprice;char......
  • C++_28_空指针、野指针、智能指针 - 重写版
     野指针:没有明确执向的指针,可能会胡乱指向任意一个变量;编程的时候绝不允许出现野指针;int*p;//错误,未初始化指向一块地址,会变成野指针如果确实需要先不指定到需要使用的内存,那就先定义一个空指针;-------》宁愿指向为空也不要为野;因为野指针你把握不住; 空指针:让指针指向N......
  • 实验三 类与数组,指针
    Task1:point.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;voidm......