Task1
t.h
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
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 }View Code
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 }View Code
result1
问题一
在tas1.cpp中identifier "func" is undefined。
这个错误是由于 task1.cpp 文件中没有正确引用 func() 函数的声明,导致编译器无法识别 func()。虽然 func() 是 T 类的友元函数,并在 t.cpp 文件中实现,但 task1.cpp 没有包含该函数的声明要解决这个问题,需要在 t.h 文件中确保 func() 函数的声明可以被 task1.cpp 看到。虽然已经在 t.h 文件中声明了 func() 为友元函数,但还需要在全局作用域显式声明 func(),这样其他文件才能正确引用该函数。
问题二
带默认值的普通构造函数,会在初始化一个对象的时候调用,并同时赋值。
复制构造函数,在用另一个对象来初始化对象的时候调用。
移动构造函数,在需要将一个对象的资源所有权转移到另一个对象时调用。
析构函数,在对象生命期结束,销毁时调用。
问题三
被多次定义,编译错误。
Task2
Complex.h
1 #pragma once 2 3 #include<iostream> 4 #include<cmath> 5 6 using std::string; 7 using std::cout; 8 9 class Complex { 10 public: 11 Complex(double m_real = 0, double m_imag = 0); 12 Complex(const Complex& c); 13 ~Complex() = default; 14 15 double get_real() const; 16 double get_imag() const; 17 Complex add(const Complex& c); 18 19 friend Complex add(const Complex& c1, const Complex& c2); 20 friend bool is_equal(const Complex& c1, const Complex& c2); 21 friend bool is_not_equal(const Complex& c1, const Complex& c2); 22 friend double abs(const Complex& c); 23 friend void output(const Complex& c); 24 25 private: 26 double real, imag; 27 28 public: 29 static string doc; 30 }; 31 32 Complex add(const Complex& c1, const Complex& c2); 33 34 bool is_equal(const Complex& c1, const Complex& c2); 35 36 bool is_not_equal(const Complex& c1, const Complex& c2); 37 38 void output(const Complex& c); 39 40 double abs(const Complex& c);
Complex.cpp
1 #include "Complex.h" 2 #include <iostream> 3 4 string Complex::doc{ "a simplified complex class" }; 5 6 Complex::Complex(double m_real, double m_imag) : real{ m_real }, imag{ m_imag } { 7 } 8 9 Complex::Complex(const Complex& c) :real{ c.real }, imag{ c.imag } { 10 } 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 Complex Complex::add(const Complex& c) { 22 this->real += c.real; 23 this->imag += c.imag; 24 return *this; 25 } 26 27 28 Complex add(const Complex& c1, const Complex& c2) { 29 Complex temp; 30 temp.real = c1.real + c2.real; 31 temp.imag = c1.imag + c2.imag; 32 return temp; 33 } 34 35 bool is_equal(const Complex& c1, const Complex& c2) { 36 if (c1.real == c2.real && c1.imag == c2.imag) 37 return true; 38 else 39 return false; 40 } 41 42 bool is_not_equal(const Complex& c1, const Complex& c2) { 43 if (c1.real == c2.real && c1.imag == c2.imag) 44 return false; 45 else 46 return true; 47 } 48 49 void output(const Complex& c) { 50 cout << c.real; 51 if (c.imag < 0) 52 cout << " - " << abs(c.imag) << "i"; 53 else 54 cout << " + " << c.imag << "i"; 55 56 } 57 58 double abs(const Complex& c) { 59 double temp = c.real * c.real + c.imag * c.imag; 60 return sqrt(temp); 61 }
task2.cpp
1 // 备用代码3 2 #include <iostream> 3 #include "Complex.h" 4 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
result2
Task3
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() << 21 endl; 22 cout << endl; 23 24 cout << "复数运算测试: " << endl; 25 cout << "abs(c2) = " << abs(c2) << endl; 26 c1 += c2; 27 cout << "c1 += c2, c1 = " << c1 << endl; 28 cout << boolalpha; 29 cout << "c1 == c2 : " << (c1 == c2) << endl; 30 cout << "c1 != c3 : " << (c1 != c3) << endl; 31 c4 = c2 + c3; 32 cout << "c4 = c2 + c3, c4 = " << c4 << endl; 33 } 34 35 int main() { 36 test(); 37 }View Code
result3
Task4
Fraction.h
1 #pragma once 2 3 #include <iostream> 4 5 using std::string; 6 using std::cout; 7 8 class Fraction { 9 public: 10 Fraction(int m_up, int m_down = 1); 11 Fraction(const Fraction& f); 12 ~Fraction() = default; 13 14 int get_up() const; 15 int get_down() const; 16 Fraction negative() const; 17 18 private: 19 int up, down; 20 void simp(); 21 22 public: 23 static string doc; 24 25 public: 26 friend void output(const Fraction& f); 27 friend Fraction add(const Fraction& f1, const Fraction& f2); 28 friend Fraction sub(const Fraction& f1, const Fraction& f2); 29 friend Fraction mul(const Fraction& f1, const Fraction& f2); 30 friend Fraction div(const Fraction& f1, const Fraction& f2); 31 }; 32 33 void output(const Fraction& f); 34 35 Fraction add(const Fraction& f1, const Fraction& f2); 36 37 Fraction sub(const Fraction& f1, const Fraction& f2); 38 39 Fraction mul(const Fraction& f1, const Fraction& f2); 40 41 Fraction div(const Fraction& f1, const Fraction& f2);
Fraction.cpp
1 #include "Fraction.h" 2 #include <iostream> 3 #include <cmath> 4 5 using std::string; 6 using std::cout; 7 using std::endl; 8 9 string Fraction::doc{ "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算." }; 10 11 Fraction::Fraction(int m_up, int m_down) :up{ m_up }, down{ m_down } { 12 simp(); 13 } 14 15 Fraction::Fraction(const Fraction& f) : up{ f.up }, down{ f.down } { 16 simp(); 17 } 18 19 void Fraction::simp() { 20 //调整符号 21 if (down < 0) { 22 up = -up; 23 down = -down; 24 } 25 26 //化简操作 27 bool sign = false; 28 if (up < 0) sign = true; 29 30 int t_up = abs(up); 31 for (int div = t_up; div > 1; div--) { 32 if (down % div == 0 && t_up % div == 0) { 33 t_up /= div; 34 down /= div; 35 if (sign) 36 up = -t_up; 37 else 38 up = t_up; 39 return; 40 } 41 } 42 } 43 44 int Fraction::get_up() const { 45 return up; 46 } 47 48 int Fraction::get_down() const { 49 return down; 50 } 51 52 Fraction Fraction::negative() const { 53 return Fraction{ -up, down }; 54 } 55 56 void output(const Fraction& f) { 57 if (f.up == 0) 58 cout << 0; 59 else if (f.down == 0) 60 cout << "分母不能为0"; 61 else if (f.down == 1) 62 cout << f.up; 63 else 64 cout << f.up << "/" << f.down; 65 } 66 67 Fraction add(const Fraction& f1, const Fraction& f2) { 68 return Fraction{ f1.up * f2.down + f2.up * f1.down, f1.down * f2.down };; 69 } 70 71 Fraction sub(const Fraction& f1, const Fraction& f2) { 72 return Fraction{ f1.up * f2.down - f2.up * f1.down, f1.down * f2.down };; 73 } 74 75 Fraction mul(const Fraction& f1, const Fraction& f2) { 76 return Fraction{ f1.up * f2.up, f1.down * f2.down }; 77 } 78 79 Fraction div(const Fraction& f1, const Fraction& f2) { 80 return Fraction{ f1.up * f2.down, f1.down * f2.up }; 81 }
task4.cpp
1 #include "Fraction.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 void test1() { 8 cout << "Fraction类测试: " << endl; 9 cout << Fraction::doc << endl << endl; 10 11 Fraction f1(5); 12 Fraction f2(3, -4), f3(-18, 12); 13 Fraction f4(f3); 14 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() = " << 23 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
result4
Task5
account.h
1 #ifndef __ACCOUNT_H__ 2 #define __ACCOUNT_H__ 3 4 class SavingAccount { 5 private: 6 int id; 7 double balance; 8 double rate; 9 int lastDate; 10 double accumulation; 11 12 static double total; 13 14 void record(int data, double amount); 15 double accumulate(int date) const { 16 return accumulation + balance * (date - lastDate); 17 } 18 19 public: 20 SavingAccount(int date, int id, double rate); 21 int getId() const { return id; } 22 double getBalance() const { return balance; } 23 double getRate() const { return rate; } 24 25 static double getTotal() { return total; } 26 27 void deposit(int date, double amount); 28 void withdraw(int date, double amount); 29 30 void settle(int date); 31 32 void show() const; 33 }; 34 35 #endif
account.cpp
1 #include "account.h" 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 6 double SavingAccount::total = 0; 7 8 SavingAccount::SavingAccount(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 13 void SavingAccount::record(int date, double amount) { 14 accumulation = accumulate(date); 15 lastDate = date; 16 amount = floor(amount * 100 + 0.5) / 100; 17 balance += amount; 18 total += amount; 19 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 20 } 21 22 void SavingAccount::deposit(int date, double amount) { 23 record(date, amount); 24 } 25 26 void SavingAccount::withdraw(int date, double amount) { 27 if (amount > getBalance()) 28 cout << "Error: not enough money" << endl; 29 else 30 record(date, -amount); 31 } 32 33 void SavingAccount::settle(int date) { 34 double interest = accumulate(date) * rate / 365; 35 if (interest != 0) 36 record(date, interest); 37 accumulation = 0; 38 } 39 40 void SavingAccount::show() const { 41 cout << "#" << id << "\tBalance: " << balance; 42 }
5_11.cpp
1 #include "account.h" 2 #include <iostream> 3 using namespace std; 4 5 int main() { 6 SavingAccount sa0(1, 21325302, 0.015); 7 SavingAccount sa1(1, 58320212, 0.015); 8 9 sa0.deposit(5, 5000); 10 sa1.deposit(25, 10000); 11 sa0.deposit(45, 5500); 12 sa1.deposit(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: " << SavingAccount::getTotal() << endl; 20 }
result5
改进点:
利息是按天数计算的,使用了 rate / 365
来假设一年有 365 天。然而,对于利息计算,会有闰年这类特殊情况存在。
在取款函数 withdraw
中,只有在余额不足时输出错误信息,但没有抛出异常或者返回错误码。