實驗任務1:
t.h程式碼:
1 #pragma once 2 #include <string> 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 void adjust(int ratio); // 按係數成倍調整資料 12 void display() const; // 以(m1, m2)形式顯示T類物件資訊 13 private: 14 int m1, m2; 15 // 類屬性、方法 16 public: 17 static int get_cnt(); // 顯示當前T類物件總數 18 public: 19 static const std::string doc; // 類T的描述資訊 20 static const int max_cnt; // 類T物件上限 21 private: 22 static int cnt; // 當前T類物件數目 23 // 類T友元函式宣告 24 friend void func(); 25 }; 26 // 普通函式宣告 27 void func(); 28 29 30 // static成員資料類外初始化 31 const std::string T::doc{"a simple class sample"}; 32 const int T::max_cnt = 999; 33 int T::cnt = 0;
t.cpp程式碼:
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 }
task1.cpp程式碼:
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 }
執行結果截圖:
問題1:
可以執行,因為在t.cpp中有func函式的定義,主程式執行時可以查詢到func函式的定義,那麼即使不在類T的定義中再次宣告其為友元,編譯器也能正確識別和處理func函式對類私有成員的訪問。正常情況下,如果func函式在類外部沒有任何宣告或定義,且嘗試在原始檔中呼叫它,那麼編譯器將會報錯,因為它不知道func函式的存在。
問題2:
普通建構函式 (T(int x = 0, int y = 0)):用於建立類T的物件,並初始化成員變數m1和m2為指定的值。在建立物件時自動呼叫。
複製建構函式 (T(const T& t)):用於建立一個新的物件,並將其初始化為另一個同型別物件的副本。當一個物件以值傳遞的方式傳遞給函式、或從函式返回物件、或進行物件賦值操作時。
移動建構函式 (T(T&& t)):用於獲得另一個右值物件的資源,而不是複製它們。當右值物件被用作初始值來構造新物件時。
解構函式 (~T()):在物件生命週期結束時自動呼叫,用於執行清理操作,如釋放物件所佔用的資源。物件離開其作用域時。
問題3:
我使用的時DevC++中可以執行,調整的程式碼與t.cpp中其他程式碼未產生關聯,故無影響。
實驗任務2:
Complex.h程式碼:
1 #pragma once 2 #include<string> 3 4 class Complex{ 5 public: 6 Complex(double x=0,double y=0); 7 Complex(const Complex &c); 8 9 ~Complex(); 10 void display() cosnt; 11 12 double const get_real(){ 13 return r; 14 }; 15 double const get_imag(){ 16 return i; 17 }; 18 19 private: 20 double r,i; 21 22 friend Complex add(const Complex& c1,const Complex& c2); 23 friend bool is_equal(const Complex& c1,const Complex& c2); 24 friend bool is_not_equal(const Complex& c1,const Complex& c3); 25 friend double abs(const Complex& c); 26 friend void output(const Complex& c); 27 28 public: 29 static const std::string doc; 30 }; 31 32 Complex add(); 33 bool is_equal(); 34 bool is_not_equal(); 35 void output(); 36 void abs();
Complex.cpp程式碼:
1 #include"Complex.h" 2 #include<bits/stdc++.h> 3 4 using namespace std; 5 6 const string Complex::doc{"a simplified complex class"}; 7 8 Complex::Complex(double r,double i):real{r},imag{i}{ 9 } 10 11 Complex::Complex(const Complex& c):real{c.real},imag{c.imag}{ 12 13 } 14 15 16 Complex Complex::add(const Complex &c){ 17 real+=c.real; 18 imag+=c.imag; 19 } 20 21 Complex add(const Complex& c1,const Complex& c2) 22 { 23 Complex c4; 24 c4.imag=c1.imag+c2.imag; 25 c4.real=c1.real+c2.real; 26 return c4; 27 } 28 bool is_equal(const Complex& c1,const Complex& c2){ 29 if(c1.imag==c2.imag&&c1.real==c2.real) 30 return true; 31 else 32 return false; 33 } 34 bool is_not_equal(const Complex &c1,const Complex &c3){ 35 if(c1.imag==c3.imag&&c1.real==c3.real) 36 return false; 37 else 38 return true; 39 } 40 double abs(const Complex &c) 41 { 42 double s; 43 s=sqrt(c.imag*c.imag+c.real*c.real); 44 return s; 45 } 46 void output(const Complex &c){ 47 cout<<c.real; 48 if(c.imag>=0) cout<<" + "<<c.imag<<"i"<<endl; 49 else cout<<" - "<<(-1)*c.imag<<"i"<<endl; 50 } 51 Complex::~Complex(){ 52 53 }
task2.cpp程式碼:
1 #include "Complex.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 using std::boolalpha; 7 8 void test() { 9 cout << "類成員測試: " << endl; 10 cout << Complex::doc << endl; 11 12 cout << endl; 13 14 cout << "Complex物件測試: " << endl; 15 Complex c1; 16 Complex c2(3, -4); 17 const Complex c3(3.5); 18 Complex c4(c3); 19 20 cout << "c1 = "; output(c1); cout << endl; 21 cout << "c2 = "; output(c2); cout << endl; 22 cout << "c3 = "; output(c3); cout << endl; 23 cout << "c4 = "; output(c4); cout << endl; 24 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl; 25 26 cout << endl; 27 28 cout << "複數運算測試: " << endl; 29 cout << "abs(c2) = " << abs(c2) << endl; 30 c1.add(c2); 31 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 32 cout << boolalpha; 33 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 34 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 35 c4 = add(c2, c3); 36 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 37 } 38 39 int main() { 40 test(); 41 }
執行結果截圖:
實驗任務3:
task3.cpp程式碼:
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 }
執行結果截圖:
問題1:
介面有:
complex 模板類的預設建構函式、帶實部和虛部的建構函式、帶單個實部的建構函式、複製建構函式、實部和虛部的訪問函式、模的計算 abs()
問題2:
類的定義與宣告已經由C++標準庫定義和實現,提供了豐富的運算介面,包括加減乘除、取模、相等比較等,且都是經過最佳化的實現,可以自動管理記憶體和資源。
成員函式的呼叫與運算可以直接使用運算子過載進行運算,如 c1 += c2,輸出運算子 << 進行輸出,如 cout << c1。
問題3:
適當的使用標準庫complex模板類,使程式碼更簡潔,因為只需要包含標頭檔案和測試程式碼。可以直接使用標準庫提供的介面,無需手動編寫和除錯運算函式和輸出函式。
實驗任務4:
Fraction.h程式碼:
1 #pragma once 2 #include<string> 3 4 using std::string; 5 class Fraction { 6 7 public: 8 Fraction(int x,int y=1); 9 Fraction(const Fraction &f); 10 ~Fraction(); 11 12 public: 13 int get_up() const; 14 int get_down() const; 15 Fraction negative(); 16 17 Fraction simplify(); 18 19 private: 20 int up,down; 21 friend void output(const Fraction &f); 22 friend Fraction add(const Fraction &f1,const Fraction &f2); 23 friend Fraction sub(const Fraction &f1,const Fraction &f2); 24 friend Fraction mul(const Fraction &f1,const Fraction &f2); 25 friend Fraction div(const Fraction &f1,const Fraction &f2); 26 27 public: 28 static const string doc; 29 }; 30 31 void output(const Fraction& f1); 32 Fraction add(const Fraction& f1, const Fraction& f2); 33 Fraction sub(const Fraction& f1, const Fraction& f2); 34 Fraction mul(const Fraction& f1, const Fraction& f2); 35 Fraction div(const Fraction& f1, const Fraction& f2);
Fraction.cpp程式碼:
1 #include <iostream> 2 #include <string> 3 #include "Fraction.h" 4 5 using std::cout; 6 using std::endl; 7 using std::string; 8 9 const string Fraction::doc{ 10 "Fraction類 v 0.01版. 目前僅支援分數物件的構造、輸出、加/減/乘/除運算." 11 }; 12 13 Fraction::Fraction(int x,int y):up{x},down{y}{} 14 Fraction::Fraction(const Fraction &f):up{f.up},down{f.down}{} 15 Fraction::~Fraction(){} 16 17 18 int Fraction::get_up() const{ 19 return up; 20 } 21 22 int Fraction::get_down() const{ 23 return down; 24 } 25 26 Fraction Fraction::negative() { 27 Fraction t = simplify(); 28 return Fraction(-t.up,t.down); 29 } 30 31 Fraction Fraction::simplify() 32 { 33 int small, x, y, z; 34 if (up >= 0) 35 { 36 x = up; 37 if (down > 0) 38 { 39 y = down; 40 z = 1; 41 } 42 else 43 { 44 y = -down; 45 z = -1; 46 } 47 } 48 else 49 { 50 x = -up; 51 if (down > 0) 52 { 53 y = down; 54 z = -1; 55 } 56 else 57 { 58 y = -down; 59 z = 1; 60 } 61 } 62 if (x > y) 63 small = y; 64 else 65 small = x; 66 int i; 67 for (i = small; i >= 1; i--) 68 if (x % i == 0 && y % i == 0) 69 { 70 x = x / i; 71 y = y / i; 72 break; 73 } 74 if (z == 1) 75 return Fraction(x, y); 76 return Fraction(-x, y); 77 } 78 79 void output(const Fraction& f){ 80 Fraction t = Fraction(f.up,f.down); 81 t = t.simplify(); 82 if(t.down == 0) cout << "分母不能為0"; 83 else { 84 if(t.up == 0) 85 cout << "0"; 86 else if(t.up != 0 && t.down == 1) 87 cout << t.up; 88 else 89 cout << t.up << "/" << t.down; 90 cout << endl; 91 } 92 } 93 Fraction add(const Fraction &f1,const Fraction &f2) { 94 int num = f1.up * f2.down + f2.up * f1.down; 95 int denom = f1.down * f2.down; 96 return Fraction(num, denom); 97 } 98 Fraction sub(const Fraction &f1,const Fraction &f2) { 99 int num = f1.up * f2.down - f2.up * f1.down; 100 int denom = f1.down * f2.down; 101 return Fraction(num, denom); 102 } 103 Fraction mul(const Fraction &f1,const Fraction &f2) { 104 return Fraction(f1.up*f2.up,f1.down*f2.down); 105 106 } 107 Fraction div(const Fraction &f1,const Fraction &f2) { 108 return Fraction(f1.up*f2.down,f1.down*f2.up); 109 110 }
task4.cpp程式碼:
1 #include "Fraction.h" 2 #include <iostream> 3 using std::cout; 4 using std::endl; 5 void test1() { 6 cout << "Fraction類測試: " << endl; 7 cout << Fraction::doc << endl << endl; 8 Fraction f1(5); 9 Fraction f2(3, -4), f3(-18, 12); 10 Fraction f4(f3); 11 cout << "f1 = "; 12 output(f1); 13 cout << endl; 14 cout << "f2 = "; 15 output(f2); 16 cout << endl; 17 cout << "f3 = "; 18 output(f3); 19 cout << endl; 20 cout << "f4 = "; 21 output(f4); 22 cout << endl; 23 Fraction f5(f4.negative()); 24 cout << "f5 = "; 25 output(f5); 26 cout << endl; 27 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << 28 f5.get_down() << endl; 29 cout << "f1 + f2 = "; 30 output(add(f1, f2)); 31 cout << endl; 32 cout << "f1 - f2 = "; 33 output(sub(f1, f2)); 34 cout << endl; 35 cout << "f1 * f2 = "; 36 output(mul(f1, f2)); 37 cout << endl; 38 cout << "f1 / f2 = "; 39 output(div(f1, f2)); 40 cout << endl; 41 cout << "f4 + f5 = "; 42 output(add(f4, f5)); 43 cout << endl; 44 } 45 void test2() { 46 Fraction f6(42, 55), f7(0, 3); 47 cout << "f6 = "; 48 output(f6); 49 cout << endl; 50 cout << "f7 = "; 51 output(f7); 52 cout << endl; 53 cout << "f6 / f7 = "; 54 output(div(f6, f7)); 55 cout << endl; 56 } 57 int main() { 58 cout << "測試1: Fraction類基礎功能測試\n"; 59 test1(); 60 cout << "\n測試2: 分母為0測試: \n"; 61 test2(); 62 }
執行結果截圖:
實驗任務5:
account.h程式碼:
1 #pragma once 2 class SavingsAccount { 3 private: 4 int id; 5 double balance; 6 double rate; 7 int lastDate; 8 double accumulation; 9 static double total; 10 void record(int date, double amount); 11 double accumulate(int date) const 12 { 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 };
account.cpp程式碼:
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 double SavingsAccount::total = 0; 6 7 8 SavingsAccount::SavingsAccount(int date, int id, double rate) :id(id), balance(0), rate(rate), lastDate(date), accumulation(0) 9 { 10 cout << date << "\t#" << id << " is created" << endl; 11 } 12 13 void SavingsAccount::record(int date, double amount) 14 { 15 accumulation = accumulate(date); 16 lastDate = date; 17 amount = floor(amount * 100 + 0.5) / 100; 18 balance += amount; 19 total += amount; 20 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 21 } 22 void SavingsAccount::deposit(int date, double amount) { 23 record(date, amount); 24 } 25 void SavingsAccount::withdraw(int date, double amount) { 26 if (amount > getBalance()) 27 cout << "Error:not enough money" << endl; 28 else 29 { 30 record(date, -amount); 31 } 32 } 33 void SavingsAccount::settle(int date) { 34 double interest = accumulate(date) * rate / 365; 35 if (interest != 0) 36 record(date, interest); 37 accumulation = 0; 38 } 39 void SavingsAccount::show()const { 40 cout << "#" << id << "\tBalance:" << balance; 41 }
5.11.cpp程式碼:
1 #include"account.h" 2 #include<iostream> 3 using namespace std; 4 int main() { 5 SavingsAccount sa0(1, 21325302, 0.015); 6 SavingsAccount sa1(1, 58320212, 0.015); 7 sa0.deposit(5, 5000); 8 sa1.deposit(25, 10000); 9 sa0.deposit(45, 5500); 10 sa1.withdraw(60, 4000); 11 sa0.settle(90); 12 sa1.settle(90); 13 sa0.show(); cout << endl; 14 sa1.show(); cout << endl; 15 cout << "Total: " << SavingsAccount::getTotal() << endl; 16 return 0; 17 }
執行結果截圖:
理解和體會:
類透過私有成員變數和公有成員函式實現了封裝。外界只能透過公有成員函式來訪問和操作賬戶資訊,保證了資料的安全性和完整性。程式中使用了靜態成員變數來記錄總賬戶數,這是資料共享。同時,透過類的封裝和訪問許可權控制,保護了賬戶資訊不被非法訪問和修改。對銀行賬戶進行了抽象,使得程式碼更加簡潔、易於理解和維護。
最佳化:
可以使用介面或抽象類來定義更多基本的功能,並透過多型性來實現不同的銀行賬戶型別。同時,加強程式碼的安全性,使用一些加密函式等等。