类的成员函数

#include <iostream>
#include <string>

using namespace std;

class Sales_item  // class要变成对象才能用,
{
public:
	double avg_price() const; // 函数原型声明,
	bool same_isbn(const Sales_item &rhs) const  // 公有类的成员函数有const则不能变,
	{
		return isbn == rhs.isbn;// return this -> isbn == rhs.isbn;
	}
   // private:  // 私有的,
public:   // 共有的,
	std::string isbn;
	unsigned untis_sold;
	double revenue;
};
double Sales_item::avg_price() const
{
	if(this->units_sold)
		return (this->units_sold);  //return (this->revenue),
	else
		return 0;
}
int main ()
{
    Sales_item item1,item2;
	item1.isbn = "0-201-78345-X";
	item1.untis_sold = 10;
	item1.revenue = 300.00;
	cout << item1.avg_price() << endl;
    
    item2.isbn = "0-201-78345-X";
	item2.untis_sold = 2;
	item2.revenue = 70.00;
	cout << item2.avg_price() << endl;

	if(item1.same_isbn(item2))
         cout << "这两个Sales item 是同一种书!" << endl;
	else
		cout << "这两个Sales item 不是同一种书!" << endl;
	return 0;
}

你可能感兴趣的:(类的成员函数)