qq_45735316 2020-03-26 20:28 采纳率: 94.1%
浏览 293
已采纳

为什么Point类的无参数构造函数无法调用?还有根据题意我应该在CRect的构造函数里面写些什么?有没有一种可能能在CRect的构造函数中调用CRect的成员函数?

图片说明
/*7.定义一个点类Point,包括x坐标和y坐标(int)。
定义一个CRect类,代表一个矩形,
要求CRect类中有代表矩形的左上角坐标(x1,y1)
和右下角坐标(x2,y2)点类的对象,
要求CRect类中有两个成员函数RectHeight()和RectWidth(),
通过这两个函数能得到矩形的高和宽;
此外还需要有求此矩形面积的成员函数。
要求每个类至少有两个以上构造函数,
在main函数中测试他门的每一个成员函数。
注:只考虑四条边和横纵轴垂直平行的矩形。*/

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

class Point{           //定义一个点类Point 
    public: 
        Point()
        {
            x=0;
            y=0;
            cout<<"calling the constructor"<<endl; 
            cout<<"(x,y)为"<<"("<<x<<","<<y<")"; 
        }
        Point(int xx=0,int yy=0)      //Point类的构造函数 
        {
            x=xx;
            y=yy;
            cout<<"calling the constructor"<<endl; 
            cout<<"(x,y)为"<<"("<<x<<","<<y<")"; 
        }
        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 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-26 20:52
    关注

    因为
    Point()

    Point(int x=0,int y=0)
    两个混淆了。
    编译器遇到
    Point p不知道是调用p()好,还是p(x=0,y=0)好。

    #include <iostream>
    #include <math.h>
    using namespace std;
    /*与7.3相比较 为什么Point类的无参数构造函数无法调用?
    应该在CRect的构造函数里面写些什么?*/ 
    
    class Point{           //定义一个点类Point 
        public: 
            Point()
            {
                x=0;
                y=0;
                cout<<"calling the constructor"<<endl; 
                cout<<"(x,y)为"<<"("<<x<<","<<y<")"; 
            }
            Point(int xx,int yy)      //Point类的构造函数 
            {
                x=xx;
                y=yy;
                cout<<"calling the constructor"<<endl; 
                cout<<"(x,y)为"<<"("<<x<<","<<y<")"; 
            }
            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 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;
    }
    
    

    或者这样写

    class Point{           //定义一个点类Point 
        public: 
            Point(int xx,int yy):x(xx), y(yy)      //Point类的构造函数 
            {
                cout<<"calling the constructor"<<endl; 
                cout<<"(x,y)为"<<"("<<x<<","<<y<")"; 
            }
    
            Point()
            {
                Point(0,0);
                cout<<"calling the constructor"<<endl; 
                cout<<"(x,y)为"<<"("<<x<<","<<y<")"; 
            }
    
            int getX(){
                return x;
            }
            int getY(){
                return y;
            }
        private:
            int x,y;
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码