point1.hpp
1 #pragma once 2 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 class Point { 8 public: 9 Point():x{0},y{0} {} 10 Point(int x0,int y0):x{x0},y{y0} {} 11 ~Point() = default; 12 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 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 }
task1.cpp
1 #include <iostream> 2 #include "point1.hpp" 3 #include <vector> 4 5 using std::vector; 6 using std::cin; 7 8 9 // 测试动态数组类接口 10 void test1() { 11 int n; 12 cin >> n; 13 14 vector<Point> x(n); // 创建一个vector<Point>对象,指定包含n个元素 15 x.at(0).show(); 16 x.at(0).move(30, 50); 17 x.at(0).show(); 18 } 19 20 // 测试复制构造 21 void test2() { 22 int n; 23 cin >> n; 24 25 vector<Point> x(n); 26 x.at(0).show(); 27 28 vector<Point> y(x); // 基于vector<Point>对象x构建对象y 29 y.at(0).show(); 30 31 x.at(0).move(30, 50); 32 33 x.at(0).show(); 34 y.at(0).show(); 35 } 36 37 int main() { 38 cout << "测试动态数组类接口...\n"; 39 test1(); 40 41 cout << "测试复制构造...\n"; 42 test2(); 43 }
point2.hpp
1 #pragma once 2 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 class Point { 8 public: 9 Point() :x{ 0 }, y{ 0 } {cout << "Point: default constructor called.\n"; } 10 Point(int x0, int y0) :x{ x0 }, y{ y0 } {cout << "Point: constructor called.\n"; } 11 ~Point() { cout << "Point: destructor called.\n"; } 12 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 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 }
vectorPoint.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <cassert> 5 #include "point2.hpp" 6 7 class vectorPoint { 8 public: 9 vectorPoint(int n); 10 vectorPoint(const vectorPoint& vp); // 复制构造函数 11 ~vectorPoint(); 12 13 Point& at(int index); // 返回下标为index的元素引用 14 15 private: 16 int size; // 动态数组的大小 17 Point* p; 18 }; 19 20 vectorPoint::vectorPoint(int n) :size{ n } { 21 cout << "dynamic create array..." << endl; 22 p = new Point[n]; 23 } 24 25 vectorPoint::vectorPoint(const vectorPoint& vp): size{ vp.size }{ 26 cout << "copy array..." << endl; 27 28 p = new Point[size]; 29 30 for (auto i = 0; i < size; i++) 31 p[i] = vp.p[i]; 32 } 33 34 vectorPoint::~vectorPoint() { 35 cout << "dynamic delete array..." << endl; 36 delete[] p; 37 } 38 39 Point& vectorPoint::at(int index) { 40 assert(index >= 0 && index < size); // 宏,通常测试用。如果不满足条件,终止程序 41 return p[index]; 42 }
task2.cpp
1 #include <iostream> 2 #include <cstdlib> 3 #include "point2.hpp" 4 #include "vectorPoint.hpp" 5 6 using std::cin; 7 8 // 测试动态数组类接口 9 void test1() { 10 int n; 11 cin >> n; 12 13 vectorPoint x(n); // 创建一个vectoePoint对象,指定包含n个元素 14 x.at(0).show(); 15 x.at(0).move(30, 50); 16 x.at(0).show(); 17 } 18 19 // 测试复制构造 20 void test2() { 21 int n; 22 cin >> n; 23 24 vectorPoint x(n); 25 x.at(0).show(); 26 27 vectorPoint y(x); 28 y.at(0).show(); 29 30 x.at(0).move(30, 50); 31 32 x.at(0).show(); 33 y.at(0).show(); 34 } 35 36 int main() { 37 cout << "测试动态数组接口...\n"; 38 test1(); 39 40 system("pause"); 41 42 cout << "测试复制构造...\n"; 43 test2(); 44 }
task3_1.cpp
1 #include <iostream> 2 3 // 函数声明 4 void swap1(int& rx, int& ry); // 引用作为形参 5 void swap2(int* px, int* py); // 指针作为形参 6 void print(int x, int y); // 普通变量作为形参 7 8 int main() { 9 using namespace std; 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 25 // 函数定义 26 void swap1(int& rx, int& ry) { 27 int t; 28 t = rx; 29 rx = ry; 30 ry = t; 31 } 32 33 void swap2(int* px, int* py) { 34 int t; 35 36 t = *px; 37 *px = *py; 38 *py = t; 39 } 40 41 void print(int x, int y) { 42 std::cout << "x = " << x << ", y = " << y << std::endl; 43 }
task3_2.cpp
1 #include <typeinfo> 2 #include <iostream> 3 4 int main() { 5 using namespace std; 6 7 int a; 8 9 int& ra = a; 10 ra = 4; 11 12 int* pa = &a; 13 *pa = 5; 14 15 // 以十六进制形式输出普通变量a,引用变量ra,指针变量pa的地址 16 cout << "&a = " << hex << &a << endl; 17 cout << "&ra = " << hex << &ra << endl; 18 cout << "&pa = " << hex << &pa << "\n\n"; 19 20 // 输出普通变量a,应用变量ra,指针变量pa的值 21 cout << "a = " << a << endl; 22 cout << "ra = " << a << endl; 23 cout << "pa = " << hex << pa << endl; 24 25 // 输出指针变量pa指向的变量的值 26 cout << "*pa = " << *pa << "\n\n"; 27 28 // 输出普通变量a,应用变量ra,指针变量pa的类型信息 29 cout << "typa a: " << typeid(a).name() << endl; 30 cout << "type ra: " << typeid(ra).name() << endl; 31 cout << "type pa: " << typeid(pa).name() << endl; 32 }
task3_3.cpp
1 #include <iostream> 2 #include <vector> 3 4 template<typename T> 5 void output(const T& x) { 6 for (auto i : x) 7 std::cout << i << ", "; 8 std::cout << "\b\b \n"; 9 } 10 11 template<typename T> 12 void square1(T& x) { 13 for (auto& i : x) // i是引用类型 14 i *= i; 15 } 16 17 template<typename T> 18 void square2(T& x) { 19 for (auto i : x) // i是普通类型 20 i *= i; 21 } 22 23 int main() { 24 using namespace std; 25 vector<int> x{ 1,2,3,4,5 }; 26 vector<int> y{ x }; 27 28 output(x); 29 square1(x); 30 output(x); 31 32 output(y); 33 square2(y); 34 output(y); 35 }
utils.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 #include <cstdlib> 6 using std::cout; 7 using std::string; 8 9 // 函数声明 10 void print_blank_lines(int n); // 打印n行空白行 11 void print_spaces(int n); // 打印n个空格 12 void Print_ball(int x, int y); // 在坐标(x,y)处打印小球 13 void set_color(string bg, string fg); // 设置屏幕背景色、前景色 14 15 // 函数定义 16 // 打印n行空行 17 void print_blank_lines(int n) { 18 for (int line = 1; line <= n; line++) 19 cout << "\n"; 20 } 21 22 // 打印n个空格 23 void print_spaces(int n) { 24 for (int i = 1; i <= n; i++) 25 cout << " "; 26 } 27 28 // 在坐标(x,y)处打印小球 29 void print_ball(int x, int y) { 30 // 清屏 31 system("cls"); 32 33 // 打印y-1行空行 34 print_blank_lines(y - 1); 35 36 // 打印x-1个空格 37 print_spaces(x - 1); 38 39 // 打印小球 40 cout << "O" << "\n"; 41 } 42 43 // 设置屏幕背景色、前景色 44 void set_color(string bg, string fg) { 45 string color = "color "; 46 47 color += bg; 48 color += fg; 49 system( color.c_str() ); 50 }
ball.hpp
1 #pragma once 2 3 #include <iostream> 4 #include "utils.hpp" 5 6 using std::cout; 7 using std::endl; 8 9 const int SIZE_X = 50; // 小球x轴移动范围0 ~ SIZE_X - 1 10 const int SIZE_Y = 50; // 小球y轴移动范围0 ~ SIZE_Y - 1 11 12 class Ball { 13 public: 14 Ball(int x = 0, int y = 0); // 在坐标(x,y)处构造一个小球 15 void left(int step = 1); // 左移step 16 void right(int step = 1); // 右移step 17 void up(int step = 1); // 上移step 18 void down(int step = 1); // 下移step 19 20 private: 21 int x; // x坐标 22 int y; // y坐标 23 }; 24 25 Ball::Ball(int x0, int y0) :x{ x0 }, y{ y0 }{ 26 print_ball(x, y); 27 } 28 29 void Ball::left(int step) { 30 x -= step; 31 32 if (x <= 0) 33 x = 0; 34 35 // 在更新后的坐标(x,y)处打印小球 36 print_ball(x, y); 37 } 38 39 void Ball::right(int step) { 40 x += step; 41 42 if (x >= SIZE_X) 43 x = SIZE_X; 44 45 // 在更新后的坐标(x,y)处打印小球 46 print_ball(x, y); 47 } 48 49 void Ball::up(int step) { 50 y -= step; 51 52 if (y <= 0) 53 y = 0; 54 55 // 在更新后的坐标(x,y)处打印小球 56 print_ball(x, y); 57 } 58 59 void Ball::down(int step) { 60 y += step; 61 62 if (y >= SIZE_Y) 63 y = SIZE_Y; 64 65 // 在更新后的坐标(x,y)处打印小球 66 print_ball(x, y); 67 }
canvas.hpp
1 #pragma once 2 3 #include <iostream> 4 #include "utils.hpp" 5 using std::string; 6 7 class Canvas { 8 public: 9 Canvas(string bg0 = "0", string fg0 = "A"); 10 void change_bg(string bg_new); // 改变背景色 11 void change_fg(string fg_new); // 改变前景色 12 void change_color(string bg_new, string fg_new); // 同时改变前景色和背景色 13 14 private: 15 string bg; // 背景色 16 string fg; // 前景色 17 }; 18 19 Canvas::Canvas(string bg0, string fg0) :bg{ bg0 }, fg{ fg0 }{ 20 set_color(bg, fg); 21 } 22 23 void Canvas::change_bg(string new_bg) { 24 bg = new_bg; 25 set_color(bg, fg); 26 } 27 28 void Canvas::change_fg(string new_fg) { 29 fg = new_fg; 30 set_color(bg, fg); 31 } 32 33 void Canvas::change_color(string new_bg, string new_fg) { 34 bg = new_bg; 35 fg = new_fg; 36 set_color(bg, fg); 37 }
task4.cpp
1 #include <iostream> 2 #include "ball.hpp" 3 #include "canvas.hpp" 4 5 void test() { 6 Canvas canvas; 7 8 Ball ball1(10, 10); 9 system("pause"); 10 11 ball1.left(5); 12 system("pause"); 13 canvas.change_fg("E"); // 更新画布前景色 14 system("pause"); 15 16 canvas.change_bg("D"); // 更新画布背景色 17 system("pause"); 18 } 19 20 int main() { 21 test(); 22 }
vectorInt.hpp
1 #pragma once 2 #include <iostream> 3 #include <cassert> 4 5 using std::cout; 6 using std::endl; 7 8 class vectorInt { 9 public: 10 vectorInt(int n); 11 vectorInt(int n, int value); 12 vectorInt(const vectorInt& vi); 13 ~vectorInt(); 14 int& at(int index); 15 int get_size(); 16 friend void output(vectorInt& vi); 17 18 private: 19 int size; 20 int* p; 21 }; 22 23 // 函数定义 24 vectorInt::vectorInt(int n) :size{ n } { 25 cout << "constructor 1 called." << endl; 26 p = new int[n]; 27 } 28 29 vectorInt::vectorInt(int n, int value) : size{ n } { 30 cout << "constructor 2 called." << endl; 31 p = new int[n]; 32 for (auto i = 0; i < n; i++) 33 p[i] = value; 34 } 35 36 vectorInt::vectorInt(const vectorInt& vi) :size{ vi.size } { 37 cout << "copy constructor called." << endl; 38 p = new int[size]; 39 for (auto i = 0; i < size; i++) 40 p[i] = vi.p[i]; 41 } 42 43 vectorInt::~vectorInt() { 44 cout << "destructor called." << endl; 45 } 46 47 int& vectorInt::at(int index) { 48 assert(index >= 0 && index < size); 49 return p[index]; 50 } 51 52 int vectorInt::get_size() { 53 return size; 54 } 55 56 void output(vectorInt& vi) { 57 cout << vi.at(0); 58 for (auto i = 1; i < vi.get_size(); i++) 59 cout << ", " << vi.at(i); 60 cout << endl; 61 }
task5.cpp
1 #include <iostream> 2 #include <cassert> 3 #include "vectorInt.hpp" 4 5 void test() { 6 using namespace std; 7 8 int n; 9 cin >> n; 10 vectorInt x1(n); 11 for (auto i = 0; i < n; i++) 12 x1.at(i) = i * i; 13 14 output(x1); 15 16 vectorInt x2(n, 42); 17 vectorInt x3(x2); 18 19 output(x2); 20 output(x3); 21 22 x2.at(0) = 77; 23 24 output(x2); 25 output(x3); 26 } 27 28 int main() { 29 test(); 30 }
Matrix.hpp
1 #pragma once 2 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 class Matrix { 9 public: 10 Matrix(int n); // 构造函数,构造一个n*n的矩阵 11 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 12 Matrix(const Matrix& X); // 复制构造函数,使用已有矩阵X构造 13 ~Matrix(); // 析构函数 14 15 void set(const double* pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 16 void set(int i, int j, int value); // 设置矩阵第i行第j列元素值为value 17 double& at(int i, int j); // 返回矩阵第i行第j列元素的引用 18 double at(int i, int j) const; // 返回矩阵第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* p; // 指向存放矩阵数据的内存块的首地址 27 }; 28 29 30 // 函数定义 31 Matrix::Matrix(int n) :lines{ n }, cols{ n }{ 32 p = new double[n * n]; 33 } 34 35 Matrix::Matrix(int n, int m) : lines{ n }, cols{ m }{ 36 p = new double[n * m]; 37 } 38 39 Matrix::Matrix(const Matrix& X) : lines{ X.lines }, cols{ X.cols }{ 40 p = new double[X.lines * X.cols]; 41 for (auto i = 0; i < X.lines * X.cols; i++) 42 p[i] = X.p[i]; 43 } 44 45 Matrix::~Matrix() { 46 delete[] p; 47 } 48 49 void Matrix::set(const double* pvalue) { 50 for (auto i = 0; i < lines * cols; i++) 51 p[i] = pvalue[i]; 52 } 53 54 void Matrix::set(int i, int j, int value) { 55 p[i * cols + j] = value; 56 } 57 58 double& Matrix::at(int i, int j) { 59 return p[i * cols + j]; 60 } 61 62 double Matrix::at(int i, int j) const { 63 return p[i * cols + j]; 64 } 65 66 int Matrix::get_lines() const { 67 return lines; 68 } 69 70 int Matrix::get_cols() const { 71 return cols; 72 } 73 74 void Matrix::print() const { 75 for (auto i = 0; i < lines; i++) { 76 for (auto j = 0; j < cols; j++) 77 cout << p[i * cols + j] << " "; 78 cout << endl; 79 } 80 }
task6.cpp
1 #include <iostream> 2 #include "Matrix.hpp" 3 4 void test() { 5 using namespace std; 6 7 double x[] = { 1,2,3,4,5,6 }; 8 9 Matrix m1(3, 2); 10 m1.set(x); 11 m1.print(); 12 cout << "the first line is: " << endl; 13 cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; 14 cout << endl; 15 16 Matrix m2(2, 3); 17 m2.set(x); 18 m2.print(); 19 cout << "the first line is: " << endl; 20 cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; 21 cout << endl; 22 23 Matrix m3(m2); 24 m3.set(0, 0, 999); 25 m3.print(); 26 } 27 28 int main() { 29 test(); 30 }
标签:std,const,Matrix,int,void,实验,数组,include,指针 From: https://www.cnblogs.com/xiao-shi/p/16863822.html