網站首頁 編程語言 正文
上一篇封裝直達
創建圓心類
point.h
#pragma once
#include<iostream>
using namespace std;
//創建圓心類
class Point
{
public:
void setM_x(int x);
int getM_x();
void setM_y(int y);
int getM_y();
private:
int m_x;
int m_y;
};
把圓心的橫縱坐標設為私有,公共屬性寫了兩對成員方法,用來給圓心屬性賦值和讀取,這里只寫了方法的聲明,具體實現將在point.cpp文件實現。
point.cpp
#include"point.h"
void Point::setM_x(int x)
{
m_x = x;
}
int Point::getM_x()
{
return m_x;
}
void Point::setM_y(int y)
{
m_y = y;
}
int Point::getM_y()
{
return m_y;
}
這個文件就是point頭文件中定義成員方法的具體實現了,引入point.h頭文件,在四個方法的返回值類型和方法名面前加::,表示屬于圓心類的方法,然后完成賦值和取值的實現。
創建圓類
Circle.h
#pragma once
#include<iostream>
#include "point.h"
using namespace std;
//創建 Circle 類
class Circle
{
public:
void setM_r(int r);
int getM_r();
void setCenter(Point ¢er);
Point getCenter();
private:
int m_r;
Point m_center;
};
這里和point.h類似,設置了圓心和圓半徑的屬性并設置為私有;在公共權限下定義為半徑賦值和取值的方法;對于圓心,我引入point.h的頭文件,在圓類中創建了圓心m_center,設置圓心方法中采用引用傳參,將圓心類完成賦值的圓心傳入到圓類中;獲取圓心的途徑是通過 getCenter 方法調用point 類中的 get 方法。
Circle.cpp
#include"Circle.h"
void Circle::setM_r(int r)
{
m_r = r;
}
int Circle::getM_r()
{
return m_r;
}
void Circle::setCenter(Point ¢er)
{
m_center = center;
}
Point Circle::getCenter()
{
return m_center;
}
引入Circle.h頭文件,將.h的方法完成具體實現。
判斷點圓關系函數
void relative(Circle& c, Point& p)
{
//圓心到點距離的平方為distance
int distance =
(c.getCenter().getM_x() - p.getM_x()) * (c.getCenter().getM_x() - p.getM_x()) +
(c.getCenter().getM_y() - p.getM_y()) * (c.getCenter().getM_y() - p.getM_y());
//半徑的平方 rDistance
int rDistance = c.getM_r() * c.getM_r();
if (distance > rDistance) cout << "點在圓外" << endl;
else if (distance == rDistance) cout << "點在圓上" << endl;
else cout << "點在圓內" << endl;
}
數學上點和圓的關系是圓心到點的距離和半徑的比較,這里我把等號兩邊平方,比較兩個數據的大小即可;若距離大于半徑,點在圓外;距離等于半徑,點在圓上;距離小于半徑,點在圓內;relative 函數傳入Circle 類和 Point 類的對象,分別計算圓心到點距離的平方 distance和半徑的平方 rDistance,利用多重If語句輸出不同結果即可。
最終實現
#include"Circle.h"
void relative(Circle& c, Point& p)
{
//圓心到點距離的平方為distance
int distance =
(c.getCenter().getM_x() - p.getM_x()) * (c.getCenter().getM_x() - p.getM_x()) +
(c.getCenter().getM_y() - p.getM_y()) * (c.getCenter().getM_y() - p.getM_y());
//半徑的平方 rDistance
int rDistance = c.getM_r() * c.getM_r();
if (distance > rDistance) cout << "點在圓外" << endl;
else if (distance == rDistance) cout << "點在圓上" << endl;
else cout << "點在圓內" << endl;
}
int main()
{
int x = 0, y = 0, a = 0, b = 0, r = 0;
Circle C;
Point center, P;
cout << "圓心橫坐標 x 為:"; cin >> x; cout << endl;
cout << "圓心縱坐標 y 為:"; cin >> y; cout << endl;
center.setM_x(x);
center.setM_y (y);
C.setCenter(center);
cout << "此圓 半徑 r 為:"; cin >> r; cout << endl;
C.setM_r(r);
cout << "點橫坐標 x 為:"; cin >> a; cout << endl;
cout << "點縱坐標 y 為:"; cin >> b; cout << endl;
P.setM_x(a);
P.setM_y(b);
relative(C, P);
}
主函數創建Circle類對象 C,和Point 類對象 P,center;通過set方法設置center點的坐標并傳入C的setCenter方法中,這樣圓心數據賦值完成,然后利用相同方法對圓半徑以及點賦值,最后調用relative函數比較即可,附上三種運行結果,如下:
總結
這個案例應該是很好理解的,總共是五個文件,兩個.h三個.cpp。這樣看著非常的舒服和整潔,建議大家在做稍微復雜的項目或者案例時可以多利用分文件編寫,那么C++實現點和圓的關系到此結束,期待你的鼓勵和支持
原文鏈接:https://blog.csdn.net/m0_58618795/article/details/124801542
相關推薦
- 2023-10-15 el-input有時候添加不了有時候刪不了
- 2022-06-16 Python數據結構之遞歸方法詳解_python
- 2021-12-04 C語言實現可排序通訊錄的示例代碼_C 語言
- 2022-07-21 Eslint代碼保存自動格式化
- 2022-10-22 react實現Modal彈窗效果_React
- 2022-06-04 C語言超詳細講解輪轉數組_C 語言
- 2022-08-14 python中matplotlib調整圖例位置的方法實例_python
- 2022-11-06 Android?Navigation重建Fragment問題分析及解決_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支