5.9-实现完整类.cpp

来自「Visual C++的课件」· C++ 代码 · 共 45 行

CPP
45
字号
// Begin rect.cpp
#include "rect.hpp"
Rectangle::Rectangle(int top, int left, int bottom, int right)
{
	itsTop = top;
	itsLeft = left;
	itsBottom = bottom;
	itsRight = right;

	itsUpperLeft.SetX(left);
	itsUpperLeft.SetY(top);

	itsUpperRight.SetX(right);
	itsUpperRight.SetY(top);

	itsLowerLeft.SetX(left);
	itsLowerLeft.SetY(bottom);

	itsLowerRight.SetX(right);
	itsLowerRight.SetY(bottom);
}


// compute area of the rectangle by finding cornerssides,
// establish width and height and then multiply
int Rectangle::GetArea() const
{
	int Width = itsRight-itsLeft;
	int Height = itsTop - itsBottom;
	return (Width * Height);
}

int main()
{
	//initialize a local Rectangle variable
	Rectangle MyRectangle (100, 20, 50, 80 );

	int Area = MyRectangle.GetArea();

	cout << "Area: " << Area << "\n";
	cout << "Upper Left X Coordinate: ";
	cout << MyRectangle.GetUpperLeft().GetX();
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?