知迩

实验2 类和对象_基础编程1

1. 实验任务1

 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();
t.h
 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 
13 
14 // static成员数据类外初始化
15 const std::string T::doc{"a simple class sample"};
16 const int T::max_cnt = 999;
17 int T::cnt = 0;
18 
19 
20 
21 // 对象方法
22 T::T(int x, int y): m1{x}, m2{y} { 
23     ++cnt; 
24     cout << "T constructor called.\n";
25 } 
26 
27 T::T(const T &t): m1{t.m1}, m2{t.m2} {
28     ++cnt;
29     cout << "T copy constructor called.\n";
30 }
31 
32 T::T(T &&t): m1{t.m1}, m2{t.m2} {
33     ++cnt;
34     cout << "T move constructor called.\n";
35 }    
36 
37 T::~T() {
38     --cnt;
39     cout << "T destructor called.\n";
40 }           
41 
42 void T::adjust(int ratio) {
43     m1 *= ratio;
44     m2 *= ratio;
45 }    
46 
47 void T::display() const {
48     cout << "(" << m1 << ", " << m2 << ")" ;
49 }     
50 
51 // 类方法
52 int T::get_cnt() {
53    return cnt;
54 }
55 
56 // 友元
57 void func() {
58     T t5(42);
59     t5.m2 = 2049;
60     cout << "t5 = "; t5.display(); cout << endl;
61 }
t.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 }
task1.cpp

 

运行测试结果截图

 

回答

1、说明可以去掉line36

2、

普通构造函数:
功能:初始化对象的状态,为成员变量分配默认值或用户指定的初始值。
调用时机:在对象被显式创建时,如通过直接声明、使用new操作符或作为其他对象的成员被初始化时。


复制构造函数:
功能:通过另一个同类型的对象来初始化新对象,实现对象的深拷贝或浅拷贝。
调用时机:当对象被另一个同类型对象按值初始化、作为函数参数传递(按值)、作为返回值返回(按值)或插入到需要复制元素的容器中时。


移动构造函数:
功能:通过右值引用来初始化新对象,实现资源的“窃取”语义,避免不必要的复制操作,提高性能。
调用时机:当对象作为右值(如临时对象)被移动初始化、容器操作(如插入、重新分配)需要移动元素以优化性能时。


析构函数:
功能:清理对象在生命周期中分配的资源,如内存、文件句柄、网络连接等,确保对象在销毁时不会造成资源泄露。
调用时机:当对象超出其作用域(对于局部变量)、被delete操作符释放(对于动态分配的对象)、作为类的成员被销毁(如果成员对象有析构函数,则它们也会被调用)或当数组或容器被销毁时(其中的对象也会被销毁)。

3、不能 

 

2. 实验任务2

 1 #ifndef COMPLEX_H  
 2 #define COMPLEX_H  
 3   
 4 #include <iostream>  
 5 #include <string>  
 6 #include <cmath>  
 7   
 8 class Complex {  
 9 public:  
10     static const std::string doc;  // 类的描述信息  
11   
12     // 构造函数  
13     Complex();  
14     Complex(double real);  
15     Complex(double real, double imag);  
16     Complex(const Complex& other);  
17   
18     // 成员函数  
19     double get_real() const;  
20     double get_imag() const;  
21     void add(const Complex& other);  
22   
23     // 友元函数声明  
24     friend Complex add(const Complex& c1, const Complex& c2);  
25     friend bool is_equal(const Complex& c1, const Complex& c2);  
26     friend bool is_not_equal(const Complex& c1, const Complex& c2);  
27     friend void output(const Complex& c);  
28     friend double abs(const Complex& c);  
29   
30 private:  
31     double real;  // 实部  
32     double imag;  // 虚部  
33 };  
34   
35 #endif // COMPLEX_H
Complex.h
 1 #include "Complex.h"  
 2   
 3 // 类的描述信息  
 4 const std::string Complex::doc = "a simplified complex class";  
 5   
 6 // 构造函数实现  
 7 Complex::Complex() : real(0), imag(0) {}  
 8 Complex::Complex(double real) : real(real), imag(0) {}  
 9 Complex::Complex(double real, double imag) : real(real), imag(imag) {}  
10 Complex::Complex(const Complex& other) : real(other.real), imag(other.imag) {}  
11   
12 // 成员函数实现  
13 double Complex::get_real() const {  
14     return real;  
15 }  
16   
17 double Complex::get_imag() const {  
18     return imag;  
19 }  
20   
21 void Complex::add(const Complex& other) {  
22     real += other.real;  
23     imag += other.imag;  
24 }  
25   
26 // 友元函数实现  
27 Complex add(const Complex& c1, const Complex& c2) {  
28     return Complex(c1.real + c2.real, c1.imag + c2.imag);  
29 }  
30   
31 bool is_equal(const Complex& c1, const Complex& c2) {  
32     return c1.real == c2.real && c1.imag == c2.imag;  
33 }  
34   
35 bool is_not_equal(const Complex& c1, const Complex& c2) {  
36     return !(is_equal(c1, c2));  
37 }  
38   
39 void output(const Complex& c) {  
40     std::cout << c.real;  
41     if (c.imag >= 0) std::cout << " + ";  
42     std::cout << c.imag << "i";  
43 }  
44   
45 double abs(const Complex& c) {  
46     return std::sqrt(c.real * c.real + c.imag * c.imag);  
47 }
Complex.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 }
task2.cpp

 

运行测试结果截图

 

3. 实验任务3

 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

 

运行测试结果截图

 

4. 实验任务4

 1 #ifndef FRACTION_H  
 2 #define FRACTION_H  
 3   
 4 #include <iostream>  
 5 #include <algorithm>  
 6   
 7 class Fraction {  
 8 public:  
 9     static const std::string doc;  
10   
11     // Constructors  
12     Fraction(int up = 0, int down = 1);  
13     Fraction(const Fraction& other);  
14   
15     // Getters  
16     int get_up() const;  
17     int get_down() const;  
18   
19     // Methods  
20     Fraction negative() const;  
21   
22     // Friend functions  
23     friend void output(const Fraction& f);  
24     friend Fraction add(const Fraction& f1, const Fraction& f2);  
25     friend Fraction sub(const Fraction& f1, const Fraction& f2);  
26     friend Fraction mul(const Fraction& f1, const Fraction& f2);  
27     friend Fraction div(const Fraction& f1, const Fraction& f2);  
28   
29 private:  
30     int up;  
31     int down;  
32   
33     // Helper function to simplify fraction  
34     void simplify();  
35 };  
36   
37 #endif // FRACTION_H
Fraction.h
 1 #include "Fraction.h"  
 2   
 3 // 自定义的 gcd 函数  
 4 int gcd(int a, int b) {  
 5     while (b != 0) {  
 6         int temp = b;  
 7         b = a % b;  
 8         a = temp;  
 9     }  
10     return a;  
11 }  
12   
13 const std::string Fraction::doc = "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算.";  
14   
15 Fraction::Fraction(int up, int down) : up(up), down(down) {  
16     simplify();  
17 }  
18   
19 Fraction::Fraction(const Fraction& other) : up(other.up), down(other.down) {}  
20   
21 int Fraction::get_up() const {  
22     return up;  
23 }  
24   
25 int Fraction::get_down() const {  
26     return down;  
27 }  
28   
29 Fraction Fraction::negative() const {  
30     return Fraction(-up, down);  
31 }  
32   
33 void Fraction::simplify() {  
34     int common_divisor = gcd(std::abs(up), std::abs(down)); // 使用自定义的 gcd 函数  
35     up /= common_divisor;  
36     down /= common_divisor;  
37   
38     if (down < 0) {  
39         up = -up;  
40         down = -down;  
41     }  
42 }  
43   
44 void output(const Fraction& f) {  
45     if (f.down == 1) {  
46         std::cout << f.up; // 如果分母为1,只输出分子  
47     } else {  
48         std::cout << (f.up >= 0 ? "" : "-") << std::abs(f.up) << "/" << f.down; // 否则,输出标准的分数形式  
49     }  
50 }
51   
52 Fraction add(const Fraction& f1, const Fraction& f2) {  
53     int new_up = f1.up * f2.down + f2.up * f1.down;  
54     int new_down = f1.down * f2.down;  
55     Fraction result(new_up, new_down);  
56     result.simplify(); // 简化结果分数  
57     return result;  
58 }  
59   
60 Fraction sub(const Fraction& f1, const Fraction& f2) {  
61     int new_up = f1.up * f2.down - f2.up * f1.down;  
62     int new_down = f1.down * f2.down;  
63     Fraction result(new_up, new_down);  
64     result.simplify(); // 简化结果分数  
65     return result;  
66 }  
67   
68 Fraction mul(const Fraction& f1, const Fraction& f2) {  
69     int new_up = f1.up * f2.up;  
70     int new_down = f1.down * f2.down;  
71     Fraction result(new_up, new_down);  
72     result.simplify(); // 简化结果分数  
73     return result;  
74 }  
75   
76 Fraction div(const Fraction& f1, const Fraction& f2) {  
77     if (f2.up == 0) {  
78         std::cout<<"分母不能为0"<<std::endl; 
79         return false; 
80         //return std::nullopt; 
81     }  
82     int new_up = f1.up * f2.down;  
83     int new_down = f1.down * f2.up;  
84     Fraction result(new_up, new_down);  
85     result.simplify(); // 简化结果分数  
86     return result;  
87 }
Fraction.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 }
task4.cpp

 

运行测试结果截图

 

5. 实验任务5

 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 
12     void record(int date, double amount);
13    
14     double accumulate(int date) const {
15         return accumulation + balance * (date - lastDate);
16     }
17 public:
18    
19     SavingsAccount(int date, int id, double rate);
20     int getId() const { return id; }
21     double getBalance() const { return balance; }
22     double getRate() const { return rate; }
23     static double getTotal() { return total; }
24     void deposit(int date, double amount);         
25     void withdraw(int date, double amount); 
26    
27     void settle(int date);
28     
29     void show() const;
30 };
31 #endif //__ACCOUNT_H__
account.h
 1 #include "account.h"
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 double SavingsAccount::total = 0;
 7 
 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 void SavingsAccount::settle(int date) {
30     double interest = accumulate(date) * rate / 365;    
31     if (interest != 0)
32         record(date, interest);
33     accumulation = 0;
34 }
35 void SavingsAccount::show() const {
36     cout << "#" << id << "\tBalance: " << balance;
37 }
account.cpp
 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  
 9     sa0.deposit(5, 5000);
10     sa1.deposit(25, 10000);
11     sa0.deposit(45, 5500);
12     sa1.withdraw(60, 4000);
13   
14     sa0.settle(90);
15     sa1.settle(90);
16     
17     sa0.show();    cout << endl;
18     sa1.show();    cout << endl;
19     cout << "Total: " << SavingsAccount::getTotal() << endl;
20     return 0;
21 }
5_11.cpp

 

运行测试结果截图

 

posted on 2024-10-23 10:41  知迩  阅读(2)  评论(0编辑  收藏  举报

导航