C++primer 第七章

本章主要一步步構造類。

7.2

#pragma once
#include <iostream>
#include <string>
using namespace std;

struct Sales_data {
	std::string isbn() const { return bookNo; }
	Sales_data& combine(const Sales_data&);

	std::string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
};

Sales_data& Sales_data::combine(const Sales_data &rhs)
{
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;

	return *this;
}

7.4&&7.5

#include <iostream>
#include <string>

struct Person {
	std::string get_name() const { return name; }
	std::string get_adress()const { return adress; }

	std::string name;
	std::string adress;
};

7.6

#pragma once
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <iostream>
#include <string>
using namespace std;

struct Sales_data {
	std::string isbn() const { return bookNo; }
	Sales_data& combine(const Sales_data&);
	double avg_price()const;

	std::string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
};
std::istream& read(istream&, Sales_data&);
std::ostream& print(ostream&, const Sales_data&);
Sales_data add(const Sales_data&, const Sales_data&);

Sales_data& Sales_data::combine(const Sales_data &rhs)
{
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;

	return *this;
}

double Sales_data::avg_price() const
{
	if (units_sold) {
		return revenue / units_sold;
	}
	else {
		return 0;
	}
}

istream& read(istream& is, Sales_data& item)
{
	double price = 0;
	is >> item.bookNo >> item.units_sold >> price;
	item.revenue = item.units_sold*price;

	return is;
}

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

Sales_data add(const Sales_data& lhs, const Sales_data& rhs)
{
	Sales_data sum = lhs;
	sum.combine(rhs);

	return sum;
}

#endif

7.9

#pragma once
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
using namespace std;

struct Person {
	std::string get_name() const { return name; }
	std::string get_adress()const { return adress; }

	std::string name;
	std::string adress;
};
std::istream& read(istream&, Person&);
std::ostream& print(ostream&, const Person&);

istream& read(istream& is, Person& people)
{
	is >> people.name >> people.adress;

	return is;
}

ostream& print(ostream& os, const Person& people)
{
	os << people.get_name() << " " << people.get_adress();

	return os;
}

#endif

7.11

#include <iostream>
#include <string>
#include "Sales_data.h"
using namespace std;

int main()
{
	Sales_data item1;
	Sales_data item2("C++ Primer");
	Sales_data item3("C++ Primer", 10, 128);
	Sales_data item4(cin);
	
	print(cout, item1);
	cout << endl;
	print(cout, item2);
	cout << endl;
	print(cout, item3);
	cout << endl;
	print(cout, item4);
	cout << endl;
}

7.12
	Sales_data(std::istream& is)
	{
		double price = 0;
		is >> bookNo >> units_sold >> price;
		revenue = price*units_sold;
	}

7.15

	Person() = default;
	Person(const string& s) :name(s), adress("Can't get adress") {}
	Person(const string& s1, const string& s2) :name(s1), adress(s2) {}

7.32

#pragma once
#ifndef SCREEN_H
#define SCREEN_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Screen {
	friend class Windows_mgr;
public:
	typedef std::string::size_type pos;
	Screen() = default;
	Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) {}
	
	char get() const { return contents[cursor]; }
	char get(pos, pos) const;
	Screen& move(pos, pos);
	Screen& set(char);
	Screen& set(pos, pos, char);
	Screen& display(std::ostream& os) { do_display(os); return *this; }
	const Screen& display(std::ostream& os) const { do_display(os); return *this; }
private:
	void do_display(std::ostream& os)const { os << contents; }

	pos cursor = 0;
	pos height = 0;
	pos width = 0;
	std::string contents;
};

inline char Screen::get(pos r, pos c) const
{
	pos row = r*width;
	
	return contents[row + c];
}
inline Screen& Screen::move(pos r, pos c)
{
	pos row = r*width;
	cursor = row + c;
	
	return *this;
}
inline Screen& Screen::set(char ch)
{
	contents[cursor] = ch;
	
	return *this;
}
inline Screen& Screen::set(pos r, pos c, char ch)
{
	contents[r*width + c] = ch;

	return *this;
}

class Windows_mgr {
public:
	using ScreenIndex = std::vector<Screen>::size_type;
	void clear(ScreenIndex);
private:
	std::vector<Screen> screens{ Screen(24,80,' ') };
};

void Windows_mgr::clear(ScreenIndex i)
{
	Screen &s = screens[i];
	s.contents = string(s.height*s.width, ' ');
}
#endif


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章