實驗2 類和物件

易安135發表於2024-10-30
實驗任務1:
實驗2 類和物件
 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();
View Code
實驗2 類和物件
 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 }
View Code
實驗2 類和物件
 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 }
View Code

執行截圖:

問題1:

不能。在類外部遮蔽定義之後,func會認為是沒有定義的,因而報錯。至於類內部的定義,這個定義被封裝到了類的內部,是不可見的,因此遮蔽外部定義之後會報錯。

問題2:

建構函式:用於建立物件,初始化類的物件,最開始初始化物件時使用。

複製建構函式:將一個同類的物件作為引數,從而複製一個新的類物件,會新建立一個物件,最開始初始化物件時使用。

移動建構函式:改變物件的指向,可以節省建立新物件的消耗,最開始初始化物件時使用。

解構函式:銷燬物件,在物件呼叫結束時使用,節省空間。

問題3:

不能。

實驗任務2:

實驗2 類和物件
 1 #pragma once
 2 #include<string>
 3 using namespace std;
 4 
 5 class Complex {
 6 public:
 7     static const string doc;
 8 private:
 9     double real;
10     double imag;
11 public:
12     Complex(double x = 0, double y = 0);
13     Complex(const Complex& c);
14     //~Complex();
15 
16     double get_real() const;
17     double get_imag() const;
18     void add(const Complex& c);
19 
20     friend Complex add(const Complex& c1, const Complex& c2);
21     friend bool is_equal(const Complex& c1, const Complex& c2);
22     friend bool is_not_equal(const Complex& c1, const Complex& c2);
23     friend void output(const Complex& c);
24     friend double abs(const Complex& c);
25 };
26 Complex add(const Complex& c1, const Complex& c2);
27 bool is_equal(const Complex& c1, const Complex& c2);
28 bool is_not_equal(const Complex& c1, const Complex& c2);
29 void output(const Complex& c);
30 double abs(const Complex& c);
View Code
實驗2 類和物件
 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include<string>
 3 #include "標頭.h"
 4 #include<math.h>
 5 #include <iostream>
 6 using namespace std;
 7 
 8 const string Complex::doc{ "a simplified complex class" };
 9 
10 //建構函式: 
11 Complex::Complex(double x, double y) :real(x), imag(y) {
12 }
13 
14 Complex::Complex(const Complex& c) {
15     real = c.real; imag = c.imag;
16 }
17 
18 double Complex::get_real() const {
19     return real;
20 }
21 
22 double Complex::get_imag() const {
23     return imag;
24 }
25 
26 void Complex::add(const Complex& c) {
27     imag = c.imag + imag; real = real + c.real;
28 }
29 
30 Complex add(const Complex& c1, const Complex& c2) {
31     Complex c3;
32     c3.real = c1.real + c2.real;
33     c3.imag = c1.imag + c2.imag;
34     return c3;
35 }
36 
37 bool is_equal(const Complex& c1, const Complex& c2) {
38     if (c1.real == c2.real && c1.imag == c2.imag) {
39         return true;
40     }
41     return false;
42 }
43 
44 bool is_not_equal(const Complex& c1, const Complex& c2) {
45     if (c1.real == c2.real && c1.imag == c2.imag) {
46         return false;
47     }
48     return true;
49 }
50 
51 void output(const Complex& c) {
52     if (c.real >= 0) {
53         if (c.imag >= 0) {
54             cout << c.real << " " << "+" << " " << c.imag << "i";
55         }
56         else {
57             cout << c.real << " " << "-" << " " << c.imag * -1 << "i";
58         }
59     }
60     else {
61         if (c.imag >= 0) {
62             cout << c.real * -1 << " " << "+" << " " << c.imag << "i";
63         }
64         else {
65             cout << c.real * -1 << " " << "-" << " " << c.imag * -1 << "i";
66         }
67     }
68 }
69 
70 double abs(const Complex& c) {
71     return sqrt(c.real * c.real + c.imag * c.imag);
72 }
View Code
實驗2 類和物件
 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include "標頭.h"
 3 #include <iostream>
 4 using namespace std;
 5 using std::cout;
 6 using std::endl;
 7 using std::boolalpha;
 8 
 9 void test() {
10     cout << "類成員測試: " << endl;
11     cout << Complex::doc << endl;
12 
13     cout << endl;
14 
15     cout << "Complex物件測試: " << endl;
16     Complex c1;
17     Complex c2(3, -4);
18     const Complex c3(3.5);
19     Complex c4(c3);
20 
21     cout << "c1 = "; output(c1); cout << endl;
22     cout << "c2 = "; output(c2); cout << endl;
23     cout << "c3 = "; output(c3); cout << endl;
24     cout << "c4 = "; output(c4); cout << endl;
25     cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;
26 
27     cout << endl;
28 
29     cout << "複數運算測試: " << endl;
30     cout << "abs(c2) = " << abs(c2) << endl;
31     c1.add(c2);
32     cout << "c1 += c2, c1 = "; output(c1); cout << endl;
33     cout << boolalpha;
34     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
35     cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
36     c4 = add(c2, c3);
37     cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
38 }
39 
40 int main() {
41     test();
42 }
View Code

執行截圖:

實驗任務3:

實驗2 類和物件
 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 }
View Code

執行截圖:

使用了判斷相等,加,不等,取模等介面。

實驗任務4:

實驗2 類和物件
 1 #pragma once
 2 #include<string>
 3 
 4 using namespace std;
 5 
 6 class Fraction {
 7 public:
 8     static const string doc;
 9 private:
10     int up, down;
11 public:
12     Fraction(int x = 0, int y = 1);
13     Fraction(const Fraction& f);
14     int get_up() const;
15     int get_down() const;
16     Fraction negative();
17 
18     friend void output(const Fraction &f);
19     friend Fraction add(const Fraction &f1,const Fraction &f2);
20     friend Fraction sub(const Fraction& f1, const Fraction& f2);
21     friend Fraction mul(const Fraction& f1, const Fraction& f2);
22     friend Fraction div(const Fraction& f1, const Fraction& f2);
23 };
24  void output(const Fraction& f);
25  Fraction add(const Fraction& f1, const Fraction& f2);
26  Fraction sub(const Fraction& f1, const Fraction& f2);
27  Fraction mul(const Fraction& f1, const Fraction& f2);
28  Fraction div(const Fraction& f1, const Fraction& f2);
View Code
實驗2 類和物件
  1 #define _CRT_SECURE_NO_WARNINGS 1
  2 #include "標頭.h"
  3 #include<string>
  4 #include<iostream>
  5 
  6 using namespace std;
  7 
  8 const string Fraction::doc{"Fraction類 v 0.01版.\n目前只支援分數物件的構造、輸出、加/減/乘/除運算"};
  9 
 10 Fraction::Fraction(int x, int y) :up{ x }, down{ y } {};
 11 Fraction::Fraction(const Fraction& f) {
 12     up = f.up; down = f.down;
 13 }
 14 
 15 int Fraction::get_up() const {
 16     int up1 = up;
 17     int down1 = down;
 18     int min = 0;
 19     if (up <= down) {
 20         min = up;
 21     }
 22     else {
 23         min = down;
 24     }
 25     for (int i = 2; i <= min; i++) {
 26         if (up % i == 0 && down % i == 0) {
 27             up1 = up / i;
 28             down1 = down / i;
 29         }
 30     }
 31     return up1;
 32 }
 33 int Fraction::get_down() const {
 34     int up1 = up;
 35     int down1 = down;
 36     int min = 0;
 37     if (up <= down) {
 38         min = up;
 39     }
 40     else {
 41         min = down;
 42     }
 43     for (int i = 2; i <= min; i++) {
 44         if (up % i == 0 && down % i == 0) {
 45             up1 = up / i;
 46             down1 = down / i;
 47         }
 48     }
 49     return down1;
 50 }Fraction Fraction::negative() {
 51     Fraction f1;
 52     f1.up = up * -1;
 53     f1.down = down;
 54     return f1;
 55 }
 56 
 57 void output(const Fraction& f) {
 58     Fraction f1;
 59     f1.up = f.up; f1.down = f.down;
 60     if (f1.down == 0) {
 61         cout << "分母不能為0";
 62     }
 63     else if (f1.up == 0) {
 64         cout << f1.up;
 65     }else {
 66         int min = (f1.up - f1.down) ? f1.down : f1.up;
 67         for (int i = 2; i <= min; i++) {
 68             if (f1.up % i == 0 && f1.down % i == 0) {
 69                 f1.up = f1.up / i;
 70                 f1.down = f1.down / i;
 71             }
 72         }
 73         if (f1.up >= 0) {
 74             if (f1.down >= 0) {
 75                 if (f1.up % f1.down == 0) {
 76                     cout << f1.up / f1.down;
 77                 }
 78                 else {
 79                     cout << f1.up << "/" << f1.down;
 80                 }
 81             }
 82             else {
 83                 if (f1.up % (f1.down*-1) == 0) {
 84                     cout << (f1.up / f1.down)*-1;
 85                 }
 86                 else {
 87                     cout << f1.up * -1 << "/" << f1.down * -1;
 88                 }
 89             }
 90         }
 91         else {
 92             if (f1.down >= 0) {
 93                 if ((f1.up * -1) % f1.down == 0) {
 94                     cout << ((f1.up * -1) / f1.down) * -1;
 95                 }
 96                 else {
 97                     cout << f1.up << "/" << f1.down;
 98                 }
 99             }
100             else {
101                 if (f1.up % f1.down == 0) {
102                     cout << f1.up / f1.down;
103                 }
104                 else {
105                     cout << f1.up * -1 << "/" << f1.down * -1;
106                 }
107             }
108         }
109     }
110 }
111 Fraction add(const Fraction& f1, const Fraction& f2) {
112     Fraction f3;
113     int f11up = f1.up, f11down = f1.down;
114     int f22up = f2.up, f22down = f2.down;
115     if (f1.down != f2.down) {
116         f11down = f1.down * f2.down;
117         f22down = f1.down * f2.down;
118         f11up = f11up * f2.down;
119         f22up = f22up * f1.down;
120         f3.down = f11down;
121         f3.up = f11up + f22up;
122         int min = (f3.up - f3.down) ? f3.down : f3.up;
123         for (int i = 2; i <= min; i++) {
124             if (f3.up % i == 0 && f3.down % i == 0) {
125                 f3.up = f3.up / i;
126                 f3.down = f3.down / i;
127             }
128         }
129     }
130     else {
131         f3.down = f1.down;
132         f3.up = f1.up + f2.up;
133         int min = (f3.up - f3.down) ? f3.down : f3.up;
134         for (int i = 2; i <= min; i++) {
135             if (f3.up % i == 0 && f3.down % i == 0) {
136                 f3.up = f3.up / i;
137                 f3.down = f3.down / i;
138             }
139         }
140     }
141     return f3;
142 }
143 Fraction sub(const Fraction& f1, const Fraction& f2) {
144     Fraction f3;
145     int f11up = f1.up, f11down = f1.down;
146     int f22up = f2.up, f22down = f2.down;
147     if (f1.down != f2.down) {
148         f11down = f1.down * f2.down;
149         f22down = f1.down * f2.down;
150         f11up = f11up * f2.down;
151         f22up = f22up * f1.down;
152         f3.down = f11down;
153         f3.up = f11up - f22up;
154         int min = (f3.up - f3.down) ? f3.down : f3.up;
155         for (int i = 2; i <= min; i++) {
156             if (f3.up % i == 0 && f3.down % i == 0) {
157                 f3.up = f3.up / i;
158                 f3.down = f3.down / i;
159             }
160         }
161     }
162     else {
163         f3.down = f1.down;
164         f3.up = f1.up - f2.up;
165         int min = (f3.up - f3.down) ? f3.down : f3.up;
166         for (int i = 2; i <= min; i++) {
167             if (f3.up % i == 0 && f3.down % i == 0) {
168                 f3.up = f3.up / i;
169                 f3.down = f3.down / i;
170             }
171         }
172     }
173     return f3;
174 }
175 Fraction mul(const Fraction& f1, const Fraction& f2) {
176     Fraction f3;
177     f3.up = f1.up * f2.up;
178     f3.down = f1.down * f2.down;
179     return f3;
180 }
181 Fraction div(const Fraction& f1, const Fraction& f2) {
182     Fraction f3;
183     f3.up = f1.up * f2.down;
184     f3.down = f1.down * f2.up;
185     return f3;
186 }
View Code
實驗2 類和物件
 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include "標頭.h"
 3 #include <iostream>
 4 
 5 using std::cout;
 6 using std::endl;
 7 
 8 
 9 void test1() {
10     cout << "Fraction類測試: " << endl;
11     cout << Fraction::doc << endl << endl;
12 
13     Fraction f1(5);
14     Fraction f2(3, -4), f3(-18, 12);
15     Fraction f4(f3);
16     cout << "f1 = "; output(f1); cout << endl;
17     cout << "f2 = "; output(f2); cout << endl;
18     cout << "f3 = "; output(f3); cout << endl;
19     cout << "f4 = "; output(f4); cout << endl;
20 
21     Fraction f5(f4.negative());
22     cout << "f5 = "; output(f5); cout << endl;
23     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl;
24 
25     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
26     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
27     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
28     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
29     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
30 }
31 
32 void test2() {
33     Fraction f6(42, 55), f7(0, 3);
34     cout << "f6 = "; output(f6); cout << endl;
35     cout << "f7 = "; output(f7); cout << endl;
36     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
37 }
38 
39 int main() {
40     cout << "測試1: Fraction類基礎功能測試\n";
41     test1();
42 
43     cout << "\n測試2: 分母為0測試: \n";
44     test2();
45 }
View Code

執行截圖:

實驗任務5:

實驗2 類和物件
 1 #pragma once
 2 #ifndef __ACCOUNT_H__
 3 #define __ACCOUNT_H__
 4 class SavingsAccount {
 5 private:
 6     int id;
 7     double balance;
 8     double rate;
 9     int lastDate;
10     double accumulation;
11     static double total;
12 
13     void record(int date, double account);
14     double accumulate(int date)const {
15         return accumulation + balance * (date - lastDate);
16     }
17 public:
18     SavingsAccount(int date, int id, double rate);
19     int getId()const { return id; }
20     double getBalance()const { return balance; }
21     double getRate()const { return rate; }
22     static double getTotal() { return total; }
23     void deposit(int date, double account);
24     void withdraw(int date, double account);
25 
26     void settle(int date);
27     void show()const;
28 };
29 #endif
View Code
實驗2 類和物件
 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include "account.h"
 3 #include<cmath>
 4 #include<iostream>
 5 using namespace std;
 6 
 7 double SavingsAccount::total = 0;
 8 SavingsAccount::SavingsAccount(int date, int id, double rate)
 9     :id{ id }, balance{ 0 }, rate{ rate }, lastDate(date), accumulation{ 0 } {
10     cout << date << "\t#" << id << "is created" << endl;
11 }
12 void SavingsAccount::record(int date, double amount) {
13     accumulation = accumulate(date);
14     lastDate = date;
15     amount = floor(amount * 100 + 0.5) / 100;
16     balance += amount;
17     total += amount;
18     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
19 }
20 void SavingsAccount::deposit(int date, double amount) {
21     record(date, amount);
22 }
23 void SavingsAccount::withdraw(int date, double amount) {
24     if (amount > getBalance()) {
25         cout << "Error: not enough money" << endl;
26     }else {
27         record(date, -amount);
28     }
29 }
30 void SavingsAccount::settle(int date) {
31     double interst = accumulate(date) * rate / 365;
32     if (interst != 0) {
33         record(date, interst);
34     }
35     accumulation = 0;
36 }
37 void SavingsAccount::show()const {
38     cout << "#" << id << "/tBalance:" << balance;
39 }
View Code
實驗2 類和物件
 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include "account.h"
 3 #include<iostream>
 4 using namespace std;
 5 int main()
 6 {
 7     SavingsAccount sa0(1, 21325302, 0.015);
 8     SavingsAccount sa1(1, 58320212, 0.015);
 9 
10     sa0.deposit(5, 5000);
11     sa1.deposit(25, 10000);
12     sa0.deposit(45, 5500);
13     sa1.withdraw(60, 4000);
14 
15     sa0.settle(90);
16     sa1.settle(90);
17 
18     sa0.show(); cout << endl;
19     sa1.show(); cout << endl;
20     cout << "Total:" << SavingsAccount::getTotal() << endl;
21     return 0;
22 }
View Code

執行截圖:

相關文章