#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
//类的封装,明确什么是想让用户看到的 什么是不想让用户看到的。
//第一步 先把所有成员变量设为private。 第二步 添加函数接口,提供外部操作成员变量的可能。
//设置getter和setter getter是用来读取成员变量的 setter是用来改变成员变量的
using namespace std;
class circle {
public:
int getX() { //读取x
return myX;
}
int getY() {//读取y
return myY;
}
int getR() {//读取r
return myR;
}
void setR(int r) {
myR = r;
}
void setX(int x) {
myX = x;
}
void setY(int y) {
myY = y;
}
void moveTo(int x, int y) { //留好函数接口
setX(x);
setY(y);
}
void show_cir(){
cout << myX << "," << myY << " " << "R=" <<myR<< endl;
}
private:
int myX;
int myY;
int myR;
};
void contain(circle a, circle b) {// 判断两个圆是否存在包涵关系
if (a.getR() > b.getR()) {
int x = a.getX() - b.getX();
int y = a.getY() - b.getY();
if (sqrt(x*x+y*y)+(b.getR())<a.getR()) {
cout << "circle first contain circle 2nd" << endl;
}
else {
cout << "circle first not contain circle 2nd" << endl;
}
}
else {
int x = a.getX() - b.getX();
int y = a.getY() - b.getY();
if (sqrt(x * x + y * y) + (a.getR()) < b.getR()) {
cout << "circle 2nd contain circle 1st" << endl;
}
else {
cout << "circle 2nd not contain circle 1st" << endl;
}
}
}
int main() {
circle cir1;
cir1.setR(15);
cir1.moveTo(3, 4);
cir1.show_cir();
circle cir2;
cir2.setR(2);
cir2.moveTo(1, 0);
cir2.show_cir();
contain(cir1, cir2);
return 0;
}```
类的封装
最新推荐文章于 2021-09-29 10:36:15 发布