类和对象的使用示例
将类的声明和实现文件分开
头文件:类的声明
eg:
头文件:Point.h
#include <iostream>
using namespace std;
//类的声明
class Point
{
public:
Point();
Point(float _x,float _y);
void print();
private:
float x;
float y;
};
Point.cpp文件
#include "Point.h"
Point::Point()
{
x=0;
y=0;
}
Point::Point(float _x,float _y)
{
x=_x;
y=_y;
}
void Point::print()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
main.c文件
#include <iostream>
using namespace std;
#include "Point.h"
int main()
{
Point p;
p.print();
return 0;
}