C++ Primer ex 7.31

A

arnuld

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

anon

arnuld said:
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
?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

arnuld said:
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::eek:stream& operator<<(std::eek:stream& 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.
 
L

LR

arnuld said:
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::eek:stream& to
write and a std::istream& to read.

LR
 
T

tom

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


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::eek:stream& 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.
 
A

anon

Erik said:
arnuld said:
/* 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::eek:stream& operator<<(std::eek:stream& 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;
?
 
A

arnuld

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.
 
A

arnuld

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::eek:stream& operator<<(std::eek:stream& 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 ?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Erik said:
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::eek:stream& operator<<(std::eek:stream& 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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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

Because that's how you overload the >> operator.
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.
 
P

Philip Potter

arnuld said:
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
 
W

werasm

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.

You could always write public functions and
implement the operators in terms of these:

#include <iostream>

struct Foo
{
std::eek:stream& writeTo(std::eek:stream& os) const
{
return os << i;
}

std::istream& readFrom(std::istream& is)
{
return is >> i;
}
private:
int i;
};

std::eek:stream& operator << ( std::eek:stream& 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
 
J

Jerry Coffin

[ ... ]
* 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::eek:stream &Sales_item::write(std::eek:stream &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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Question about copy constructor 1
member functions and implicit "this" 3
C++ Primer ex 5.18 5
C++ Primer section 1.6 example 1
constructor problem 9
C++ Primer ex 6.20 36
C++ Primer ex 7.12 2
C++ Primer ex 9.27 4

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top