Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Do you think this friend operator '>>" needs defined?

21 views
Skip to first unread message

fl

unread,
Jun 8, 2015, 3:20:19 PM6/8/15
to
Hi,

I am learning C++ with an example, see below please.
I find the friend function about >> redefinition in the class Item_base.
I think that it is similar to a friend function, is it right?
But I cannot find the operator '>>' function definition in the project, which
can be built and run.
I am worry about the std >> operator cannot understand the second parameter

Item_base&

Sales_item item5(Item_base("def", 35));


double total = sale.total();
cout << "Total Sale: " << total << endl;


I am still new to C++. Whether operator >> has been derived to the Basket
class?

Could you help me?

Thanks,



////////////
class Sales_item {
friend bool operator<(const Sales_item &lhs, const Sales_item &rhs);
friend class Basket;
public:
// default constructor: unbound handle
Sales_item(): h() { }

// copy item and attach handle to the copy
Sales_item(const Item_base &item): h(item.clone()) { }

// no copy control members: synthesized versions work

// member access operators: forward their work to the Handle class
const Item_base& operator*() const { return *h; }
const Item_base* operator->() const
{ return h.operator->(); }
private:
Handle<Item_base> h; // use-counted handle
};

// holds items being purchased
class Basket {
std::multiset<Sales_item> items;
public:
// useful typedefs modeled after corresponding container types
typedef std::multiset<Sales_item>::size_type size_type;
typedef std::multiset<Sales_item>::const_iterator const_iter;

void display(std::ostream&) const;

void add_item(const Sales_item &item)
{ items.insert(item); }
size_type item_count(const Sales_item &i) const
{ return items.count(i); }
double total() const; // sum of net prices for all items in the basket
};


*******************
class Item_base {
friend std::istream& operator>>(std::istream&, Item_base&);
friend std::ostream& operator<<(std::ostream&, const Item_base&);
public:
virtual Item_base* clone() const
{ return new Item_base(*this); }
public:
Item_base(const std::string &book = "",
double sales_price = 0.0):
isbn(book), price(sales_price) { }

std::string book() const { return isbn; }

// returns total sales price for a specified number of items
// derived classes will override and apply different discount algorithms
virtual double net_price(std::size_t n) const
{ return n * price; }

// no work, but virtual destructor needed
// if base pointer that points to a derived object is ever deleted
virtual ~Item_base() { }
private:
std::string isbn; // identifier for the item
protected:
double price; // normal, undiscounted price

};
Message has been deleted

Paavo Helde

unread,
Jun 8, 2015, 4:11:01 PM6/8/15
to
fl <rxj...@gmail.com> wrote in
news:3dbc21a7-9b9a-4c3b...@googlegroups.com:

> Hi,
>
> I am learning C++ with an example, see below please.
> I find the friend function about >> redefinition in the class
> Item_base. I think that it is similar to a friend function, is it
> right?

Sorry, cannot parse that. Do you wanted to ask if user-defined operator>>
is similar to a function? Yes, it is.

And nothing is redefined here. The type Item_base is specific to your
program, if there appears a function which takes an argument of that
type, then it is a new function and specific to your program, not a
redefinition. If it has common name with some other functions
("operator>>" here), then it is called an overload. It still is a
distinct function as parameter types are part of the function signature
in C++.

> But I cannot find the operator '>>' function definition in the
> project, which can be built and run.

Then probably the program does not use this function. If a function is
never used then its definition is allowed to be absent.

> I am worry about the std >> operator cannot understand the second
> parameter

There is no "std >> operator". There are several operator>> overloads,
some declared by the standared headers, one declared in your program. Not
sure which one you are worried about and why.

>
> Item_base&
>
> Sales_item item5(Item_base("def", 35));
>
>
> double total = sale.total();
> cout << "Total Sale: " << total << endl;
>
>
> I am still new to C++. Whether operator >> has been derived to the
> Basket class?

One says "derived from", not "derived to". And one derives classes, not
operators. And no, Basket is not derived from anything and does not even
contain an operator>> declaration.

The line

cout << "Total Sale: " << total << endl

uses 3 different operator<< overloads, all defined in standard headers.
As there is no object of type Item_base appearing after any <<, your
operator<< overload is not used.

hth
Paavo
0 new messages