任务1:
测试代码:
task1.cpp
#pragma once #include <string> // 类T: 声明 class T { // 对象属性、方法 public: T(int x = 0, int y = 0); // 普通构造函数 T(const T &t); // 复制构造函数 T(T &&t); // 移动构造函数 ~T(); // 析构函数 t.cpp void adjust(int ratio); // 按系数成倍调整数据 void display() const; // 以(m1, m2)形式显示T类对象信息 private: int m1, m2; // 类属性、方法 public: static int get_cnt(); // 显示当前T类对象总数 public: static const std::string doc; // 类T的描述信息 static const int max_cnt; // 类T对象上限 private: static int cnt; // 当前T类对象数目 // 类T友元函数声明 friend void func(); }; // 普通函数声明 void func(); t.cpp #include "t.h" #include <iostream> using std::cout; using std::endl; void test(); int main() { test(); cout << "\nmain: \n"; cout << "T objects'current count: " << T::get_cnt() << endl; } test.cpp void test() { cout << "test class T: \n"; cout << "T info: " << T::doc << endl; cout << "T objects'max count: " << T::max_cnt << endl; cout << "T objects'current count: " << T::get_cnt() << endl << endl; T t1; cout << "t1 = "; t1.display(); cout << endl; T t2(3, 4); cout << "t2 = "; t2.display(); cout << endl; T t3(t2); t3.adjust(2); cout << "t3 = "; t3.display(); cout << endl; T t4(std::move(t2)); cout << "t3 = "; t4.display(); cout << endl; cout << "T objects'current count: " << T::get_cnt() << endl; func(); }
问题1:
去掉后不能正常编译运行,报错信息截图如下:
可能的原因是:虽然在类中声明了func()函数是类T的友元函数,但这仅仅是阐述了二者之间的关系,如果在类外不去声明func()函数,则相当于该函数没有被声明,函数的声明和定义部分并不完整,则函数无法被调用。
问题2:
-
默认构造函数:不带参数,用于初始化对象的默认状态。对象在栈上或动态分配时自动调用。
-
参数化构造函数:接受参数以初始化对象的特定状态。在通过参数创建对象时调用,例如
T obj(42);
。 -
拷贝构造函数:用于通过已有对象创建新对象,通常用于深拷贝。在对象作为参数传递(非引用)、从对象返回或赋值时调用。
-
移动构造函数(C++11 及之后):用于将资源从临时对象转移到新对象,提高效率。当对象被移动时调用。
析构函数用于释放对象的资源和执行清理工作。它在对象生命周期结束时自动调用。
问题3:
不能够实现,报错信息截图如下:
因为static修饰的静态变量在头文件中只能声明,不能够定义。如果在头文件中定义可能会导致多重定义错误。
任务2:
代码:
头文件Complex.h:
#pragma once #include <iostream> #include <string> #include <cmath>using namespace std;class Complex {friend Complex add(const Complex &, const Complex &);friend bool is_equal(const Complex &, const Complex &);friend bool is_not_equal(const Complex &, const Complex &);friend double abs(const Complex &);friend void output(const Complex &); public:Complex(double real = 0.0, double image = 0.0) : real_{real}, image_{image} {};Complex(const Complex &other) : real_{other.real_}, image_{other.image_} {};double get_real() const;double get_image() const;Complex add(Complex &other);static string doc;double real_;double image_;};
Complex类定义文件Complex.cpp:
#include <iostream> #include "Complex.h"double Complex::get_real() const {return real_; }double Complex::get_image() const {return image_; }Complex Complex::add(Complex &other) {Complex::real_+=other.real_;Complex::image_+=other.image_; }Complex add(const Complex &c1, const Complex &c2) {return Complex(c1.real_ + c2.real_, c1.image_ + c2.image_); }bool is_equal(const Complex &c1, const Complex &c2) {return c1.real_ == c2.real_ && c1.image_ == c2.image_; }bool is_not_equal(const Complex &c1, const Complex &c2) {return !is_equal(c1, c2); }double abs(const Complex &c) {return sqrt(c.real_ * c.real_ + c.image_ * c.image_); }void output(const Complex &c){if(c.image_>=0)cout<<c.real_<<"+"<<c.image_<<"i"<<endl;elsecout<<c.real_<<c.image_<<"i"<<endl; } std::string Complex::doc = "a simplified Complex class";
测试文件test.cpp:
// 待补足(多文件组织代码时,需要包含的头文件) #include <iostream> #include "Complex.h" using namespace std; void test() {cout << "类成员测试: " << endl;cout << Complex::doc << endl;cout << endl;cout << "Complex对象测试: " << endl;Complex c1;Complex c2(3, -4);const Complex c3(3.5);Complex c4(c3);cout << "c1 = "; output(c1); cout << endl;cout << "c2 = "; output(c2); cout << endl;cout << "c3 = "; output(c3); cout << endl;cout << "c4 = "; output(c4); cout << endl;cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_image() << endl;cout << endl;cout << "复数运算测试: " << endl;cout << "abs(c2) = " << abs(c2) << endl;c1.add(c2);cout << "c1 += c2, c1 = "; output(c1); cout << endl;cout << boolalpha;cout << "c1 == c2 : " << is_equal(c1, c2) << endl;cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;c4 = add(c2, c3);cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; }int main() {test(); }
运行结果截图:
任务3:
源代码:
#include <iostream> #include <complex> using std::cout; using std::endl; using std::boolalpha; using std::complex; void test() { cout << "标准库模板类comple测试: " << endl; complex<double> c1; complex<double> c2(3, -4); const complex<double> c3(3.5); complex<double> c4(c3); cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl; cout << "c3 = " << c3 << endl; cout << "c4 = " << c4 << endl; cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl; cout << endl; cout << "复数运算测试: " << endl; cout << "abs(c2) = " << abs(c2) << endl; c1 += c2; cout << "c1 += c2, c1 = " << c1 << endl; cout << boolalpha; cout << "c1 == c2 : " << (c1 == c2) << endl; cout << "c1 != c3 : " << (c1 != c3) << endl; c4 = c2 + c3; cout << "c4 = c2 + c3, c4 = " << c4 << endl; } int main() { test(); }
问题思考:
该段代码使用了标准库complex类的哪些接口:
构造函数(默认、带参数、拷贝构造)
成员函数(real()
和 imag()
)
全局函数(abs()
)
运算符重载(+=
, ==
, !=
, +
)
这些接口共同实现了对复数的创建、运算、比较及获取属性的功能。
对比任务2:
思考1:在标准库的complex类中,复数的输出附带了()括号和逗号,使复数的输出变得更加明了,同时也不需要像自定义Complex类通过cout输出流来访问Complex类中的类成员real_,image_,然后再人为加上+,—号来输出复数,可以直接输出complex类的对象来输出复数。在复数的运算时,利用标准库complex类的函数,省去了赋值等步骤,减少了测试文件的代码数量。
思考2:标准库的complex类应该重载了构造函数,可以初始化不同数据类型的complex类,然后将complex类的输入和输出都加上了括号和逗号。使得在测试文件中加减和是否相等等验证代码变得更加简洁。
任务4:
源代码:
Fraction.h:
#include <iostream> #include <string>using namespace std;class Fraction {friend void output(const Fraction &);friend Fraction sub(const Fraction &f1, const Fraction &f2);friend Fraction mul(const Fraction &f1, const Fraction &f2);friend Fraction add(const Fraction &f1, const Fraction &f2);friend Fraction div(const Fraction &f1, const Fraction &f2);public:Fraction(int up, int down=1) : Up{up}, Down{down} {simplify();}Fraction(const Fraction &other) : Up{other.Up}, Down{other.Down} {}int get_up() const { return Up; }int get_down() const { return Down; }Fraction negative() const { return Fraction(-Up, Down); }static string doc; private:int Up;int Down;void simplify(); };
Fraction.cpp:
#include <iostream> #include "Fraction.h" using namespace std;string Fraction::doc = "Fraction类 v 0.01版. \n目前仅支持分数对象的构造、输出、加/减/乘/除运算。";int gcd(int a, int b) {while (b != 0) {int temp = b;b = a % b;a = temp;}return a; }void Fraction::simplify() {if (Up == 0) { Down = 1; return;}if (Down < 0) {Down = -Down;Up = -Up;}int divisor = gcd(abs(Up), abs(Down));Up /= divisor;Down /= divisor; }void output(const Fraction &f) {if(f.Down!=1){std::cout << f.get_up() << "/" << f.get_down() << endl;}else{std::cout << f.get_up() << endl;} }Fraction add(const Fraction &f1, const Fraction &f2) {return Fraction(f1.get_up() * f2.get_down() + f2.get_up() * f1.get_down(),f1.get_down() * f2.get_down()); }Fraction sub(const Fraction &f1, const Fraction &f2) {return add(f1, f2.negative()); }Fraction mul(const Fraction &f1, const Fraction &f2) {return Fraction(f1.get_up() * f2.get_up(), f1.get_down() * f2.get_down()); }Fraction div(const Fraction &f1, const Fraction &f2) {if (f2.get_up() == 0) {cout << "除数不能为零" << endl;system("pause");}return mul(f1, Fraction(f2.get_down(), f2.get_up())); }
task4.cpp:
#include "Fraction.h" #include <iostream>using std::cout; using std::endl;void test1() {cout << "Fraction类测试: " << endl;cout << Fraction::doc << endl << endl;Fraction f1(5);Fraction f2(3, -4), f3(-18, 12);Fraction f4(f3);cout << "f1 = "; output(f1); cout << endl;cout << "f2 = "; output(f2); cout << endl;cout << "f3 = "; output(f3); cout << endl;cout << "f4 = "; output(f4); cout << endl;Fraction f5(f4.negative());cout << "f5 = "; output(f5); cout << endl;cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl;cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; }void test2() {Fraction f6(42, 55), f7(0, 3);cout << "f6 = "; output(f6); cout << endl;cout << "f7 = "; output(f7); cout << endl;cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; }int main() {cout << "测试1: Fraction类基础功能测试\n";test1();cout << "\n测试2: 分母为0测试: \n";test2(); }
运行结果截图:
任务5:
源代码:
account.h:
#ifndef __ACCOUNT_H__ #define __ACCOUNT_H__class SavingsAccount{private:int id;double balance;double rate;int lastDate;double accumulation;static double total;void record(int date,double amount);double accumulate(int date)const{return accumulation+balance*(date-lastDate);}public:SavingsAccount(int date,int id,double rate);int getId()const{return id;}double getBalance()const{return balance;}double getRate()const{return rate;}static double getTotal(){return total;}void deposit(int date,double amount);void withdraw(int date,double amount);void settle(int date);void show()const; }; #endif//__ACCOUNT_H__
account.cpp:
#include"account.h" #include<cmath> #include<iostream> using namespace std; double SavingsAccount::total=0; SavingsAccount::SavingsAccount(int date,int id,double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0){cout<<date<<"\t#"<<id<<"is created"<<endl; } void SavingsAccount::record(int date,double amount){accumulation=accumulate(date);lastDate=date;amount=floor(amount*100+0.5)/100;balance+=amount;total+=amount;cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl; } void SavingsAccount::deposit(int date,double amount){record(date,amount); } void SavingsAccount::withdraw(int date,double amount){if(amount>getBalance())cout<<"Error:not enough money"<<endl;else record(date,-amount); } void SavingsAccount::settle(int date){double interest=accumulate(date)*rate/365;if(interest!=0)record(date,interest);accumulation=0; } void SavingsAccount::show()const{cout<<"#"<<id<<"\tBalance:"<<balance; }
task5.cpp
#include"account.h" #include<iostream> using namespace std; int main(){SavingsAccount sa0(1,21325302,0.015);SavingsAccount sa1(1,58320212,0.015);sa0.deposit(5,5000);sa1.deposit(25,10000);sa0.deposit(45,5500);sa1.withdraw(60,4000);sa0.settle(90);sa1.settle(90);sa0.show();cout<<endl;sa1.show();cout<<endl;cout<<"Total:"<<SavingsAccount::getTotal()<<endl;return 0; }
运行结果截图:
改进建议:
-
数据成员的访问控制
- 缺点:虽然
id
,balance
,rate
, 和lastDate
是私有的,但可能需要更多的保护措施,尤其是balance
和accumulation
。 - 改进:可以考虑引入友元函数或其他安全机制,以确保对这些字段的访问受到限制。
- 缺点:虽然
-
日期处理
- 缺点:使用
int
来表示日期不是最理想的,因为日期的处理可能会引入复杂性,例如跨月和跨年问题。 - 改进:可以考虑使用标准库中的日期类型,例如
std::chrono
或自定义日期类来更好地管理日期。
- 缺点:使用
-
累积利息的计算
- 缺点:
accumulate
函数和settle
函数的关系不够明确。每次调用settle
时,可能需要更新accumulation
和lastDate
,而目前的设计中没有清晰地表现出这一点。 - 改进:在
settle
函数中明确更新accumulation
和lastDate
的逻辑,使其更清晰。
- 缺点:
-
异常处理
- 缺点:
deposit
和withdraw
函数没有处理负数存款或取款超出余额的情况。 - 改进:可以引入异常处理机制,抛出异常或返回错误代码,以增强程序的健壮性。
- 缺点:
-
静态成员的管理
- 缺点:
static double total
用于跟踪所有账户的总余额,但没有线程安全的机制,可能在多线程环境中引发问题。 - 改进:可以考虑使用互斥量(mutex)来保护对
total
的访问。
- 缺点:
-
接口的灵活性
- 缺点:当前的接口较为固定,扩展性有限。
- 改进:可以考虑使用策略模式或模板方法模式,以便更灵活地处理不同的存款和取款策略。
-
缺少拷贝构造函数和赋值运算符
- 缺点:如果需要拷贝
SavingsAccount
对象,编译器生成的默认拷贝构造函数和赋值运算符可能不足以满足需求。 - 改进:可以实现自定义的拷贝构造函数和赋值运算符,以控制对象的拷贝行为。
- 缺点:如果需要拷贝
-
缺少接口文档
- 缺点:缺乏详细的文档和注释,使得类的使用和维护变得困难。
- 改进:可以添加函数的详细注释和使用示例,提升代码可读性和可维护性。