传送类时 引用

#include<iostream>
#include<cstring>
using namespace std;
class Point{
public:
 Point(int a=0,int b=0){
   x=a;
   y=b;
   numOfPoints++;
  cout << "The Point ("<< x <<", "<< y <<") is created! Now, we have "<< numOfPoints <<" points." << endl;}
 Point(const Point &h){
     x=h.x;
     y=h.y;
   numOfPoints++;
   cout << "A Point ("<< x <<", "<< y <<") is copied! Now, we have "<< numOfPoints <<" points." << endl;
 }
 ~Point(){
    numOfPoints--;
 cout << "A Point ("<< x <<", "<< y <<") is erased! Now, we have "<< numOfPoints <<" points." << endl;
 }
static int getNumOfPoints(){
   return numOfPoints;
}
 int getX(){
   return x;
 }
 int getY(){
   return y;
 }
private:
    int x,y;
    static int numOfPoints;
};
int Point::numOfPoints;
class Circle{
public:
    Circle(int a,int b,int c):center(a,b),r(c){
      numOfCircles++;
     cout << "A circle at ("<< center.getX() <<", "<< center.getY() <<") and radius "<< r <<" is created! Now, we have "<< numOfCircles <<" circles." << endl;

    }
    Circle(Point p,int c):center(p),r(c){
       // Point p 相当于Point p(*bPoint);而Point &p相当于Point *bPoint;
    numOfCircles++;

         cout << "A circle at ("<< center.getX() <<", "<< center.getY() <<") and radius "<< r <<" is created! Now, we have "<< numOfCircles <<" circles." << endl;

    }
    ~Circle()
    {
    numOfCircles--;
 cout << "A circle at ("<< center.getX() <<", "<< center.getY() <<") and radius "<< r <<" is erased! Now, we have "<< numOfCircles <<" circles." << endl;

    }
  static int getNumOfCircles(){
     return numOfCircles;
    }
    Point &getCenter(){
       return center;
    }
    bool pointInCircle(Point &h)
    {
        int d;
        d=(center.getX()-h.getX())*(center.getX()-h.getX())+(center.getY()-h.getY())*(center.getY()-h.getY());
        return d>=r?false:true;
    }
private:
  Point center;
  int r;
  static int numOfCircles;
};
int Circle::numOfCircles;
int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    int cases,num;
    int x, y, r, px, py;
    Point aPoint(0,0), *bPoint;
    Circle aCircle(1,1,1);
    cin>>cases;
    cout<<"We have "<<Point::getNumOfPoints()<<" points and "<<Circle::getNumOfCircles()<<" circles now."<<endl;
    for (int i = 0; i < cases; i++)
    {
        cin>>x>>y>>r;
        bPoint = new Point(x,y);
        Circle circle(*bPoint, r);//*bPoint为bPoint所为指向的对象;
        cin>>num;
        for (int j = 0; j < num; j++)
        {
            cin>>px>>py;
            if (circle.pointInCircle(*(new Point(px, py))))
            {
                cout<<"("<<px<<", "<<py<<") is in the circle at (";
                cout<<circle.getCenter().getX()<<", "<<circle.getCenter().getY()<<")."<<endl;
            }
            else
            {
                cout<<"("<<px<<", "<<py<<") is not in the circle at (";
                cout<<circle.getCenter().getX()<<", "<<circle.getCenter().getY()<<")."<<endl;
            }
        }
        delete bPoint;
    }
    cout<<"We have "<<Point::getNumOfPoints()<<" points, and "<<Circle::getNumOfCircles()<<" circles."<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值