.


:




:

































 

 

 

 





 

3

1. 5

1.1. 5

1.2. 8

1.3. 10

1.4. 14

1.5. this 16

1.6. 17

1.7. 22

1.8. 24

1.9. 26

1.10. 28

1.11. 32

2. 34

2.1. 35

2.2. 36

2.3. 37

2.4. 39

2.5. 44

45

46

 

, (, ) . - ( - ) ( , ).

, : , . . , -, - , . , .

, , ( , ) , ( - ). - . .

, , . ( 1967 ), : , , ., . , Smalltalk. - .

( ), - , . , C. - . , - Qt, C++.

 

. , - , , , . , .

, , . .

, , . , .

- , , . , , , .

. , . , - . , , .

, . , , .

. . - .

- , , . , , .

, . - ( ) . , (, , , , , ).

, . . , , .

- (). Simula-67 Smalltalk, - . ++ , .

( , ), .

"" , . , . - , . . , "- ", .

 

, .

(encapsulation). : , . , , (, ).

, , .

- , , . , . - - , .

, , - . ++ . , - .

, , - .

++ . , . - , , . .

, , . , - .

. , - . , , , .

 

- . , . , - : , , . . ++ , ++.

( ), - . . :

class <>{

[ private: ]

< >

public:

< >

}; //

private public . , private, . . public. . private public, .

:

1. , , (.. , , );

2. ( const), ( ) ;

3. static, auto, extern register.

( ) ( , , ). .

:

  • ;
  • , (static) (extern) , ;
  • ;
  • (inline);
  • , - .

, . (, , ) . , , .

class monster

{

int health, ammo;

public:

monster(int he = 100, int am = 10)

{ health = he; ammo = am;}

void draw(int x, int y, int scale, int position);

int get_health(){return health;}

int get_ammo(){return ammo;}};

- health ammo, get_health() get_ammo(). , , , . , , .

. , , .

( draw). , (inline). , . () , :

void monster::draw(int x, int y, int scale, int position)

{ /* */}

inline ( , ):

inline int monster::get_ammo()

{return ammo;}

( ), , ( ).

, . . . , . struct union .

 

"" , . ++:

monster Vasia; // monster

monster Super(200, 300);//

monster stado[100]; //

/* ( ) */

monster *beavis = new monster (10);

monster &butthead = Vasia; //

, , , . . , .

(public) . . () -> :

.

->

(*).

.()

-> ()

(*).()

:

_[ ].

_[ ].()

:

int n = Vasia.get_ammo();

stado[5].draw;

cout << beavis->get_health();

private .

, . :

class monster{

...

int get_health() const {return health;}

};

const monster Dead (0,0); //

cout << Dead.get_health();

:

  • const ;
  • ;
  • ;
  • ( ) .

, .

 

this

. . , "", .

, , : , . ++ this. , (, this -> num).

*this . , (return *this;).

this monster , ( health) , , ( public ):

monster & the_best(monster &M)

{

if(health > M.get_health())

return *this;

return M;

}...

monster Vasia(50), Super(200);

// Best Super

monster Best = Vasia.the_best(Super);

. .

  • , void. .
  • ( ).
  • , , .
  • , . . .
  • , ( , , ). .
  • .
  • , ( const, virtual static).
  • main. , . (, ).

. , - , - (.. , ).

. :

  • ;
  • ;
  • ;
  • ;
  • ;
  • .

:

monster Super(200, 300), Vasia(50);

monster X = monster(1000);

, health = 1000 ( ).

.

monster, , (skin) (name):

enum color {red, green, blue}; //

class monster

{

int health, ammo;

color skin;

char *name;

public:

monster(int he = 100, int am = 10);

monster(color sk);

monster(char * nam);

...

};

//--------------------------------

monster::monster(int he, int am)

{ health = he; ammo = am; skin = red; name = 0;}

//--------------------------------

monster::monster(color sk)

{

switch (sk)

{

case red: health = 100; ammo = 10; skin = red; name = 0; break;

case green: health = 100;ammo = 20;skin = green;name = 0;break;

case blue: health = 100; ammo = 40; skin = blue;name = 0;break;

}

}

//--------------------------------

monster::monster(char * nam)

{

/* 1 - */

name = new char [strlen(nam) + 1];

strcpy(name, nam);

health = 100; ammo = 10; skin = red;

}

//--------------------------------

monster * m = new monster ("Ork");

monster Green (green);

, . monster , . , - .

- , :

monster::monster(int he, int am):

health (he), ammo (am), skin (red), name (0){}

. , . -, - -. , .

- , :

T::T(const T&) {... /* */ }

T - . , :

  • ;
  • ;
  • .

, . . , , , , , .

monster. name, , :

monster::monster(const monster &M)

{

if (M.name)

{

name = new char [strlen(M.name) + 1];

strcpy(name, M.name);

}

else name = 0;

health = M.health; ammo = M.ammo; skin = M.skin;

}

...

monster Vasia (blue);

monster Super = Vasia; //

monster *m = new monster ("Ork");

monster Green = *m; //





:


: 2016-12-05; !; : 559 |


:

:

, .
==> ...

1711 - | 1492 -


© 2015-2024 lektsii.org - -

: 0.094 .