ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

模块化程序 点与圆的关系 类中成员函数的声明和实现分开写入头文件和源文件中

2021-01-31 12:06:08  阅读:207  来源: 互联网

标签:头文件 cout Point int void getC 源文件 Circle 类中


@[TOC]模块化程序
在黑马程序员课程4.1.3成员属性设置为私有 课后案例 点和圆的关系中 谈到了文件的封装
此案例是判断点与圆的关系,重点是以另外一个类作为本类中的成员;
在比较大的开发中,会有许多个类,以及许多个函数。因此,以封装的思想,来模块化程序。

#include<iostream>
using namespace std;

class Point
{
public:
	void setX(int x)
	{
		m_X = x;
	}
	int getX()
	{
		return m_X;
	}
	void setY(int y)
	{
		this->m_Y = y;
	}
	int getY()
	{
		return m_Y;
	}
	//Point(int x,int y)
	//{
	//	m_X = x;
	//	m_Y = y;
	//}
private:
	int m_X;
	int m_Y;
};

class Circle
{
public:
	//设置半径和获取半径
	void setR(int r)
	{
		m_R = r;
	}
	int getR()
	{
		return m_R;
	}
	//设置圆心和获取圆心
	void setC(Point center)
	{
		m_Center = center;
	}
	Point getC()
	{
		return m_Center;
	}
private:
	int m_R;
	Point m_Center;
};

void judge( Circle &c, Point &p)//易忘点,用引用的方式传。
{
	int a;
	a = (c.getC().getX() - p.getX()) * (c.getC().getX() - p.getX()) + (c.getC().getY() - p.getY()) * (c.getC().getY() - p.getY());
	if ( a == (c.getR())* (c.getR())) { cout << "点在圆上" << endl; return; }
	if (a > (c.getR()) * (c.getR())) { cout << "点在圆外" << endl; return;}
	if (a < (c.getR()) * (c.getR())) { cout << "点在圆内" << endl; return;}
	return;
}

void test01()
{
	Circle C;
	Point P;
	Point cc;
	int cx, cy,r, px, py;
	cout << "请设置圆心横坐标X:" << endl;
	cin >> cx;
	cout << "请设置圆心纵坐标Y:" << endl;
	cin >> cy;
	cout << "请设置圆的半径R:" << endl;
	cin >> r;
	cout << "请设置点的横坐标X:" << endl;
	cin >> px;
	cout << "请设置点的纵坐标Y:" << endl;
	cin >> py;
	cc.setX(cx);
	cc.setY(cy);
	C.setC(cc);
	C.setR(r);
	P.setX(px);
	P.setY(py);
	judge(C,P);
}

int main()
{
	for (int i = 0; i < 3; i++) { test01(); }
	system("pause");
	return 0;
}

在程序中有point类和Circle类,
在项目中新创建两个文件point.cpp和point.h,以及circle.cpp和circle.h。简单来说就是把类的成员函数的声明放在头文件里,把类的成员函数的实现放在源文件中。
需要注意的是,在头文件中要有#pragma once 以避免重复包含头文件
在这里插入图片描述
主源文件

#include<iostream>
#include"point.h"
#include"circle.h"
using namespace std;

void judge( Circle &c, Point &p)//易忘点,用引用的方式传。
{
	int a;
	a = (c.getC().getX() - p.getX()) * (c.getC().getX() - p.getX()) + (c.getC().getY() - p.getY()) * (c.getC().getY() - p.getY());
	if ( a == (c.getR())* (c.getR())) { cout << "点在圆上" << endl; return; }
	if (a > (c.getR()) * (c.getR())) { cout << "点在圆外" << endl; return;}
	if (a < (c.getR()) * (c.getR())) { cout << "点在圆内" << endl; return;}
	return;
}

void test01()
{
	Circle C;
	Point P;
	Point cc;
	int cx, cy,r, px, py;
	cout << "请设置圆心横坐标X:" << endl;
	cin >> cx;
	cout << "请设置圆心纵坐标Y:" << endl;
	cin >> cy;
	cout << "请设置圆的半径R:" << endl;
	cin >> r;
	cout << "请设置点的横坐标X:" << endl;
	cin >> px;
	cout << "请设置点的纵坐标Y:" << endl;
	cin >> py;
	cc.setX(cx);
	cc.setY(cy);
	C.setC(cc);
	C.setR(r);
	P.setX(px);
	P.setY(py);
	judge(C,P);
}

int main()
{
	for (int i = 0; i < 3; i++) { test01(); }
	system("pause");
	return 0;
}

point.h

#pragma once //这是为了防止头文件重复包含
#include<iostream>
using namespace std;

//在这里只需要保留成员函数的声明和成员变量的声明

	class Point
	{
	public:
		void setX(int x);

		int getX();

		void setY(int y);

		int getY();

	private:
		int m_X;
		int m_Y;
	};

point.cpp

#include"point.h"
void Point::setX(int x)
{
	m_X = x;
}
int Point::getX()
{
	return m_X;
}
void Point::setY(int y)
{
	m_Y = y;
}
int Point::getY()
{
	return m_Y;
}

circle.h

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

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

	int getR();

	//设置圆心和获取圆心
	void setC(Point center);

	Point getC();

private:
	int m_R;
	Point m_Center;
};

circle.cpp

#include"circle.h"

	//设置半径和获取半径
void Circle::setR(int r)
{
	m_R = r;
}
int Circle::getR()
{
	return m_R;
}
//设置圆心和获取圆心
void Circle::setC(Point center)
{
	m_Center = center;
}
Point Circle::getC()
{
	return m_Center;
}

类的成员的声明与实现的分开写。

标签:头文件,cout,Point,int,void,getC,源文件,Circle,类中
来源: https://blog.csdn.net/legendary_bruce/article/details/113461004

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有