c++ - 如何改进我的类(class)的运算符重载?

标签 c++ c++17 operator-overloading operators

我已经开始学习 C++。我的老师布置了作业。我完成了它(留下了一些润色工作)并且一切似乎都有效但存在冗余。我的主要问题是重载。 如何改善我类的重载。与其编写所有四个函数(两个用于 fstream,两个用于 iostream),是否可以只编写两个?还有其他进一步改进代码的建议吗?

#include <iostream>
#include <fstream>
#include <string>
#include "../../my_func.hpp"
#define usi unsigned short int

using namespace std;

class book
{
    public:
    usi book_id = 0, price = 0, no_of_pages = 0, year_of_publishing = 0;
    string author_name = "NONE", publisher = "NONE";
    book(usi b_id = 0, usi b_price = 0, usi b_no_of_pages = 0, usi b_year_of_publishing = 0,
         const string& b_author_name = "NONE", const string& b_publisher = "NONE")
    {
        book_id = b_id;
        price = b_price;
        no_of_pages = b_no_of_pages;
        year_of_publishing = b_year_of_publishing;
        author_name = b_author_name;
        publisher = b_publisher;
    }
    friend fstream& operator >> (fstream& is, book& obj);
    friend fstream& operator << (fstream& os, const book& obj);
    friend istream& operator >> (istream &is, book& obj);
    friend ostream& operator << (ostream &os, const book& obj);
};

fstream& operator >> (fstream &is, book& obj)
{
    char ch;
    is >> obj.book_id >> obj.price 
       >> obj.no_of_pages >> obj.year_of_publishing;
    is.ignore(1, '\n'); //To take care of new line character
    getline(is, obj.author_name);
    getline(is, obj.publisher);
    return is;
}

fstream& operator << (fstream &os, const book& obj)
{
    os.operator<<(obj.book_id) << '\n' //calling operator function cuz it works 
               << obj.price << '\n'
               << obj.no_of_pages << '\n'
               << obj.year_of_publishing << '\n';
    os << obj.author_name << '\n'
       << obj.publisher << '\n';
    return os;
}

istream& operator >> (istream &is, book& obj)
{
    is >> obj.book_id >> obj.price 
       >> obj.no_of_pages >> obj.year_of_publishing;
    is.ignore(1, '\n'); //To take care of new line character
    getline(is, obj.author_name);
    getline(is, obj.publisher);
    return is;
}

ostream& operator << (ostream &os, const book& obj)
{
    os << obj.book_id << '\n'
       << obj.price << '\n'
       << obj.no_of_pages << '\n'
       << obj.year_of_publishing << '\n'
       << obj.author_name << '\n'
       << obj.publisher << '\n';
    return os;
}
int main()
{
    string path = ".\\C++_Experiment\\Exp-7\\Files\\Source.txt";
    book b1(12, 3000, 100, 2003, "Lol", "Pew"), b2, b3;
    fstream fio;
    fio.open(path, ios::out | ios::app | ios::in);
    if(fio) fio << b1;
    else cout <<  "error"; 
    fio.seekg(0, ios::beg);
    if(fio) fio >> b2 >> b3;
    cout << b2 << b3;
    fio.close();
    cout << "DONE";
    return 0;
}

最佳答案

这里你只需要两个重载。 ifstreamofstream 分别继承自 istreamostream 所以如果你有

friend istream& operator >> (istream &is, book& obj);
friend ostream& operator << (ostream &os, const book& obj);

那么它们将与 coutcin 以及任何 fstreamstringstream 对象一起工作,因为它们也继承来自 istreamostream

关于c++ - 如何改进我的类(class)的运算符重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71913815/

相关文章:

c++ - 间接运算符的定义质量是什么?

c++ - 如何在 Code::Blocks 中使用 wmain() 入口点?

c++ - 链接器如何处理链接到共享库的 C++ 静态库的唯一类型信息约束?

c++ - 获取当前线程时间id的操作开销大吗?

c++ - 如何在 C++ 中初始化返回元组的变量?

c++ - 在 C++ 中重载 `=` 运算符

c++ - 丢弃视频帧

c++ - 实现动态插件管理器

c++ - 为什么将 lambda 用于非类型模板参数时 gcc 会失败?

c++ - 为 std::pair 重载运算符 <