实验二

task1

T.h

 1 #pragma once
 2 
 3 #include <string>
 4 
 5 // 类T: 声明
 6 class T {
 7 // 对象属性、方法
 8 public:
 9     T(int x = 0, int y = 0);   // 普通构造函数
10     T(const T &t);  // 复制构造函数
11     T(T &&t);       // 移动构造函数
12     ~T();           // 析构函数
13 
14     void adjust(int ratio);      // 按系数成倍调整数据
15     void display() const;           // 以(m1, m2)形式显示T类对象信息
16 
17 private:
18     int m1, m2;
19 
20 // 类属性、方法
21 public:
22     static int get_cnt();          // 显示当前T类对象总数
23 
24 public:
25     static const std::string doc;       // 类T的描述信息
26     static const int max_cnt;           // 类T对象上限
27 
28 private:
29     static int cnt;         // 当前T类对象数目
30 
31 // 类T友元函数声明
32     friend void func();
33 };
34 
35 // 普通函数声明
36 void func();

T.cpp

 1 #include "T.h"
 2 #include <iostream>
 3 #include <string>
 4 
 5 // 类T实现
 6 
 7 // static成员数据类外初始化
 8 const std::string T::doc{"a simple class sample"};
 9 const int T::max_cnt = 999;
10 int T::cnt = 0;
11 
12 // 类方法
13 int T::get_cnt() {
14    return cnt;
15 }
16 
17 // 对象方法
18 T::T(int x, int y): m1{x}, m2{y} { 
19     ++cnt; 
20     std::cout << "T constructor called.\n";
21 } 
22 
23 T::T(const T &t): m1{t.m1}, m2{t.m2} {
24     ++cnt;
25     std::cout << "T copy constructor called.\n";
26 }
27 
28 T::T(T &&t): m1{t.m1}, m2{t.m2} {
29     ++cnt;
30     std::cout << "T move constructor called.\n";
31 }    
32 
33 T::~T() {
34     --cnt;
35     std::cout << "T destructor called.\n";
36 }           
37 
38 void T::adjust(int ratio) {
39     m1 *= ratio;
40     m2 *= ratio;
41 }    
42 
43 void T::display() const {
44     std::cout << "(" << m1 << ", " << m2 << ")" ;
45 }     
46 
47 // 普通函数实现
48 void func() {
49     T t5(42);
50     t5.m2 = 2049;
51     std::cout << "t5 = "; t5.display(); std::cout << '\n';
52 }

task1.cpp

 1 #include "T.h"
 2 #include <iostream>
 3 
 4 void test_T();
 5 
 6 int main() {
 7     std::cout << "test Class T: \n";
 8     test_T();
 9 
10     std::cout << "\ntest friend func: \n";
11     func();
12 }
13 
14 void test_T() {
15     using std::cout;
16     using std::endl;
17 
18     cout << "T info: " << T::doc << endl;
19     cout << "T objects'max count: " << T::max_cnt << endl;
20     cout << "T objects'current count: " << T::get_cnt() << endl << endl;
21 
22     T t1;
23     cout << "t1 = "; t1.display(); cout << endl;
24 
25     T t2(3, 4);
26     cout << "t2 = "; t2.display(); cout << endl;
27 
28     T t3(t2);
29     t3.adjust(2);
30     cout << "t3 = "; t3.display(); cout << endl;
31 
32     T t4(std::move(t2));
33     cout << "t4 = "; t4.display(); cout << endl;
34 
35     cout << "test: T objects'current count: " << T::get_cnt() << endl;
36 }

运行结果:

屏幕截图 2025-10-22 194518

问题1:
T.h中,在类T内部,已声明 func 是T的友元函数。在类外部,去掉line36,重新编译,程序能否正常运
行。
如果能,回答YES;如果不能,以截图形式提供编译报错信息,说明原因。
答:
屏幕截图 2025-10-22 201247

不行,T.h中在类内部声明 friend void func(); 只是声明func是T的友元函数,让func可以访问T的私有成员但这只是友元声明,不是函数声明本身,如果去掉T.h第36行的 void func(); 函数声明,编译器在编译task1.cpp时不知道func函数的存在

问题2:
T.h中,line9-12给出了各种构造函数、析构函数。总结它们各自的功能、调用时机。
答:
第一个是普通构造函数,功能:用指定参数初始化对象,调用时机:创建新对象时,如T m1,T m2(3,4)
第二个是复制构造函数,功能:用已有对象的值初始化新对象,调用时机:对象拷贝时,如 T t3(t2) 或函数传值参数。
第三个是移动构造函数,功能:转移资源,调用时机:使用std::move或临时对象时
第四个是析构函数,功能:销毁对象释放资源,调用时机:对象离开作用域或被delete时
问题3:
T.cpp中,line13-15,剪切到T.h的末尾,重新编译,程序能否正确编译。
如不能,以截图形式给出报错信息,分析原因。
答:
屏幕截图 2025-10-22 202924

不行,静态函数必须在类外定义,不然会重复定义

 

task2

Complex.h

 1 #pragma once
 2 #include <string>
 3 
 4 class Complex {
 5 public:
 6 
 7     static const std::string doc;
 8 
 9     Complex(double real = 0.0, double imag = 0.0);
10     Complex(const Complex& other);  
11 
12     double get_real() const;
13     double get_imag() const;
14     void add(const Complex& other);
15 
16     friend void output(const Complex& c);
17     friend double abs(const Complex& c);
18     friend Complex add(const Complex& c1, const Complex& c2);
19     friend bool is_equal(const Complex& c1, const Complex& c2);
20     friend bool is_not_equal(const Complex& c1, const Complex& c2);
21 
22 private:
23     double real;  
24     double imag;  
25 };
26 
27 void output(const Complex& c);
28 double abs(const Complex& c);
29 Complex add(const Complex& c1, const Complex& c2);
30 bool is_equal(const Complex& c1, const Complex& c2);
31 bool is_not_equal(const Complex& c1, const Complex& c2);

Complex.cpp

 1 #include "Complex.h"
 2 #include <iostream>
 3 #include <cmath>
 4 
 5 
 6 const std::string Complex::doc = "a simplified complex class";
 7 
 8 Complex::Complex(double r, double i) : real(r), imag(i) {}
 9 
10 Complex::Complex(const Complex& other) : real(other.real), imag(other.imag) {}
11 
12 double Complex::get_real() const {
13     return real;
14 }
15 
16 double Complex::get_imag() const {
17     return imag;
18 }
19 
20 void Complex::add(const Complex& other) {
21     real += other.real;
22     imag += other.imag;
23 }
24 
25 void output(const Complex& c) {
26     std::cout << c.real;
27     if (c.imag >= 0) {
28         std::cout << " + " << c.imag << "i";
29     }
30     else {
31         std::cout << " - " << -c.imag << "i";
32     }
33 }
34 
35 double abs(const Complex& c) {
36     return std::sqrt(c.real * c.real + c.imag * c.imag);
37 }
38 
39 Complex add(const Complex& c1, const Complex& c2) {
40     return Complex(c1.real + c2.real, c1.imag + c2.imag);
41 }
42 
43 bool is_equal(const Complex& c1, const Complex& c2) {
44     return c1.real == c2.real && c1.imag == c2.imag;
45 }
46 
47 bool is_not_equal(const Complex& c1, const Complex& c2) {
48     return !is_equal(c1, c2);
49 }

task2.cpp

 1 // 待补足头文件
 2 // xxx
 3 #include"Complex.h"
 4 #include <iostream>
 5 #include <iomanip>
 6 #include <complex>
 7 
 8 void test_Complex();
 9 void test_std_complex();
10 
11 int main() {
12     std::cout << "*******测试1: 自定义类Complex*******\n";
13     test_Complex();
14 
15     std::cout << "\n*******测试2: 标准库模板类complex*******\n";
16     test_std_complex();
17 }
18 
19 void test_Complex() {
20     using std::cout;
21     using std::endl;
22     using std::boolalpha;
23 
24     cout << "类成员测试: " << endl;
25     cout << Complex::doc << endl << endl;
26 
27     cout << "Complex对象测试: " << endl;
28     Complex c1;
29     Complex c2(3, -4);
30     Complex c3(c2);
31     Complex c4 = c2;
32     const Complex c5(3.5);
33 
34     cout << "c1 = "; output(c1); cout << endl;
35     cout << "c2 = "; output(c2); cout << endl;
36     cout << "c3 = "; output(c3); cout << endl;
37     cout << "c4 = "; output(c4); cout << endl;
38     cout << "c5.real = " << c5.get_real()
39         << ", c5.imag = " << c5.get_imag() << endl << endl;
40 
41     cout << "复数运算测试: " << endl;
42     cout << "abs(c2) = " << abs(c2) << endl;
43     c1.add(c2);
44     cout << "c1 += c2, c1 = "; output(c1); cout << endl;
45     cout << boolalpha;
46     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
47     cout << "c1 != c2 : " << is_not_equal(c1, c2) << endl;
48     c4 = add(c2, c3);
49     cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
50 }
51 
52 void test_std_complex() {
53     using std::cout;
54     using std::endl;
55     using std::boolalpha;
56 
57     cout << "std::complex<double>对象测试: " << endl;
58     std::complex<double> c1;
59     std::complex<double> c2(3, -4);
60     std::complex<double> c3(c2);
61     std::complex<double> c4 = c2;
62     const std::complex<double> c5(3.5);
63 
64     cout << "c1 = " << c1 << endl;
65     cout << "c2 = " << c2 << endl;
66     cout << "c3 = " << c3 << endl;
67     cout << "c4 = " << c4 << endl;
68 
69     cout << "c5.real = " << c5.real()
70         << ", c5.imag = " << c5.imag() << endl << endl;
71 
72     cout << "复数运算测试: " << endl;
73     cout << "abs(c2) = " << abs(c2) << endl;
74     c1 += c2;
75     cout << "c1 += c2, c1 = " << c1 << endl;
76     cout << boolalpha;
77     cout << "c1 == c2 : " << (c1 == c2) << endl;
78     cout << "c1 != c2 : " << (c1 != c2) << endl;
79     c4 = c2 + c3;
80     cout << "c4 = c2 + c3, c4 = " << c4 << endl;
81 }

运行结果:

屏幕截图 2025-10-22 215446

问题1:
比较自定义类 Complex 和标准库模板类 complex 的用法,在使用形式上,哪一种更简洁?函数和运算内在有关
联吗?
答:
标准库更简洁,有关联
问题2:
2-1:自定义 Complex 中, output/abs/add/ 等均设为友元,它们真的需要访问 私有数据 吗?(回答“是/否”并
给出理由)
2-2:标准库 std::complex 是否把 abs 设为友元?(查阅 cppreference后回答)
2-3:什么时候才考虑使用 friend?总结你的思考。
答:
2-1:否,因为可以通过公有接口来访问
2-2:否
2-3:需要频繁访问私有成员的函数
问题3:
如果构造对象时禁用=形式,即遇到 Complex c4 = c2; 编译报错,类Complex的设计应如何调整?
答:
Complex (const Complex &c)=delete;
额外设置一个函数重新实现
 
task3
PlayerControl.h
 1 #pragma once
 2 #include <string>
 3 
 4 enum class ControlType { Play, Pause, Next, Prev, Stop, Unknown };
 5 
 6 class PlayerControl {
 7 public:
 8     PlayerControl();
 9 
10     ControlType parse(const std::string& control_str);   // 实现std::string --> ControlType转换
11     void execute(ControlType cmd) const;   // 执行控制操作(以打印输出模拟)       
12 
13     static int get_cnt();
14 
15 private:
16     static int total_cnt;
17 };

PlayerControl.cpp

 1 #include "PlayerControl.h"
 2 #include <iostream>
 3 #include <algorithm>   
 4 
 5 int PlayerControl::total_cnt = 0;
 6 
 7 PlayerControl::PlayerControl() {}
 8 
 9 // 待补足
10 // 1. 将输入字符串转为小写,实现大小写不敏感
11 // 2. 匹配"play"/"pause"/"next"/"prev"/"stop"并返回对应枚举
12 // 3. 未匹配的字符串返回ControlType::Unknown
13 // 4. 每次成功调用parse时递增total_cnt
14 ControlType PlayerControl::parse(const std::string& control_str) {
15     std::string result = control_str;
16     std::transform(result.begin(), result.end(), result.begin(),
17         [](unsigned char c) { return std::tolower(c); });
18     if (result == "play") {
19         ++total_cnt;
20         return ControlType::Play;
21     }
22     else if (result == "pause") {
23         ++total_cnt;
24         return ControlType::Pause;
25     }
26     else if (result == "next") {
27         ++total_cnt;
28         return ControlType::Next;
29     }
30     else if (result == "prev") {
31         ++total_cnt;
32         return ControlType::Prev;
33     }
34     else if (result == "stop") {
35         ++total_cnt;
36         return ControlType::Stop;
37     }
38     else {
39         return ControlType::Unknown;
40     }
41 }
42 
43 void PlayerControl::execute(ControlType cmd) const {
44     switch (cmd) {
45     case ControlType::Play:  std::cout << "[play] Playing music...\n"; break;
46     case ControlType::Pause: std::cout << "[Pause] Music paused\n";    break;
47     case ControlType::Next:  std::cout << "[Next] Skipping to next track\n"; break;
48     case ControlType::Prev:  std::cout << "[Prev] Back to previous track\n"; break;
49     case ControlType::Stop:  std::cout << "[Stop] Music stopped\n"; break;
50     default:                 std::cout << "[Error] unknown control\n"; break;
51     }
52 }
53 
54 int PlayerControl::get_cnt() {
55     return total_cnt;
56 }

task3.cpp

 1 #include "PlayerControl.h"
 2 #include <iostream>
 3 
 4 void test() {
 5     PlayerControl controller;
 6     std::string control_str;
 7     std::cout << "Enter Control: (play/pause/next/prev/stop/quit):\n";
 8 
 9     while (std::cin >> control_str) {
10         if (control_str == "quit")
11             break;
12 
13         ControlType cmd = controller.parse(control_str);
14         controller.execute(cmd);
15         std::cout << "Current Player control: " << PlayerControl::get_cnt() << "\n\n";
16     }
17 }
18 
19 int main() {
20     test();
21 }

运行结果:

image

 

task4

Fraction.h

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 class Fraction {
 5     public:
 6         static const std::string doc;
 7         Fraction(int up, int down = 1);
 8         Fraction(const Fraction& f);
 9         int get_up() const;
10         int get_down() const;
11         Fraction negative();
12         friend void output(const Fraction &f);
13         friend Fraction add(const Fraction& f1, const Fraction& f2);
14         friend Fraction sub(const Fraction& f1, const Fraction& f2);
15         friend Fraction mul(const Fraction& f1, const Fraction& f2);
16         friend Fraction div(const Fraction& f1, const Fraction& f2);
17     private:
18         int up;
19         int down;
20 
21 };
22 void output(const Fraction &f);
23 Fraction add(const Fraction& f1, const Fraction& f2);
24 Fraction sub(const Fraction& f1, const Fraction& f2);
25 Fraction mul(const Fraction& f1, const Fraction& f2);
26 Fraction div(const Fraction& f1, const Fraction& f2);

Fraction.cpp

  1 #include"Fraction.h"
  2 #include<iostream>
  3 
  4 static int gcd_int(int a, int b);
  5 
  6 const std::string Fraction::doc="Fraction类 v 0.01版. \n目前仅支持分数对象的构造、输出、加 / 减 / 乘 / 除运算";
  7 
  8 Fraction::Fraction(int up, int down):up(up),down(down) {}
  9 
 10 Fraction::Fraction(const Fraction& f) {
 11     up = f.up;
 12     down = f.down;
 13 }
 14 
 15 int Fraction::get_up() const {
 16     int u = up;
 17     int d = down;
 18     if (d == 0) return u;      
 19     if (u == 0) return 0;      
 20     if (d < 0) { u = -u; d = -d; } 
 21     int g = gcd_int(u, d);
 22     return u / g;
 23 }
 24 
 25 int Fraction::get_down() const {
 26     int u = up;
 27     int d = down;
 28     if (d == 0) return 0;     
 29     if (u == 0) return 1;      
 30     if (d < 0) { u = -u; d = -d; }
 31     int g = gcd_int(u, d);
 32     return d / g;
 33 }
 34 
 35 Fraction Fraction::negative() {
 36     Fraction f(0);
 37     if (this->up >0&& this->down < 0) {
 38         f.up = this->up;
 39         f.down = -this->down;
 40     }
 41     else if (this->up < 0 && this->down>0) {
 42         f.up = -this->up;
 43         f.down = this->down;
 44     }
 45     else {
 46         f.up = this->up;
 47         f.down = this->down;
 48     }
 49     return f;
 50 }
 51 
 52 Fraction add(const Fraction& f1, const Fraction& f2) {
 53     // a/b + c/d = (a*d + c*b) / (b*d)
 54     int up = f1.up * f2.down + f2.up * f1.down;
 55     int down = f1.down * f2.down;
 56 
 57     if (down == 0) {
 58         // 分母为0的情况直接返回不作约分(由调用方/输出处理)
 59         return Fraction(up, down);
 60     }
 61 
 62     int g = gcd_int(up, down);
 63     up /= g;
 64     down /= g;
 65 
 66     // 规范符号:分母为正
 67     if (down < 0) {
 68         up = -up;
 69         down = -down;
 70     }
 71 
 72     return Fraction(up, down);
 73 }
 74 
 75 Fraction sub(const Fraction& f1, const Fraction& f2) {
 76     // a/b - c/d = (a*d - c*b) / (b*d)
 77     int up = f1.up * f2.down - f2.up * f1.down;
 78     int down = f1.down * f2.down;
 79 
 80     if (down == 0) {
 81         return Fraction(up, down);
 82     }
 83 
 84     int g = gcd_int(up, down);
 85     up /= g;
 86     down /= g;
 87 
 88     if (down < 0) {
 89         up = -up;
 90         down = -down;
 91     }
 92 
 93     return Fraction(up, down);
 94 }
 95 
 96 Fraction mul(const Fraction& f1, const Fraction& f2) {
 97     Fraction f3(f1);
 98     f3.up *= f2.up;
 99     f3.down *= f2.down;
100     return f3;
101 }
102 
103 Fraction div(const Fraction& f1, const Fraction& f2) {
104     Fraction f3(f1);
105     f3.up *= f2.down;
106     f3.down *= f2.up;
107     return f3;
108 }
109 static int gcd_int(int a, int b) {
110     a = a < 0 ? -a : a;
111     b = b < 0 ? -b : b;
112     if (a == 0) return b;
113     if (b == 0) return a;
114     while (b) {
115         int t = a % b;
116         a = b;
117         b = t;
118     }
119     return a;
120 }
121 
122 void output(const Fraction& f) {
123     int up = f.up;
124     int down = f.down;
125 
126     if (down == 0) {
127         std::cout << "分母不能为0";
128         return;
129     }
130 
131     if (up == 0) {
132         std::cout << 0;
133         return;
134     }
135 
136     int g = gcd_int(up, down);
137     up /= g;
138     down /= g;
139 
140     if (down < 0) {
141         up = -up;
142         down = -down;
143     }
144 
145     if (down == 1) {
146         std::cout << up;
147     }
148     else {
149         std::cout << up << "/" << down;
150     }
151 }

task4.cpp

 1 #include "Fraction.h"
 2 #include <iostream>
 3 
 4 void test1();
 5 void test2();
 6 
 7 int main() {
 8     std::cout << "测试1: Fraction类基础功能测试\n";
 9     test1();
10 
11     std::cout << "\n测试2: 分母为0测试: \n";
12     test2();
13 }
14 
15 void test1() {
16     using std::cout;
17     using std::endl;
18 
19     cout << "Fraction类测试: " << endl;
20     cout << Fraction::doc << endl << endl;
21 
22     Fraction f1(5);
23     Fraction f2(3, -4), f3(-18, 12);
24     Fraction f4(f3);
25     cout << "f1 = "; output(f1); cout << endl;
26     cout << "f2 = "; output(f2); cout << endl;
27     cout << "f3 = "; output(f3); cout << endl;
28     cout << "f4 = "; output(f4); cout << endl;
29 
30     const Fraction f5(f4.negative());
31     cout << "f5 = "; output(f5); cout << endl;
32     cout << "f5.get_up() = " << f5.get_up()
33         << ", f5.get_down() = " << f5.get_down() << endl;
34 
35     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
36     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
37     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
38     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
39     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
40 }
41 
42 void test2() {
43     using std::cout;
44     using std::endl;
45 
46     Fraction f6(42, 55), f7(0, 3);
47     cout << "f6 = "; output(f6); cout << endl;
48     cout << "f7 = "; output(f7); cout << endl;
49     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
50 }

运行结果:

屏幕截图 2025-10-23 192550

问题回答
分数的输出和计算, output/add/sub/mul/div ,你选择的是哪一种设计方案?(友元/自由函数/命名
空间+自由函数/类+static)
你的决策理由?如友元方案的优缺点、静态成员函数方案的适用场景、命名空间方案的考虑因素等。
答:
我选择的是友元函数,优点:增强灵活性,提高性能。缺点:破坏封装性,降低可维护性。

 

 

posted @ 2025-10-23 19:30  Asahina  阅读(8)  评论(0)    收藏  举报
//雪花飘落效果