⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rectangle.cpp

📁 《C++程序设计教程》电子教案及例题源码
💻 CPP
字号:
#include <iostream.h>
class Rect
{
private:
    int X1;
    int Y1;
    int X2;  //或长  int len;
    int Y2;  //或宽  int wid;
public:
	Rect(int x1, int y1, int x2, int y2);
	Rect(Rect &r);
	void SetRect(int x1, int y1, int x2, int y2);
	void ShowRect();
	int GetArea();
	int GetLength();
};

int Rect::GetArea()
{
	return (X2-X1)*(Y2-Y1);
}

int Rect::GetLength()
{
	return 2*(X2-X1) + 2*(Y2-Y1);
}

Rect::Rect(int x1, int y1, int x2, int y2)
{
	X1=x1;
	Y1=y1;
	X2=x2;
	Y2=y2;
}

void Rect::SetRect(int x1, int y1, int x2, int y2)
{
	X1=x1;
	Y1=y1;
	X2=x2;
	Y2=y2;
}

void Rect::ShowRect()
{
	cout << "(" << X1 << "," << Y1 << ")   (" << X2 << "," << Y2 << ")" << endl;
}

Rect::Rect(Rect &r)
{
	X1=r.X1;
	Y1=r.Y1;
	X2=r.X2;
	Y2=r.Y2;
}


void main(void)
{
	Rect r1(2,3,4,5);
	r1.ShowRect();
	cout << r1.GetArea() << endl;
	cout << r1.GetLength() << endl;

	r1.SetRect(100,200,300,400);
	r1.ShowRect();
	cout << r1.GetArea() << endl;
	cout << r1.GetLength() << endl;

	Rect r2(r1);
	r2.ShowRect();
	cout << r1.GetArea() << endl;
	cout << r1.GetLength() << endl;
}

⌨️ 快捷键说明

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