Code
my_sales_data类
在my_sales_data.h中:
#ifndef MY_SALES_DATA_H
#define MY_SALES_DATA_H
#include<iostream>
#include<string>
class my_sales_data
{
public:
//构造函数(声明)
//my_sales_data() : units_sold(0), revenue(0.0) {}; //使用类内初始值
my_sales_data(std::string s) : bookNo(s) {};
my_sales_data(std::string s, unsigned n, double r) : bookNo(s), units_sold(n), revenue(r) { std::cout << 'a' << std::endl; };
my_sales_data() : my_sales_data("1001", 0, 0) { std::cout << 'b' << std::endl; }; //使用委托构造函数
my_sales_data(std::istream&);
//成员函数(声明)
my_sales_data& combine(const my_sales_data&);
inline double avg_price() const;
//成员函数(声明)
std::string isbn() const { return bookNo; }
private:
//成员变量
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
//友元函数
friend my_sales_data add(const my_sales_data&, const my_sales_data&);
friend std::ostream& print(std::ostream&, const my_sales_data&);
friend std::istream& read(std::istream&, my_sales_data&);
};
//非成员接口函数(声明)
my_sales_data add(const my_sales_data&, const my_sales_data&);
std::ostream& print(std::ostream&, const my_sales_data&);
std::istream& read(std::istream&, my_sales_data&);
#endif
在my_sales_data.cpp中:
#include"my_sales_data.h"
//===== 构造函数 =====
my_sales_data::my_sales_data(std::istream& is)
{
read(is, *this);
}
//===== 成员函数 =====
inline double my_sales_data::avg_price() const
{
if (units_sold)
return revenue / units_sold;
else
return 0;
}
my_sales_data& my_sales_data::combine(const my_sales_data& rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
//===== 非成员接口函数 ======
std::istream& read(std::istream& is, my_sales_data& item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = item.units_sold * price;
return is;
}
std::ostream& print(std::ostream& os, const my_sales_data & item)
{
os << item.bookNo << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
return os;
}
my_sales_data add(const my_sales_data& lhs, const my_sales_data& rhs)
{
my_sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
Homework
person 类
在person.h中:
#pragma once
#include<iostream>
#include<string>
struct person
{
friend std::istream& read(std::istream& is, person& p);
friend std::ostream& print(std::ostream& os, const person& p);
public:
person() = default;
person(std::string& n, std::string& a) : name(n), address(a){};
explicit person(std::istream&);
auto get_name() const -> std::string const& { return name; }
auto get_address() const -> std::string const& { return address; }
private:
std::string name;
std::string address;
};
std::istream& read(std::istream& is, person& p);
std::ostream& print(std::ostream& os, const person& p);
在person.cpp中:
#include"person.h"
//===== 构造函数 =====
person::person(std::istream& is)
{
read(is, *this);
}
//===== 非成员接口函数 =====
std::istream& read(std::istream& is, person& p)
{
is >> p.name >> p.address;
return is;
}
std::ostream& print(std::ostream& os, const person& p)
{
os << p.get_name() << " " << p.get_address();
return os;
}
screen类
在window_mgr.h中:
#pragma once
#include<vector>
#include<string>
class screen;
class window_mgr
{
public:
typedef std::vector<screen>::size_type screenIndex;
void clear(screenIndex);
private:
std::vector<screen> screens;
};
在screen.h中:
#pragma once
#include<iostream>
#include<string>
#include"window_mgr.h"
class screen
{
friend void window_mgr::clear(screenIndex);
public:
typedef std::string::size_type pos;
screen() = default;
screen(pos ht, pos wd) :height(ht), width(wd), contents(ht* wd, ' ') {};
screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht * wd, c){};
char get() const { return contents[cursor]; }
inline 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:
pos cursor = 0;
pos height = 0, width = 0;
std::string contents;
void do_display(std::ostream& os) const { os << contents; }
};
在screen.cpp中:
#include"screen.h"
inline char screen::get(pos r, pos c) const
{
pos row = r * width;
return contents[row + c];
}
screen& screen::move(pos r, pos c)
{
pos row = r * width;
cursor = row + c;
return *this;
}
screen& screen::set(char c)
{
contents[cursor] = c; //设置光标位置的字符
return *this;
}
screen& screen::set(pos r, pos c, char a)
{
contents[r * width + c] = a; //设置r行c列的字符
return *this;
}
void window_mgr::clear(screenIndex i)
{
screen& s = screens[i];
s.contents = std::string(s.height * s.width, ' ');
}