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