實驗2 類與物件

张朦丹發表於2024-10-28

實驗任務一

t.h

 1 // 類T: 宣告
 2 class T {
 3 // 物件屬性、方法
 4 public:
 5     T(int x = 0, int y = 0);   // 普通建構函式
 6     T(const T &t);  // 複製建構函式
 7     T(T &&t);       // 移動建構函式
 8     ~T();           // 解構函式
 9 
10     void adjust(int ratio);      // 按係數成倍調整資料
11     void display() const;           // 以(m1, m2)形式顯示T類物件資訊
12 
13 private:
14     int m1, m2;
15 
16 // 類屬性、方法
17 public:
18     static int get_cnt();          // 顯示當前T類物件總數
19 
20 public:
21     static const std::string doc;       // 類T的描7述資訊
22     static const int max_cnt;           // 類T物件上限
23 
24 private:
25     static int cnt;         // 當前T類物件數目
26 
27 // 類T友元函式宣告
28     friend void func();
29 };
30 
31 // 普通函式宣告
32 void func();

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 }

執行結果

問題一:不可以去掉func()

原因可能是因為func()函式沒有在t.h中提前被宣告

問題二 各種建構函式的功能,以及它們與解構函式 的呼叫時機。

解構函式:當物件結束其生命週期的時候,如物件所在函式已經呼叫完畢時,系統會自動執行解構函式。解構函式沒有任何的引數也沒有返回值,只能有一個解構函式,不能過載。如果使用者沒有編寫,系統也會自動生成預設的解構函式。

建構函式:是一種特殊的成員函式,它在物件建立時自動呼叫,用於初始化物件的成員變數。其名稱必須與類名相同,且沒有返回型別(即使是 void 也不能)。

複製建構函式:使用一個已經存在的物件去初始化同類的一個新物件

移動建構函式:它能夠從一個右值引用建立新的物件,而無需進行深複製

問題三 不能正確編譯執行

實驗任務二

Complex.h

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 
 6 class Complex{
 7 public:
 8     Complex(double x=0,double y=0);
 9     Complex(const Complex &c);
10     ~Complex();
11 
12 private:
13     double real,imag;
14 
15 public:
16     double get_real() const;
17     double get_imag() const;
18     void add(const Complex &c);
19     
20 public:
21     static const string doc; 
22 
23 private:    
24     friend Complex add(const Complex &c1,const Complex &c2);
25     
26     friend bool is_equal(const Complex &c1,const Complex &c2);
27     
28     friend bool is_not_equal(const Complex &c1,const Complex &c2);    
29     
30     friend void output(const Complex &c);
31     
32     friend double abs(const Complex &c);
33 };

Complex.cpp

#include<iostream>
#include<string>
#include<cmath>
#include"Complex.h"

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

const std::string Complex::doc{"a simplified complex class"};

double Complex::get_real() const{
    return real;
}

double Complex::get_imag() const {
    return imag;
}

Complex::Complex(double x,double y):real{x},imag{y}{}

Complex::Complex(const Complex &c):real{c.get_real()},imag{c.get_imag()}{
}

Complex::~Complex(){}

void Complex::add(const Complex &c){
    real+=c.get_real();
    imag+=c.get_imag();
}

Complex add(const Complex &c1,const Complex &c2){
    return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
}
    
bool is_equal(const Complex &c1,const Complex &c2){
    if (c1.get_real()==c2.get_real() && c1.get_imag()==c2.get_imag()){
        return true;
    }else{
        return false;
    }
}
    
bool is_not_equal(const Complex &c1,const Complex &c2){
    if (c1.get_real()==c2.get_real() && c1.get_imag()==c2.get_imag()){
        return false;
    }else{
        return true;
    }
}
    
void output(const Complex &c){
    if(c.get_imag()>=0){
        cout<<c.get_real()<<"+"<<c.get_imag()<<"i"<<endl;
    }else{
        cout<<c.get_real()<<c.get_imag()<<"i"<<endl;
    }
}
    
double abs(const Complex &c){
    return sqrt(c.get_imag()*c.get_imag()+c.get_real()*c.get_real());
}

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 }

執行結果

實驗任務三

 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 }

實驗任務四

Fraction.h

 1 #pragma once
 2 #include<string>
 3 #include<iostream>
 4 using namespace std;
 5 
 6 class Fraction{
 7 
 8 public:
 9     Fraction(int x,int y=1);
10     Fraction(const Fraction &f);
11     ~Fraction();
12     static const string doc;
13 
14 public:
15     int get_up() const;
16     int get_down() const;
17     const Fraction negative();
18     
19 private:
20     int up,down;
21     
22 public:
23     friend void output(const Fraction &f);
24     
25     friend Fraction add(const Fraction &f1,const Fraction &f2);
26     
27     friend Fraction sub(const Fraction &f1,const Fraction &f2);
28     
29     friend Fraction mul(const Fraction &f1,const Fraction &f2);
30     
31     friend Fraction div(const Fraction &f1,const Fraction &f2);
32     
33 };

Fraction.cpp

 1 #include"fraction.h"
 2 #include<string>
 3 #include<iostream>
 4 #include<cmath>
 5 
 6 using std::cout;
 7 using std::endl;
 8 
 9 const std::string Fraction::doc="Fraction類 v0.01版.\n目前僅支援分數物件的構造、輸出、加/減/乘/除運算.";
10 
11 Fraction::Fraction(int x,int y){
12     int a=abs(x);
13     int b=y;
14     int t;
15     if (a%b==0){
16         t=b;
17         up=x/b;down=y/b;
18     }else if(b%a==0){
19         t=a;
20         up=x/a;down=y/b;
21     }else{
22         t=a%b;
23         while(t){
24             a=b;
25             b=t;
26             t=a%b;
27         }
28         up=x/b;down=y/b;
29     }
30 }
31 
32 int Fraction::get_up() const{
33     return up;
34 }
35 int Fraction::get_down() const {
36     return down;
37 }
38 Fraction::Fraction(const Fraction &f):up{f.get_up()},down{f.get_down()}{};
39 
40 Fraction::~Fraction(){};
41 
42 const Fraction Fraction::negative(){
43     return Fraction((-1)*up,down);
44 }
45 
46 void output(const Fraction &f){
47     if(f.get_down()==0){
48         cout<<"分母不能為0"<<endl;
49     }else if(f.get_down()==1){
50         cout<<f.get_up()<<endl;
51     }else{
52         cout<<f.get_up()<<"/"<<f.get_down()<<endl;
53     }
54 }
55     
56 Fraction add(const Fraction &f1,const Fraction &f2){
57     int up,down;
58     up=f1.get_up()*f2.get_down()+f1.get_down()*f2.get_up();
59     down=f1.get_down()*f2.get_down();
60     return Fraction(up,down);
61 }
62     
63 Fraction sub(const Fraction &f1,const Fraction &f2){
64     int up,down;
65     up=f1.get_up()*f2.get_down()-f1.get_down()*f2.get_up();
66     down=f1.get_down()*f2.get_down();
67     return Fraction(up,down);
68 }
69     
70 Fraction mul(const Fraction &f1,const Fraction &f2){
71     return Fraction(f1.get_up()*f2.get_up(),f1.get_down()*f2.get_down());
72 }
73     
74 Fraction div(const Fraction &f1,const Fraction &f2){
75     if (f2.get_up()==0){
76         printf("分母不能為0\n"); 
77         return Fraction(0);
78     }else{
79     return Fraction(f1.get_up()*f2.get_down(),f1.get_down()*f2.get_up());
80 }    
81 }

task4.cpp

 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 }

實驗任務五

user.h

實驗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 amount);
14         double accumulate(int date)const {
15             return accumulation + balance * (date - lastDate);
16         }
17 
18 
19     public:
20         SavingsAccount(int date, int id, double rate);
21         int getId()const {
22             return id;
23         }
24         double getBanlance()const {
25             return balance;
26         }
27         double getRate()const {
28             return rate;
29         }
30         static double getTotal() {
31             return total;
32         }
33         void deposit(int date, double amcount);
34         void withdraw(int date, double amount);
35         void settle(int date);
36         void show()const;
37 };
38 #endif
View Code

user.cpp

實驗2 類與物件
 1 #include"user.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):id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
 7     cout << date << "\t#" << id << " is created" << endl;
 8 }
 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 
19 void SavingsAccount::deposit(int date, double amount) {
20     record(date, amount);
21 }
22 
23 void SavingsAccount::withdraw(int date, double amount) {
24     if (amount > getBanlance())
25         cout << "Error: not enough money" << endl;
26     else
27         record(date, -amount);
28 }
29 
30 void SavingsAccount::settle(int date) {
31     double interest = accumulate(date) * rate / 365;
32     if (interest != 0)
33         record(date, interest);
34     accumulation = 0;
35 }
36 
37 void SavingsAccount::show()const {
38     cout << "#" << id << "\tBalance:" << balance;
39 }
View Code

account.cpp

實驗2 類與物件
 1 #include"user.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 
 8     sa0.deposit(5, 5000);
 9     sa1.deposit(25, 10000);
10     sa0.deposit(45, 5500);
11     sa1.withdraw(60, 4000);
12 
13     sa0.settle(90);
14     sa1.settle(90);
15 
16     sa0.show(); cout << endl;
17     sa1.show(); cout << endl;
18     cout << "Total:" << SavingsAccount::getTotal() << endl;
19     return 0;
20 }
View Code

相關文章