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