上一篇也是实现类和对象,只是方法略微不同,结果都是一样的。 Point类 新建一个Point.h文件,一个Point.cpp文件, 一个Source文件(主函数) Point.h文件,声明定义 #ifndef _POINT_H_
#define _POINT_H_
class Point
{
public:
//这里是构造函数的实现,所以.cpp中就不用再写了
Point()
{
}
//这里是析构函数的实现,所以.cpp中就不用再写了
~Point()
{
}
void setPoint(int x,int y);
void printPoint();
private:
int xPos;
int yPos;
};
#endif // !_POINT_H_ Point.cpp文件,实现部分 #include <iostream> #include "Point.h" using namespace std; void Point::setPoint(int x, int y) { this->xPos = x; this->yPos = y; } void Point::printPoint() { cout << "x=" << xPos << endl; cout << "y=" << yPos << endl; }主函数 |
|