您当前的位置: 首页 >  c++

txwtech

暂无认证

  • 2浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

txwtech 发布时间:2020-02-08 17:45:59 ,浏览量:2

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

txwtech */

 

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

txwtech
*/

#include 
#include 
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             
关注
打赏
1665060526
查看更多评论
0.0412s