//习题12.8
//将Sales_item::avg_price定义为内联函数
//有三种方式
//方式一:将函数的定义写在类定义体内部。
class Sales_item
{
public:
double Sales_item::avg_price() const
{
if(unite_sold)
return revenue/unite_sold;
else
return 0;
}
//....其他成员函数省略
private:
std::string isbn;
unsighed unite_sold;
double revenue;
};
//方式二:在类定义体内部的函数声明上用inline显式指定
class Sales_item
{
public:
inline double Sales_item::avg_price() const;
//...其他成员函数
private:
std::string isbn;
unsigned units_sold;
double revenue;
};
//....avg_price的定义略
//方式三:在类定义体外部的函数定义用inline显式指定
class Sales_item
{
public:
double Sales_item::avg_price() const;
//....其他成员函数略
private:
std::string isbn;
unsigned unite_sold;
double revenue;
};
inline double Sales_item::avg_price()const
{
if(unite_sold)
return revenue/units_sold;
else
return 0;
}