C++ Primer ex 7.31



 DEVELOP > c-Plus-Plus > C++ Primer ex 7.31

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "arnuld"
Date: 24 Aug 2007 04:09:47 AM
Object: C++ Primer ex 7.31
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
.

User: "Jerry Coffin"

Title: Re: C++ Primer ex 7.31 25 Aug 2007 05:22:52 PM
In article <pan.2007.08.24.09.09.44.797971@gmail.com>,
geek.arnuld@gmail.com says...
[ ... ]

* 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

Probably not. In a normal I/O statement like this:
cin >> whatever;
or:
cout << whatever;
the stream object is on the left of the operator. When implemented as
member functions, those would translate as:
cin.operator>>(whatever);
and:
cout.operator<<(whatever);
The problem with that is probably fairly obvious: these have to be
members of the _stream_ object, not the object being read from/written
to the stream.
The alternative (and by far more common) is to implement those operators
as free functions (i.e. not a member of any class) instead. Since you're
required to implement these operations as members of the class whose
objects will be read/written, you probably do NOT want to write them as
overloaded operators. When I'm doing this, I usually name them 'read'
and 'write'.
[ ... ]

inline void Sales_item::<<( const Sales_item& rsio )
{
/* i cannot think of anything :( */
}

Since you're implementing this as a member function, you don't need to
tell it what Sales_item to read/write -- the member function will always
operate on the object of which it's a member. OTOH, you do (normally)
want to tell it what stream object to read from or write to. Most I/O
oprations want to read the stream object to make it easy to keep track
of the stream's state and such. That would give a signatures like:
std::ostream &Sales_item::write(std::ostream &os) {
}
std::istream &Sales_item::read(std::istream &is) {
}
As far as what they really do, you need to write out to the stream
whatever data will be needed to read it back in later and re-constitute
a Sales_item equivalent to the one you wrote out. That will frequently
include all the non-pointer data items in the object. If the obect
contains any pointers, you'll have to figure out what to do -- if the
object "owns" what's pointed at, you'll generally write out the pointed-
to object along with the original object's data. OTOH, if there might be
more than one pointer to a particular piece of data, you might do
something like writin that other object out separately, and the file
offset of that object every where there was a pointer to it in memory.
--
Later,
Jerry.
The universe is a figment of its own imagination.
.

User: "anon"

Title: Re: C++ Primer ex 7.31 24 Aug 2007 05:44:31 AM
arnuld wrote:

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]
Would this work:
http://www.codersource.net/cpp_stream_operators.html
?
.
User: "arnuld"

Title: Re: C++ Primer ex 7.31 24 Aug 2007 08:16:40 AM

On Fri, 24 Aug 2007 12:44:31 +0200, anon wrote:
Would this work:
http://www.codersource.net/cpp_stream_operators.html
?

anon, thanks and why will i use "iostream.h" and "string.h" is this C++
standard ?
--
http://arnuld.blogspot.com
.
User: "anon"

Title: Re: C++ Primer ex 7.31 24 Aug 2007 09:04:39 AM
arnuld wrote:

On Fri, 24 Aug 2007 12:44:31 +0200, anon wrote:


Would this work:
http://www.codersource.net/cpp_stream_operators.html
?


anon, thanks and why will i use "iostream.h" and "string.h" is this C++
standard ?

You should not. Also use std::string instead of char[100]
This link explains nicely how to write operators << and >> if you want
to serialize/deserialize a class.
I just clicked "I am feeling lucky" on
http://www.google.de/search?hl=en&q=c%2B%2B+overload+stream+operator&btnG=Google+Search&meta=
;)
.

User: "Philip Potter"

Title: Re: C++ Primer ex 7.31 24 Aug 2007 10:23:35 AM
arnuld wrote:

On Fri, 24 Aug 2007 12:44:31 +0200, anon wrote:


Would this work:
http://www.codersource.net/cpp_stream_operators.html
?


anon, thanks and why will i use "iostream.h" and "string.h" is this C++
standard ?

You shouldn't. Older (pre-standard) C++ programs may use them; but new
code should use <iostream> and <cstring> instead.
<iostream.h> was the old name for the iostream C++ header. It was
created before C++ had such luxuries as namespaces, so cin, cout, cerr,
istream, ostream etc all got dumped in the global namespace. The C++
standard changed the header's name to <iostream> and at the same time
changed its behaviour to put everything in the std namespace.
<string.h> is a C (not C++) header. It provides the strcpy() function in
the example above. However, it also puts all its declarations in the
global namespace. The C++ standard created namespace-aware versions of
standard C headers, and renamed them to reflect this change. <string.h>
became <cstring>, <stdio.h> became <cstdio> and so on. (The extra 'c'
was added partly to avoid a clash between <cstring> and <string>, the
C++ string header.)
Bottom line, don't use those old headers in new code.
Phil
--
Philip Potter pgp <at> doc.ic.ac.uk
.


User: "=?ISO-8859-1?Q?Erik_Wikstr=F6m?="

Title: Re: C++ Primer ex 7.31 24 Aug 2007 05:59:02 AM
On 2007-08-24 12:44, anon wrote:

arnuld wrote:

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]


Would this work:
http://www.codersource.net/cpp_stream_operators.html

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
.
User: "arnuld"

Title: Re: C++ Primer ex 7.31 24 Aug 2007 08:20:11 AM

On Fri, 24 Aug 2007 10:59:02 +0000, Erik Wikström wrote:
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;
}
};

one thing i did not get is: why you used "operator>>" rather than ">>" ?


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.

you mean no OOP, wrting iostream operators using procedural method ?
--
http://arnuld.blogspot.com
.
User: "=?ISO-8859-1?Q?Erik_Wikstr=F6m?="

Title: Re: C++ Primer ex 7.31 24 Aug 2007 09:12:34 AM
On 2007-08-24 15:20, arnuld wrote:

On Fri, 24 Aug 2007 10:59:02 +0000, Erik Wikström wrote:



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;
}
};


one thing i did not get is: why you used "operator>>" rather than ">>" ?

Because that's how you overload the >> operator.

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.


you mean no OOP, wrting iostream operators using procedural method ?

Just because it's not a member-function does not make it less OO, IMO.
--
Erik Wikström
.

User: "anon"

Title: Re: C++ Primer ex 7.31 24 Aug 2007 08:58:46 AM
arnuld wrote:

On Fri, 24 Aug 2007 10:59:02 +0000, Erik Wikström wrote:
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;
}
};


one thing i did not get is: why you used "operator>>" rather than ">>" ?

Because it is an operator.
.


User: "werasm"

Title: Re: C++ Primer ex 7.31 24 Aug 2007 11:13:34 AM
Erik Wikstr=F6m wrote:

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.

You could always write public functions and
implement the operators in terms of these:
#include <iostream>
struct Foo
{
std::ostream& writeTo(std::ostream& os) const
{
return os << i;
}
std::istream& readFrom(std::istream& is)
{
return is >> i;
}
private:
int i;
};
std::ostream& operator << ( std::ostream& os, const Foo& foo )
{
return foo.writeTo( os );
}
std::istream& operator >> ( std::istream& is, Foo& foo )
{
return foo.readFrom( is );
}
int main()
{
Foo f;
std::cin >> f;
std::cout << f;
}
Alternatively, use friends, but that is not what the exercise
requests.
Werner
.

User: "anon"

Title: Re: C++ Primer ex 7.31 24 Aug 2007 07:47:27 AM
Erik Wikström wrote:

On 2007-08-24 12:44, anon wrote:

arnuld wrote:


/* 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]


Would this work:
http://www.codersource.net/cpp_stream_operators.html


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.

Sorry I missed that :(
Why do you think it can't end up into something 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;
}

I never saw something like this. Should not this be:
std::cin >> f;
std::cout << f;
?
.
User: "=?ISO-8859-1?Q?Erik_Wikstr=F6m?="

Title: Re: C++ Primer ex 7.31 24 Aug 2007 09:11:07 AM
On 2007-08-24 14:47, anon wrote:

Erik Wikström wrote:

On 2007-08-24 12:44, anon wrote:

arnuld wrote:


/* 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]


Would this work:
http://www.codersource.net/cpp_stream_operators.html


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.


Sorry I missed that :(
Why do you think it can't end up into something 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;
}


I never saw something like this. Should not this be:
std::cin >> f;
std::cout << f;
?

Yes, it should, but you can't get it with member functions. If, however,
the intention was to use functions, as stated else-thread, things starts
to make sense.
--
Erik Wikström
.




User: "LR"

Title: Re: C++ Primer ex 7.31 24 Aug 2007 06:02:25 AM
arnuld wrote:

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.

Although I don't think you copied the text of the exercise verbatim, I
agree that it's confusing.

*
* SOLUTION IDEA
* author wants us to overload the default input and output operators
*
*/

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
.
User: "tom"

Title: Re: C++ Primer ex 7.31 24 Aug 2007 06:55:13 AM
On Aug 24, 7:02 pm, LR <lr...@superlink.net> wrote:

arnuld wrote:

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.


Although I don't think you copied the text of the exercise verbatim, I
agree that it's confusing.

*
* SOLUTION IDEA
* author wants us to overload the default input and output operators
*
*/


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

The question in the book stated as below: (I copied from the book)
Write your own version of the sales_item class, adding two new public
members to read and write sales_item objects. There functions should
operate similarly to the input and output operations used in chapter
1. Transactions should look like the ones defined in that chapter as
well. Use this class to read and write a set of transactions.
The chapter is talking about functions, and I think the author wants
to get you familiar with how to write functions.
.
User: "arnuld"

Title: Re: C++ Primer ex 7.31 24 Aug 2007 08:14:44 AM

On Fri, 24 Aug 2007 04:55:13 -0700, tom wrote:
The question in the book stated as below: (I copied from the book)
Write your own version of the sales_item class, adding two new public
members to read and write sales_item objects. There functions should
operate similarly to the input and output operations used in chapter
1. Transactions should look like the ones defined in that chapter as
well. Use this class to read and write a set of transactions.


The chapter is talking about functions, and I think the author wants
to get you familiar with how to write functions.

1st, read again, author wants us to write member functions (not
functions).
2nd, if i could have used that exact statement then i will get
bombarded with 1 question "What is Sales_item class?". i needed to put
some code here and that is what exactly i did.
--
http://arnuld.blogspot.com
.
User: "=?ISO-8859-1?Q?Erik_Wikstr=F6m?="

Title: Re: C++ Primer ex 7.31 24 Aug 2007 09:15:29 AM
On 2007-08-24 15:14, arnuld wrote:

On Fri, 24 Aug 2007 04:55:13 -0700, tom wrote:


The question in the book stated as below: (I copied from the book)


Write your own version of the sales_item class, adding two new public
members to read and write sales_item objects. There functions should
operate similarly to the input and output operations used in chapter
1. Transactions should look like the ones defined in that chapter as
well. Use this class to read and write a set of transactions.


The chapter is talking about functions, and I think the author wants
to get you familiar with how to write functions.


1st, read again, author wants us to write member functions (not
functions).

Member functions are functions, hence the name.
--
Erik Wikström
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER