qq_45735316 2020-03-25 17:23 采纳率: 94.1%
浏览 317
已采纳

为什么无法调用构造函数?代码应该怎样修改?

图片说明
/*5、编写C++程序,实现如下功能:
a.定义一个类,Point, 具有x, y轴坐标两个私有数据成员(float类型);
b.具有获取和设置x,y值的公有函数;写出两种构造函数;其原型为:
Point();//此时初始化x,y为0
Point(float xx,float yy);
c.具有计算与另一个类对象p1之间距离的公有函数:float calculate_distance(Point p1);
d. 在主函数中,提示输入p1, p2两点的x,y坐标,并计算两点的距离;
e.在main函数中测试你所设计的所有成员函数。*/

#include <iostream>
#include <math.h>
using namespace std; 

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class Point{
    public:
        Point(){
            x=0;
            y=0;
        }                  //无参数的构造函数 
        Point(float xx,float yy);  //有参数的构造函数 
        float calculate_distance(Point p1);
    private:
        float x,y;
};
Point::Point(float xx,float yy){
            x=xx;
            y=yy;
            cout<<"("<<x<<","<<y<<")"<<endl;
}
float Point::calculate_distance(Point p1){
            return (float)sqrt((x-p1.x)*(x-p1.x)+(y-p1.y)*(y-p1.y));

}
int main(int argc, char** argv) {
    Point p,p1;     //定义对象p、p1 
    float x,y;      //对象p的私有数据成员 
    float x1,y1;    //对象p1的私有对象成员
    float d1=0,d2=0;
    cin>>x>>y;
    cin>>x1>>y1;
    Point p;         //调用无参数的构造函数 
    Point p(x,y);    //调用有参数的构造函数 
    p1.Point(x1,y1);
    d1=p.calculate_distance(p1);
    d2=p1.calculate_distance(p);
    cout<<"两点间距离为"<<d1<<endl;
    cout<<"两点间距离为"<<d2<<endl;
    return 0;
}
  • 写回答

3条回答 默认 最新

  • threenewbee 2020-03-25 17:27
    关注

    34行已经有Point p, p1;了
    40行又有Point p;
    导致重复定义。
    41行又定义了Point p(x, y);
    可以改名:
    40行
    Point p2;
    41行
    Point p3(x,y);
    下面调用相应修改

    #include <iostream>
    #include <math.h>
    using namespace std; 
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    class Point{
        public:
            Point(){
                x=0;
                y=0;
            }                  //无参数的构造函数 
            Point(float xx,float yy);  //有参数的构造函数 
            float calculate_distance(Point p1);
        private:
            float x,y;
    };
    Point::Point(float xx,float yy){
                x=xx;
                y=yy;
                cout<<"("<<x<<","<<y<<")"<<endl;
    }
    float Point::calculate_distance(Point p1){
                return (float)sqrt((x-p1.x)*(x-p1.x)+(y-p1.y)*(y-p1.y));
    
    }
    int main(int argc, char** argv) {
        Point p,p1;     //定义对象p、p1 
        float x,y;      //对象p的私有数据成员 
        float x1,y1;    //对象p1的私有对象成员
        float d1=0,d2=0;
        cin>>x>>y;
        cin>>x1>>y1;
        Point p2;         //调用无参数的构造函数 
        Point p3(x,y);    //调用有参数的构造函数 
        p1 = Point(x1,y1);
        d1=p.calculate_distance(p1);
        d2=p1.calculate_distance(p);
        cout<<"两点间距离为"<<d1<<endl;
        cout<<"两点间距离为"<<d2<<endl;
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置