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

实验三 类与指针、数组

时间:2023-11-06 10:00:11浏览次数:24  
标签:Matrix const vectorPoint Point int void 实验 数组 指针

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

 

 1 1 #pragma once
 2  2 
 3  3 #include <iostream>
 4  4 using std::cout;
 5  5 using std::endl;
 6  6 
 7  7 class Point {
 8  8 public:
 9  9     Point(int x0 = 0, int y0 = 0);
10 10     ~Point() = default;
11 11 
12 12     int get_x() const;
13 13     int get_y() const;
14 14     void show() const;
15 15     void move(int new_x, int new_y);
16 16 
17 17 private:
18 18     int x, y;
19 19 };
20 20 
21 21 Point::Point(int x0, int y0): x{x0}, y{y0} {   
22 22 }
23 23 
24 24 int Point::get_x() const {
25 25     return x;
26 26 }
27 27 
28 28 int Point::get_y() const {
29 29     return y;
30 30 }
31 31 
32 32 void Point::show() const {
33 33     cout << "(" << x << ", " << y << ")" << endl;
34 34 }
35 35 
36 36 void Point::move(int new_x, int new_y) {
37 37     x = new_x;
38 38     y = new_y;
39 39 }
Point.hpp
 1 1 #pragma once
 2  2 #include "point.hpp"
 3  3 #include <cassert>
 4  4 #include <iostream>
 5  5 class vectorPoint {
 6  6 public:
 7  7     vectorPoint(int n);
 8  8     ~vectorPoint();
 9  9     int get_size() const; // 获得当前动态数组内元素个数
10 10     Point& at(int index); // 返回下标为index的元素引用
11 11     Point& at(int index) const; // 返回下标为index的元素const引用
12 12 private:
13 13     int size; // 动态数组的大小
14 14     Point* ptr;
15 15 };
16 16 vectorPoint::vectorPoint(int n) : size{ n } {
17 17     ptr = new Point[n];
18 18 }
19 19 vectorPoint::~vectorPoint() {
20 20     delete[] ptr;
21 21 }
22 22 int vectorPoint::get_size() const {
23 23     return size;
24 24 }
25 25 Point& vectorPoint::at(int index) {
26 26     assert(index >= 0 && index < size); // 宏,在测试模式下工作。如果不满足条件,则程序终止
27 27         return ptr[index];
28 28 }
29 29 Point& vectorPoint::at(int index) const {
30 30     assert(index >= 0 && index < size);
31 31     return ptr[index];
32 32 }
vectorpoint.hpp
 1 1 #include "vectorPoint.hpp"
 2  2 #include <iostream>
 3  3 // 输出vectorPoint对象内的所有数据
 4  4 void output(const vectorPoint& v) {
 5  5     for (auto i = 0; i < v.get_size(); ++i)
 6  6         v.at(i).show();
 7  7 }
 8  8 // 测试vectorPoint类:构造对象、复制构造对象
 9  9 void test() {
10 10     using namespace std;
11 11     int n;
12 12     cout << "输入vectorPoint对象中元素个数: ";
13 13     cin >> n;
14 14     vectorPoint x(n);
15 15     cout << "x对象中所有点坐标信息: " << endl;
16 16     output(x);
17 17     vectorPoint y(x);
18 18     cout << "\ny对象中所有点坐标信息: " << endl;
19 19     output(y);
20 20     cout << "\n更新x对象中点坐标信息......" << endl;
21 21     x.at(0).move(30, 50);
22 22     x.at(1).move(-1, -1);
23 23     cout << "x对象中所有点坐标信息: " << endl;
24 24     output(x);
25 25     cout << "\ny对象中所有点坐标信息: " << endl;
26 26     output(y);
27 27 }
28 28 int main() {
29 29     test();
30 30 }
task2.cpp

 

 1 1 #pragma once
 2  2 
 3  3 #include <iostream>
 4  4 using std::cout;
 5  5 using std::endl;
 6  6 
 7  7 class Point {
 8  8 public:
 9  9     Point(int x0 = 0, int y0 = 0);
10 10     ~Point() = default;
11 11 
12 12     int get_x() const;
13 13     int get_y() const;
14 14     void show() const;
15 15     void move(int new_x, int new_y);
16 16 
17 17 private:
18 18     int x, y;
19 19 };
20 20 
21 21 Point::Point(int x0, int y0): x{x0}, y{y0} {   
22 22 }
23 23 
24 24 int Point::get_x() const {
25 25     return x;
26 26 }
27 27 
28 28 int Point::get_y() const {
29 29     return y;
30 30 }
31 31 
32 32 void Point::show() const {
33 33     cout << "(" << x << ", " << y << ")" << endl;
34 34 }
35 35 
36 36 void Point::move(int new_x, int new_y) {
37 37     x = new_x;
38 38     y = new_y;
39 39 }
point.hpp
 1 1 #pragma once
 2  2 
 3  3 #include "point.hpp"
 4  4 #include <cassert>
 5  5 #include <iostream>
 6  6 
 7  7 class vectorPoint{
 8  8 public:
 9  9     vectorPoint(int n);
10 10     vectorPoint(const vectorPoint &vp);
11 11     ~vectorPoint();
12 12     
13 13     int get_size() const;         
14 14     Point& at(int index); 
15 15     Point& at(int index) const;  
16 16 
17 17 private:
18 18     int size;
19 19     Point *ptr;
20 20 };
21 21 
22 22 vectorPoint::vectorPoint(int n) : size{n} {
23 23     ptr = new Point[n];
24 24 }
25 25 
26 26 vectorPoint::vectorPoint(const vectorPoint &vp): size{vp.size}, ptr{new Point[size]} {
27 27     for(auto i = 0; i < size; ++i)
28 28         ptr[i] = vp.ptr[i];
29 29 }
30 30 
31 31 vectorPoint::~vectorPoint() {
32 32     delete[] ptr;
33 33 }
34 34 
35 35 int vectorPoint::get_size() const {
36 36     return size;
37 37 }
38 38 
39 39 Point& vectorPoint::at(int index) {
40 40     assert(index >= 0 && index < size);  
41 41     return ptr[index];
42 42 }
43 43 
44 44 Point& vectorPoint::at(int index) const {
45 45     assert(index >= 0 && index < size);  
46 46     return ptr[index];
47 47 }
vectorPoint.hpp
 1 1 #include "vectorPoint.hpp"
 2  2 #include <iostream>
 3  3 
 4  4 void output(const vectorPoint &v) {
 5  5     for(auto i = 0; i < v.get_size(); ++i)
 6  6         v.at(i).show();
 7  7 }
 8  8 
 9  9 void test() {
10 10     using namespace std;
11 11 
12 12     int n;
13 13     cout << "输入vectorPoint对象中元素个数: ";
14 14     cin >> n;
15 15 
16 16     vectorPoint x(n);
17 17     cout << "x对象中所有点坐标信息: " << endl;
18 18     output(x); 
19 19 
20 20     vectorPoint y(x);
21 21     cout << "\ny对象中所有点坐标信息: " << endl;
22 22     output(y); 
23 23 
24 24     cout << "\n更新x对象中点坐标信息......" << endl;
25 25     x.at(0).move(30, 50);
26 26     x.at(1).move(-1, -1);
27 27 
28 28     cout << "x对象中所有点坐标信息: " << endl;
29 29     output(x); 
30 30 
31 31     cout << "\ny对象中所有点坐标信息: " << endl;
32 32     output(y); 
33 33 }
34 34 
35 35 int main() {
36 36     test();
37 37 }
task3.cpp

 

 1 1 #include <iostream>
 2  2 using namespace std;
 3  3 // 函数声明
 4  4 void swap1(int& rx, int& ry); // 引用作为形参
 5  5 void swap2(int* px, int* py); // 指针作为形参
 6  6 void print(int x, int y); // 普通变量作为形参
 7  7 // 测试代码
 8  8 void test() {
 9  9     int x = 3, y = 4;
10 10     print(x, y);
11 11     swap1(x, y); // 函数调用,注意:引用作为形参时,实参形式
12 12     print(x, y);
13 13     cout << endl;
14 14     x = 3, y = 4;
15 15     print(x, y);
16 16     swap2(&x, &y); // 函数调用,注意:指针作为形参时,实参形式
17 17     print(x, y);
18 18 }
19 19 int main() {
20 20     test();
21 21 }
22 22 // 函数定义:交换两个变量(引用变量作为形参)
23 23 void swap1(int& rx, int& ry) {
24 24     int t;
25 25     t = rx; rx = ry; ry = t;
26 26 }
27 27 // 函数定义:交换两个变量(指针变量作为形参)
28 28 void swap2(int* px, int* py) {
29 29     int t;
30 30     t = *px; *px = *py; *py = t;
31 31 }
32 32 // 函数定义:输出两个变量(普通变量作为形参)
33 33 void print(int x, int y) {
34 34         std::cout << "x = " << x << ", y = " << y << "\n";
35 35 }
task4.1.cpp

 

 1 1 #include <iostream>
 2  2 #include <typeinfo>
 3  3 using namespace std;
 4  4 int main() {
 5  5     int a;
 6  6     int& ra = a;
 7  7     ra = 4;
 8  8     int* pa = &a;
 9  9     *pa = 5;
10 10     // 以十六进制形式输出普通变量a, 引用变量ra,指针变量pa的地址
11 11     cout << "&a = " << hex << &a << endl;
12 12     cout << "&ra = " << hex << &ra << endl;
13 13     cout << "&pa = " << hex << &pa << "\n\n";
14 14     // 输出普通变量a, 引用变量ra,指针变量pa的值
15 15     cout << "a = " << a << endl;
16 16     cout << "ra = " << a << endl;
17 17     cout << "pa = " << hex << pa << endl;
18 18     // 输出指针变量pa指向的变量的值
19 19     cout << "*pa = " << *pa << "\n\n";
20 20     // 输出普通变量a,引用变量ra, 指针变量pa的类型信息
21 21     cout << "type a: " << typeid(a).name() << endl;
22 22     cout << "type ra: " << typeid(ra).name() << endl;
23 23     cout << "type pa: " << typeid(pa).name() << endl;
24 24 }
task4.2.cpp

 

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

 

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

 

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

相关文章

  • 实验三:类与数组、指针。
    实验任务11#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point(intx0=0,inty0=0);10~Point()=default;1112intget_x()const;13intget_y()const;14......
  • 实验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......