实验任务1
task1.cpp:
1 // 标准库string, vector, array基础用法 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 #include <array> 6 // 函数模板 7 // 对满足特定条件的序列类型T对象,使用范围for输出 8 template<typename T> 9 void output1(const T &obj) { 10 for(auto i: obj) 11 std::cout << i << ", "; 12 std::cout << "\b\b \n"; 13 } 14 // 函数模板 15 // 对满足特定条件的序列类型T对象,使用迭代器输出 16 template<typename T> 17 void output2(const T &obj) { 18 for(auto p = obj.begin(); p != obj.end(); ++p) 19 std::cout << *p << ", "; 20 std::cout << "\b\b \n"; 21 } 22 // array模板类基础用法 23 void test_array() { 24 using namespace std; 25 array<int, 5> x1; // 创建一个array对象,包含5个int元素,未初始化 26 cout << "x1.size() = " << x1.size() << endl; // 输出元素个数 27 x1.fill(42); // 把x1的所有元素都用42填充 28 x1.at(0) = 999; // 把下标为0的元素值修改为999 29 x1[4] = -999; // 把下表为4的元素值修改为-999 30 cout << "x1: "; 31 output1(x1); // 调用模板函数output1输出x1 32 cout << "x1: "; 33 output2(x1); // 调用模板函数output1输出x1 34 array<int, 5> x2{x1}; 35 cout << boolalpha << (x1 == x2) << endl; 36 x2.fill(22); 37 cout << "x2: "; 38 output1(x2); 39 swap(x1, x2); // 交换array对象x1, x2 40 cout << "x1: "; 41 output1(x1); 42 cout << "x2: "; 43 output1(x2); 44 } 45 // vector模板类基础用法 46 void test_vector() { 47 using namespace std; 48 vector<int> v1; 49 cout << v1.size() << endl; // 输出目前元素个数 50 cout << v1.max_size() << endl; // 输出元素个数之最大可能个数 51 v1.push_back(55); // 在v1末尾插入元素 52 cout << "v1: "; 53 output1(v1); 54 vector<int> v2 {1, 0, 5, 2}; 55 v2.pop_back(); // 从v2末尾弹出一个元素 56 v2.erase(v2.begin()); // 删除v2.begin()位置的数据项 57 v2.insert(v2.begin(), 999); // 在v1.begin()之前的位置插入 58 v2.insert(v2.end(), -999); // 在v1.end()之前的位置插入 59 cout << v2.size() << endl; 60 cout << "v2: "; 61 output2(v2); 62 vector<int> v3(5, 42); //创建vector对象,包含5个元素,每个元素值都是42 63 cout << "v3: "; 64 output1(v3); 65 vector<int> v4(v3.begin(), v3.end()-2); // 创建vector对象,以v3对象的[v3.begin(), v3.end()-2)区间作为元素值 66 cout << "v4: "; 67 output1(v4); 68 } 69 // string类基础用法 70 void test_string() { 71 using namespace std; 72 string s1{"oop"}; 73 cout << s1.size() << endl; 74 for(auto &i: s1) 75 i -= 32; 76 s1 += "2023"; 77 s1.append(", hello"); 78 cout << s1 << endl; 79 } 80 int main() { 81 using namespace std; 82 83 cout << "===========测试1: array模板类基础用法===========" << endl; 84 test_array(); 85 86 cout << "\n===========测试2: vector模板类基础用法===========" << endl; 87 test_vector(); 88 89 cout << "\n===========测试3: string类基础用法===========" << endl; 90 test_string(); 91 }
运行测试截图:
实验任务2
task2.cpp:
1 #include <iostream> 2 #include <complex> 3 // 测试标准库提供的复数类模板complex 4 void test_std_complex() { 5 using namespace std; 6 complex<double> c1{3, 4}, c2{4.5}; 7 const complex<double> c3{c2}; 8 cout << "c1 = " << c1 << endl; 9 cout << "c2 = " << c2 << endl; 10 cout << "c3 = " << c3 << endl; 11 cout << "c3.real = " << c3.real() << ", " << "c3.imag = " << c3.imag() 12 << endl; 13 cout << "c1 + c2 = " << c1 + c2 << endl; 14 cout << "c1 - c2 = " << c1 - c2 << endl; 15 cout << "abs(c1) = " << abs(c1) << endl; // abs()是标准库数学函数,对复数取模 16 cout << boolalpha; // 设置bool型值以true/false方式输出 17 cout << "c1 == c2: " << (c1 == c2) << endl; 18 cout << "c3 == c2: " << (c3 == c2) << endl; 19 complex<double> c4 = 2; 20 cout << "c4 = " << c4 << endl; 21 c4 += c1; 22 cout << "c4 = " << c4 << endl; 23 } 24 int main() { 25 test_std_complex(); 26 }
运行测试截图:
实验任务3
task3.cpp:
1 // 一个简单的类T:定义、使用 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 // 类T的声明 6 class T { 7 public: 8 T(int x = 0, int y = 0); // 带有默认形值的构造函数 9 T(const T &t); // 复制构造函数 10 T(T &&t); // 移动构造函数 11 ~T(); // 析构函数 12 void set_m1(int x); // 设置T类对象的数据成员m1 13 int get_m1() const; // 获取T类对象的数据成员m1 14 int get_m2() const; // 获取T类对象的数据成员m2 15 void display() const; // 显示T类对象的信息 16 friend void func(); // 声明func()为T类友元函数 17 private: 18 int m1, m2; 19 public: 20 static void disply_count(); // 类方法,显示当前T类对象数目 21 public: 22 static const string doc; // 类属性,用于描述T类 23 static const int max_count; // 类属性,用于描述T类对象的上限 24 private: 25 static int count; // 类属性,用于描述当前T类对象数目 26 }; 27 // 类的static数据成员:类外初始化 28 const string T::doc{"a simple class"}; 29 const int T::max_count = 99; 30 int T::count = 0; 31 // 类T的实现 32 T::T(int x, int y): m1{x}, m2{y} { 33 ++count; 34 cout << "constructor called.\n"; 35 } 36 T::T(const T &t): m1{t.m1}, m2{t.m2} { 37 ++count; 38 cout << "copy constructor called.\n"; 39 } 40 T::T(T &&t): m1{t.m1}, m2{t.m2} { 41 ++count; 42 cout << "move constructor called.\n"; 43 } 44 T::~T() { 45 --count; 46 cout << "destructor called.\n"; 47 } 48 void T::set_m1(int x) { 49 m1 = x; 50 } 51 int T::get_m1() const { 52 return m1; 53 } 54 int T::get_m2() const { 55 return m2; 56 } 57 void T::display() const { 58 cout << m1 << ", " << m2 << endl; 59 } 60 // 类方法 61 void T::disply_count() { 62 cout << "T objects: " << count << endl; 63 } 64 // 友元函数func():实现 65 void func() { 66 T t1; 67 t1.set_m1(55); 68 t1.m2 = 77; // 虽然m2是私有成员,依然可以直接访问 69 t1.display(); 70 } 71 // 测试 72 void test() { 73 cout << "T class info: " << T::doc << endl; 74 cout << "T objects max_count: " << T::max_count << endl; 75 T::disply_count(); 76 T t1; 77 t1.display(); 78 t1.set_m1(42); 79 T t2{t1}; 80 t2.display(); 81 T t3{std::move(t1)}; 82 t3.display(); 83 t1.display(); 84 T::disply_count(); 85 } 86 // 主函数 87 int main() { 88 cout << "============测试类T============" << endl; 89 test(); 90 cout << endl; 91 cout << "============测试友元函数func()============" << endl; 92 func(); 93 }
运行测试截图:
实验任务4
task4.cpp:
1 #include <iostream> 2 #include <string> 3 #include <iomanip> 4 using namespace std; 5 // 矩形类Rect的定义 6 class Rect { 7 static int count; // 静态成员变量,记录矩形对象的数量 8 public: 9 double length; 10 double width; 11 static string doc; // 静态成员变量,矩形类的信息 12 Rect() : length(2.00), width(1.00) 13 { 14 count++; 15 } 16 Rect(double l, double w) : length(l), width(w) 17 { 18 count++; 19 } 20 Rect(const Rect& r) : length(r.length), width(r.width) 21 { 22 count++; 23 } 24 ~Rect() 25 { 26 count--; 27 } 28 void resize(double l, double w) 29 { 30 length = l; 31 width = w; 32 } 33 void resize(double s) 34 { 35 length = s; 36 width = s; 37 } 38 double area() const 39 { 40 return length * width; 41 } 42 double perimeter() const 43 { 44 return 2 * (length + width); 45 } 46 47 static int size_info() 48 { 49 return count; 50 } 51 }; 52 int Rect::count = 0; 53 string Rect::doc = "a simple Rect class"; 54 // 普通函数:输出矩形信息 55 void output(const Rect &r) { 56 cout << "矩形信息: " << endl; 57 cout << fixed << setprecision(2); 58 cout << "长: " << r.length << endl; 59 cout << "宽: " << r.width << endl; 60 cout << "面积: " << r.area() << endl; 61 cout << "周长: " << r.perimeter() << endl; 62 } 63 64 // 测试代码 65 void test() { 66 cout << "矩形类信息: " << Rect::doc << endl; 67 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 68 Rect r1; 69 output(r1); 70 Rect r2(4, 3); 71 output(r2); 72 Rect r3(r2); 73 r3.resize(2); 74 output(r3); 75 r3.resize(5, 2); 76 output(r3); 77 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 78 } 79 80 // 主函数 81 int main() { 82 test(); 83 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 84 return 0; 85 }
运行测试截图:
实验任务5
task5.cpp:
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 // 复数类Complex的定义 5 class Complex { 6 private: 7 double real; // 实部 8 double imag; // 虚部 9 public: 10 Complex() : real(0), imag(0) {} 11 Complex(double r) : real(r), imag(0) {} 12 Complex(double r, double i) : real(r), imag(i) {} 13 Complex(const Complex& c) : real(c.real), imag(c.imag) {} 14 double get_imag() const { 15 return imag; 16 } 17 void show() const { 18 cout << real; 19 if (imag >= 0) { 20 cout << " + " << imag << "i"; 21 } else { 22 cout << " - " << -imag << "i"; 23 } 24 } 25 friend double abs(const Complex& c); 26 friend bool is_equal(const Complex& c1, const Complex& c2); 27 friend Complex add(const Complex& c1, const Complex& c2); 28 void add(const Complex& c) { 29 real += c.real; 30 imag += c.imag; 31 } 32 Complex& operator+=(const Complex& c) { 33 add(c); 34 return *this; 35 } 36 }; 37 // 求复数的绝对值 38 double abs(const Complex& c) { 39 return sqrt(c.real * c.real + c.imag * c.imag); 40 } 41 // 判断两个复数是否相等 42 bool is_equal(const Complex& c1, const Complex& c2) { 43 return c1.real == c2.real && c1.imag == c2.imag; 44 } 45 // 复数相加 46 Complex add(const Complex& c1, const Complex& c2) { 47 Complex result; 48 result.real = c1.real + c2.real; 49 result.imag = c1.imag + c2.imag; 50 return result; 51 } 52 53 // 复数类Complex的测试 54 void test() { 55 Complex c1(3, -4); 56 const Complex c2(4.5); 57 Complex c3(c1); 58 cout << "c1 = "; 59 c1.show(); 60 cout << endl; 61 cout << "c2 = "; 62 c2.show(); 63 cout << endl; 64 cout << "c2.imag = " << c2.get_imag() << endl; 65 cout << "c3 = "; 66 c3.show(); 67 cout << endl; 68 cout << "abs(c1) = "; 69 cout << abs(c1) << endl; 70 cout << boolalpha; 71 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 72 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 73 Complex c4; 74 c4 = add(c1, c2); 75 cout << "c4 = c1 + c2 = "; 76 c4.show(); 77 cout << endl; 78 c1 += c2; 79 cout << "c1 += c2, " << "c1 = "; 80 c1.show(); 81 cout << endl; 82 } 83 84 int main() { 85 test(); 86 return 0; 87 }
运行测试截图: