mian.cpp测试文件
#include <iostream>
using namespace std;
#include "circle.h"
#include "point.h"
void isInCircle(Point& p, Circle& c)
{
int distence =
(c.getCenter().getX() - p.getX())* (c.getCenter().getX() - p.getX()) +
(c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());
int rr = c.getR() * c.getR();
if (distence == rr)
{
cout << "On the circle." << endl;
}
else if (distence < rr)
{
cout << "In the circle." << endl;
}
else
{
cout << "Out the circle." << endl;
}
}
int main()
{
Circle c1;
Point center;
center.setX(10);
center.setY(10);
c1.setCenter(center);
c1.setR(5);
Point p1;
p1.setX(10);
p1.setY(16);
isInCircle(p1, c1);
system("pause");
return 0;
}
point.h点类的头文件,只保留点类的成员函数声明,实现放在另一个文件中
#pragma once
#include <iostream>
using namespace std;
class Point
{
public:
void setX(int x);
void setY(int y);
int getX();
int getY();
private:
int m_X;
int m_Y;
};
point.cpp 在成员函数前加上其所属类的作用域,并完成成员函数的实现
#include "point.h"
void Point::setX(int x)
{
m_X = x;
}
void Point::setY(int y)
{
m_Y = y;
}
int Point::getX()
{
return m_X;
}
int Point::getY()
{
return m_Y;
}
circle.h
#pragma once
#include <iostream>
using namespace std;
#include "point.h"
class Circle
{
public:
void setCenter(Point& center);
Point getCenter();
void setR(int r);
int getR();
private:
int m_R;
Point m_Center;
};
circle.cpp
#include "circle.h"
void Circle::setCenter(Point& center)
{
m_Center = center;
}
Point Circle::getCenter()
{
return m_Center;
}
void Circle::setR(int r)
{
m_R = r;
}
int Circle::getR()
{
return m_R;
}
通过对类的分文件实现,可以在大型项目中使我们的代码更加易于阅读和维护。