ca65a_c++_类的成员函数_txwtech_this指针

/*ca65a_c++_类的成员函数_txwtech_this指针
函数原型必须在类中定义
函数体:
1.在类中定义函数体
2.在类外定义函数体-一般定义都写在类的外面
this 指针
const成员函数

txwtech
*/

 

/*ca65a_c++_类的成员函数_txwtech_this指针
函数原型必须在类中定义
函数体:
1.在类中定义函数体
2.在类外定义函数体-一般定义都写在类的外面
this 指针
const成员函数

txwtech
*/

#include <iostream>
#include <string>
using namespace std;

int sum(int x,int y)//全局函数,外部函数
{
	return x + y;
}

class Sales_item
{
public:
	double avg_price() const;//常成员函数声明
	//类的成员函数
	bool same_isbn(const Sales_item &rhs) const//添加最后的const后,数据不允许修改
	{
		//return isbn == rhs.isbn;
		//或者可以这样写:this是自动传进来的
		return this->isbn == rhs.isbn;
	}


//private:

	std::string isbn;
	unsigned units_sold;
	double revenue;
};
//txwtech
//avg_price的函数体可以写在类的外面
double Sales_item::avg_price() const//::4个点表示范围解析,表示函数属于哪个类
 {
	if (this->units_sold)//判断数量是否为0
		return(this->revenue / this->units_sold);
	
	else
		return 0;

  }

int main()
{
	Sales_item item1, item2;//item1就是一个对象
	item1.isbn = "0-201-78345-X";
	item1.units_sold = 10;
	item1.revenue = 300.0;

	item2.isbn= "0-201-78345-X";
	item2.units_sold = 2;
	item2.revenue = 70.0;

	if (item1.same_isbn(item2))
		cout << "这两个sales item是同一种书" << endl;
	else
		cout << "这两个sales item不是同一种书" << endl;
	cout << item1.avg_price() << endl;
	cout << item2.avg_price() << endl;

	return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

txwtech笛克特科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值