1
:
1.1.
, .
:
o ;
o ( );
o ().
(). .
. .
, , . , . . , , , .
(class) () .
(object) , . , () ().
, , , . , . . , . . , , . : .
(methods) , .
(message) , . " " - . "" : , . ,
. , .
class CWorld
{
}
int main ()
{
CWorld world;
}
1
, C/C++. , :
|
|
( .obj).
( , , ), .
( , , , ..). , -, .
C/C++ .obj. ( Intel .) , , . , : ( , , ) ( , ). , , .
, , (.. ), . .
C/C++ , . , , , , . C/C++ , . , , .
( .h .hpp) (. .) #include, , :
#include <stdlib.h>
include "myfile.h"
#include ; , .
. , , . , , , .
- . . . , , . .
|
|
++ , , . , .cpp. , , . ( .hpp .h) , , , . . .hpp, .cpp. , , , (, , #ifndef - #define, ).
. CWorld .
// CWorld.hpp
#ifndef CWORLD_HPP
#define CWORLD_HPP
#include "CIObject.hpp"
#include "CPlayer.hpp"
#define MAXOBJECTS 20
class CWorld
{
private:
unsigned long time, //
btime; //
protected:
CIObject *objects[MAXOBJECTS];
int objectsn;
CPlayer player;
public:
CWorld();
~CWorld();
};
#endif
2 CWorld
// CWorld.cpp
#ifndef CWORLD_CPP
#define CWORLD_CPP
#include "CIObject.hpp"
#include "CPlayer.hpp"
#include "CWorld.hpp"
CWorld::CWorld(): player(200,300, 0, 0) //
{
objectsn=0;
time = 0;
btime = 0;
}
CWorld::~CWorld()
{
delete [] objects;
}
}
#endif
3 CWorld
. , , :
class World
{
unsigned long objectsn=0, //
time = 0; //
};
4
, .
, , , objectsn time, . ++ . (construct - ) - - , .
class World
{
public:
CWorld(); // CWorld
};
CWorld::CWorld()
{
time = 0;
objectsn = 0;
}
5- CWorld
, . return. , . - .
. - . CWorld . time objectsn . , CWorld, , "".
, . . , , - , .
|
|
( ), , :
CWorld world;
6- CWorld
, .
, , my_Time, world, CWorld,
CWorld world();
7- CWorld
.
, . .
class CObject
{
float x;
float y;
CObject(float a, float b);
};
Object::CObject(a,b)
{
x = a;
y = b;
}
8- CWorld
, . (destruct - ) - - . (~) .
~Word()
{ //
}
9- CWorld
- , . , . , , .
. , - . .
, , , , , .
:
public - ;
protected - ( public protected);
private - , .
class CPlayer: public CIObject
{
private: //
float ax; //
float dx; //
long atime; //
protected: //
void accelerate(float d);
public: //
CPlayer(){};
~CPlayer(){};
void draw(); //
void interact(unsigned long t);
};
10-
:
private , , . , ( -). , .
protected , -, . , , , private.
|
|
public , , , . , , , , . , , . , .
, . , -.
:
public ;
protected public- protected;
private private.
class CPlayer: public CIObject
{
}
11- public-.
- , , .
, :
_ _;
- , , .
, , , , . , ;.
, , , .
:
_ _ (_);
:
_ _ (_)
{ _ }
, .
class CWorld
{
private: //
unsigned long time, //
btime; //
protected:
CIObject *objects[MAXOBJECTS]; // -
int objectsn; //
CPlayer player; // - CPlayer
public:
CWorld(); //
~CWorld(); //
void render(); //
void addObject(CIObject *o); //
void deleteObject(int i); //
void interact(unsigned long t); //
};
12-
inline. , , .
inline void Fx(void) {
std::cout<<" Fx"<<std::endl;}
...
Fx(void);
//
13
1.2
( ). , ( ) , . , .
1. , .
2.
3. ().
4. , .
5. .
6. . ( - ).
7.
8. -, .
9. -, .
10.
11.
12.
13. , - ,
|
|
1.3.
1) .
2) , , .
3) .
4) .
5) .
1.4.
1) .
2) UML .
3) .
4) .
5)
1.5.
2
:
, , , . , . , . , . , , . - .
- virtual. , , , . , virtual .
, , .
class CObject
{
protected:
float x,y;
public:
CObject() {x=0; y=0;};
CObject(float x,float y){this->x=x; this->y=y;}
~CObject() {};
virtual void draw() = 0;
};
14
(pure) , , , . , , , , . , , .
. CWorld CIObject. CIObject draw(), . CIObject. , - CIObject, , CCircle, draw() .
class CCircle: public CIObject
{
public:
CCircle() {x=0;y=0;};
~CCircle() {};
virtual void draw(); //
}
void CCircle::draw()
{
setcolor(14);
circle(x,y,10);
}
15 - draw()
void CWorld::render()
{
for (int i=0;i<objectsn; i++)
objects[i]->draw();
player.draw();
}
16 - draw()
, - .
, , , . ? : . , . . , , .
++ , - (friend). - . .
, , friend. , , .
class Player
{
friend int F(const CPlayer &player)
}
17 -
- - this , . , Player F , , , player.F(), player - CPlayer. F (& player).
, CPlayer CCircle. , . :
int intersect(CPlayer &p, CCircle &o)
{
return fabs(o.x-p.x) < 20 && fabs(o.y-p.y) < 20;
}
18 , CPlayer CCircle
, CPlayer CCircle, friend
class Player
{
friend int intersect(CPlayer &, CCircle &);
}
class Circle
{
friend int intersect(CPlayer &, CCircle &);
}
19 , CPlayer CCircle