定义了两个类,一个是MyPoint,一个是MyCircle;
在测试程序的项目中添加类;
1.MyPoint.h//类的声明
#pragma once
class MyPoint
{
private:
int x0;
int y0;
public:
void setxy(int _x0, int _y0);
int getx();
int gety();
};
2.MyPoint.cpp//类的实现,首先应该包含声明
#include "MyPoint.h"
void MyPoint::setxy(int _x0, int _y0)
{
x0 = _x0;
y0 = _y0;
}
int MyPoint::getx()
{
return x0;
}
int MyPoint::gety()
{
return y0;
}
3.MyCircle.h//声明类MyCircle
#pragma once
#include"MyPoint.h"//若有使用另外的类的内容就需要包含此类的头文件
class MyCircle
{
public:
void setxyr(int _x1, int _y1, int _r);
void judge(int x0, int y0);
void judge(MyPoint &p);
private:
int x1;
int y1;
int r;
};
4.MyCircle.cpp//类的实现
#include "MyCircle.h"
#include<iostream>
using namespace std;
void MyCircle::setxyr(int _x1, int _y1, int _r)
{
x1 = _x1;
y1 = _y1;
r = _r;
}
void MyCircle::judge(int x0, int y0)
{
int a = (x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0) - r*r;
if (a > 0)
{
cout << "it is out of the circle";
}
if (a < 0)
{
cout << "it is in the circle";
}
}
void MyCircle::judge(MyPoint &p)
{
int a = (x1 - p.getx())*(x1 - p.getx()) + (y1 - p.gety())*(y1 - p.gety()) - r*r;
if (a > 0)
{
cout << "it is out of the circle";
}
if (a < 0)
{
cout << "it is in the circle";
}
}
5.MainClass.cpp//主程序实现
#include<iostream>
#include"MyPoint.h"
#include"MyCircle.h"
using namespace std;
void main()
{
MyPoint myp;
myp.setxy(1, 1);
MyCircle c1;
c1.setxyr(2, 2, 3);
c1.judge(myp);
system("pause");
}