矩形类继承实现

矩形类继承实现,第1张

矩形类继承实现 矩形类继承实现_C++
// 
题目描述
1、定义一个Point类:
    包含:两个私有成员:m_x, m_y;
    包含:三个成员函数:Move(float xOff, float yOff),GetX(),GetY()
  
2、定义一个Rectangle继承自Point类
   包含:两个私有成员 m_width, m_height;
   包含:三个成员函数:GetHeight()、GetWidth()、printInfo()
   
要求:通过构造函数实现初始化。
输入:矩形个数。如 5
输出:矩形信息。举例如下:


X: 5,Y: 5, GetWidth: 20, GetHeight: 10
X: 6,Y: 6, GetWidth: 21, GetHeight: 11
X: 7,Y: 7, GetWidth: 22, GetHeight: 12
X: 8,Y: 8, GetWidth: 23, GetHeight: 13
X: 9,Y: 9, GetWidth: 24, GetHeight: 14

注意:X,Y width,height的初始值分别为:2,3, 20,10
后面值的变化是根据循环变量i递增的。


需要先执行 Move 函数使得X,Y变成:2+3, 3+2


输入
5

输出
X: 5,Y: 5, GetWidth: 20, GetHeight: 10
X: 6,Y: 6, GetWidth: 21, GetHeight: 11
X: 7,Y: 7, GetWidth: 22, GetHeight: 12
X: 8,Y: 8, GetWidth: 23, GetHeight: 13
X: 9,Y: 9, GetWidth: 24, GetHeight: 14
样例输入 Copy
10
样例输出 Copy
X: 5,Y: 5, GetWidth: 20, GetHeight: 10
X: 6,Y: 6, GetWidth: 21, GetHeight: 11
X: 7,Y: 7, GetWidth: 22, GetHeight: 12
X: 8,Y: 8, GetWidth: 23, GetHeight: 13
X: 9,Y: 9, GetWidth: 24, GetHeight: 14
X: 10,Y: 10, GetWidth: 25, GetHeight: 15
X: 11,Y: 11, GetWidth: 26, GetHeight: 16
X: 12,Y: 12, GetWidth: 27, GetHeight: 17
X: 13,Y: 13, GetWidth: 28, GetHeight: 18
X: 14,Y: 14, GetWidth: 29, GetHeight: 19

C++代码:

// 
#include
#include
using namespace std;

class Point {
public:
	Point() {}
	Point(float X, float Y) {
		m_x = X;
		m_y = Y;
	}
	void Move(float xOff, float yOff) {
		m_x = m_x + xOff;
		m_y = m_y + yOff;
	}
	float  GetX() {
		return m_x;
	}
	float GetY() {
		return m_y;
	};
private:
	float m_x;
	float m_y;
};

class Rectangle :public Point {
public:
	Rectangle(float X, float Y, int wi, int he) :Point(X, Y) {
		m_width = wi;
		m_height = he;
	}
	int  GetHeight() {
		return m_height;
	};
	int  GetWidth() {
		return m_width;
	};
	void printInfo(int countm) {
		Move(3, 2);
		for (int i = 0; i < countm; i++) {
			cout << "X: " << GetX() + i << ",Y: " << GetY() + i << ", GetWidth: " << GetWidth() + i << ", GetHeight: " << GetHeight() + i << endl;
		}
	};
private:
	int m_width;
	int m_height;
};


int main() {
	int count = 0;
	cin >> count;
	Rectangle obj(2, 3, 20, 10);

	obj.printInfo(count);
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5502279.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存