zoukankan      html  css  js  c++  java
  • 两点的距离(类的组合成员、冒号语法)

    【问题描述】
    定义一个坐标点类Point和求两点距离的距离类Distance,
    在每个类的构造函数函数体里加上cout输出相应的提示语句,以便观察构造函数被调用的顺序。
    类的设计和主函数如下:(请勿修改)
    class Point
    {
    public:
    Point(int xx,int yy);
    Point(Point &r);
    int GetX();
    int GetY();
    ~Point();
    private:
    int x,y;
    };
    class Distance
    {
    private:
    Point p1,p2;
    double dist;
    public:
    Distance(Point a,Point b);
    double GetDis();
    ~Distance();
    };
    int main()
    {
    Point myp1(1,1),myp2(4,5);
    Distance myd(myp1,myp2);
    cout<<endl;
    cout<<“the distance is:”<<myd.GetDis()<<endl;
    }

    【样例输入】 无输入

    【样例输出】
    Point’s constructor was called
    Point’s constructor was called
    Point’s copyConstructor was called
    Point’s copyConstructor was called
    Point’s copyConstructor was called
    Point’s copyConstructor was called
    Distance’s constructor was called

    Point’s destructor was called
    Point’s destructor was called

    the distance is:5

    Distance’s destructor was called
    Point’s destructor was called
    Point’s destructor was called
    Point’s destructor was called
    Point’s destructor was called

    #include <iostream>
    #include <math.h>
    using namespace std;
    
    class Point
    {
    public:
        Point(int xx,int yy);//构造函数
        Point(Point &r);//拷贝构造函数
        int GetX(){return x;}
        int GetY(){return y;}
        ~Point();//析构函数
    private:
        int x,y;
    };
    
    Point::Point(int xx,int yy) : x(xx), y(yy)//构造函数
    {
        cout << "Point's constructor was called" << endl;
    }
    /*也可以这样写
    Point::Point(int xx,int yy)//构造函数
    {
    	x = xx; y = yy;
        cout << "Point's constructor was called" << endl;
    }
    */
    
    Point::Point(Point &r) : x(r.x), y(r.y) //拷贝构造函数
    {
        cout << "Point's copyConstructor was called" << endl;
    }
    /*也可以这样写
    Point::Point(Point &r)//拷贝构造函数
    {
    	x = r.x; y = r.y;
        cout << "Point's copyConstructor was called" << endl;
    }
    */
    
    Point::~Point()//析构函数
    {
        cout << "Point's destructor was called" << endl;
    }
    
    class Distance
    {
    private:
        Point p1,p2;
        double dist;
    public:
        Distance(Point a,Point b);//构造函数
        double GetDis();//得到两点之间的距离
        ~Distance();//析构函数
    };
    
    Distance::Distance(Point a,Point b) : p1(a), p2(b) //构造函数
    {
        cout << "Distance's constructor was called" << endl << endl;
    }
    
    double Distance::GetDis()//得到两点之间的距离
    {
        double x = fabs(p1.GetX() - p2.GetX());
        double y = fabs(p1.GetY() - p2.GetY());
        return sqrt(pow(x,2)+pow(y,2));
    }
    
    Distance::~Distance()//析构函数
    {
        cout << endl << "Distance's destructor was called" << endl;
    }
    
    int main()
    {
      Point myp1(1,1),myp2(4,5);
      Distance myd(myp1,myp2);
      cout << endl;
      cout << "the distance is:" << myd.GetDis() << endl;
    }
    
    
  • 相关阅读:
    修改oracle用户密码永不过期
    mysql中的union操作(整理)
    mysql条件查询and or使用实例及优先级介绍
    vue实现购物清单列表添加删除
    vue实现全选框效果
    vue实现穿梭框效果
    legend3---13、vue是真的好用
    黑马在线教育项目---34-37、webuploader实现用户头像的异步上传
    lareval重命名created_at和updated_at字段
    js的dom操作(整理)(转)
  • 原文地址:https://www.cnblogs.com/yuzilan/p/10626149.html
Copyright © 2011-2022 走看看