c++/类和对象

/*This program is to create a circle class and realize the function
of judging the position relationship between two circles
*/
#include

using namespace std;

class Point{//the class of point
public:
int setX(int x){
myX=x;
}
int setY(int y){
myY=y;
}
int getX() const {
return myX;
}
int getY() const{
return myY;
}

private:
int myX;
int myY;
};
class Circle{//the class of circle
public:
int setR(int r){//set the radius
myR=r;
}
int getR() const{//get the radius
return myR;
}
int setCenter(Point center){//set the center of the circle
myCenter=center;
}
Point getCenter() const{//get the center of the circle and use const to make it unmodifiable
return myCenter;
}

private:
int myR;//radius
Point myCenter;//customize a class and instantiate an object named myCenter
};

void isInCircle(Circle &c,Point &p){
int disdanceCirclePoint=(c.getCenter().getX()-p.getX())(c.getCenter().getX()-c.getCenter().getX())+
(c.getCenter().getY()-p.getY())
(c.getCenter().getY()-p.getY());//calculate the disdance between circle and point

int radiusSqure=c.getR()*c.getR();//calculate the radius of a square

if(disdanceCirclePoint == radiusSqure){//judge the relationship between circle and point and cout the result
    cout<<"point on the circle"<<endl;
}else if(disdanceCirclePoint>radiusSqure){
    cout<<"point outside the circle"<<endl;
}else{
    cout<<"point in the circle"<<endl;
}

}

int main()
{ //create the object of class Circle and class Point
Circle circleA;
Point pointP,circleCenterA;

//initialization data of circleA and pointP
circleCenterA.setX(3);
circleCenterA.setY(2);
circleA.setCenter(circleCenterA);

circleA.setR(5);
pointP.setX(3);
pointP.setY(8);

isInCircle(circleA,pointP);
return 0;

}

1、类中可以用另一个类的实例化对象作为数据成员
2、类的实例化对象可以作为形参写在自定义函数中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值