C ++ Primer ex 7.31 [英] C++ Primer ex 7.31
问题描述
这是我见过的最不清楚和最混乱的运动。我不是
能够编写甚至单行解决方案。她的eis我有什么
试过:
/ * C ++ Primer - 4 / e
*
*章7,练习7.31
*声明
*给出Sales_item课程。添加2个新的公共成员添加阅读和
写Sales_item对象。这些公共成员应该像标准库的输入和输出操作符那样进行
交易。
*
*解决方案IDEA
*作者希望我们重载默认的输入和输出操作符
*
* /
#include< iostream>
#include< string>
/ *默认Sales_item类。
Sales_item的目的是存储ISBN并跟踪销售的数量
,该书的收入和平均销售价格。 class
Sales_item {
public:
//对Sales_item对象的操作
double avg_price()const;
bool same_isbn(const Sales_item& rsi)const {
return isbn == rsi.isbn;
}
私人:
std :: string isbn;
unsigned units_sold;
双倍收入;
};
//在课堂外定义的成员函数
double Sales_item :: avg_price()const
{
如果(units_sold)
{
返回收入/ units_sold;
}
其他
{
返回0;
}
}
* /
>
/ *我自己的Slaes_item类* /
类Sales_item {
公开:
/ *对Sales_item对象的操作* /
double avg_price()const;
bool same_isbn(const Sales_item& rsio)const {
return isbn == rsio.isbn;
}
/ * new outpu t operator * /
void<<(const Sales_item& rsio)const;
/ *新输入运算符* /
void>>(const Sales_item& rsio)const;
私人:
std :: string isbn;
unsigned units_sold;
双倍收入;
};
inline double Sales_item :: avg_price()const
{
if(units_sold)
{
返回收入/ units_sold;
}
其他
{
返回0;
}
}
inline void Sales_item ::<<(const Sales_item& rsio)
{
/ *我想不出任何东西:( * /
}
int main()
{
Sales_item sales_obj;
返回0;
}
-
http://arnuld.blogspot.com
this is the most unclear and messy exercise i have ever seen. i am not
able to to write up even single line of solution. her eis what i have
tried:
/* C++ Primer - 4/e
*
* Chapter 7, exercise 7.31
* STATEMENT
* Sales_item class is given. add 2 new public members add to read and
write Sales_item objects. these public members should do the
transactions like the input and output operators of standard library.
*
* SOLUTION IDEA
* author wants us to overload the default input and output operators
*
*/
#include <iostream>
#include <string>
/* Default Sales_item class.
the purpose of Sales_item is to store an ISBN and keep track of the number
of copies sold, revenue and average sales price for that book. class
Sales_item {
public:
// operations on Sales_item objects
double avg_price() const;
bool same_isbn( const Sales_item& rsi ) const {
return isbn == rsi.isbn;
}
private:
std::string isbn;
unsigned units_sold;
double revenue;
};
// member funtion defined outside of class
double Sales_item::avg_price() const
{
if( units_sold )
{
return revenue/units_sold;
}
else
{
return 0;
}
}
*/
/* my own Slaes_item Class */
class Sales_item {
public:
/* operations on Sales_item objects */
double avg_price() const;
bool same_isbn( const Sales_item& rsio ) const {
return isbn == rsio.isbn;
}
/* new output operator */
void <<( const Sales_item& rsio ) const;
/* new input operator */
void >>( const Sales_item& rsio ) const;
private:
std::string isbn;
unsigned units_sold;
double revenue;
};
inline double Sales_item::avg_price() const
{
if( units_sold )
{
return revenue/units_sold;
}
else
{
return 0;
}
}
inline void Sales_item::<<( const Sales_item& rsio )
{
/* i cannot think of anything :( */
}
int main()
{
Sales_item sales_obj;
return 0;
}
--
http://arnuld.blogspot.com
推荐答案
arnuld写道:
arnuld wrote:
这是最多的我见过的学习和凌乱的运动。我不是
能够编写甚至单行解决方案。她的eis我有什么
试过:
/ * C ++ Primer - 4 / e
*
*第7章,练习7.31
*声明
*给出了Sales_item课程。添加2个新的公共成员添加阅读和
写Sales_item对象。这些公共成员应该像标准库的输入和输出操作符那样进行
交易。
*
*解决方案IDEA
*作者希望我们重载默认的输入和输出操作符
*
* /
this is the most unclear and messy exercise i have ever seen. i am not
able to to write up even single line of solution. her eis what i have
tried:
/* C++ Primer - 4/e
*
* Chapter 7, exercise 7.31
* STATEMENT
* Sales_item class is given. add 2 new public members add to read and
write Sales_item objects. these public members should do the
transactions like the input and output operators of standard library.
*
* SOLUTION IDEA
* author wants us to overload the default input and output operators
*
*/
[snip]
这是否有效:
http ://www.codersource.net/cpp_stream_operators.html
?
[snip]
Would this work:
http://www.codersource.net/cpp_stream_operators.html
?
2007-08-24 12:44, anon写道:
On 2007-08-24 12:44, anon wrote:
arnuld写道:
arnuld wrote:
>这是我最不清楚和凌乱的练习曾见过。我甚至不能写出单行解决方案。她已经尝试了以下内容:
/ * C ++ Primer - 4 / e
*
*第7章,练习7.31
*声明
*给出了Sales_item类。添加2个新的公共成员添加以阅读和编写Sales_item对象。这些公共成员应该像标准库的输入和输出操作符那样进行交易。
*
*解决方案IDEA
*作者希望我们重载默认的输入和输出操作符
*
* /
>this is the most unclear and messy exercise i have ever seen. i am not
able to to write up even single line of solution. her eis what i have
tried:
/* C++ Primer - 4/e
*
* Chapter 7, exercise 7.31
* STATEMENT
* Sales_item class is given. add 2 new public members add to read and
write Sales_item objects. these public members should do the
transactions like the input and output operators of standard library.
*
* SOLUTION IDEA
* author wants us to overload the default input and output operators
*
*/
[snip]
这会有效:
< a rel =nofollowhref =http://www.codersource.net/cpp_stream_operators.htmltarget =_ blank> http://www.codersource.net/cpp_stream_operators.html
问题是,任务是使用添加公共成员,坦率地说,我不知道这应该是什么,最终应该是有用的。
使用会员你可以这样做:
class Foo
{
int i;
public:
std :: ostream& operator<<(std :: ostream& s)
{
return s<<我;
}
std :: istream&运算符>>(std :: istream& s)
{
返回s> i;
}
};
int main()
{
Foo f;
f> std :: cin;
f<< std :: cout;
}
但是它不像标准库的输入和输出运算符那么。
我会和非会员朋友一起做练习,因为那个
会有一些有用的经验。
-
Erik Wikstr?m
The problem is that the task is to use add public members, and frankly I
can''t see how that is supposed to end up in anything useful.
Using members you could do something like this:
class Foo
{
int i;
public:
std::ostream& operator<<(std::ostream& s)
{
return s << i;
}
std::istream& operator>>(std::istream& s)
{
return s >i;
}
};
int main()
{
Foo f;
f >std::cin;
f << std::cout;
}
But then it''s not "like the input and output operators of standard library".
I would do the exercise with non-member friends instead, since that
would be some useful experience.
--
Erik Wikstr?m
arnuld写道:
arnuld wrote:
这是最多的我见过的不清楚和凌乱的运动。我不是
能够编写甚至单行解决方案。她的eis我有什么
试过:
/ * C ++ Primer - 4 / e
*
*第7章,练习7.31
*声明
*给出了Sales_item课程。添加2个新的公共成员添加阅读和
写Sales_item对象。这些公共成员应该像标准库的输入和输出操作符那样进行
事务。
this is the most unclear and messy exercise i have ever seen. i am not
able to to write up even single line of solution. her eis what i have
tried:
/* C++ Primer - 4/e
*
* Chapter 7, exercise 7.31
* STATEMENT
* Sales_item class is given. add 2 new public members add to read and
write Sales_item objects. these public members should do the
transactions like the input and output operators of standard library.
虽然我不认为你逐字复制了练习文本,但是我认为这是令人困惑的。 br />
Although I don''t think you copied the text of the exercise verbatim, I
agree that it''s confusing.
*
*解决方案IDEA
*作者希望我们重载默认输入和输出运营商
*
* /
*
* SOLUTION IDEA
* author wants us to overload the default input and output operators
*
*/
我怀疑情况并非如此。它看起来像重载的运算符
直到后一章才被覆盖。
也许公共函数看起来像:
>
void Sales_item :: read(){/ * * * /}
和
void Sales_item :: write()const {/ * .... * /}
它可能是你传递std :: ostream& to
写和std :: istream&阅读。
LR
I suspect this is not the case. It looks like overloaded operators
aren''t covered until a later chapter.
Perhaps the public functions should look like:
void Sales_item::read() { /*....*/ }
and
void Sales_item::write() const { /*....*/ }
It''s possible that it was intended that you pass a std::ostream& to
write and a std::istream& to read.
LR
这篇关于C ++ Primer ex 7.31的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!