#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; }

#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() cons 复制代码 #include "vectorPoint.hpp" #include <iostream> void output(const vectorPoint &v) { for(auto i = 0; i < v.get_size(); ++i) v.at(i).show(); } 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; 复制代码 t { cout << "(" << x << ", " << y << ")" << endl;} void Point::move(int new_x, int new_y) { x = new_x; y = new_y;}

#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();}

#include <iostream> #include <typeinfo> using namespace std; int main() { int a; int &ra = a; ra = 4; int *pa = &a; *pa = 5; cout << "&a = " << hex << &a << endl; cout << "&ra = " << hex << &ra << endl; cout << "&pa = " << hex << &pa << "\n\n"; cout << "a = " << a << endl; cout << "ra = " << a << endl; cout << "pa = " << hex << pa << endl; cout << "*pa = " << *pa << "\n\n"; cout << "type a: " << typeid(a).name() << endl; cout << "type ra: " << typeid(ra).name() << endl; cout << "type pa: " << typeid(pa).name() << endl;}

#include "vectorInt.hpp" #include <iostream> using std::cout; using std::cin; using std::endl; // 函数output()的定义:遍历输出vectorInt对象内的所有元素 void output(const vectorInt& vi) { for (auto i = 0;i < vi.get_size();i++) cout << vi.at(i); } void test() { int n; cout << "输入vectorInt对象中元素个数: "; cin >> n; vectorInt x1(n); // 构造动态int数组对象x1,包含n个元素,不对元素初始化 for (auto i = 0; i < n; ++i) x1.at(i) = i * i; cout << "vectorInt对象x1: "; output(x1); vectorInt x2(n, 42); // 构造动态int数组对象x1,包含n个元素,每个元素初始值为42 cout << "vectorInt对象x2: "; output(x2); vectorInt x3(x2); // 使用x2构造x3 cout << "vectorInt对象x3: "; output(x3); cout << "更新vectorInt对象x2......\n"; x2.at(0) = 77; x2.at(1) = -999; cout << "vectorInt对象x2: "; output(x2); cout << "vectorInt对象x3: "; output(x3); } int main() { test(); }

#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(); }
