qq_45735316 2020-03-27 10:29 采纳率: 94.1%
浏览 156
已采纳

(1,0)是怎么来的?我只调用了一次Point类的无参数的构造函数啊

图片说明图片说明

#include <iostream>
#include <math.h>
using namespace std;
/*与7.3相比较 为什么Point类的无参数构造函数都无法调用?
应该在CRect的构造函数里面写些什么?
有没有一种可能能在CRect的构造函数中调用CRect的成员函数?*/ 

class Point{           //定义一个点类Point 
    public: 
       /* Point()
        {
            x=0;
            y=0;
            cout<<"calling the constructor"<<endl; 
            cout<<"(x,y)为"<<"("<<x<<","<<y<")"; 
        }*/
        //简化上述代码 
        Point()
        {
            Point(0,0);
            cout<<"calling the constructor"<<endl; 
            cout<<"(x,y)为"<<"("<<x<<","<<y<<")"<<endl; 
         } 
        Point(int xx,int yy):x(xx),y(yy)      //Point类的构造函数 
        {
        /*  x=xx;
            y=yy;合成到构造函数的声明里面*/ 
            cout<<"calling the constructor"<<endl; 
            cout<<"(x,y)为"<<"("<<x<<","<<y<<")"<<endl; 
        }
        int getX(){
            return x;
        }
        int getY(){
            return y;
        }
    private:
        int x,y;
};
class CRect{           //定义一个CRect类 
    public:
        CRect();                                  //无参数的构造函数
        CRect(Point p,Point p1);                              //有参数的构造函数 
        int RectHeight();       //得到矩形的高
        int RectWidth();        //得到矩形的宽
        int RectArea();         //得到矩形的面积
    private:
        Point p,p1;  
};
CRect::CRect(Point pp,Point pp1):p(pp),p1(pp1)
{
    cout<<"calling the constructor"<<endl; 

}
int CRect::RectHeight()
{
    int H=0;
    H=abs(p.getY()-p1.getY());
    return H;
}
int CRect::RectWidth()
{
    int W=0;
    W=abs(p.getX()-p1.getX());
    return W;
}
int CRect::RectArea()
{
    int A=0;
    A=abs((p.getY()-p1.getY())*(p.getX()-p1.getX()));
    return A;
}
int main(int argc, char** argv) {
    int x,y;
    int x1,y1;
    cin>>x>>y;
    cin>>x1>>y1;
    Point a;            //?????? 
    /*因为
Point()
和
Point(int x=0,int y=0)
两个混淆了。
编译器遇到
Point p不知道是调用p()好,还是p(x=0,y=0)好。*/
    Point p(x,y);       //调用Point的构造函数
    Point p1(x1,y1);

    CRect c(p,p1);      //调用CRect的构造函数 
    int H=0,W=0,A=0;
    H=c.RectHeight();
    cout<<"矩形的高为"<<H<<endl; 
    W=c.RectWidth();
    cout<<"矩形的宽为"<<W<<endl;
    A=c.RectArea();
    cout<<"矩形的面积为"<<A<<endl;
    return 0;
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-03-27 10:36
    关注

    图片说明
    图片说明

    此时x y还没有完成初始化

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 10月22日

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图