14.2. Input and Output Operators

#include <memory>
#include <iostream>

using namespace std;

class String {
	friend ostream &operator<<(ostream &, const String &);
public:
	String() : String("") { }
	String(const char *);
	String(const String &);
	String &operator=(const String &);
	~String();

	const char *c_str() const { return elements; }
	size_t size() const { return end - elements; }
	size_t length() const { return end - elements; }

private:
	pair<char *, char *> alloc_n_copy(const char *, const char *);
	void range_initializer(const char *, const char *);
	void free();

	char *elements;
	char *end;
	allocator<char> alloc;
};

ostream & operator<<(ostream &, const String &);

#include <iostream>
#include <algorithm>
#include <utility>
#include "ChenHC.h"

using namespace std;

pair<char *, char *> String::alloc_n_copy(const char *b, const char *e)
{
	auto str = alloc.allocate(e - b);
	return { str, uninitialized_copy(b, e, str) };
}

void String::range_initializer(const char *first, const char *last)
{
	auto newstr = alloc_n_copy(first, last);
	elements = newstr.first;
	end = newstr.second;
}

String::String(const char *s)
{
	char *s1 = const_cast<char *>(s);
	while (*s1) ++s1;
	range_initializer(s, ++s1);
}

String::String(const String &rhs)
{
	range_initializer(rhs.elements, rhs.end);
	cout << "copy constructor" << endl;
}

void String::free()
{
	if (elements) {
		for_each(elements, end, [this](char &c) { alloc.destroy(&c); });
		alloc.deallocate(elements, end - elements);
	}
}

String::~String()
{
	free();
}

String &String::operator=(const String &rhs)
{
	auto newstr = alloc_n_copy(rhs.elements, rhs.end);
	free();
	elements = newstr.first;
	end = newstr.second;
	cout << "copy assignment" << endl;
	return *this;
}

ostream &operator<<(ostream &os, const String &s)
{
	char *c = const_cast<char *>(s.c_str());
	while (*c) os << *c++;
	return os;
}

int main() {
	String str("Hello World!");
	String s(str);
	s = str;
	cout << s << endl;
	cout << str << endl;
	return 0;
}

//Exercise 14.10: Describe the behavior of the Sales_data input operator if
//given the following input :
(a)0 - 201 - 99999 - 9 10 24.95
(b)10 24.95 0 - 210 - 99999 - 9
//Exercise 14.11 : What, if anything, is wrong with the following Sales_data
//input operator ? What would happen if we gave this operator the data in the
//previous exercise ?
//no input check. nothing happend.

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

};

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

int main() {
	Sales_data s;
	cin >> s;
	cout << s << endl;
	//if cin 10 24.95 0-210-99999-9, cout would be 10 24 22.8 0.95
	int a; double b;
	cin >> a >> b;
	cout << a << b << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值