14.1.Basic Concepts

//14.1. Basic Concepts
//Exercises Section 14.1


//Exercise 14.1: In what ways does an overloaded operator differ from a
//built - in operator ? In what ways are overloaded operators the same as the
//built - in operators ?
//We can call an overloaded operator function directly.
//An overloaded operator function must either be a member of a class or have at least one parameter of class type.
//A few operators guarantee the order in which operands are evaluated.These overloaded versions of these operators do not preserve order of evaluation and/or short - circuit evaluation, it is usually a bad idea to overload them.
//An overloaded operator has the same precedence and associativity as the corresponding built - in operator.

//Exercise 14.2 : Write declarations for the overloaded input, output, addition,
//and compound - assignment operators for Sales_data.
/*#include <iostream>
#include <string>

using namespace std;

class Sales_data {
	friend istream &operator>>(istream &, Sales_data &);
	friend ostream &operator<<(ostream &, const Sales_data &);
	friend Sales_data operator+(const Sales_data &, const Sales_data &);

public:
	Sales_data(const string &s, unsigned n, double p) :
		bookNo(s), units_sold(n), revenue(p) { }
	Sales_data() : Sales_data("", 0, 0.0) { }
	Sales_data(const string &s) : Sales_data(s, 0, 0.0) { }
	Sales_data(istream &is);

	Sales_data &operator += (const Sales_data &);
	string isbn() const { return bookNo; }

private:
	inline double avg_price() const;

	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;

};

istream &operator>>(istream &, Sales_data &);
ostream &operator<<(ostream &, const Sales_data &);
Sales_data operator+(const Sales_data &, const Sales_data &);

inline double Sales_data::avg_price() const
{
	return units_sold ? revenue / units_sold : 0;
}

#include "ChenHC.h"

using namespace std;

Sales_data::Sales_data(istream &is) : Sales_data() 
{
	is >> *this;
}

Sales_data &Sales_data::operator+=(const Sales_data &rhs)
{
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}

istream &operator>>(istream &is, Sales_data &item)
{
	double price = 0.0;
	is >> item.bookNo >> item.units_sold >> price;
	if (is)
		item.revenue = price * item.units_sold;
	else 
		item = Sales_data();
	return is;
}

ostream &operator<<(ostream &os, const Sales_data &item)
{
	os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
	return os;
}

Sales_data operator+(const Sales_data &lhs, const Sales_data &rhs)
{
	Sales_data sum = lhs;
	sum += rhs;
	return sum;
}*/

//Exercise 14.3 : Both string and vector define an overloaded == that can
//be used to compare objects of those types.Assuming svec1 and svec2 are
//vectors that hold strings, identify which version of == is applied in each
//of the following expressions :
(a) "cobble" == "stone"//neither, it matches (char *, char *) better than (const string, const string) version.
(b)svec1[0] == svec2[0]//string
(c) svec1 == svec2//vector
(d) svec1[0] == "stone" //string

//Exercise 14.4: Explain how to decide whether the following should be class
//members :
	(a) % //symmetric, non-member 
	(b) %=//change the objects, member
	(c) ++//change the objects, member
	(d) ->//=()[]->must be member
	(e) <<//non-member  ?
	(f) &&//symetric, non-member
	(g) ==//sysmetric, non-member
	(h)()//as (d)

//Exercise 14.5: In exercise 7.40 from § 7.5.1 (p. 291) you wrote a sketch of
//one of the following classes.Decide what, if any, overloaded operators your
//class should provide.
	(a)Book
	(b) Date
	(c) Employee
	(d) Vehicle
	(e) Object
	(f) Tree

#include <iostream>
#include <string>

using namespace std;

class Book {
	friend istream& operator>>(istream &, Book &);
	friend ostream& operator<<(ostream &, const Book &);
	friend bool operator==(const Book &, const Book &);
	friend bool operator!=(const Book &, const Book &);

public:
	Book() = default;
	Book(unsigned no, string name, string author, string pubdate) :
		no_(no), name_(name), author_(author), pubdate_(pubdate) { }
	Book(istream &in) { in >> *this; }

private:
	unsigned no_;
	string name_;
	string author_;
	string pubdate_;
};

istream &operator>>(istream &, Book &);
ostream &operator<<(ostream &, const Book &);
bool operator==(const Book &, const Book &);
bool operator!=(const Book &, const Book &);



#include <iostream>
#include <string>
#include "ChenHC.h"

using namespace std;

istream &operator>>(istream &in, Book &book)
{
	in >> book.no_ >> book.name_ >> book.author_ >> book.pubdate_;
	return in;
}

ostream &operator<<(ostream &out, const Book &book)
{
	out << book.no_ << " " << book.name_ << " " << book.author_ << " " << book.pubdate_;
	return out;
}

bool operator==(const Book &lhs, const Book &rhs)
{
	return lhs.no_ == rhs.no_;
}

bool operator!=(const Book &lhs, const Book &rhs)
{
	return !(lhs.no_ == rhs.no_);
}

int main() {
	Book book1(123, "aaa", "bbb", "2016");
	Book book2(123, "aaa", "bbb", "2016");
	
	if (book1 == book2) cout << book1 << endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值