實驗2 類和物件_基礎程式設計1

枫羽秋茜發表於2024-11-03

實驗任務1:

實驗程式碼:

實驗2 類和物件_基礎程式設計1
 1 #include <string>
 2 
 3 // 類T: 宣告
 4 class T {
 5 // 物件屬性、方法
 6 public:
 7     T(int x = 0, int y = 0);   // 普通建構函式
 8     T(const T &t);  // 複製建構函式
 9     T(T &&t);       // 移動建構函式
10     ~T();           // 解構函式
11 
12     void adjust(int ratio);      // 按係數成倍調整資料
13     void display() const;           // 以(m1, m2)形式顯示T類物件資訊
14 
15 private:
16     int m1, m2;
17 
18 // 類屬性、方法
19 public:
20     static int get_cnt();          // 顯示當前T類物件總數
21 
22 public:
23     static const std::string doc;       // 類T的描述資訊
24     static const int max_cnt;           // 類T物件上限
25 
26 private:
27     static int cnt;         // 當前T類物件數目
28 
29 // 類T友元函式宣告
30     friend void func();
31 };
32 
33 // 普通函式宣告
34 void func();
t.h
實驗2 類和物件_基礎程式設計1
 1 // 類T: 實現
 2 // 普通函式實現
 3 
 4 #include "t.h"
 5 #include <iostream>
 6 #include <string>
 7 
 8 using std::cout;
 9 using std::endl;
10 using std::string;
11 
12 // static成員資料類外初始化
13 const std::string T::doc{"a simple class sample"};
14 const int T::max_cnt = 999;
15 int T::cnt = 0;
16 
17 
18 // 物件方法
19 T::T(int x, int y): m1{x}, m2{y} { 
20     ++cnt; 
21     cout << "T constructor called.\n";
22 } 
23 
24 T::T(const T &t): m1{t.m1}, m2{t.m2} {
25     ++cnt;
26     cout << "T copy constructor called.\n";
27 }
28 
29 T::T(T &&t): m1{t.m1}, m2{t.m2} {
30     ++cnt;
31     cout << "T move constructor called.\n";
32 }    
33 
34 T::~T() {
35     --cnt;
36     cout << "T destructor called.\n";
37 }           
38 
39 void T::adjust(int ratio) {
40     m1 *= ratio;
41     m2 *= ratio;
42 }    
43 
44 void T::display() const {
45     cout << "(" << m1 << ", " << m2 << ")" ;
46 }     
47 
48 // 類方法
49 int T::get_cnt() {
50    return cnt;
51 }
52 
53 // 友元
54 void func() {
55     T t5(42);
56     t5.m2 = 2049;
57     cout << "t5 = "; t5.display(); cout << endl;
58 }
t.cpp
實驗2 類和物件_基礎程式設計1
 1 #include "t.h"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 void test();
 8 
 9 int main() {
10     test();
11     cout << "\nmain: \n";
12     cout << "T objects'current count: " << T::get_cnt() << endl;
13 }
14 
15 void test() {
16     cout << "test class T: \n";
17     cout << "T info: " << T::doc << endl;
18     cout << "T objects'max count: " << T::max_cnt << endl;
19     cout << "T objects'current count: " << T::get_cnt() << endl << endl;
20 
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 << "t3 = "; t4.display(); cout << endl;
34 
35     cout << "T objects'current count: " << T::get_cnt() << endl;
36 
37     func();
38 }
task1.cpp

執行結果截圖:

問題1:

不能執行。

報錯截圖:

可能原因:

友元宣告在類內,定義在類外。如果不在類外進行普通函式宣告的話,那麼在task1.cpp進行編譯的時候,由於在類外無法訪問類內的私有成員,將找不到t.h中關於func 宣告。

問題2:

功能:

普通建構函式 T(int x = 0, int y = 0):這是類的預設建構函式,用於初始化物件的成員變數 m1m2。當建立類 T 的物件時,如果沒有提供特定的建構函式引數,將呼叫此建構函式。

複製建構函式 T(const T &t):這個建構函式用於建立一個新物件,它是另一個同型別物件的副本。它透過複製已有物件的成員變數來初始化新物件。

移動建構函式 T(T &&t):移動建構函式用於在不進行復制的情況下,將資源從一個物件轉移到另一個物件。它接受一個右值引用作為引數。

解構函式 ~T():解構函式用於執行清理工作,釋放資源。

呼叫時機:

普通建構函式在物件建立時呼叫。

複製建構函式在物件透過另一個物件初始化時呼叫。

移動建構函式在物件透過另一個物件的移動初始化時呼叫。

解構函式在物件被銷燬時呼叫,例如當物件離開作用域或被顯式刪除時。

問題3:

不能。

因為靜態成員變數的定義和初始化應該在類定義之外進行,而不是在類定義內部。

實驗任務2:

實驗程式碼:

實驗2 類和物件_基礎程式設計1
 1 #pragma once
 2 #include<iostream>
 3 class Complex {
 4 public:
 5     static const std::string doc;
 6     Complex();
 7     Complex(double x);
 8     Complex(double x, double y);
 9     Complex(const Complex& c);
10     double get_real()const;
11     double get_imag()const;
12     void add(const Complex& c);
13     friend Complex add(const Complex& c1, const Complex& c2);
14 
15     friend bool is_equal(const Complex& c1, const Complex& c2);
16 
17     friend bool is_not_equal(const Complex& c1, const Complex& c2);
18 
19     friend void output(const Complex& c);
20 
21     friend double abs(const Complex& c);
22 
23 
24 private:
25     double real;
26     double imag;
27 };
complex.h
實驗2 類和物件_基礎程式設計1
 1 #include"complex.h"
 2 #include<iostream>
 3 #include<math.h>
 4 const std::string Complex::doc = "a simplified complex class";
 5 Complex::Complex():real(0), imag(0) {}
 6 Complex::Complex(double x) : real(x), imag(0) {}
 7 Complex::Complex(double x, double y) : real(x), imag(y) {}
 8 Complex::Complex(const Complex& c) : real(c.real), imag(c.imag) {}
 9 double Complex::get_real() const {
10 
11     return real;
12 
13 }
14 double Complex::get_imag() const {
15 
16     return imag;
17 
18 }
19 void Complex::add(const Complex& other) {
20 
21     real += other.real;
22 
23     imag += other.imag;
24 
25 }
26 Complex add(const Complex& c1, const Complex& c2) {
27 
28     return Complex(c1.real + c2.real, c1.imag + c2.imag);
29 
30 }
31 
32 
33 
34 bool is_equal(const Complex& c1, const Complex& c2) {
35 
36     return (c1.real == c2.real) && (c1.imag == c2.imag);
37 
38 }
39 bool is_not_equal(const Complex& c1, const Complex& c2)
40 {
41     return !is_equal(c1, c2);
42 }
43 double abs(const Complex& c) {
44 
45     return std::sqrt(c.real * c.real + c.imag * c.imag);
46 
47 }
48 void output(const Complex& c) {
49     if (c.imag >= 0) {
50         std::cout << c.real << " + " << std::abs(c.imag) << "i";
51     }
52     else
53     {
54         std::cout << c.real << " - " << std::abs(c.imag) << "i";
55     }
56 
57 }
complex.cpp
實驗2 類和物件_基礎程式設計1
#include"complex.h"
#include <iostream>

using std::cout;
using std::endl;
using std::boolalpha;

void test() {
    cout << "類成員測試: " << endl;
    cout << Complex::doc << endl;

    cout << endl;

    cout << "Complex物件測試: " << endl;
    Complex c1;
    Complex c2(3, -4);
    const Complex c3(3.5);
    Complex c4(c3);

    cout << "c1 = "; output(c1); cout << endl;
    cout << "c2 = "; output(c2); cout << endl;
    cout << "c3 = "; output(c3); cout << endl;
    cout << "c4 = "; output(c4); cout << endl;
    cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;

    cout << endl;

    cout << "複數運算測試: " << endl;
    cout << "abs(c2) = " << abs(c2) << endl;
    c1.add(c2);
    cout << "c1 += c2, c1 = "; output(c1); cout << endl;
    cout << boolalpha;
    cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
    cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
    c4 = add(c2, c3);
    cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
}

int main() {
    test();
}
main.cpp

執行結果截圖:

實驗任務3:

實驗程式碼:

實驗2 類和物件_基礎程式設計1
 1 #include <iostream>
 2 #include <complex>
 3 
 4 using std::cout;
 5 using std::endl;
 6 using std::boolalpha;
 7 using std::complex;
 8 
 9 void test() {
10     cout << "標準庫模板類comple測試: " << endl;
11     complex<double> c1;
12     complex<double> c2(3, -4);
13     const complex<double> c3(3.5);
14     complex<double> c4(c3);
15 
16     cout << "c1 = " << c1 << endl;
17     cout << "c2 = " << c2 << endl;
18     cout << "c3 = " << c3 << endl;
19     cout << "c4 = " << c4 << endl;
20     cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl;
21     cout << endl;
22 
23     cout << "複數運算測試: " << endl;
24     cout << "abs(c2) = " << abs(c2) << endl;
25     c1 += c2;
26     cout << "c1 += c2, c1 = " << c1 << endl;
27     cout << boolalpha;
28     cout << "c1 == c2 : " << (c1 == c2) << endl;
29     cout << "c1 != c3 : " << (c1 != c3) << endl;
30     c4 = c2 + c3;
31     cout << "c4 = c2 + c3, c4 = " << c4 << endl;
32 }
33 
34 int main() {
35     test();
36 }
task3.cpp

執行結果截圖:

問題:

介面:

complex<double> c1使用預設建構函式建立了一個複數物件c1
complex<double> c2(3, -4) 使用初始化列表構造複數物件c2
const complex<double> c3(3.5) 使用建構函式建立了一個常量複數物件c3
complex<double> c4(c3) 使用複製建構函式建立了複數物件c4

使用std::cout和<<運算子直接輸出複數物件

c4.real()和 c4.imag() 分別獲取複數物件的實部和虛部。

c1 += c2 使用+=運算子將c2加到c1上

(c1 == c2)使用==運算子比較兩個複數物件是否相等
(c1 != c3)使用!=運算子比較兩個複數物件是否不相等

abs(c2)計算複數物件c2的模;

c4 = c2 + c3使用+運算子將兩個複數物件相加

寫法更簡潔之處:

1.建構函式:

任務2:需要手動定義各種建構函式來初始化複數物件。
任務3:可以直接使用列表初始化或者提供特定的建構函式來建立複數物件,程式碼更為簡潔。
2.運算:

任務2:需要手動實現加法、減法、乘法和除法等運算子的過載。
任務3:標準庫complex類已經過載了基本的算術運算子,可以直接使用這些運算子進行復數運算。
3.訪問:

任務2:需要透過成員函式(如get_real()和get_imag())來獲取實部和虛部。
任務3:可以直接使用.real()和.imag()成員函式來訪問複數的實部和虛部,程式碼更為直觀和簡潔。
4.輸出:

任務2:需要自定義一個output()函式來格式化輸出複數。
任務3:可以直接使用std::cout和<<運算子來輸出複數
5.模運算:

任務2:需要手動實現複數的模運算。
任務3:可以直接使用標準庫提供的abs()函式來計算複數的模。

啟發:標準庫complex類的介面設計簡潔直觀,易於理解和使用,可以使程式碼更加簡潔和直觀。

實驗任務4:

實驗程式碼:

實驗2 類和物件_基礎程式設計1
 1  #pragma once
 2  #include <string>
 3 
 4  class Fraction {
 5  public:
 6          Fraction(int up, int down = 1);
 7         Fraction(const Fraction&);
 8     
 9              int get_up() const;
10          int get_down() const;
11          Fraction negative() const;
12          int gcd()const;
13          friend Fraction add(const Fraction&, const Fraction&);
14         friend Fraction sub(const Fraction&, const Fraction&);
15          friend Fraction mul(const Fraction&, const Fraction&);
16          friend Fraction div(const Fraction&, const Fraction&);
17          friend void output(const Fraction&);
18     
19         
20      static const std::string doc;
21     
22  private:
23         int up;
24          int down;
25     
26 };
Fraction.h
實驗2 類和物件_基礎程式設計1
 1  #include "Fraction.h"
 2 #include <iostream>
 3  #include<cmath>
 4  const std::string Fraction::doc = "Fraction類v0.01版. 目前僅支援分數物件的構造、輸出、加/減/乘/除運算";
 5 
 6 Fraction::Fraction(int up, int down) : up(up), down(down) {}
 7 
 8 Fraction::Fraction(const Fraction & other) : up(other.up), down(other.down) {}
 9 
10  int Fraction::get_up() const {
11      int a = gcd();
12          return up/a;
13     
14 }
15 
16  int Fraction::get_down() const {
17      int a = gcd();
18      return down/a;
19     
20 }
21 
22  Fraction Fraction::negative() const {
23          return Fraction(-up, down);
24     
25 }
26 
27  Fraction add(const Fraction & f1, const Fraction & f2) {
28         return Fraction(f1.up * f2.down + f2.up * f1.down, f1.down * f2.down);
29     
30 }
31 
32  Fraction sub(const Fraction & f1, const Fraction & f2) {
33          return Fraction(f1.up * f2.down - f2.up * f1.down, f1.down * f2.down);
34     
35 }
36 
37  Fraction mul(const Fraction & f1, const Fraction & f2) {
38         return Fraction(f1.up * f2.up, f1.down * f2.down);
39     
40 }
41 
42  Fraction div(const Fraction & f1, const Fraction & f2) {
43          return Fraction(f1.up * f2.down, f1.down * f2.up);
44     
45 }
46 
47  void output(const Fraction & f) {
48          int a = f.down, b = f.up;
49          int c = a, d = b;
50          int temp;
51          while (d != 0)
52              {
53                  temp = c % d;
54                  c = d;
55                  d = temp;
56              }
57          int gcd = c;
58          b /= gcd;
59          a /= gcd;
60          if (a == 0)
61          {
62              std::cout << "分母不能為0";
63              return;
64          }
65          if (a < 0) {
66                  b = -b;
67                  a = -a;
68         
69     }
70          if (b == 0) {
71                  std::cout << "0";
72         
73     }
74     else if (a == 1) {
75                  std::cout << b;
76         
77     }
78     else {
79                  std::cout << b << "/" << a;
80         
81     }
82     
83 }
84  int Fraction::gcd()const 
85  {
86      int c = up;
87      int d = down;
88      int temp;
89      while (d != 0)
90      {
91          temp = c % d;
92          c = d;
93          d = temp;
94      }
95      return c;
96  }
Fraction.cpp
實驗2 類和物件_基礎程式設計1
 1 #include "Fraction.h"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 
 8 void test1() {
 9     cout << "Fraction類測試: " << endl;
10     cout << Fraction::doc << endl << endl;
11 
12     Fraction f1(5);
13     Fraction f2(3, -4), f3(-18, 12);
14     Fraction f4(f3);
15     cout << "f1 = "; output(f1); cout << endl;
16     cout << "f2 = "; output(f2); cout << endl;
17     cout << "f3 = "; output(f3); cout << endl;
18     cout << "f4 = "; output(f4); cout << endl;
19 
20     Fraction f5(f4.negative());
21     cout << "f5 = "; output(f5); cout << endl;
22     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl;
23 
24     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
25     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
26     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
27     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
28     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
29 }
30 
31 void test2() {
32     Fraction f6(42, 55), f7(0, 3);
33     cout << "f6 = "; output(f6); cout << endl;
34     cout << "f7 = "; output(f7); cout << endl;
35     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
36 }
37 
38 int main() {
39     cout << "測試1: Fraction類基礎功能測試\n";
40     test1();
41 
42     cout << "\n測試2: 分母為0測試: \n";
43     test2();
44 }
task4.cpp

執行結果截圖:

實驗任務5:

實驗程式碼:

實驗2 類和物件_基礎程式設計1
 1 #ifndef _ _ACCOUNT_H_ _
 2 #define _ _ACCOUNT_H_ _
 3 class SavingsAccount{
 4 private:
 5     int id;
 6     double balance;
 7     double rate;
 8     int lastDate;
 9     double accumulation;
10     static double total;
11     void record(int date,double amount);
12     double accumulate(int date)const{
13         return accumulation+balance*(date-lastDate);
14     }
15 public:
16     SavingsAccount(int date,int id,double rate);
17     int getId()const{return id;}
18     double getBalance()const{return balance;}
19     double getRate()const{return rate;}
20     static double getTotal(){return total;}
21     void deposit(int date,double amount);
22     void withdraw(int date,double amount);
23     void settle(int date);
24     void show()const;
25     
26 };
27 #endif//_ _ACCOUNT_H_ _
account.h
實驗2 類和物件_基礎程式設計1
 1 #include"account.h"
 2 #include<cmath>
 3 #include<iostream>
 4 using namespace std;
 5 double SavingsAccount::total=0;
 6 SavingsAccount::SavingsAccount(int date,int id,double rate)
 7 :id(id),balance(0),rate(rate),lastDate(date),accumulation(0){
 8     cout<<date<<"\t#"<<id<<" is created"<<endl;
 9 } 
10 void  SavingsAccount::record(int date,double amount){
11     accumulation=accumulate(date);
12     lastDate=date;
13     amount=floor(amount*100+0.5)/100;
14     balance+=amount;
15     total+=amount;
16     cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
17 }
18 void  SavingsAccount::deposit(int date,double amount){
19     record(date,amount);
20 }
21 void  SavingsAccount::withdraw(int date,double amount){
22     if(amount>getBalance())
23         cout<<"Error:not enough money"<<endl;
24     else
25         record(date,-amount);
26 }
27 void  SavingsAccount::settle(int date){
28     double interest=accumulate(date)*rate/365;
29     if(interest!=0)
30         record(date,interest);
31         accumulation=0;
32 }
33 void SavingsAccount::show()const{
34     cout<<"#"<<id<<"\tBalance:"<<balance;
35 }
account.cpp
實驗2 類和物件_基礎程式設計1
 1 #include"account.h"
 2 #include<iostream>
 3 using namespace std;
 4 int main()
 5 {
 6      SavingsAccount sa0(1,21325302,0.015);
 7      SavingsAccount sa1(1,58320212,0.015);
 8      sa0.deposit(5,5000);
 9      sa1.deposit(25,10000);
10      sa0.deposit(45,5500);
11      sa1.withdraw(60,4000);
12      sa0.settle(90);
13      sa1.settle(90);
14      sa0.show();cout<<endl;
15      sa1.show();cout<<endl;
16      cout<<"Total:"<< SavingsAccount::getTotal()<<endl;
17      return 0;
18 }
main.cpp
可以在 accumulate()getBalance() 加上 const 修飾符,可以防止資料被更改。
可以在輸出時加一些提示語,可以讓輸出更直觀。

總結:

透過本次實驗,我明白了使用標準庫所帶來的簡潔,以及知道了怎麼透過多檔案的方式來編寫程式碼。

相關文章