实验2
任务1源代码:
1 #pragma once 2 #include<string> 3 4 //类T:声明 5 class T 6 { 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 public: 24 static const std::string doc;//类T的描述信息 25 static const int max_cnt; //类T对象上限 26 27 private: 28 static int cnt; //当前T类对象数目 29 30 //类T友元函数声明 31 friend void func(); 32 }; 33 34 //普通函数声明 35 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 { 30 ++cnt; 31 std::cout << "T move constructor called.\n"; 32 } 33 34 T::~T() { 35 --cnt; 36 std::cout << "T destructor called.\n"; 37 } 38 39 void T::adjust(int ratio) 40 { 41 m1 *= ratio; 42 m2 *= ratio; 43 } 44 45 void T::display()const 46 { 47 std::cout << "(" << m1 << "," << m2 << ")"; 48 } 49 50 //普通函数实现 51 void func() 52 { 53 T t5(42); 54 t5.m2 = 2049; 55 std::cout << "t5 = ";t5.display();std::cout << '\n'; 56 57 }
1 #include "T.h" 2 #include<iostream> 3 4 void test_T(); 5 6 int main() 7 { 8 std::cout << "test class T:\n"; 9 test_T(); 10 11 std::cout << "\ntest friend func: \n"; 12 func(); 13 } 14 15 void test_T() 16 { 17 using std::cout; 18 using std::endl; 19 20 cout << "T info: " << T::doc << endl; 21 cout << "T objects'max count: " << T::max_cnt << endl; 22 cout << "T objects'current count: " << T::get_cnt() << endl << endl; 23 24 T t1; 25 cout << "t1 = ";t1.display();cout << endl; 26 27 T t2(3, 4); 28 cout << "t2 = ";t2.display();cout << endl; 29 30 T t3(t2); 31 t3.adjust(2); 32 cout << "t3 = ";t3.display();cout << endl; 33 34 T t4(std::move(t2)); 35 cout << "t4 = ";t4.display();cout << endl; 36 37 cout << "test: T objects'current count: " << T::get_cnt() << endl; 38 39 40 }
任务1截图:

问题:
1:vs2022中仍可运行,devc++中报错

2:
普通构造函数:作用为初始化对象,调用时机是当对象不提供任何实参或提供整数实参时调用
拷贝构造函数:作用是用已存在的对象的成员值初始化新对象,调用时机为用一个对象初始化另一个新对象时
移动构造函数:作用为调用一个即将销毁的对象资源,调用时机为用一个临时对象初始化新对象时
析构函数:作用为清理资源,调用时机为作用域结束
3:
vs可以正常运行
任务2源代码:
1 #pragma once 2 #include<string> 3 4 class Complex 5 { 6 private: 7 double real; //虚部 8 double imag; //实部 9 10 public: 11 static const std::string doc; 12 13 //构造函数 14 Complex(double r = 0.0, double i = 0.0); //默认构造函数 15 Complex(const Complex& other); //复制构造函数 16 17 //成员函数 18 double get_real() const; 19 double get_imag() const; 20 void add(const Complex& other); 21 22 //友元函数 23 friend bool is_equal(const Complex& c1, const Complex& c2); 24 friend bool is_not_equal(const Complex& c1, const Complex& c2); 25 friend double abs(const Complex& c); 26 friend Complex add(const Complex& c1, Complex& c2); 27 friend void output(const Complex& c); 28 29 };
1 #include"complex.h" 2 #include<iostream> 3 #include<string> 4 #include<iomanip> 5 #include<cmath> 6 7 const std::string Complex::doc{ "a simple class sample" }; 8 9 Complex::Complex(double r,double i):real(r),imag(i){} 10 Complex::Complex(const Complex& other):real(other.real),imag(other.imag){} 11 12 13 double Complex::get_real() const { 14 15 return real; 16 } 17 double Complex::get_imag()const { 18 return imag; 19 } 20 21 void Complex::add(const Complex& other) { 22 real += other.real; 23 imag += other.imag; 24 } 25 26 bool is_equal(const Complex& c1, const Complex& c2) { 27 if (c1.real == c2.real && c1.imag == c2.imag) 28 { 29 return true; 30 } 31 else 32 return false; 33 } 34 35 bool is_not_equal(const Complex& c1, const Complex& c2) { 36 if (c1.real == c2.real && c1.imag == c2.imag) 37 { 38 return false; 39 } 40 else 41 return true; 42 } 43 44 double abs(const Complex& c) { 45 double mol; 46 mol = sqrt(c.real * c.real + c.imag * c.imag); 47 return mol; 48 } 49 50 Complex add(const Complex& c1, Complex& c2) { 51 return Complex(c1.real + c2.real, c1.imag + c2.imag); 52 } 53 54 void output(const Complex& c) { 55 if (c.imag >= 0) 56 { 57 std::cout << c.real <<" + "<< c.imag << "i"; 58 } 59 else 60 { 61 62 std::cout << c.real << " - " << -c.imag << "i"; 63 } 64 65 }
1 // 待补足头文件 2 #include"complex.h" 3 #include <iostream> 4 #include <iomanip> 5 #include <complex> 6 7 void test_Complex(); 8 void test_std_complex(); 9 10 int main() { 11 std::cout << "*******测试1: 自定义类Complex*******\n"; 12 test_Complex(); 13 14 std::cout << "\n*******测试2: 标准库模板类complex*******\n"; 15 test_std_complex(); 16 } 17 18 void test_Complex() { 19 using std::cout; 20 using std::endl; 21 using std::boolalpha; 22 23 cout << "类成员测试: " << endl; 24 cout << Complex::doc << endl << endl; 25 26 cout << "Complex对象测试: " << endl; 27 Complex c1; 28 Complex c2(3, -4); 29 Complex c3(c2); 30 Complex c4 = c2; 31 const Complex c5(3.5); 32 33 cout << "c1 = "; output(c1); cout << endl; 34 cout << "c2 = "; output(c2); cout << endl; 35 cout << "c3 = "; output(c3); cout << endl; 36 cout << "c4 = "; output(c4); cout << endl; 37 cout << "c5.real = " << c5.get_real() 38 << ", c5.imag = " << c5.get_imag() << endl << endl; 39 40 cout << "复数运算测试: " << endl; 41 cout << "abs(c2) = " << abs(c2) << endl; 42 c1.add(c2); 43 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 44 cout << boolalpha; 45 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 46 cout << "c1 != c2 : " << is_not_equal(c1, c2) << endl; 47 c4 = add(c2, c3); 48 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 49 } 50 51 void test_std_complex() { 52 using std::cout; 53 using std::endl; 54 using std::boolalpha; 55 56 cout << "std::complex<double>对象测试: " << endl; 57 std::complex<double> c1; 58 std::complex<double> c2(3, -4); 59 std::complex<double> c3(c2); 60 std::complex<double> c4 = c2; 61 const std::complex<double> c5(3.5); 62 63 cout << "c1 = " << c1 << endl; 64 cout << "c2 = " << c2 << endl; 65 cout << "c3 = " << c3 << endl; 66 cout << "c4 = " << c4 << endl; 67 68 cout << "c5.real = " << c5.real() 69 << ", c5.imag = " << c5.imag() << endl << endl; 70 71 cout << "复数运算测试: " << endl; 72 cout << "abs(c2) = " << abs(c2) << endl; 73 c1 += c2; 74 cout << "c1 += c2, c1 = " << c1 << endl; 75 cout << boolalpha; 76 cout << "c1 == c2 : " << (c1 == c2) << endl; 77 cout << "c1 != c2 : " << (c1 != c2) << endl; 78 c4 = c2 + c3; 79 cout << "c4 = c2 + c3, c4 = " << c4 << endl; 80 }
任务2截图:

问题:
1:标准库模版更简洁,函数和运算内在有关联
2.1:asb不是友元函数,因为可以通过公共成员函数获取数据
2.2:函数无法通过公用接口完成操作时
2.3:显式声明构造函数,比如在复制构造函数前加关键字explicit修饰
任务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 #include <cctype> 5 6 int PlayerControl::total_cnt = 0; 7 PlayerControl::PlayerControl() {} 8 9 10 // 待补足 11 // 1. 将输入字符串转为小写,实现大小写不敏感 12 // 2. 匹配"play"/"pause"/"next"/"prev"/"stop"并返回对应枚举 13 // 3. 未匹配的字符串返回ControlType::Unknown 14 // 4. 每次成功调用parse时递增total_cnt 15 ControlType PlayerControl::parse(const std::string& control_str) { 16 std::string str = control_str; 17 std::transform(str.begin(), str.end(), str.begin(), ::tolower); 18 ++total_cnt; 19 if (str == "play")return ControlType::Play; 20 if (str == "pause")return ControlType::Pause; 21 if (str == "next")return ControlType::Next; 22 if (str == "prev")return ControlType::Prev; 23 if (str == "stop")return ControlType::Stop; 24 return ControlType::Unknown; 25 } 26 void PlayerControl::execute(ControlType cmd) const { 27 switch (cmd) { 28 case ControlType::Play: 29 std::cout << "[play] Playing music...\n"; break; 30 case ControlType::Pause: 31 std::cout << "[Pause] Music paused\n";break; 32 case ControlType::Next: 33 std::cout << "[Next] Skipping to next track\n"; break; 34 case ControlType::Prev: 35 std::cout << "[Prev] Back to previous track\n"; break; 36 case ControlType::Stop: 37 std::cout << "[Stop] Music stopped\n"; break; 38 default: 39 std::cout << "[Error] unknown control\n"; break; 40 } 41 } 42 int PlayerControl::get_cnt() { 43 return total_cnt; 44 }
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 }
任务3截图:

任务4源代码:
1 #pragma once 2 #include<string> 3 4 class Fraction { 5 private: 6 int up; 7 int down; 8 9 void reduce();//约分 10 int gcd(int a, int b)const;//求最大公约数 11 12 public: 13 static const std::string doc; 14 15 //构造函数 16 Fraction(int numerator = 0, int denominator = 1); 17 Fraction(const Fraction& other); 18 19 //成员函数 20 int get_up()const; 21 int get_down()const; 22 Fraction negative()const; 23 24 //友元函数声明 25 26 friend void output(const Fraction& f); 27 friend Fraction add(const Fraction& f1, const Fraction& f2); 28 friend Fraction sub(const Fraction& f1, const Fraction& f2); 29 friend Fraction mul(const Fraction& f1, const Fraction& f2); 30 friend Fraction div(const Fraction& f1, const Fraction& f2); 31 32 };
1 #include"Fraction.h" 2 #include<iostream> 3 #include<cmath> 4 5 6 const std::string Fraction::doc = 7 "Fraction类 v 0.01版." 8 "目前仅支持分数对象的构造、输出、加 / 减 / 乘 / 除运算." ; 9 10 11 int Fraction::gcd(int a, int b)const { 12 if (a < 0) { 13 a = -a; 14 } 15 if (b < 0) { 16 b = -b; 17 } 18 while (b) 19 { 20 int t = b; 21 b = a % b; 22 a = t; 23 } 24 return a; 25 26 27 } 28 29 void Fraction::reduce() { 30 int common = gcd(up, down); 31 if (common != 0) 32 { 33 up /= common; 34 down /= common; 35 } 36 } 37 38 39 Fraction::Fraction(int numerator, int denominator) :up(numerator), down(denominator) { 40 reduce(); 41 } 42 43 Fraction::Fraction(const Fraction& other):up(other.up),down(other.down){} 44 45 int Fraction::get_up()const { 46 return up; 47 } 48 49 int Fraction::get_down()const { 50 return down; 51 } 52 53 Fraction Fraction::negative()const { 54 return Fraction(-up, down); 55 } 56 57 void output(const Fraction& f) { 58 if (f.down == 0) { 59 std::cout << "分母不能为0"; 60 return; 61 } 62 if (f.down == 1) { 63 std::cout << f.up; 64 return; 65 } 66 if (f.up < 0 && f.down < 0) { 67 std::cout << -f.up << "/" << -f.down; 68 } 69 if (f.up > 0 && f.down > 0) { 70 std::cout << f.up << "/" << f.down; 71 } 72 if (f.up < 0 && f.down > 0) { 73 std::cout << f.up << "/" << f.down; 74 } 75 if (f.up > 0 && f.down < 0) { 76 std::cout << -f.up << "/" << -f.down; 77 } 78 } 79 80 Fraction add(const Fraction& f1, const Fraction& f2) { 81 int _down = f1.down * f2.down; 82 int _up = f1.up * f2.down + f2.up * f1.down; 83 return Fraction(_up, _down); 84 } 85 86 Fraction sub(const Fraction& f1, const Fraction& f2) { 87 int _down = f1.down * f2.down; 88 int _up = f1.up * f2.down - f2.up * f1.down; 89 return Fraction(_up, _down); 90 } 91 92 93 Fraction mul(const Fraction& f1, const Fraction& f2) { 94 int _down = f1.down * f2.down; 95 int _up = f1.up*f2.up; 96 return Fraction(_up, _down); 97 } 98 99 100 Fraction div(const Fraction& f1, const Fraction& f2) { 101 int _down = f1.down * f2.up; 102 int _up = f1.up * f2.down; 103 return Fraction(_up, _down); 104 }
1 #include "Fraction.h" 2 #include <iostream> 3 4 5 void test1(); 6 void test2(); 7 8 int main() { 9 std::cout << "测试1: Fraction类基础功能测试\n"; 10 test1(); 11 std::cout << "\n测试2: 分母为0测试: \n"; 12 test2(); 13 } 14 void test1() { 15 using std::cout; 16 using std::endl; 17 cout << "Fraction类测试: " << endl; 18 cout << Fraction::doc << endl << endl; 19 Fraction f1(5); 20 Fraction f2(3, -4), f3(-18, 12); 21 Fraction f4(f3); 22 cout << "f1 = "; output(f1); cout << endl; 23 cout << "f2 = "; output(f2); cout << endl; 24 cout << "f3 = "; output(f3); cout << endl; 25 cout << "f4 = "; output(f4); cout << endl; 26 const Fraction f5(f4.negative()); 27 cout << "f5 = "; output(f5); cout << endl; 28 cout << "f5.get_up() = " << f5.get_up() 29 << ", f5.get_down() = " << f5.get_down() << endl; 30 cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl; 31 cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl; 32 cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl; 33 cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl; 34 cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; 35 } 36 void test2() { 37 using std::cout; 38 using std::endl; 39 Fraction f6(42, 55), f7(0, 3); 40 cout << "f6 = "; output(f6); cout << endl; 41 cout << "f7 = "; output(f7); cout << endl; 42 cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; 43 }
任务4截图:

问题:我选择的是友元函数方案;
因为分数运算需要同时访问两个操作数的分子分母,友元函数可以直接访问私有成员

浙公网安备 33010602011771号