www.gusucode.com > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序 > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序/code/C++例题程序/第6章/s6_3/sclass6_3_point.cpp

    //Download by http://www.NewXing.com
//类的实现文件
//文件名:ch6_3\sclass6_3_point.cpp

#include "sclass6_3_point.h"    //包含类定义头文件

//构造函数,缺省参数为:x=0;y=0。该值在声明函数时给出,此处不能够再给出
CPoint::CPoint(int x, int y)
{ 
	m_x = x;				
	m_y = y;
}
//const关键字在声明和实现时,都得给出
int CPoint::SetX() const			//常成员函数,设置x坐标,仅仅为了观察而加const
{  
	//m_x++							//错误,常成员函数不能改变对象
	return m_x;						//返回
}
int CPoint::SetY()					//设置y坐标
{
	m_y++;							//正确。一般成员函数可以改变对象
	return m_y; 
}
void CPoint::Print()				//显示,一般成员函数
{ 
	cout << "一般成员函数调用:" << m_x << " " << m_y <<  endl; 
}
void CPoint::Print() const			//显示,常成员函数
{ 
	cout << "  常成员函数调用:" << m_x << " " << m_y <<  endl; 
}