新建一个Point.h文件
在该文件中定义Point类,代码如下
#ifndef POINT_H
#define POINT_H
class Point
{
public:
Point();
void setPoint(int x,int y);
void printPoint();
virtual ~Point();
protected:
private:
int x;
int y;
};
#endif // POINT_H
在Point.cpp文件中补充完善Point类的函数
代码如下
#include "Point.h"
#include<stdio.h>
Point::Point()
{
//ctor
}
void Point::setPoint(int x,int y)
{
this->x = x;
this->y = y;
}
void Point::printPoint()
{
printf("(%d,%d)\n",this->x,this->y);
}
Point::~Point()
{
//dtor
}
在main.cpp中写一个主函数创建类并调用该类的两个方法,代码如下
#include <iostream>
#include"Point.h"
using namespace std;
int main()
{
Point a;
a.setPoint(12,56);
a.printPoint();
return 0;
}