c ++重载操作符<<和运算符>> [英] c++ overloading operator<< and operator>>

查看:182
本文介绍了c ++重载操作符<<和运算符>>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Book 
{
string title;
int category;
public:
Book(const string& abook,int num);
string getTitle()const;
int getCategory()const;
friend ostream&运算符<<(ostream& os,const Book& abook);
friend istream& operator>>(istream& is,Book& abook);
};

class Reader //基类
{
private:
string reader_name;
vector< Book>书列表;
public:
string showname()const;
void add(const Book& abook); //组织书目列表
friend ostream& operator<<(ostream& os,const Reader& read_name);
friend istream& operator>>(istream& is,Reader& read_name);
};

class fantasyReader:public Reader {};
class horrorReader:public Reader {};
class scienceReader:public Reader {};
class mysteryReader:public Reader {};

我有两个给定的文本文件。

1)Reader.txt< ---包含读者姓名和类别


例如


David< - 读者姓名

0 < - david is幻想阅读器




2)Book.txt< ----包含图书的标题和类别

对于ex

Riddick< - 书名

0< - 书的类别是幻想




在主函数中,读者对象的指针数组指向每个派生类;

  ex 

Reader * obj [10];
int pos = 0;
obj [pos ++] = new fantasyReader();

主要目标是整理书本列表并放入适当的类别和适当的读者
将它们写入新的文本文件。

ex。


-David-

Riddick



-John-

乌鸦







我的问题

我不确定应该在运营商内部<<和运算符>>

类和类读者

解决方案

>你应该把什么放在重载的<<和>>运算符?

实际上,您可以在重载的<< c $ c>>> 运算符。

例如:

  Book obj; 
cout<< obj; // Calls your overloaded<< operator

作为一般原则,当重载运算符时,您应该遵循 ,这意味着您的代码应该进行类似于操作符对内在数据类型的操作。在上面的例子中,我希望我的<< 操作符显示我的 Book 类的内容, case我将重载它如下:

  //显示标题和类别
ostream& operator<<(ostream& os,const Book& abook);
{
os<< abook.title<< \\\
;
os<< abook.category< \\\
;

return os; //必须返回ostream对象
}



我需要返回一个流对象, 链接例如:

  Book obj1,obj2,obj3; 
cout<<< obj1<< obj2<< obj3;

同样,>> 运算符我希望运算符从用户获取数据。
例如:

  Book obj; 
cin>> obj; // Calls your overloaded>> operator

我会重载>> 运算符如下:

  //获取书名来自用户的类别
istream& operator>>(istream& is,Book& abook)
{
cout< Enter Book Title:;
是>> abook.title;
cout<< 输入书类别:;
是>> abook.category;

return is; //必须返回流对象
}

是您可以在>> << 操作符中放置任何功能,但不要忘记原则的最低惊讶!


class Book
{ 
     string title;
     int category;
 public:
    Book(const string& abook, int num);
    string getTitle() const;
    int getCategory() const;
    friend ostream& operator<<(ostream& os, const Book& abook);
    friend istream& operator>>(istream& is, Book& abook);
 };

class Reader  // base class
{
private:
    string reader_name;
    vector<Book> bookLists;
public:
   string showname() const;
   void add(const Book& abook);    // organize book list
   friend ostream& operator<<(ostream& os, const Reader& read_name);
   friend istream& operator>>(istream& is, Reader& read_name);
};

 class fantasyReader : public Reader {  };
 class horrorReader : public Reader { };
 class scienceReader : public Reader { } ;
class mysteryReader : public Reader { };

I have two given text files.
1) Reader.txt <--- contains reader's name and category

For ex.

David <- reader's name
0 <- david is Fantasy reader


2) Book.txt <---- contains Book's title and category

For ex
Riddick <- Book's title
0 <- Book's category is fantasy


In the main function, array of pointers to Reader's obj are pointing each dervied class;

 ex

Reader *obj[10];
int pos =0;
obj[pos++] = new fantasyReader();

The main goal is to organize book's list and put into a appropriate category and appropriate reader
and write them into a new text file.
ex.

-David-
Riddick

-John-
The Crow



MY QUESTION
I'm not sure what should be inside of operator<< and operator>>
for class Book and class Reader

解决方案

What should you put inside the overloaded << and >> operators?
Well, You can actually put anything inside the overloaded << and >> operator. They will just be simple function calls whenever a appropriate opportunity presents.
For eg:

Book obj;
cout<< obj;  //Calls your overloaded << operator

As a general principle while overloading operators you should follow Principle of Least Astonishment, which means your code should be doing something similar what the operator does for a intrinsic data type. In the above example I would expect my << operator to display the contents of my Book class, in that case I would overload it as follows:

// Display title and category 
ostream& operator<<(ostream& os, const Book& abook);
{
    os << abook.title << "\n";
    os << abook.category<< "\n";

    return os; // must return ostream object
}

I need to return a stream object since it allows for the chaining ex:

Book obj1,obj2,obj3;
cout<<obj1<<obj2<<obj3;

Similarly, for >> Extraction operator I would expect the operator to get the data from user. For ex:

Book obj;
cin>>obj; //Calls your overloaded >> operator

I would overload the >> operator as follows:

//Get the Book Title & Category from User
istream& operator>>(istream& is, Book& abook)
{
    cout << "Enter Book Title: ";
    is >> abook.title ;
    cout << "Enter Book Category: ";
    is >> abook.category;

    return is;  //Must return stream object
}

So, the bottomline is You can put any functionality inside >> and << operators but don't forget the Principle of Least Astonishment!

这篇关于c ++重载操作符&lt;&lt;和运算符&gt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆