rectangle.cpp
来自「《C++程序设计教程》电子教案及例题源码」· C++ 代码 · 共 75 行
CPP
75 行
#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 + =
减小字号Ctrl + -
显示快捷键?