您当前的位置: 首页 > 

txwtech

暂无认证

  • 4浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

cc2a_demo-2CppPrimer_类定义代码示范。txwtech

txwtech 发布时间:2019-11-14 13:26:19 ,浏览量:4

//cc2a_demo-2CppPrimer_类定义代码示范。txwtech

#include 
#include 
//2CppPrimer_类定义代码示范。txwtech
using namespace std;

class Sales_item
{
public://字符串传递参数用引用方式,整数浮点数直接值传递
	Sales_item(string &book,unsigned units,double amount):isbn(book),units_sold(units),revenue(amount)
	{
	}
	double avg_price() const//如果值是不改变的,可以写一个const
	{
		if (units_sold)
		{
			return revenue / units_sold;
		}
		else
		{
			return 0;
		}
	}
	bool same_isbn(const Sales_item &rhs) const
	{
		return isbn == rhs.isbn;
	}
	void add(const Sales_item &rhs)//此处变量值有改变,就不能用const
	{
		units_sold += rhs.units_sold;
		revenue += rhs.revenue;
	}
private:
	string isbn;
	unsigned units_sold;
	double revenue;

};
class People
{
public://字符串传递参数用引用方式,整数浮点数直接值传递
	People(string &r_name,string &r_addr):address(r_name),name(r_addr)
	{

	}
	string getName()
	{
		return name;
	}
	string getAddr()
	{
		return address;
	}

	

private:
	string address;
	string name;


};



int main()
{
	Sales_item a(string("112-112"),2,20.00);
	Sales_item b(string("112-112"), 3, 80.00);

	if (a.same_isbn(b))
		a.add(b);
	cout             
关注
打赏
1665060526
查看更多评论
0.1265s