- , . .
:
( , -) ;
( ,-) .
:
class <> {
[ private:] < >
public: < >
};
private public - , . private , . . public .
. private public, .
:
, ( );
const ( );
static ( ), auto, extern register.
.
( ) ( , , ).
:
, (static) (extern) , , ; ;
;
;
, - .
: , . .
class monstr{
int health, ammo;
public:
monstr(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().
, .. private- .
, ( monstr, get_health, get_ammo). , .
( draw), :
void monstr::draw(int , int , int scale, int position){
/ * * /
|
|
}
inline ( , ):
inline int monstr::get_ammo(){
return ammo;
}
, . . , .
struct union .
, . ++:
monstr Vasia; // monstr .
monstr Super(200, 300); //
monstr stado[100]; //
monstr *beavis = new monstr (10); // (
// )
monstr &butthead = Vasia; //
, , , . . , .
. () -> :
int n = Vasia.get_ammo();
stado[5].draw;
cout << beavis->get_health();
public. private .
, . :
:
const ,
,
,
( ) .
class monstr{
int get_health() const {return health;}
};
const monstr Dead(0,0); //
cout << Dead.get_health();
, .
2.3
this
. , , . this, .
this . (return this;) (return *this;) .
: monstr public , health , , ).
monstr & the_best(monstr &){
|
|
if(health > .health) return *this;
return M;
}
monstr Vasia(50), Super(200);
monstr Best = Vasia.the_best(Super); // Best //
// health, // .. Super.
this , . :
void cure(int health, int ammo) {
this -> health += health; // this
monstr:: ammo += ammo; // ::
}
2.4
:
1. ( void),
2. ,
3. ( ).
4. , , .
5. , .
6. . .
7. , . . , , , .
8. .
9. const, virtual static.
10. main. , . (, ).
11. :
_ _ [( )]; // //
_ ( ); //
// ( )
_ _ = ; //
//
:
monstr Super(200, 300), Vasia(50), Z;
monstr X = monstr(l000);
monstr Y = 500;
. .
health 1000 ( ). , X.
(health = 500), , .
( monstr skin name).
enum color {red, green, blue}; //
class monstr{
int health, ammo;
color skin;
char *name;
public:
monstr(int he = 100, int am =10);
monstr(color sk);
monstr(char * nam);
int get_health(){return health;}
int get_ammo(){return ammo;}
};
//-----------------------------------------------
monstr::monstr(int he, int am){
health = he; ammo = am; skin = red; name = 0;
}
//-----------------------------------------------
monstr::monstr(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;
}
}
//-----------------------------------------------
monstr::monstr(char * nam){
name = new char [strlen(nam) + 1]; // 1
|
|
// -
strcpy(name, nam);
health = 100; ammo =10; skin = red;
}
//-----------------------------------------------
monstr * m = new monstr ("Ork");
monstr Green (green);
, .. . monstr , .
, ( ).
.
:
( )
, . , .
monstr::monstr(in the, int am);
health (he), ammo (am), skin (red), name (0){}
-, - -. , .
, . .
, :
T::T(const &) { / * */ }
.
, :
,
,
,
.
, . . , , , , , .
: monstr. name, , .
monstr::monstr(const monstr &){
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;
}
monstr Vasia (blue);
monstr Super = Vasia; //
monstr *m = new monstr ("Orc");
monstr Green = *m; //
- , - , . .
2.5
, , . ~ (), .
:
monstr::~monstr() {delete [ ] name;}
, .
, , - , -, . .
|
|
:
;
const static;
;
.
, :
, ;
main;
, delete ( ). .
:
monstr *m;
m -> ~monstr();
, new . .
2.6