/*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、类的实例化对象可以作为形参写在自定义函数中