实验任务一:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <array> 5 template<typename T> 6 void output1(const T &obj) 7 { 8 for(auto i: obj) 9 std::cout << i << ", "; 10 std::cout << "\b\b \n"; 11 } 12 template<typename T> 13 void output2(const T &obj) 14 { 15 for(auto p = obj.begin(); p != obj.end(); ++p) 16 std::cout << *p << ", "; 17 std::cout << "\b\b \n"; 18 } 19 void test_array() 20 { 21 using namespace std; 22 array<int, 5> x1; 23 cout << "x1.size() = " << x1.size() << endl; 24 x1.fill(42); 25 x1.at(0) = 999; 26 x1[4] = -999; 27 cout << "x1: "; 28 output1(x1); 29 cout << "x1: "; 30 output2(x1); 31 array<int, 5> x2(x1); 32 cout << boolalpha << (x1 == x2) << endl; 33 x2.fill(22); 34 cout << "x2: "; 35 output1(x2); 36 swap(x1, x2); 37 cout << "x1: "; 38 output1(x1); 39 cout << "x2: "; 40 output1(x2); 41 } 42 void test_vector() 43 { 44 using namespace std; 45 vector<int> v1; 46 cout << v1.size() << endl; 47 cout << v1.max_size() << endl; 48 v1.push_back(55); 49 cout << "v1: "; 50 output1(v1); 51 vector<int> v2 {1, 0, 5, 2}; 52 v2.pop_back(); 53 v2.erase(v2.begin()); 54 v2.insert(v2.begin(), 999); 55 v2.insert(v2.end(), -999); 56 cout << v2.size() << endl; 57 cout << "v2: "; 58 output2(v2); 59 vector<int> v3(5, 42); 60 cout << "v3: "; 61 output1(v3); 62 vector<int> v4(v3.begin(), v3.end()-2); 63 cout << "v4: "; 64 output1(v4); 65 } 66 void test_string() 67 { 68 using namespace std; 69 string s1{"oop"}; 70 cout << s1.size() << endl; 71 for(auto &i: s1) 72 i -= 32; 73 s1 += "2023"; 74 s1.append(", hello"); 75 cout << s1 << endl; 76 } 77 int main() 78 { 79 using namespace std; 80 cout << "===========测试1: array模板类基础用法===========" << endl; 81 test_array(); 82 cout << "\n===========测试2: vector模板类基础用法===========" << endl; 83 test_vector(); 84 cout << "\n===========测试3: string类基础用法===========" << endl; 85 test_string(); 86 }View Code
实验任务二:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <complex> 3 4 void test_std_complex() 5 { 6 using namespace std; 7 complex<double> c1{3, 4}, c2{4.5}; 8 const complex<double> c3{c2}; 9 cout << "c1 = " << c1 << endl; 10 cout << "c2 = " << c2 << endl; 11 cout << "c3 = " << c3 << endl; 12 cout << "c3.real = " << c3.real() << ", " << "c3.imag = " << c3.imag() 13 << endl; 14 cout << "c1 + c2 = " << c1 + c2 << endl; 15 cout << "c1 - c2 = " << c1 - c2 << endl; 16 cout << "abs(c1) = " << abs(c1) << endl; 17 18 cout << boolalpha; 19 cout << "c1 == c2: " << (c1 == c2) << endl; 20 cout << "c3 == c2: " << (c3 == c2) << endl; 21 complex<double> c4 = 2; 22 cout << "c4 = " << c4 << endl; 23 c4 += c1; 24 cout << "c4 = " << c4 << endl; 25 } 26 int main() 27 { 28 test_std_complex(); 29 }View Code
实验任务三:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class T 5 { 6 public: 7 T(int x = 0, int y = 0); 8 T(const T &t); 9 T(T &&t); 10 ~T(); 11 void set_m1(int x); 12 int get_m1() const; 13 int get_m2() const; 14 void display() const; 15 friend void func(); 16 private: 17 int m1, m2; 18 public: 19 static void disply_count(); 20 public: 21 static const string doc; 22 static const int max_count; 23 private: 24 static int count; 25 }; 26 const string T::doc{"a simple class"}; 27 const int T::max_count = 99; 28 int T::count = 0; 29 T::T(int x, int y): m1{x}, m2{y} 30 { 31 ++count; 32 cout << "constructor called.\n"; 33 } 34 T::T(const T &t): m1{t.m1}, m2{t.m2} 35 { 36 ++count; 37 cout << "copy constructor called.\n"; 38 } 39 T::T(T &&t): m1{t.m1}, m2{t.m2} 40 { 41 ++count; 42 cout << "move constructor called.\n"; 43 } 44 T::~T() 45 { 46 --count; 47 cout << "destructor called.\n"; 48 } 49 void T::set_m1(int x) 50 { 51 m1 = x; 52 } 53 int T::get_m1() const 54 { 55 return m1; 56 } 57 int T::get_m2() const 58 { 59 return m2; 60 } 61 void T::display() const 62 { 63 cout << m1 << ", " << m2 << endl; 64 } 65 void T::disply_count() 66 { 67 cout << "T objects: " << count << endl; 68 } 69 void func() 70 { 71 T t1; 72 t1.set_m1(55); 73 t1.m2 = 77; 74 t1.display(); 75 } 76 void test() { 77 cout << "T class info: " << T::doc << endl; 78 cout << "T objects max_count: " << T::max_count << endl; 79 T::disply_count(); 80 T t1; 81 t1.display(); 82 t1.set_m1(42); 83 T t2{t1}; 84 t2.display(); 85 T t3{std::move(t1)}; 86 t3.display(); 87 t1.display(); 88 T::disply_count(); 89 } 90 int main() 91 { 92 cout << "============测试类T============" << endl; 93 test(); 94 cout << endl; 95 cout << "============测试友元函数func()============" << endl; 96 func(); 97 }View Code
实验任务四:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string> 3 #include <iomanip> 4 using namespace std; 5 // 矩形类Rect的定义 6 // 待补足 7 // ××× 8 // 普通函数:输出矩形信息 9 class Rect 10 { 11 public: 12 Rect(); 13 Rect(int l, int w); 14 Rect(const Rect &r); 15 16 double len()const{return lenth;} 17 double wide()const{return width;} 18 double area()const{return lenth*width;} 19 double circumference()const{return (lenth+width)*2;} 20 void resize(double times); 21 void resize(double l_times, double w_times); 22 23 static string doc; 24 static int size_info(){return count;} 25 ~Rect() {count--;} 26 27 private: 28 double lenth, width; 29 static int count; 30 }; 31 32 Rect::Rect() 33 { 34 lenth = 2.0; 35 width = 1.0; 36 count++; 37 } 38 39 Rect::Rect(int l, int w) 40 { 41 lenth = l; 42 width = w; 43 count++; 44 } 45 46 Rect::Rect(const Rect &r) 47 { 48 lenth = r.lenth; 49 width = r.width; 50 count++; 51 } 52 53 54 void Rect::resize(double times) 55 { 56 lenth *= times; 57 width *= times; 58 } 59 60 void Rect::resize(double l_times, double w_times) 61 { 62 lenth *=l_times; 63 width *=w_times; 64 } 65 66 int Rect::count = 0; 67 string Rect::doc = "a simple Rect class"; 68 69 70 void output(const Rect &r) 71 { 72 cout << "矩形信息: " << endl; 73 cout << fixed << setprecision(2); 74 // 补足代码:分行输出矩形长、宽、面积、周长 75 // ××× 76 cout << "长: " << left << r.len() << endl; 77 cout << "宽: " << left << r.wide() << endl; 78 cout << "面积: " << left << r.area() << endl; 79 cout << "周长: " << left << r.circumference() << endl; 80 } 81 // 测试代码 82 void test() 83 { 84 cout << "矩形类信息: " << Rect::doc << endl; 85 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 86 Rect r1; 87 output(r1); 88 Rect r2(4, 3); 89 output(r2); 90 Rect r3(r2); 91 r3.resize(2); 92 output(r3); 93 r3.resize(5, 2); 94 output(r3); 95 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 96 } 97 // 主函数 98 int main() 99 { 100 test(); 101 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 102 }View Code
实验任务五:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cmath> 3 // 复数类Complex:定义 4 // 待补足 5 // ××× 6 // 复数类Complex: 测试 7 class Complex{ 8 public: 9 Complex(double a = 0, double b = 0){ 10 real = a; 11 imag = b; 12 } 13 Complex(const Complex &t){ 14 real = t.real; 15 imag = t.imag; 16 } 17 double get_real() const{ 18 return real; 19 } 20 double get_imag() const{ 21 return imag; 22 } 23 void add(const Complex &t){ 24 real += t.get_real(); 25 imag += t.get_imag(); 26 } 27 void show(){ 28 std::cout << real; 29 if (imag>0) std::cout << "+" << abs(imag) << "i"; 30 if (imag<0) std::cout << "-" << abs(imag) << "i"; 31 } 32 void show() const{ 33 std::cout << real; 34 if (imag>0) std::cout << "+" << abs(imag) << "i"; 35 if (imag<0) std::cout << "-" << abs(imag) << "i"; 36 } 37 friend Complex add(const Complex &c1, const Complex &c2); 38 friend bool is_equal(const Complex &c1, const Complex &c2); 39 friend double abs(const Complex &t); 40 41 private: 42 double real; 43 double imag; 44 }; 45 46 Complex add(const Complex &c1, const Complex &c2){ 47 double a, b; 48 a = c1.get_real() + c2.get_real(); 49 b = c1.get_imag() + c2.get_imag(); 50 Complex t(a, b); 51 return t; 52 } 53 bool is_equal(const Complex &c1, const Complex &c2){ 54 bool a, b; 55 a = c1.get_real() == c2.get_real(); 56 b = c1.get_imag() == c2.get_imag(); 57 return a && b; 58 } 59 double abs(const Complex &t){ 60 return sqrt(pow(t.get_real(), 2) + pow(t.get_imag(), 2)); 61 } 62 void test() { 63 using namespace std; 64 Complex c1(3, -4); 65 const Complex c2(4.5); 66 Complex c3(c1); 67 cout << "c1 = "; 68 c1.show(); 69 cout << endl; 70 cout << "c2 = "; 71 c2.show(); 72 cout << endl; 73 cout << "c2.imag = " << c2.get_imag() << endl; 74 cout << "c3 = "; 75 c3.show(); 76 cout << endl; 77 cout << "abs(c1) = "; 78 cout << abs(c1) << endl; 79 cout << boolalpha; 80 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 81 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 82 Complex c4; 83 c4 = add(c1, c2); 84 cout << "c4 = c1 + c2 = "; 85 c4.show(); 86 cout << endl; 87 c1.add(c2); 88 cout << "c1 += c2, " << "c1 = "; 89 c1.show(); 90 cout << endl; 91 } 92 int main() { 93 test(); 94 }View Code
标签:const,cout,对象,double,void,int,实验,Rect From: https://www.cnblogs.com/xlhc/p/17775115.html