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