实验一 类和对象
实验任务1:
View Code
View Code
View Code
View Code
View Code
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<array> 5 6 7 // 函数模板 8 // 对满足特定条件的序列类型T对象,使用范围for输出 9 template<typename T> 10 void output1(const T &obj) { 11 for(auto i: obj) 12 std::cout << i << ", "; 13 std::cout << "\b\b \n"; 14 } 15 // 函数模板 16 // 对满足特定条件的序列类型T对象,使用迭代器输出 17 template<typename T> 18 void output2(const T &obj) { 19 for(auto p = obj.begin(); p != obj.end(); ++p) 20 std::cout << *p << ", "; 21 std::cout << "\b\b \n"; 22 } 23 // array模板类基础用法 24 void test_array() { 25 using namespace std; 26 27 array<int,5> x1; 28 cout<<"x1.size()="<< x1.size() << endl; 29 x1.fill(42); 30 x1.at(0)=999; 31 x1[4]=-999; 32 cout<<"x1: "; 33 output1(x1); 34 cout << "x1: "; 35 output2(x1); 36 37 array<int,5> x2 (x1); 38 cout<<boolalpha<<(x1==x2)<<endl; 39 x2.fill(22); 40 cout<<"x2: "; 41 output1(x2); 42 swap(x1,x2); 43 cout<<"x1: "; 44 output1(x1); 45 cout<<"x2: "; 46 output1(x2); 47 } 48 49 void test_vector() 50 { 51 using namespace std; 52 53 vector<int> v1; 54 cout<<v1.size()<<endl; 55 cout<<v1.max_size()<<endl; 56 v1.push_back(55); 57 cout<<"v1: "; 58 output1(v1); 59 60 vector<int> v2{1,0,5,2}; 61 v2.pop_back(); 62 v2.erase(v2.begin()); 63 v2.insert(v2.begin(),999); 64 v2.insert(v2.end(),-999); 65 cout<<v2.size()<<endl; 66 cout<<"v2: "; 67 output2(v2); 68 69 vector<int> v3(5, 42); 70 cout << "v3: "; 71 output1(v3); 72 73 vector<int> v4(v3.begin(), v3.end()-2); 74 cout << "v4: "; 75 output1(v4); 76 } 77 78 void test_string() { 79 using namespace std; 80 string s1{"oop"}; 81 cout << s1.size() << endl; 82 for(auto &i: s1) 83 i -= 32; 84 s1 += "2023"; 85 s1.append(", hello"); 86 cout << s1 << endl; 87 } 88 89 int main() { 90 using namespace std; 91 cout << "===========测试1: array模板类基础用法===========" << endl; 92 test_array(); 93 cout << "\n===========测试2: vector模板类基础用法===========" << endl; 94 test_vector(); 95 cout << "\n===========测试3: string类基础用法===========" << endl; 96 test_string(); 97 }

实验任务2:
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 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() 14 << endl; 15 16 cout << "c1 + c2 = " << c1 + c2 << endl; 17 cout << "c1 - c2 = " << c1 - c2 << endl; 18 cout << "abs(c1) = " << abs(c1) << endl; 19 20 } 21 22 int main() 23 { 24 test_std_complex(); 25 }

实验任务3:
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class T 6 { 7 public: 8 T(int x=0,int y=0); 9 T(const T &t); 10 T(T &&t); 11 ~T(); 12 13 void set_m1(int x); 14 int get_m1() const; 15 int get_m2() const; 16 void display() const; 17 friend void func(); 18 19 private: 20 int m1,m2; 21 22 public: 23 static void display_count(); 24 25 public: 26 static const string doc; 27 static const int max_count; 28 29 private: 30 static int count; 31 }; 32 33 const string T::doc{"a simple class"}; 34 const int T::max_count = 99; 35 int T::count = 0; 36 37 T::T(int x,int y):m1{x},m2{y} 38 { 39 ++count; 40 cout << "constructor called.\n"; 41 } 42 43 T::T(const T &t):m1{t.m1},m2{t.m2} 44 { 45 ++count; 46 cout << "copy constructor called.\n"; 47 } 48 49 T::T(T &&t):m1{t.m1},m2{t.m2} 50 { 51 ++count; 52 cout << "move constructor called.\n"; 53 } 54 55 T::~T() { 56 --count; 57 cout << "destructor called.\n"; 58 } 59 60 void T::set_m1(int x) 61 { 62 m1=x; 63 } 64 int T::get_m1() const { 65 return m1; 66 } 67 int T::get_m2() const { 68 return m2; 69 } 70 void T::display() const { 71 cout << m1 << ", " << m2 << endl; 72 } 73 void T::display_count() { 74 cout << "T objects: " << count << endl; 75 } 76 77 78 void func() 79 { 80 T t1; 81 t1.set_m1(55); 82 t1.m2=77; 83 t1.display(); 84 } 85 86 87 void test() { 88 cout << "T class info: " << T::doc << endl; 89 cout << "T objects max_count: " << T::max_count << endl; 90 T::display_count(); 91 T t1; 92 t1.display(); 93 t1.set_m1(42); 94 T t2{t1}; 95 t2.display(); 96 T t3{std::move(t1)}; 97 t3.display(); 98 t1.display(); 99 T::display_count(); 100 } 101 102 int main() { 103 cout << "============测试类T============" << endl; 104 test(); 105 cout << endl; 106 cout << "============测试友元函数func()============" << endl; 107 func(); 108 }

实验任务4:
1 #include <iostream> 2 #include <string> 3 #include <iomanip> 4 using namespace std; 5 // 矩形类Rect的定义 6 // 待补足 7 // ××× 8 // 普通函数:输出矩形信息 9 class Rect { 10 public: 11 Rect(double length = 2.0, double width = 1.0); 12 Rect(const Rect &r); 13 ~Rect(); 14 double len() const; 15 double wide() const; 16 double circumference() const; 17 double area() const; 18 void resize(double times); 19 void resize(double length_times, double width_times); 20 public: 21 static const string doc; 22 void display() const; 23 static int size_info(); 24 private: 25 double length, width; 26 static int size; 27 }; 28 const string Rect::doc{"a simple Rect class"}; 29 int Rect::size = 0; 30 Rect::Rect(double length, double width): length{length}, width(width) { 31 ++size; 32 } 33 Rect::Rect(const Rect &r): length{r.length}, width{r.width} { 34 ++size; 35 } 36 Rect::~Rect() { 37 --size; 38 } 39 double Rect::len() const{ 40 return length; 41 } 42 double Rect::wide() const { 43 return width; 44 } 45 double Rect::circumference() const { 46 return 2 * length + 2 * width; 47 } 48 double Rect::area() const { 49 return length * width; 50 } 51 void Rect::resize(double times) { 52 length *= times; 53 width *= times; 54 } 55 void Rect::resize(double length_times, double width_times) { 56 cout << size; 57 } 58 int Rect::size_info() { 59 return size; 60 } 61 void output(const Rect &r) { 62 cout << "矩形信息: " << endl; 63 cout << fixed << setprecision(2); // 控制输出格式:以浮点数形式输出,小数部分保留两位 64 cout << "长: " << r.len() << endl; 65 cout << "宽: " << r.wide() << endl; 66 cout << "面积: " << r.area() << endl; 67 cout << "周长: " << r.circumference() << endl; 68 // 补足代码:分行输出矩形长、宽、面积、周长 69 // ××× 70 } 71 // 测试代码 72 void test() { 73 cout << "矩形类信息: " << Rect::doc << endl; 74 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 75 Rect r1; 76 output(r1); 77 Rect r2(4, 3); 78 output(r2); 79 Rect r3(r2); 80 r3.resize(2); 81 output(r3); 82 r3.resize(5, 2); 83 output(r3); 84 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 85 } 86 // 主函数 87 int main() { 88 test(); 89 cout << "当前矩形对象数目: " << Rect::size_info() << endl; 90 }

实验任务5:
1 #include <iostream> 2 #include <cmath> 3 class Complex { 4 public: 5 Complex(double r = 0, double i = 0) { 6 real = r; 7 imag = i; 8 } 9 Complex(const Complex& x) { 10 real = x.real; 11 imag = x.imag; 12 } 13 double get_real() const 14 { return real; } 15 double get_imag() const 16 { return imag; } 17 void add(const Complex& x) { 18 real += x.real; 19 imag += x.imag; 20 } 21 void show(){ 22 std::cout << real; 23 if (imag>0) std::cout << "+" << abs(imag) << "i"; 24 if (imag<0) std::cout << "-" << abs(imag) << "i"; 25 } 26 void show() const{ 27 std::cout << real; 28 if (imag>0) std::cout << "+" << abs(imag) << "i"; 29 if (imag<0) std::cout << "-" << abs(imag) << "i"; 30 } 31 friend Complex add(const Complex& c1, const Complex& c2); 32 friend bool is_equal(const Complex& c1, const Complex& c2); 33 friend double abs(const Complex& c1); 34 private: 35 double real; 36 double imag; 37 }; 38 Complex add(const Complex& c1, const Complex& c2) { 39 double real = c1.real + c2.real; 40 double imag = c1.imag + c2.imag; 41 return Complex(real, imag); 42 } 43 bool is_equal(const Complex& c1, const Complex& c2) { 44 if (c1.real == c2.real && c1.imag == c2.imag) 45 return true; 46 else 47 return false; 48 } 49 double abs(const Complex& c1) { 50 return sqrt(c1.real * c1.real + c1.imag * c1.imag); 51 } 52 void test() { 53 using namespace std; 54 Complex c1(3, -4); 55 const Complex c2(4.5); 56 Complex c3(c1); 57 cout << "c1 = "; 58 c1.show(); 59 cout << endl; 60 cout << "c2 = "; 61 c2.show(); 62 cout << endl; 63 cout << "c2.imag = " << c2.get_imag() << endl; 64 cout << "c3 = "; 65 c3.show(); 66 cout << endl; 67 cout << "abs(c1) = "; 68 cout << abs(c1) << endl; 69 cout << boolalpha; 70 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 71 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 72 Complex c4; 73 c4 = add(c1, c2); 74 cout << "c4 = c1 + c2 = "; 75 c4.show(); 76 cout << endl; 77 c1.add(c2); 78 cout << "c1 += c2, " << "c1 = "; 79 c1.show(); 80 cout << endl; 81 } 82 int main() { 83 test(); 84 }


浙公网安备 33010602011771号