电脑网络

设有下面关于点Point类的定义,在此基础上派生出一个矩形Rectangle类

346 浏览

设有下面关于点Point类的定义,在此基础上派生出一个矩形Rectangle类

1 个回答

热心网友888用户头像
热心网友888 回答于 2024-07-20
已采纳

提问:设有下面关于点Point类的定义,在此基础上派生出一个矩形Rectangle类

1.设有下面关于点Point类的定义,请在此基础上派生出一个矩形Rectangle类,用以描述矩形的左上角的顶点和右下角的顶点,并能够计算矩形的面积,并给出测试程序。#include<iostream>using namespace std;class Point{public:Point() { x = 0; y = 0; }Point(int a, int b){x = a; y = b;}int GetY(){return y;}int GetX(){return x;}void SetX(int a) { x = a; }void SetY(int b) { y= b; }private:int x;int y;};class Rectangle :public Point{public:int a;int b;Rectangle(int x1, int y1, int m,int n):Point (a,b){chang= m;kuan = n;}int area();private:int chang;int kuan;};int Rectangle::area(){return chang* kuan;}int main(){int a, b, m, n;cout << "请输入矩形左上顶角的横纵坐标:" << endl;cin >> a >> b;cout << "请输入矩形的长:" << endl;cin >> m;cout << "请输入矩形的宽:" << endl;cin >> n;Rectangle s(a, b, m, n);cout << "The Location:(" << s.GetX() << "," << s.GetY()<< ")"<<endl;cout << "The area:" << s.area() << endl;return 0;}调试出来后,输出坐标那里有错误,求大佬帮忙解答

网友回答:

你的Rectangle类既然是基于Point的,它就不能有自己的起始点a,b了

它可用的构造为

  Rectangle(int x, int y, int m,int n):Point (x,y)

        {

            chang= m;

            kuan = n;

        }

且不能再有a,b了

另外。你的程序完全不符合要求

要求是:

用以描述矩形的左上角的顶点和右下角的顶点

而你却给了长/宽


我来回答

相关问题