oop实验二

实验二

task1

源代码:

 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 #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 
 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 }

 

运行截图:task1

问题1:删掉后不可以运行

报错显示func()未声明,说明func()函数是类外的友函数,必须要声明

task1问题1

问题2:普通构造函数主要用来初始化变量,在显示创建对象时使用

             复制构造函数用于复制对象,比如要使用的对象需返回时

             移动构造函数用于快速创建新的对象,在转移初始化对象时可以使用

              析构函数用于释放对象占用的资源,比如对象被删除时使用

问题3:还是不可以,原因是类的成员出现在了类声明的变量之外,这会导致关联错误

task1问题3

 

 

task2

源代码:

 1 #pragma once
 2 #include <string>
 3 
 4 class Complex{
 5   public:
 6     static const std::string doc;
 7 
 8     Complex ();
 9     Complex (double r);
10     Complex (double r, double i);
11     Complex (const Complex& z);
12   private:
13     double real;
14     double imag;
15   public:
16     double get_real() const;
17     double get_imag() const;
18     void add(const Complex& z);
19 
20     friend void output(const Complex& z);
21     friend double abs(const Complex& z);
22     friend Complex add(const Complex& z1, const Complex& z2);
23     friend bool is_equal(const Complex& z1, const Complex& z2);
24     friend bool is_not_equal(const Complex& z1, const Complex& z2);
25 };
 1 #include "Complex.h"
 2 #include <iostream>
 3 #include <cmath>
 4 
 5 const std::string Complex::doc{"a simplified Complex class"};
 6 
 7 Complex::Complex () :real(0.0), imag(0.0) {};
 8 Complex::Complex (double r) :real(r), imag(0.0) {};
 9 Complex::Complex (double r1, double r2) :real(r1), imag(r2) {};
10 Complex::Complex (const Complex &c) :real(c.real), imag(c.imag) {};
11 
12 double Complex::get_real () const {return real;}
13 double Complex::get_imag () const {return imag;}
14 void Complex::add(const Complex& z) {
15   real = real + z.real;
16   imag = imag + z.imag;
17 }
18 
19 void output(const Complex& z) {
20   std::cout << z.real << (z.imag >= 0?"+":"-" )<< std::abs(z.imag) << "i";
21 }
22 double abs(const Complex& z) {
23   return sqrt(z.real*z.real + z.imag*z.imag);
24 }
25 Complex add(const Complex& z1, const Complex& z2) {
26   Complex z;
27   z.real = z1.real + z2.real;
28   z.imag = z1.imag + z2.imag;
29   return z;
30 }
31 bool is_equal(const Complex& z1, const Complex& z2) {
32   return z1.real == z2.real && z1.imag == z2.imag;
33 }
34 bool is_not_equal (const Complex& z1, const Complex& z2) {
35   return z1.real != z2.real || z1.imag != z2.imag;
36 }
 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 {
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 {
20     using std::boolalpha;
21     using std::cout;
22     using std::endl;
23 
24     cout << "类成员测试: " << endl;
25     cout << Complex::doc << endl
26          << endl;
27 
28     cout << "Complex对象测试: " << endl;
29     Complex c1;
30     Complex c2(3, -4);
31     Complex c3(c2);
32     Complex c4 = c2;
33     const Complex c5(3.5);
34 
35     cout << "c1 = ";
36     output(c1);
37     cout << endl;
38     cout << "c2 = ";
39     output(c2);
40     cout << endl;
41     cout << "c3 = ";
42     output(c3);
43     cout << endl;
44     cout << "c4 = ";
45     output(c4);
46     cout << endl;
47     cout << "c5.real = " << c5.get_real()
48          << ", c5.imag = " << c5.get_imag() << endl
49          << endl;
50 
51     cout << "复数运算测试: " << endl;
52     cout << "abs(c2) = " << abs(c2) << endl;
53     c1.add(c2);
54     cout << "c1 += c2, c1 = ";
55     output(c1);
56     cout << endl;
57     cout << boolalpha;
58     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
59     cout << "c1 != c2 : " << is_not_equal(c1, c2) << endl;
60     c4 = add(c2, c3);
61     cout << "c4 = c2 + c3, c4 = ";
62     output(c4);
63     cout << endl;
64 }
65 
66 void test_std_complex()
67 {
68     using std::boolalpha;
69     using std::cout;
70     using std::endl;
71 
72     cout << "std::complex<double>对象测试: " << endl;
73     std::complex<double> c1;
74     std::complex<double> c2(3, -4);
75     std::complex<double> c3(c2);
76     std::complex<double> c4 = c2;
77     const std::complex<double> c5(3.5);
78 
79     cout << "c1 = " << c1 << endl;
80     cout << "c2 = " << c2 << endl;
81     cout << "c3 = " << c3 << endl;
82     cout << "c4 = " << c4 << endl;
83 
84     cout << "c5.real = " << c5.real()
85          << ", c5.imag = " << c5.imag() << endl
86          << endl;
87 
88     cout << "复数运算测试: " << endl;
89     cout << "abs(c2) = " << abs(c2) << endl;
90     c1 += c2;
91     cout << "c1 += c2, c1 = " << c1 << endl;
92     cout << boolalpha;
93     cout << "c1 == c2 : " << (c1 == c2) << endl;
94     cout << "c1 != c2 : " << (c1 != c2) << endl;
95     c4 = c2 + c3;
96     cout << "c4 = c2 + c3, c4 = " << c4 << endl;
97 }

问题1:标准库自然就更简洁一些,无需调用局部函数,关联上都是相同数据类型的进行操作

问题2-1:是,因为output需要访问实部与虚部,abs需要访问虚部,add需要操作两个部分

2-2;否,real与imag都不是友元单位

2-3:考虑函数friend

问题3:将复制构造函数设为私有

task3

源代码:

 1 #include "PlayerControl.h"
 2 #include <iostream>
 3 #include <algorithm>   
 4 
 5 int PlayerControl::total_cnt = 0;
 6 
 7 PlayerControl::PlayerControl() {}
 8 
 9 ControlType PlayerControl::parse(const std::string& control_str) {
10     std::string lower_str=control_str;
11     std::transform(lower_str.begin(),lower_str.end(),lower_str.begin(),
12                    [](unsigned char c){ return std::tolower(c); });
13     if (lower_str=="play"){
14         total_cnt++;  
15         return ControlType::Play;
16     }else if(lower_str=="pause"){
17         total_cnt++;
18         return ControlType::Pause;
19     }else if(lower_str=="next"){
20         total_cnt++;
21         return ControlType::Next;
22     }else if(lower_str=="prev"){
23         total_cnt++;
24         return ControlType::Prev;
25     } else if(lower_str =="stop"){
26         total_cnt++;
27         return ControlType::Stop;
28     }else{
29         return ControlType::Unknown;
30     }
31 }
32 
33 void PlayerControl::execute(ControlType cmd) const {
34     switch (cmd) {
35     case ControlType::Play:  std::cout << "[play] Playing music...\n"; break;
36     case ControlType::Pause: std::cout << "[Pause] Music paused\n";    break;
37     case ControlType::Next:  std::cout << "[Next] Skipping to next track\n"; break;
38     case ControlType::Prev:  std::cout << "[Prev] Back to previous track\n"; break;
39     case ControlType::Stop:  std::cout << "[Stop] Music stopped\n"; break;
40     default:                 std::cout << "[Error] unknown control\n"; break;
41     }
42 }
43 
44 int PlayerControl::get_cnt() {
45     return total_cnt;
46 }
 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 
 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 }

 

运行截图:task3

 

task4

源代码:

 1 #include "Fraction.h"
 2 #include<iostream>
 3 #include<cstdlib>
 4 //开始 
 5 inline int gcd(int a, int b)
 6 {
 7     a = std::abs(a);
 8     b = std::abs(b);
 9     while (b != 0)
10     {
11         int t = b;
12         b = a % b;
13         a = t;
14     }
15     return a;
16 }
17 
18 const std::string Fraction::doc{"Fraction类 v 0.01版. 目前仅支持分数对象的构造、输出、加/减/乘/除运算."};
19 
20 Fraction::Fraction(int u) : up{u}, down{1} {};
21 Fraction::Fraction(int u,int d) : up{u}, down{d} {
22     if(down<0){
23         up=-up;
24         down=-down;
25     }
26     int a = gcd(up,down);
27     up = up / a;
28     down = down / a;
29 };
30 Fraction::Fraction(const Fraction &s):up{s.up},down{s.down}{}
31 
32 int Fraction::get_up() const
33 {
34     return up;
35 }
36 
37 int Fraction::get_down()const
38 {
39     return down;
40 }
41 Fraction Fraction::negative()const
42 {
43     return Fraction(-up,down);
44 }
45 
46 void output(const Fraction &s){
47     if(s.down == 0)
48         std::cout << "分母不能为0";
49     else if(s.down == 1)
50         std::cout<<s.up;
51     else
52         std::cout<<s.up<<"/"<<s.down;
53 }
54 Fraction add(const Fraction &s1, const Fraction &s2){
55     return Fraction(s1.up * s2.down + s2.up * s1.down, s1.down * s2.down);
56 }
57 Fraction sub(const Fraction &s1, const Fraction &s2){
58     return Fraction(s1.up * s2.down - s2.up * s1.down, s1.down * s2.down);
59 }
60 Fraction mul(const Fraction &s1, const Fraction &s2){
61     return Fraction(s1.up*s2.up,s1.down*s2.down);
62 }
63 Fraction div(const Fraction &s1, const Fraction &s2)
64 {
65     if(s2.up==0)
66          return Fraction(s2.down,0);
67     return Fraction(s1.up * s2.down, s1.down * s2.up);
68 }
 1 #pragma once
 2 #include<string>
 3 //开始 
 4 class Fraction{
 5 public:
 6     static const std::string doc;
 7 
 8     Fraction(int u);
 9     Fraction(int u,int d);
10     Fraction(const Fraction&s);
11 
12     int get_up()const;
13     int get_down()const;
14     Fraction negative()const;
15 
16     friend void output(const Fraction &s);
17     friend Fraction add(const Fraction &s1,const Fraction &s2);
18     friend Fraction sub(const Fraction &s1, const Fraction &s2);
19     friend Fraction mul(const Fraction &s1, const Fraction &s2);
20     friend Fraction div(const Fraction &s1, const Fraction &s2);
21 
22 private :
23     int up,down;
24 };
 1 #include "Fraction.h"
 2 #include <iostream>
 3 //开始 
 4 void test1();
 5 void test2();
 6 
 7 int main()
 8 {
 9     std::cout << "测试1: Fraction类基础功能测试\n";
10     test1();
11 
12     std::cout << "\n测试2: 分母为0测试: \n";
13     test2();
14 }
15 
16 void test1()
17 {
18     using std::cout;
19     using std::endl;
20 
21     cout << "Fraction类测试: " << endl;
22     cout << Fraction::doc << endl
23          << endl;
24 
25     Fraction f1(5);
26     Fraction f2(3, -4), f3(-18, 12);
27     Fraction f4(f3);
28     cout << "f1 = ";
29     output(f1);
30     cout << endl;
31     cout << "f2 = ";
32     output(f2);
33     cout << endl;
34     cout << "f3 = ";
35     output(f3);
36     cout << endl;
37     cout << "f4 = ";
38     output(f4);
39     cout << endl;
40 
41     const Fraction f5(f4.negative());
42     cout << "f5 = ";
43     output(f5);
44     cout << endl;
45     cout << "f5.get_up() = " << f5.get_up()
46          << ", f5.get_down() = " << f5.get_down() << endl;
47 
48     cout << "f1 + f2 = ";
49     output(add(f1, f2));
50     cout << endl;
51     cout << "f1 - f2 = ";
52     output(sub(f1, f2));
53     cout << endl;
54     cout << "f1 * f2 = ";
55     output(mul(f1, f2));
56     cout << endl;
57     cout << "f1 / f2 = ";
58     output(div(f1, f2));
59     cout << endl;
60     cout << "f4 + f5 = ";
61     output(add(f4, f5));
62     cout << endl;
63 }
64 
65 void test2()
66 {
67     using std::cout;
68     using std::endl;
69 
70     Fraction f6(42, 55), f7(0, 3);
71     cout << "f6 = ";
72     output(f6);
73     cout << endl;
74     cout << "f7 = ";
75     output(f7);
76     cout << endl;
77     cout << "f6 / f7 = ";
78     output(div(f6, f7));
79     cout << endl;
80 }

 

运行截图:test4

 

问题:选用友元函数,访问更简单

优点就是访问简单,但是封装性很差

总结:实验二难度提升了很多,特别是task2很有难度,做起来很吃力,花费的时间和汇报比例不是很高。当然对于友元函数的使用以及认识都更深一些,有利有弊吧。

 

posted on 2025-10-27 18:14  安宁的空白  阅读(8)  评论(2)    收藏  举报