C++ design case 2-the relationship between points and circles, split the file into source file and header file

=============================================================================================

The core steps of code sub-file writing are as follows:

1. Perform function declaration and class creation in the header file, and remember to add it to #pragma onceprevent repeated inclusion of the header file; 2. Define the function in the source file. If it is a member function in a class, you need to use the location resolution operator when defining the function, otherwise the compiler will report
an ::error ;
main


First post a complete code as follows:
We will call it [Code 1]

#include<iostream>
#include<string>
using namespace std;

//一个点类(Point)
class Point
{
    
    

public:
	//设置x坐标
	void setX(int x)
	{
    
    
		p_x = x;
	}
	//获取x坐标
	int getX()
	{
    
    
		return p_x;
	}
	//设置y坐标
	void setY(int y)
	{
    
    
		p_y = y;
	}
	//获取y坐标
	int getY()
	{
    
    
		return p_y;
	}
private:
	int p_x;
	int p_y;
};
//设计一个圆类Circle
class Circle
{
    
    

public:
	//设置半径R
	void setR(int r)
	{
    
    
		R = r;
	}

	//获取半径R
	int getR()
	{
    
    
		return R;
	}

	//设置圆心
	void setCenter(Point center)
	{
    
    
		c_center = center;
	}

	//获取圆心
	Point getCenter()//c_center是Piont类的数据
	{
    
    
		return c_center;
	}


private:
	int R;
	//在类中可以让另一个类 作为本类中的成员--与结构体相似
	Point c_center;

};

//判断点和圆的关系
void isInCircle(Circle& c, Point& p)
{
    
    
	if ((p.getX() - c.getCenter().getX()) * (p.getX() - c.getCenter().getX()) + (p.getY() - c.getCenter().getY()) * (p.getY() - c.getCenter().getY()) == c.getR() * c.getR())
		cout << "点在圆上" << endl;
	else if ((p.getX() - c.getCenter().getX()) * (p.getX() - c.getCenter().getX()) + (p.getY() - c.getCenter().getY()) * (p.getY() - c.getCenter().getY()) > c.getR() * c.getR())
		cout << "点在圆外" << endl;
	else
		cout << "点在圆内" << endl;
}

int main()
{
    
    
	//创建并设置点P1
	Point P1;
	P1.setX(10);
	P1.setY(9);

	//创建并设置点P2--圆心
	Point P2;
	P2.setX(10);
	P2.setY(0);

	//设置圆C1
	Circle C1;
	C1.setR(10);
	C1.setCenter(P2);

	isInCircle(C1, P1);

	system("pause");
	return 0;
}

first step

Then we divide the Point class in [Code 1] into a header file Point.hand a source file Point.cpp:
(1) Header file:Point.h

#pragma once
#include<iostream>
#include<string>

using namespace std;

//一个点类(Point)
class Point
{
    
    

public:
	//设置x坐标
	void setX(int x);

	//获取x坐标
	int getX();

	//设置y坐标
	void setY(int y);

	//获取y坐标
	int getY();

private:
	int p_x;
	int p_y;
};
//设计一个圆类Circle

(2) Source file Point.cpp
The key here is to include the header file Point.h we created in the previous step in Point.cpp【#include"Point.h"

#include<iostream>
#include<string>
#include"Point.h"
using namespace std;

//一个点类(Point)

//设置x坐标
void Point::setX(int x)
{
    
    
	p_x = x;
}
//获取x坐标
int Point::getX()
{
    
    
	return p_x;
}
//设置y坐标
void Point::setY(int y)
{
    
    
	p_y = y;
}
//获取y坐标
int Point::getY()
{
    
    
	return p_y;
}
//设计一个圆类Circle

second step

The Circle class is divided into header files Circle.hand source filesCircle.cpp

(1) Circle.hThe code needs to be included Point.h, because Point.h has the center of the circle we need to use, the specific code is as follows:

#pragma once
#include<iostream>
#include<string>
using namespace std;
#include "Point.h"

class Circle
{
    
    
public:
	//设置半径R
	void setR(int r);


	//获取半径R
	int getR();


	//设置圆心
	void setCenter(Point center);


	//获取圆心
	Point getCenter();//c_center是Piont类的数据


private:
	int R;
	//在类中可以让另一个类 作为本类中的成员--与结构体相似
	Point c_center;

};

(2) Circle.cppneed to contain#include "Circle.h"

#include<iostream>
#include<string>
#include "Circle.h"
using namespace std;

void Circle::setR(int r)
{
    
    
	R = r;
}

//获取半径R
int Circle::getR()
{
    
    
	return R;
}

//设置圆心
void Circle::setCenter(Point center)
{
    
    
	c_center = center;
}

//获取圆心
Point Circle::getCenter()//c_center是Piont类的数据
{
    
    
	return c_center;
}

3. Add the header file to the main function #include "Circle.h"(the Circle.h at this time already includes the Point.h we created in the previous step, so just add Circlecle here), the code in the main function is as follows:

#include<iostream>
#include<string>
using namespace std;
#include "Circle.h"

//判断点和圆的关系
void isInCircle(Circle& c, Point& p)
{
    
    
	if ((p.getX() - c.getCenter().getX()) * (p.getX() - c.getCenter().getX()) + (p.getY() - c.getCenter().getY()) * (p.getY() - c.getCenter().getY()) == c.getR() * c.getR())
		cout << "点在圆上" << endl;
	else if ((p.getX() - c.getCenter().getX()) * (p.getX() - c.getCenter().getX()) + (p.getY() - c.getCenter().getY()) * (p.getY() - c.getCenter().getY()) > c.getR() * c.getR())
		cout << "点在圆外" << endl;
	else
		cout << "点在圆内" << endl;
}

int main()
{
    
    
	//创建并设置点P1
	Point P1;
	P1.setX(10);
	P1.setY(9);

	//创建并设置点P2--圆心
	Point P2;
	P2.setX(10);
	P2.setY(0);

	//设置圆C1
	Circle C1;
	C1.setR(10);
	C1.setCenter(P2);

	isInCircle(C1, P1);

	system("pause");
	return 0;
}

From this point of view, do you feel that the main function is much simpler~
Reference 1Reference
2

Guess you like

Origin blog.csdn.net/weixin_56847236/article/details/131499440