C++ primer 查漏补缺十:Sales_data类的设计

类的基本思想是数据抽象(data abstraction)和封装(encapsulation)。数据抽象是一种依赖于接口(interface)和实现(implementation)分离的编程(以及设计)技术。类的接口包括用户所能执行的操作:类的实现则包括类的数据成员、负责接口实现的函数体以及定义类所需的各种私有函数。封装实现了类的接口和实现的分离。封装后的类隐藏了它的实现细节,也就是说,类的用户只能使用接口而无法访问实现部分。

场景

书店有一个文件记录销售情况
每一条销售记录:ISBN编号 销售数量 平均价格

0-201-70353-X 4 24.99
0-201-82470-1 4 45.39
0-201-88954-4 2 15.00 
0-201-88954-4 5 12.00 
0-201-88954-4 7 12.00 
0-201-88954-4 2 12.00 
0-399-82477-1 2 45.39
0-399-82477-1 3 45.39
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00

每一种书有好几条记录,并且同一种书的记录是放在一起的
需要一个书店程序,统计(合并)每一本书的销售情况
思路:依次读取每一条记录;判断是否是同一条:是(合并记录);否(打印上一本书的合并记录,控制权转移到下一本)

Sales item类的作用是表示一本书的总销售额、售出册数和平均售价。

  • 一个isbn成员函数,用于返回对象的ISBN编号
  • 一个combine成员函数,用于将一个Sales data对象加到另一个对象上
  • 一个名为add的函数,执行两个Sales data对象的加法
  • 一个read函数,将数据从istream读入到Sales data对象中
  • 一个print函数,将Sales data对象的值输出到ostream
#include <iostream>
using std::cerr; using std::cin; using std::cout; using std::endl;

#include "Sales_data.h"

int main()
{
	Sales_data total;         // variable to hold the running sum
	if (read(cin, total))  {  // read the first transaction
		Sales_data trans;     // variable to hold data for the next transaction
		while(read(cin, trans)) {      // read the remaining transactions
			if (total.isbn() == trans.isbn())   // check the isbns
				total.combine(trans);  // update the running total
			else {
				print(cout, total) << endl;  // print the results
				total = trans;               // process the next book
			}
		}
		print(cout, total) << endl;          // print the last transaction
	} else {                                 // there was no input
		cerr << "No data?!" << endl;         // notify the user
	}

	return 0;
}

类的设计

需要三个变量

std::string bookNo; //isbn号
unsigned units_sold = 0;// 售出册数
double revenue = 0.0;//总销售额

先看一下Sales_data类的代码

#ifndef SALES_DATA_H
#define SALES_DATA_H

#include "Version_test.h"

#include <string>
#include <iostream>

class Sales_data {
friend Sales_data add(const Sales_data&, const Sales_data&);
friend std::ostream &print(std::ostream&, const Sales_data&);
friend std::istream &read(std::istream&, Sales_data&);
public:
	// constructors
// using the synthesized version is safe only
// if we can also use in-class initializers
#if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
	Sales_data() = default;
#else
	Sales_data(): units_sold(0), revenue(0.0) { }
#endif
#ifdef IN_CLASS_INITS
	Sales_data(const std::string &s): bookNo(s) { }
#else
	Sales_data(const std::string &s): 
	           bookNo(s), units_sold(0), revenue(0.0) { }
#endif
	Sales_data(const std::string &s, unsigned n, double p):
	           bookNo(s), units_sold(n), revenue(p*n) { }
	Sales_data(std::istream &);

	// operations on Sales_data objects
	std::string isbn() const { return bookNo; }
	Sales_data& combine(const Sales_data&);
	double avg_price() const;
private:
	std::string bookNo;
#ifdef IN_CLASS_INITS   // using the synthesized version is safe only
	unsigned units_sold = 0;
	double revenue = 0.0;
#else
	unsigned units_sold;
	double revenue;
#endif
};


// nonmember Sales_data interface functions
Sales_data add(const Sales_data&, const Sales_data&);
std::ostream &print(std::ostream&, const Sales_data&);
std::istream &read(std::istream&, Sales_data&);

// used in future chapters
inline bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
{
	return lhs.isbn() < rhs.isbn();
}
#endif

 #include <iostream>
using std::istream; using std::ostream;

#include "Sales_data.h"
Sales_data::Sales_data(std::istream &is) 
{
	// read will read a transaction from is into this object
	read(is, *this);
}

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

// add the value of the given Sales_data into this object
Sales_data&  Sales_data::combine(const Sales_data &rhs)
{
	units_sold += rhs.units_sold; // add the members of rhs into 
	revenue += rhs.revenue;       // the members of ``this'' object
	return *this; // return the object on which the function was called
}

Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
	Sales_data sum = lhs;  // copy data members from lhs into sum
	sum.combine(rhs);      // add data members from rhs into sum
	return sum;
}

// transactions contain ISBN, number of copies sold, and sales price
istream& read(istream &is, Sales_data &item)
{
	double price = 0;
	is >> item.bookNo >> item.units_sold >> price;
	item.revenue = price * item.units_sold;
	return is;
}

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

成员函数

这里有三个成员函数,在h文件中

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

尽管所有成员都必须在类的内部声明,但是成员函数体可以定义在类内也可以定义在类外。对于Sales data类来说,isbn函数定义在了类内,而combine和avg price定义在了类外。

this

成员函数外部调用成员函数

Sales_data total; 
total.isbn();

调用成员函数时,实际上是在替某个对象调用它。编译器将total的地址传给 isbn的隐式形参 this

成员函数通过一个 this 的额外的隐式参数来访问调用它的那个对象。
实际的执行过程:

Sales_data::isbn(&total);

成员函数内部调用
无需this指针,可直接调用成员函数,因为this所指就是这个对象

返回this对象的函数

Sales_data&  Sales_data::combine(const Sales_data &rhs)
{
	units_sold += rhs.units_sold; // add the members of rhs into 
	revenue += rhs.revenue;       // the members of ``this'' object
	return *this; // return the object on which the function was called
}
//调用方式
total.combine(trans);

实际上 total的地址绑定到this上, rhs绑定在trans上
units_sold += rhs.units_sold; 等价于 this.units_sold += trans.units_sold;

返回 *this是指,返回调用者的对象。调用者是total

类的作用域和成员函数

类本身就是一个作用域。类的成员函数的定义嵌套在类的作用域之内,因此,isbn中用到的名字bookNo其实就是定义在Sales data内的数据成员。值得注意的是,即使bookNo定义在isbn之后,isbn也还是能够使用bookNo。

编译器分两步处理类:首先编译成员的声明,然后才轮到成员函数体(如果有的话)。因此,成员函数体可以随意使用类中的其他成员而无须在意这些成员出现的次序。

友元函数

类的作者常常需要定义一些辅助函数,比如add、read和print等。尽管这些函数定义的操作从概念上来说属于类的接口的组成部分,但它们实际上并不属于类本身。我们定义非成员函数的方式与定义其他函数一样,通常把函数的声明和定义分离开来(参见6.1.2节,第168页)。如果函数在概念上属于类但是不定义在类中,则它一般应与类声明(而非定义)在同一个头文件内。在这种方式下,用户使用接口的任何部分都只需要引入一个文件。

类可以允许其他类或者函数访问它的非公有成员,方法是令其他类或者函数成为它的友元(friend)。如果类想把一个函数作为它的友元,只需要增加一条以friend关键字开始的函数声明语句即可

友元的声明仅仅指定了访问的权限,而非一个通常意义上的函数声明。如果我们希望类的用户能够调用某个友元函数,那么我们就必须在友元声明之外再专门对函数进行一次声明。为了使友元对类的用户可见,我们通常把友元的声明与类本身放置在同一个头文件中(类的外部)。因此,我们的Sales data头文件应该为read、print和add提供独立的声明(除了类内部的友元声明之外)。

//在类的内部申明友元函数
class Sales_data {
	friend Sales_data add(const Sales_data&, const Sales_data&);
	friend std::ostream &print(std::ostream&, const Sales_data&);
	friend std::istream &read(std::istream&, Sales_data&);
	public:
	...
}

//在类的外部再申明一次
// nonmember Sales_data interface functions
Sales_data add(const Sales_data&, const Sales_data&);
std::ostream &print(std::ostream&, const Sales_data&);
std::istream &read(std::istream&, Sales_data&);

调用方式如下

Sales_data total;         // variable to hold the running sum
if (read(cin, total))  {  // read the first transaction
	Sales_data trans;     // variable to hold data for the next transaction
	while(read(cin, trans)) {      // read the remaining transactions
		。。。
	}
	print(cout, total) << endl;          // print the last transaction
} 

为什么需要友元函数
Sales_data add(const Sales_data&, const Sales_data&);函数举例,把它改成成员函数

这个函数的作用就是,合并两个Sales_data,返回一个新的Sales_data。
就有两种设计思路:如下

void Sales_data::add(const Sales_data &lhs, const Sales_data &rhs)
{
	*this = lhs;   
	*this.combine(rhs);      
}
//调用
Sales_data sum;
sum.add(lhs, rhs); //lhs, rhs之前申明

使用方法就是:首先声明 Sales_data 的变量用于存放 和,然后输入被求和的两个变量

第二种思路

Sales_data& Sales_data::add(const Sales_data &rhs)
{
	Sales_data sum = *this;  // copy data members from lhs into sum
	sum.combine(rhs);      // add data members from rhs into sum
	return sum;     
}
//调用
Sales_data sum = lhs.add(rhs); //lhs, rhs之前申明

是不是怎么调用都不顺手

按照我们的习惯,这个函数的调用方式应该是

sum = add (lhs , rhs);
sum = add (rhs , lhs);

这三个函数中,只有友元函数能做到这种调用方式

友元函数
【C++】友元函数和友元类(作用及优缺点)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值