#include <iostream> #include <string> class Book { private: std::string title = "标题待定"; std::string author = "张三"; int pages = 100; double price = 10.0; public: // Getter and Setter std::string getTitle() const { return title; } void setTitle(const std::string& t) { title = t; } std::string getAuthor() const { return author; } void setAuthor(const std::string& a) { author = a; } int getPages() const { return pages; } void setPages(int p) { pages = p; } double getPrice() const { return price; } void setPrice(double pr) { price = pr; } // Operator overloading Book operator+(const Book& other) const { return Book("标题待定", "无名氏", this->pages + other.pages, this->price + other.price); } Book& operator+=(double increment) { this->price += increment; return *this; } Book operator-(const Book& other) const { return Book("标题待定", "无名氏", this->pages - other.pages, this->price - other.price); } }; Book::Book(const std::string& t, const std::string& a, int p, double pr) : title(t), author(a), pages(p), price(pr) {} int main() { Book book1("C++ Primer", "Stanley B. Lippman", 1000, 50.0); Book book2("Effective C++", "Scott Meyers", 300, 30.0); Book sum = book1 + book2; std::cout << "Sum: " << sum.getTitle() << ", " << sum.getAuthor() << ", " << sum.getPages() << " pages, $" << sum.getPrice() << std::endl; book1 += 5.0; std::cout << "Book1 after +=: $" << book1.getPrice() << std::endl; Book diff = book1 - book2; std::cout << "Difference: " << diff.getTitle() << ", " << diff.getAuthor() << ", " << diff.getPages() << " pages, $" << diff.getPrice() << std::endl; return 0; }修改代码
时间: 2025-03-25 10:10:27 浏览: 28
以下是您提供的代码的一个改进版本,其中对某些功能进行了优化,并增加了注释以便于理解:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 定义书籍类
class Book {
private:
string title; // 标题
string author; // 作者
int pages; // 页数
double price; // 价格
public:
// 构造函数
Book(string t = "标题未设定", string a = "未知", int p = 0, double pr = 0.0)
: title(move(t)), author(move(a)), pages(p), price(pr) {}
// 获取和设置属性的方法
string getTitle() const { return title; }
void setTitle(const string &t) { title = t; }
string getAuthor() const { return author; }
void setAuthor(const string &a) { author = a; }
int getPages() const { return pages; }
void setPages(int p) { if (p >= 0) pages = p; } // 确保页数非负
double getPrice() const { return price; }
void setPrice(double pr) { if (pr >= 0) price = pr; } // 确保价格非负
// 运算符重载:加法运算符 (+),返回新书的对象
Book operator+(const Book &other) const {
return Book(title + "+" + other.title, "合并版", pages + other.pages,
price + other.price);
}
// 运算符重载:增加运算符 (+=),更新当前书的价格
Book &operator+=(double increment) {
if (increment > 0) price += increment; // 增量需为正数
return *this;
}
// 运算符重载:减法运算符 (-),计算两本书之间的差异
Book operator-(const Book &other) const {
return Book("差值", "", abs(pages - other.pages),
abs(price - other.price)); // 取绝对值避免负数结果
}
// 输出信息至控制台
friend ostream &operator<<(ostream &os, const Book &book); // 友元函数声明
};
// 实现友元函数,用于将书籍内容打印出来
ostream &operator<<(ostream &os, const Book &book) {
os << "Title: " << book.title << "\n"
<< "Author: " << book.author << "\n"
<< "Pages: " << book.pages << "\n"
<< "Price: $" << book.price;
return os;
}
int main() {
// 创建两个初始书籍对象
Book book1("C++ Primer", "Stanley B. Lippman", 1000, 50.0);
Book book2("Effective C++", "Scott Meyers", 300, 30.0);
cout << "原始 Book1:\n" << book1 << endl;
cout << "原始 Book2:\n" << book2 << endl;
// 测试加法操作符 +
Book sum = book1 + book2;
cout << "\nBook1 加上 Book2 的结果:\n" << sum << endl;
// 测试自增操作符 +=
book1 += 5.0;
cout << "\nBook1 经过 += 操作后的结果:\n" << book1 << endl;
// 测试减法操作符 -
Book diff = book1 - book2;
cout << "\nBook1 减去 Book2 后的结果(仅展示数值差异):\n" << diff << endl;
return 0;
}
```
---
### 改动点说明:
1. **构造函数改进**
提供了默认参数,在创建空对象时更方便;同时使用 `std::move()` 来提升性能。
2. **边界条件检查**
对输入数据(如页面、价格)加入合法性验证,防止出现不合理情况(例如负数的页码或价格)。
3. **输出流重载**
添加了一个全局的 `operator<<` 以简化调试和显示过程,可以直接用 `cout` 打印整个对象的信息。
4. **字符串拼接优化**
当两本图书相加的时候,新的标题采用组合形式而不是固定名称 ("标题待定"),使逻辑更具意义。
---
阅读全文