如何重载 I/O 运算符 C++

发布于 2024-08-13 21:02:49 字数 356 浏览 4 评论 0原文

我创建了一个类,允许用户输入他们的邮寄地址、订购日期、订购的 cookie 类型和数量。还有其他错误,但我熬夜并在教授的帮助下修复了它们。现在剩下的就是我需要能够更改代码以重载 I/O 流运算符,以便可以在标准输入和输出语句中使用对象。

我不确定每个人都需要看到代码的所有部分,但我将发布我认为我想做的事情所需的部分。

我需要将它放在输出()中,我有 cout <<订单<< endl; 我会上网查一下,希望今晚能准备好。感谢大家的意见。

由于课堂上的其他学生复制我的代码片段来完成他们的工作,因此被指示删除我的代码(知道这是可能的,但没有考虑到)

但是,我的代码是完整的。

I have created a class that allows the user to input their mailing address, order date, type of cookie ordered and the quantity. There were other errors, but I stayed late and with the assistance of my prof, I have fixed them. Now all that is left is that I need to be able to change code to overload the I/O stream operators so that the objects may be used in standard input and output statements.

I'm not sure what all part of the code everyone will need to see, but I'm going to post the parts I believe are needed for what I'm trying to do.

I need to have it where in the output(), I have cout << order << endl; I will look over the net and will hopefully have it ready by tonight. Thanks for everyone's input.

Was instructed to take down my code due to other students from class copying my code pieces to do their work (knew it was possible but didn't think about it)

However, my code is complete.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自在安然 2024-08-20 21:02:49

实现两个功能:
basic_ostream &运算符<< (basic_ostream&ostr, const CookieOrder&co)
basic_istream &运算符>> (basic_istream& istr, CookieOrder& co)

当您使用cout <<时,将调用operator<<=函数。订单<< endl; 并且当您使用 >>(流提取)运算符时,将调用 operator>> 函数。如何实现流提取运算符要非常小心。

您可能需要将其中任何一个声明为 CookieOrderfriend,因为这将允许该函数访问该类的私有部分,就好像该函数是 CookieOrder 的成员一样班级。


编辑以响应问题

delcare your class 中的更改,如以前一样:

类 CookieOrder {
公众:
// 和以前一样的公共方法
私人:
// 像以前一样的私有部分
};
basic_ostream &amp;运算符<< (basic_ostream&ostr, const CookieOrder&co);
basic_istream &amp;运算符>> (basic_istream&istr, CookieOrder&co);

仅使用 CookieOrder 类的公共接口来实现这两个函数。

例如:

basic_ostream &运算符<< (basic_ostream&ostr, const CookieOrder&co)
{
奥斯特<< co.get_customerName() <<结束;
/* 其余输出 */
}

函数不是 CookieOrder 类的成员,它们是普通函数,没有对 CookieOrder 类或类实例的特殊访问权限。

Implement two functions:
basic_ostream & operator<< (basic_ostream& ostr, const CookieOrder& co)
basic_istream & operator>> (basic_istream& istr, CookieOrder& co)

the operator<<= function will be called when you use cout << order << endl; and the operator>> function will be called when you use the >> (stream extraction) operator. Be very careful how you implement the stream extraction operator.

You may want to declare either of these as friend to the CookieOrder, as that will allow the function to access the private parts of the class as if the function is a member of the class.


edit to respond to changes in the question

delcare your class as before:

class CookieOrder {
public:
// public methods as before
private:
// private parts as before
};
basic_ostream & operator<< (basic_ostream& ostr, const CookieOrder& co);
basic_istream & operator>> (basic_istream& istr, CookieOrder& co);

Implement the two functions using only the public interface of the CookieOrder class.

For example:

basic_ostream & operator<< (basic_ostream& ostr, const CookieOrder& co)
{
ostr << co.get_customerName() << endl;
/* the rest of the output */
}

These functions are not members of the CookieOrder class, they are normal functions with no special access to the CookieOrder class or instanaces of the class.

负佳期 2024-08-20 21:02:49

就比较而言,您最好比较所有大写或所有小写(而不是每个单词的第一个字母大写),这样设置更简单。

此外,您应该养成在代码两边加上大括号的习惯。

为什么循环中会有一个神奇的数字 6?特别是当您只有五 (5) 个元素时。

也许循环应该是

......

int loop_size = sizeof(flavors)/sizeof(flavors[0]);

for (int i = 0; i < loop_size; ++i)

{

   if (flavors[i] == cookieOrdered)
   {
       valid_option = true;
       break;
  }

}

As far as comparison is concerned, you'd be better off comparing all upper or all lower (rather than each word's first letter upper), it's simpler to set things that way.

Moreover you should get into the habit of putting braces around code

Why do you have a magic number of 6 in your loop? Especially when you only have five (5) elements.

Perhaps the loop should be

...

int loop_size = sizeof(flavors)/sizeof(flavors[0]);

for (int i = 0; i < loop_size; ++i)

{

   if (flavors[i] == cookieOrdered)
   {
       valid_option = true;
       break;
  }

}
嗳卜坏 2024-08-20 21:02:49

提示:C++ 中查找不区分大小写的字符串比较

Hint: lookup Case insensitive string comparison in C++

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文