实验1 类和对象

实验任务1

task1.cpp源码

 1 // 标准库string, vector, array基础用法
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <array>
 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 // 函数模板
17 // 对满足特定条件的序列类型T对象,使用迭代器输出
18 template<typename T>
19 void output2(const T &obj) {
20     for (auto p = obj.begin(); p != obj.end(); ++p)
21         std::cout << *p << ", ";
22     std::cout << "\b\b \n";
23 }
24 
25 // array模板类基础用法
26 void test_array() {
27     using namespace std;
28     array<int, 5> x1; // 创建一个array对象,包含5个int元素,未初始化
29     cout << "x1.size() = " << x1.size() << endl; // 输出元素个数
30     x1.fill(42); // 把x1的所有元素都用42填充
31     x1.at(0) = 999; // 把下标为0的元素值修改为999
32     x1[4] = -999; // 把下表为4的元素值修改为-999
33     cout << "x1: ";
34     output1(x1); // 调用模板函数output1输出x1
35     cout << "x1: ";
36     output2(x1); // 调用模板函数output1输出x1
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); // 交换array对象x1, x2
43     cout << "x1: ";
44     output1(x1);
45     cout << "x2: ";
46     output1(x2);
47 }
48 
49 // vector模板类基础用法
50 void test_vector() {
51     using namespace std;
52     vector<int> v1;
53     cout << v1.size() << endl; // 输出目前元素个数
54     cout << v1.max_size() << endl; // 输出元素个数之最大可能个数
55     v1.push_back(55); // 在v1末尾插入元素
56     cout << "v1: ";
57     output1(v1);
58     vector<int> v2 {1, 0, 5, 2};
59     v2.pop_back(); // 从v2末尾弹出一个元素
60     v2.erase(v2.begin()); // 删除v2.begin()位置的数据项
61     v2.insert(v2.begin(), 999); // 在v1.begin()之前的位置插入
62     v2.insert(v2.end(), -999); // 在v1.end()之前的位置插入
63     cout << v2.size() << endl;
64     cout << "v2: ";
65     output2(v2);
66     vector<int> v3(5, 42); //创建vector对象,包含5个元素,每个元素值都是42
67     cout << "v3: ";
68     output1(v3);
69     vector<int> v4(v3.begin(), v3.end() - 2); // 创建vector对象,以v3对象的[v3.begin(), v3.end()-2)区间作为元素值
70     cout << "v4: ";
71     output1(v4);
72 }
73 
74 // string类基础用法
75 void test_string() {
76     using namespace std;
77     string s1{"oop"};
78     cout << s1.size() << endl;
79     for (auto &i : s1)
80         i -= 32;
81     s1 += "2023";
82     s1.append(", hello");
83     cout << s1 << endl;
84 }
85 
86 int main() {
87     using namespace std;
88     cout << "===========测试1: array模板类基础用法===========" << endl;
89     test_array();
90     cout << "\n===========测试2: vector模板类基础用法===========" << endl;
91     test_vector();
92     cout << "\n===========测试3: string类基础用法===========" << endl;
93     test_string();
94 }
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     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; // abs()是标准库数学函数,对复数取模
17     cout << boolalpha; // 设置bool型值以true/false方式输出
18     cout << "c1 == c2: " << (c1 == c2) << endl;
19     cout << "c3 == c2: " << (c3 == c2) << endl;
20     complex<double> c4 = 2;
21     cout << "c4 = " << c4 << endl;
22     c4 += c1;
23     cout << "c4 = " << c4 << endl;
24 }
25 
26 int main() {
27     test_std_complex();
28 }
View Code

运行测试结果

 

实验任务3

task3.cpp源码

  1 // 一个简单的类T:定义、使用
  2 #include <iostream>
  3 #include <string>
  4 using namespace std;
  5 
  6 // 类T的声明
  7 class T {
  8     public:
  9         T(int x = 0, int y = 0); // 带有默认形值的构造函数
 10         T(const T &t); // 复制构造函数
 11         T(T &&t); // 移动构造函数
 12         ~T(); // 析构函数
 13         void set_m1(int x); // 设置T类对象的数据成员m1
 14         int get_m1() const; // 获取T类对象的数据成员m1
 15         int get_m2() const; // 获取T类对象的数据成员m2
 16         void display() const; // 显示T类对象的信息
 17         friend void func(); // 声明func()为T类友元函数
 18     private:
 19         int m1, m2;
 20     public:
 21         static void disply_count(); // 类方法,显示当前T类对象数目
 22     public:
 23         static const string doc; // 类属性,用于描述T类
 24         static const int max_count; // 类属性,用于描述T类对象的上限
 25     private:
 26         static int count; // 类属性,用于描述当前T类对象数目
 27 };
 28 
 29 // 类的static数据成员:类外初始化
 30 const string T::doc{"a simple class"};
 31 const int T::max_count = 99;
 32 int T::count = 0;
 33 
 34 // 类T的实现
 35 T::T(int x, int y): m1{x}, m2{y} {
 36     ++count;
 37     cout << "constructor called.\n";
 38 }
 39 T::T(const T &t): m1{t.m1}, m2{t.m2} {
 40     ++count;
 41     cout << "copy constructor called.\n";
 42 }
 43 T::T(T &&t): m1{t.m1}, m2{t.m2} {
 44     ++count;
 45     cout << "move constructor called.\n";
 46 }
 47 T::~T() {
 48     --count;
 49     cout << "destructor called.\n";
 50 }
 51 void T::set_m1(int x) {
 52     m1 = x;
 53 }
 54 int T::get_m1() const {
 55     return m1;
 56 }
 57 int T::get_m2() const {
 58     return m2;
 59 }
 60 void T::display() const {
 61     cout << m1 << ", " << m2 << endl;
 62 }
 63 
 64 // 类方法
 65 void T::disply_count() {
 66     cout << "T objects: " << count << endl;
 67 }
 68 
 69 // 友元函数func():实现
 70 void func() {
 71     T t1;
 72     t1.set_m1(55);
 73     t1.m2 = 77; // 虽然m2是私有成员,依然可以直接访问
 74     t1.display();
 75 }
 76 
 77 // 测试
 78 void test() {
 79     cout << "T class info: " << T::doc << endl;
 80     cout << "T objects max_count: " << T::max_count << endl;
 81     T::disply_count();
 82     T t1;
 83     t1.display();
 84     t1.set_m1(42);
 85     T t2{t1};
 86     t2.display();
 87     T t3{std::move(t1)};
 88     t3.display();
 89     t1.display();
 90     T::disply_count();
 91 }
 92 
 93 // 主函数
 94 int main() {
 95     cout << "============测试类T============" << endl;
 96     test();
 97     cout << endl;
 98     cout << "============测试友元函数func()============" << endl;
 99     func();
100 }
View Code
运行测试结果

 

实验任务4

task4.cpp源码

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

运行测试结果

 

实验任务5

task5.cpp源码

 1 #include <iostream>
 2 #include <cmath>
 3 
 4 // 复数类Complex:定义
 5 class Complex {
 6     private:
 7         double real, imag;
 8     public:
 9         Complex(double r = 0, double i = 0) : real{r}, imag{i} {};
10         Complex(const Complex &c) :real{c.real},imag{c.imag} {};
11         double get_real() const {
12             return real;
13         }
14         double get_imag() const {
15             return imag;
16         }
17         void add(const Complex &c) {
18             real += c.get_real();
19             imag += c.get_imag();
20         }
21         void show() {
22             std::cout << real;
23             if (imag>0) std::cout << " + " << abs(imag) << 'i';
24             if (imag<0) std::cout << " - " << fabs(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 << " - " << fabs(imag) << 'i';
30         }
31     public:
32         friend Complex add(const Complex&, const Complex&);
33         friend bool is_equal(const Complex&, const Complex&);
34         friend double abs(const Complex&);
35 };
36 Complex add(const Complex &c1, const Complex &c2) {
37     double r, i;
38     r = c1.get_real() + c2.get_real();
39     i = c1.get_imag() + c2.get_imag();
40     Complex c(r, i);
41     return c;
42 }
43 bool is_equal(const Complex &c1, const Complex &c2) {
44     bool r, i;
45     r = c1.get_real() == c2.get_real();
46     i = c1.get_imag() == c2.get_imag();
47     return r && i;
48 }
49 double abs(const Complex &c) {
50     return sqrt(pow(c.get_real(), 2) + pow(c.get_imag(), 2));
51 }
52 
53 // 复数类Complex: 测试
54 void test() {
55     using namespace std;
56     Complex c1(3, -4);
57     const Complex c2(4.5);
58     Complex c3(c1);
59     cout << "c1 = ";
60     c1.show();
61     cout << endl;
62     cout << "c2 = ";
63     c2.show();
64     cout << endl;
65     cout << "c2.imag = " << c2.get_imag() << endl;
66     cout << "c3 = ";
67     c3.show();
68     cout << endl;
69     cout << "abs(c1) = ";
70     cout << abs(c1) << endl;
71     cout << boolalpha;
72     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
73     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
74     Complex c4;
75     c4 = add(c1, c2);
76     cout << "c4 = c1 + c2 = ";
77     c4.show();
78     cout << endl;
79     c1.add(c2);
80     cout << "c1 += c2, " << "c1 = ";
81     c1.show();
82     cout << endl;
83 }
84 
85 int main() {
86     test();
87 }
View Code

运行测试结果

posted @ 2023-10-13 13:30  E5rgeXcJV1x5J  阅读(63)  评论(0)    收藏  举报