类的封装

#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;
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值