实验2
任务1:
源代码:
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();
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 }
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 }
截图:

Q1:重新编译后,会出现以下报错。原因是去掉该声明,在main函数中调用func()时,编译器无法识别func函数。

Q2:
①普通构造函数功能是初始化对象的m1和m2成员,调用时机是创建普通的对象。
②拷贝构造函数功能是用已有对象t的m1和m2初始化新对象,调用时机是用一个已存在的对象初始化新对象。
③移动构造函数功能是利用右值对象t的m1和m2初始化新对象,调用时机是用右值对象初始化新对象。
④析构函数功能是在对象销毁,调用时机是对象生命周期结束时。
Q3:重新编译会出现多重定义错误,因为 T.h 中已经声明了get_cnt()

任务2:
源代码:
1 #pragma once 2 3 #include <string> 4 #include <iostream> 5 6 class Complex { 7 public: 8 //类属性 9 static const std::string doc; 10 11 //构造函数 12 Complex(double r=0.0, double i=0.0); 13 Complex(const Complex &t); 14 15 16 //返回复数实部和虚部 17 double get_real() const; 18 double get_imag() const; 19 20 //加法 21 void add(const Complex &t); 22 23 //友元函数 24 friend void output(const Complex &c); 25 friend double abs(const Complex &c); 26 friend Complex add(const Complex &a, const Complex &b); 27 friend bool is_equal(const Complex &a, const Complex &b); 28 friend bool is_not_equal(const Complex &a, const Complex &b); 29 30 //对象属性 31 private: 32 double real; 33 double imag; 34 };
1 #include "Complex.h" 2 #include <cmath> 3 #include <iostream> 4 5 //类属性 6 const std::string Complex::doc = "a simplified complex class"; 7 8 //构造函数 9 Complex::Complex(double r, double i) : real(r), imag(i) {} 10 11 Complex::Complex(const Complex &t) : real(t.real), imag(t.imag) {} 12 13 //返回复数实部 14 double Complex::get_real() const { 15 return real; 16 } 17 18 //返回复数虚部 19 double Complex::get_imag() const { 20 return imag; 21 } 22 23 //加法 24 void Complex::add(const Complex &t) { 25 real += t.real; 26 imag += t.imag; 27 } 28 29 //友元函数 30 void output(const Complex &c) { 31 if (c.imag >= 0) { 32 std::cout << c.real << " + " << c.imag << "i"; 33 } else { 34 std::cout << c.real << " - " << -c.imag << "i"; 35 } 36 } 37 38 double abs(const Complex &c) { 39 return std::sqrt(c.real * c.real + c.imag * c.imag); 40 } 41 42 Complex add(const Complex &a, const Complex &b) { 43 return Complex(a.real + b.real, a.imag + b.imag); 44 } 45 46 bool is_equal(const Complex &a, const Complex &b) { 47 return (a.real==b.real) && (a.imag==b.imag); 48 } 49 50 bool is_not_equal(const Complex &a, const Complex &b) { 51 return !is_equal(a, b); 52 }
1 #include "Complex.h" 2 #include <iostream> 3 #include <iomanip> 4 #include <complex> 5 6 void test_Complex(); 7 void test_std_complex(); 8 9 int main() { 10 std::cout << "*******测试1: 自定义类Complex*******\n"; 11 test_Complex(); 12 13 std::cout << "\n*******测试2: 标准库模板类complex*******\n"; 14 test_std_complex(); 15 } 16 17 void test_Complex() { 18 using std::cout; 19 using std::endl; 20 using std::boolalpha; 21 22 cout << "类成员测试: " << endl; 23 cout << Complex::doc << endl << endl; 24 25 cout << "Complex对象测试: " << endl; 26 Complex c1; 27 Complex c2(3, -4); 28 Complex c3(c2); 29 Complex c4 = c2; 30 const Complex c5(3.5); 31 32 cout << "c1 = "; output(c1); cout << endl; 33 cout << "c2 = "; output(c2); cout << endl; 34 cout << "c3 = "; output(c3); cout << endl; 35 cout << "c4 = "; output(c4); cout << endl; 36 cout << "c5.real = " << c5.get_real() 37 << ", c5.imag = " << c5.get_imag() << endl << endl; 38 39 cout << "复数运算测试: " << endl; 40 cout << "abs(c2) = " << abs(c2) << endl; 41 c1.add(c2); 42 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 43 cout << boolalpha; 44 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 45 cout << "c1 != c2 : " << is_not_equal(c1, c2) << endl; 46 c4 = add(c2, c3); 47 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 48 } 49 50 void test_std_complex() { 51 using std::cout; 52 using std::endl; 53 using std::boolalpha; 54 55 cout << "std::complex<double>对象测试: " << endl; 56 std::complex<double> c1; 57 std::complex<double> c2(3, -4); 58 std::complex<double> c3(c2); 59 std::complex<double> c4 = c2; 60 const std::complex<double> c5(3.5); 61 62 cout << "c1 = " << c1 << endl; 63 cout << "c2 = " << c2 << endl; 64 cout << "c3 = " << c3 << endl; 65 cout << "c4 = " << c4 << endl; 66 67 cout << "c5.real = " << c5.real() 68 << ", c5.imag = " << c5.imag() << endl << endl; 69 70 cout << "复数运算测试: " << endl; 71 cout << "abs(c2) = " << abs(c2) << endl; 72 c1 += c2; 73 cout << "c1 += c2, c1 = " << c1 << endl; 74 cout << boolalpha; 75 cout << "c1 == c2 : " << (c1 == c2)<< endl; 76 cout << "c1 != c2 : " << (c1 != c2) << endl; 77 c4 = c2 + c3; 78 cout << "c4 = c2 + c3, c4 = " << c4 << endl; 79 }
截图:

Q1:标准库模板类 complex 的用法更简洁,函数和运算内在逻辑一致。
Q2-1:是,因这些函数需直接访问私有成员 real 和 imag 以实现各自功能。
Q2-2:是,std::abs (std::complex) 需访问其私有成员,故设为友元。
Q2-3:当外部函数、类需访问类的私有成员且无法通过公有接口实现时使用。
Q3:将拷贝构造函数声明为私有即可禁用该形式。
任务3:
源代码:
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 };
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 lower_str; 16 for (auto c : control_str) { 17 lower_str += static_cast<char>(tolower(c)); 18 } 19 20 total_cnt++; 21 if (lower_str == "play") { 22 return ControlType::Play; 23 } else if (lower_str == "pause") { 24 return ControlType::Pause; 25 } else if (lower_str == "next") { 26 return ControlType::Next; 27 } else if (lower_str == "prev") { 28 return ControlType::Prev; 29 } else if (lower_str == "stop") { 30 return ControlType::Stop; 31 } else { 32 return ControlType::Unknown; 33 } 34 35 } 36 37 void PlayerControl::execute(ControlType cmd) const { 38 switch (cmd) { 39 case ControlType::Play: std::cout << "[play] Playing music...\n"; break; 40 case ControlType::Pause: std::cout << "[Pause] Music paused\n"; break; 41 case ControlType::Next: std::cout << "[Next] Skipping to next track\n"; break; 42 case ControlType::Prev: std::cout << "[Prev] Back to previous track\n"; break; 43 case ControlType::Stop: std::cout << "[Stop] Music stopped\n"; break; 44 default: std::cout << "[Error] unknown control\n"; break; 45 } 46 } 47 48 int PlayerControl::get_cnt() { 49 return total_cnt; 50 }
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 }
截图:

选做:可以使用使用 Unicode 编码实现。
任务4:
源代码:
1 #pragma once 2 3 #include <string> 4 #include <iostream> 5 6 class Fraction { 7 public: 8 static const std::string doc; 9 10 Fraction(int u=0, int d=1); 11 Fraction(const Fraction &t); 12 13 int get_up() const; 14 int get_down() const; 15 16 Fraction negative() const; 17 18 private: 19 int up; 20 int down; 21 22 int gcd(int a, int b) const; 23 }; 24 25 void output(const Fraction& f); 26 Fraction add(const Fraction& f1, const Fraction& f2); 27 Fraction sub(const Fraction& f1, const Fraction& f2); 28 Fraction mul(const Fraction& f1, const Fraction& f2); 29 Fraction div(const Fraction& f1, const Fraction& f2);
1 #include "Fraction.h" 2 #include <stdexcept> 3 #include <iostream> 4 #include <cstdlib> 5 6 const std::string Fraction::doc = "Fraction类 v 0.01版. \n目前仅支持分数对象的构造、输出、加/减/乘/除运算."; 7 8 9 Fraction::Fraction(int u, int d) : up(u), down(d) { 10 if (down < 0) { 11 up *= -1; 12 down *= -1; 13 } 14 int common = gcd(up, down); 15 if (common != 0) { 16 up /= common; 17 down /= common; 18 } 19 } 20 21 Fraction::Fraction(const Fraction &t) : up(t.up), down(t.down) {} 22 23 int Fraction::get_up() const { 24 return up; 25 } 26 27 int Fraction::get_down() const { 28 return down; 29 } 30 31 Fraction Fraction::negative() const { 32 return Fraction(-up, down); 33 } 34 35 //辗转相除法求最大公约数 36 int Fraction::gcd(int a, int b) const { 37 a = std::abs(a); 38 b = std::abs(b); 39 while (b != 0) { 40 int temp = b; 41 b = a % b; 42 a = temp; 43 } 44 return a; 45 } 46 47 48 void output(const Fraction& f) { 49 if(f.get_down()==1){ 50 std::cout << f.get_up(); 51 }else if(f.get_down()==0){ 52 std::cout << "分母不能为0" << std::endl; 53 }else 54 std::cout << f.get_up() << "/" << f.get_down(); 55 } 56 57 Fraction add(const Fraction& f1, const Fraction& f2) { 58 int new_up = f1.get_up() * f2.get_down() + f2.get_up() * f1.get_down(); 59 int new_down = f1.get_down() * f2.get_down(); 60 return Fraction(new_up, new_down); 61 } 62 63 Fraction sub(const Fraction& f1, const Fraction& f2) { 64 return add(f1, f2.negative()); 65 } 66 67 Fraction mul(const Fraction& f1, const Fraction& f2) { 68 int new_up = f1.get_up() * f2.get_up(); 69 int new_down = f1.get_down() * f2.get_down(); 70 return Fraction(new_up, new_down); 71 } 72 73 Fraction div(const Fraction& f1, const Fraction& f2) { 74 int new_up = f1.get_up() * f2.get_down(); 75 int new_down = f1.get_down() * f2.get_up(); 76 return Fraction(new_up, new_down); 77 }
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 }
截图:

Q:选择自由函数方案。
理由:自由函数无需依赖类的内部实现细节,相比友元更易维护,比静态成员函数更体现运算的独立性,无需额外命名空间即可清晰组织。
实验总结:
~加深了我对类中属性、函数的认识,清楚了友元/自由函数/命名空间/类的区别。
~加深了我对类中属性、函数的认识,清楚了友元/自由函数/命名空间/类的区别。
浙公网安备 33010602011771号