# 学童的EOP。

========================================================================================================================

========================================================================================================================

1 假设我们正在对坐标平面上的点做一些事情。

`double x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8, x9, y9, x10, y10;`

```double pointsX[10];
double pointsY[10];
```

`double points[10][2];`

```struct POINT
{
double x;
double y;
};

POINT points[10];
```

```POINT p1, p2;

double D = Distance( p1, p2 );

double Distance( const POINT& p1, const POINT& p2 )
{
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return sqrt( dx * dx + dy * dy );
}
```

2.多态性

```int iA = 1;
int iB = 3;

double dA = 4.5;
double dB = 3.14;

string sA = "abcd";
string sB = "efgh";

int iC = iA + iB;

double dC = dA + dB;

string sC = sA + sB;
```

OOP允许我们将这个动作扩展到其他数据。

```#include <matrix.mqh>

matrix< double > mA( 2, 2 );
mA[0][0] = 1.0;
mA[0][1] = 2.0;
mA[1][0] = 3.0;
mA[1][1] = 4.0;

matrix< double > mB( 2, 2 );
mB[0][0] = 5.0;
mB[0][1] = 6.0;
mB[1][0] = 7.0;
mB[1][1] = 8.0;

matrix< double > mC = mA + mB;
```

3.虚拟功能

```class Shape
{
public:
Shape();

virtual void Draw() = 0;
};

class Circle : public Shape
{
public:
Circle();

virtual void Draw();
};

class Rectangle : public Shape
{
public:
Rectangle();

virtual void Draw();
};

class Star : public Shape
{
public:
Star();

virtual void Draw();
};
```

`Shape* shapes[10];`

```void Init()
{
for( int i = 0; i < 10; i++ )
{
switch( i % 3 ){
case 0:
shapes[i] = new Circle();
break;
case 1:
shapes[i] = new Rectangle();
break;
case 2:
shapes[i] = new Star();
break;
}
}
}
```

```void OnPaint()
{
for( int i = 0; i < 10; i++ )
{
shapes[i].Draw();
}
}
```

```void OnDeinit()
{
for( int i = 0; i < 10; i++ ){
delete shapes[i];
}
}
```

[删除]
Dmitry Fedoseev:

4.封装。

```class ColorButton
{
color myColor;

public:
ColorButton( color clr ) : myColor( clr ){}

color GetColor() const
{
return myColor;
}

void SetColor( color clr )
{
myColor = clr;
Update();
}

void Update();
};
```

Koldun Zloy:

4.封装。

Roman:

Dmitry Fedoseev: