Вы находитесь на странице: 1из 58

.

++.

++, e ..................................................................................2
..........................................................................................................................................2
..............................................................................2
. ...............................................................................3
, .........5
. ..................................................................................5
const volatile ++. ...............................................................................................6
. ...................................................................................................................................................6
.....................................................................................................................10
. .........................................................................11
. ...........................................................................................................................13
...............................................................................................................................15
........................................................................................................................16
- ..................................................................18
. . .......................................................................................................................18
..................................................................................................................22
. ......................................................................................................................................24
...................................................................................................25
. ........................................................31
.................................................................................................................................39
. . ......................................................................................................40
.....................................................................................................40
......................................................................................................42
. ............................................................................................................45
. ...........................................................................................48
..................................................................................48
. ......................................................................................................................................49
-.............................................................................................................49
..................................................................................................................54
. ...........................................................................................................................55
-. ..............................................................................................56

++.

1
++, e .
++ (). , , .
, , - ++,
.

++ : ,
, , // .
//
/* ,
*/
/* - /* */- */
/* - // ! -*/
// - /* !*/
, :
= //* */ z;
, :
= //* */ z;

.
, . ++ ( ) .
1. .
// '' ++
#include <stdio.h>
int main()
{
// ++ ""
// for
for (int counterl=0; counterl<3; counter1++)
// counter1 "", main
// ( for!)
// 0 .
{
// i 0
// ,
int i=0;
// j !
static int j=0;
for (int counter2=0; counter2<10; counter2++ )
printf("i=%d j=%d\n", i++, j++ ) ;
}
// counter2 "" .
char quit_message [ ] = " Bye !" ;
2

++.

printf ( "%s", quit_message) ;


return 0 ;
}
, 1:
int i=0;
:
int i ;
i=0;
0 .
, , .
static int j=0;

static int j; j =0 ;
, j 0 . . : , , 1
static int j=0;

static int j=j+i;


// !
. Borland C++ 3.0, , j , . . :
int k;
k=int i=0; // !
print f ( " %d" , int j=i;); // !
for (int count=0; count<max_count; count++)
{
// ..
}
// ! count
for (int count=0; count<max_count; count++)
{
// ...
}

. .
. ,
, (definition) . ++ : , , ,
, , (declaration)
, ,
++, . .
, #include. 2:
mod1.cpp mod2.cpp , header.h , mod2.cpp.
3

++.

2. .
// header, h ( ++ 'h')
void func1(int, double*);
// void , ;
// : void func1 (int j, double* x_ptr);
// , .
Double func2() ;
// ! ,
// , ++
// (void).
Void func3(int, ...);
// - int, .
// mod1.cpp
#include "header.h"
...
int main()
{
int i;
double x[20] , y;
func1(i, x) ;
y=func2() ;
func3(i, x, func2);
}
// mod2.cpp
// , modl.cpp. ,
// "-".
// : ++ int f(i, j) int i,j;
// { ), - .
Void func1(int i, double *x)
{
// ...
}
double func2 ()
{
...
}
void func3(int i, ...)
{
...
}
, . , DrawCircle,
, :
void DrawCircle(int x=100, int =100, int radius=100);
:
void DrawCircle(int=100, int=100, int=100);
, -, .
, , :
4

++.

DrawCircle ();
// (100, 100) 100 DrawCircle(200) ;
// (200, 100), 100
DrawCircle (200, 300);
// (200, 300), 100
DrawCircle (200, 300, 400); // (200, 300), 400
DrawCircle (, ,400);
// ! !
,
. , , :
void DrawCircle(int x, int y=200,
int radius);
// !
:
void func (double*=0) ; // ! *= !
void func (double* =0); // .

,
.
:: (scope resolution operator )
, :
int i=0;
// i
...
int f()
{
int i=0;
// i f
i++;
// i
i++;
// i
...
}

.
, ++ (. 3).
, ,
, ( ).
, .
3. .
Int , b;
Typedef char* PChar;
Typedef void* PVoid;
Typedef int* Pint;
PChar c_ptr, d_ptr;
PVoid v_ptr;
// . . .
a=(int*)v_ptr;
//
b=PInt (v_ptr);
//
c_ptr= (PChar) v_ptr; //
d_ptr=PChar (v_ptr); //
v_ptr=PVoid(PChar(v_ptr)+1); //
v_ptr=(void*)((char*)v_ptr+1);
5

++.

const volatile ++.


const, , . , ,
. , ++ , const, , .
4 , , pi mod2.cpp .
++ const , ( , ),
, , #define, . .
, . const
# define, , .
4. const .
// .
// mod1.cpp
const float pi=3.14159;
// .
// mod2.cpp
#include <stdio.h>
extern float pi;
int main()
{
printf(%f', pi );
return 0;
}
volatile, , ,
- , .
, ,
, ( , , ).
const volatile ++ -,
.

.
(by reference), (by value).
, , ,
. : , , , .
;
-
, .
++ ,
, . 5
swap, : swap1 swap2 (, , )
, swap3 swap4 .
6

++.

5. .
#include <stdio.h>
void swapl(int x, int )
{
printf(" swap1 x=%d, y=%d\n", x, );
int z=y;
y=x;
x=z;
printf(" swap1 x=%d, y=%d\n", x, y);
void swap2(int *x, int. *y)
{
printf(" swap2 *=%d, *y=%d\n", *, *);
int z=*y;
*y=*x;
*x=z;
printf (" swap2 *x=%d, *y=%d\n", *x, *);
void swap3(int& x, int& y)
// & x , ,
// swap3 , swap3
// , swap3 swap1!
// : type & " type".
{
print f (" swap3 x=%d, y=%d\n", x, y);
int z=y;
=;
x=z ;
printf (" swap3 x=%d, y=%d\n", x, y);
}
void swap4 (double &x, double &y)
{
printf(" swap4 x=%f, y=%f\n", x, y);
int z=y;
y=x;
x=z;
print f (" swap4 x=%f, y=%f\n", x, );
}
int main()
{
int a=0, b=1;
print f (" a=%d, b=%d\n", a, b);
swap1(a, b) ;
printf("noce swapl a=%d, b=%d\n", a, b);
swap2(&a, &b) ;
printf (" swap2 a=%d, b=%d\n", a, b);
swap3(a, b) ;
printf (" swap3 a=%d, b=%d\n", a, b);
swap4(a, b) ;
printf (" swap4 a=%d, b=%d\n", a, b);
return 0 ;
}
7

++.

, , ,
, ,
swap2 swap3, . , ,
swap4. ,
, swap4, ,
. ,
Borland C++ 3.0, :
Warning 2. 52: Temporary used for parameter 'x' in call to 'swap4(double &, double &)' in
function main()
Warning 2. 52: Temporary used for parameter 'y' in call to 'swap4(double &, double &)' in
function main()
,
. , , (,
, !), . . , f int, ch char.
...
void f(int&); // f
...
char ch;
f(ch) ;
...
f ch , , :
int Tmp=(int)ch, f(Tmp);
, f Tmp ( !); ch .
, ,
. , , :
f(6) ;
:
int Tmp=6, f(Tmp) ;
,
,
, .
,
, const:
void f( const LargeStructure&; nonmodif_par) { ...}
, f , nonmodif_par, . , f1(f2(f3(object))), , .
6 . ,
, & , , .
? - ?
. , , , ( ).

++.

6. .
#include <stdio.h>
#include <string.h>
struct Student
{
char FirstName[20]; //
char SecondName[20] //
char Surname[20];
//
int Age;
//
char Dept[20] ;
//
int Year;
//
};
// . ,
// Student, (!)
// .
Student& PrintStudentInf(Student& st)
{
printf("Firstname %s\n"
"Secondname %s\n"
"Surname %s\n"
"Age %d\n"
"Department %s\n"
"Year %d\n"
* * * * * * * * * * * * * * * * * *\n,
st.FirstName,
st.SecondName,
st.Surname,
st.Age,
st.Dept,
st.Year) ;
return st;
}
//
Student& ShortenNames(Student& st)
( st.SecondName[1]=st.FirstName[1]='.' ;
st.SecondName[2]=st.FirstName[2]='\x0' ;
return st;
//
StudentSe Capitalize(Student& st)
{
strupr(st .Surname) ; // string.h
return st ;
}
int main()
{
// B ++ struct
// .
Student stl={ "Ivan",
"Ivanovitch",
9

++.

"Sidorov",
24,
"Chemistry",
3
};
Student st2={
"Sidor",
"Sidorovitch",
"Ivanov",
26,
"Math",
5
}
PrintStudentInfo(st1) ;
Student st1_mod=Capitalize(ShortenNames(st1)) ;
// :
// ShortenNames(Capitalize(st1)) ;
PrintStudentInfo(st1_mod)=st2 ; // ""
// - , ++ : PrintStudentInfo
// st1_m, ae
// , ( st1_mod).
PrintStudentInfo (stl_mod) ; // st1_mod , st2
Return 0 ;
}
,
(aliases) .
int =1;
int &r=; //
xr=2 ;
// , =2,
xr++;
// , ++
,
int x=l;
char &xr=x; // ,
// char, ,
// (char)x, . . '\1'.
r=2;
// !
++ ,
.

.

. , ,
.
, , 7.
- , MacrosCube(a++) (++)*(++)*(++),
.

10

++.

7.
.
#include <stdio.h>
int FunctionCube(int )
{ return x*x*x; }
#define MacrosCube() (()*()*())
int main()
{
int a=2 ;
printf(" FunctionCube(a++)=%d, ",
FunctionCube(a++));
printf ("a=%d\n", a) ;
a=2;
printf ("MacrosCube(a++)=%d, ", MacrosCube(a++));
printf("a=%d\n", a);
return 0;
}
++? inline ,
( register!), , , . , :
...
inline int InlineFunctionCube(int )
{
return x*x*x;
}
...
b=InlineFunctionCube(a) ;
c=InlineFunctionCube(a++) ;
...
, ! , , inline, . ,
if, case, for, while, goto. , inline,
, (static), ,
, .
,
. , , , , ,
.

.
,
++ new delete,
malloc, calloc free.
(. 8).

11

++.

8. new delete.
#include <stdio.h>
int main()
{
int *i_ptr;
double *d_ptr;
char *string;
int str_len=80;
i_ptr=new int; // int i_ptr
// . delete ,
// , .
// *i_ptr !
d_ptr=new double (3.1415); // , *d_ptr //
3.1415
string=new char [str_len] ; //
// str_len char. string .
// , new NULL, . . (void*)0
if (!(i_ptr && d_ptr && string))
{
printf("He "
" !");
return 1;
}
string[0]=''; string[1]='i';
string[2]='!'; string[3]='\x0' ;
printf("i_ptr=%p *i_ptr=%d\n", i_ptr, *i_ptr''):
delete i_ptr;
// , i_ptr. :
// , delete, ,
// new, NULL,
// !!!
printf("d_ptr=%p *d_ptr=%f\n", d_ptr, *d_ptr);
delete d_ptr;
print f("string=%p string contents=%s\n", string, string);
pelete[str_len] string;
// : delete string; ...
return 0 ;
}
new ,
( , , ),
. delete . , , delete, .
, ,
(, Borland C++ 3.0 16), .
new , , (
). , ,
new, , . new ,
12

++.

(void*)0, , , .
, ++
, ,
, new , , , , exit abort. ,
: new
, , -- , .
,
_new_handler, new.h
typedef void (*pvf) () ; // ,
//
extern pvf _new_handler;
set_new_handler,
new.h:
pvf set_new_handler(pvf);
set_new_handler -, .
, . , new delete
, . , ,
. , , new delete,
malloc, free . .

.
, int, double
char*. (. 9}?
9.
.
void print_int(int i)
{printf("%d", i);}
void print_double(double x)
{printf("%f" , x);}
void print_string(char* s)
{printf("%s", s);}
...
int j=5;
print_int(j) ;
print_double(3.141592) ;
print_string("Hi, there'");
...
, ++
print, (.
10):

13

++.

10. .
#include <stdio.h>
void print(int i)
{printf("%d ", i);}
void print(double x)
{print("%f ", x);}
void print(char* s)
{printf("%s ", s);}
int main()
{
int j=5;
double e=2.7183;
float pi=3.1415926;
print(j);
print(e) ;
print(pi) ;
print("Hi, there!");
return 0 ;
}
, ! , (!) print ( ++ overloaded) .
, , , ,
, , , , . .
:
void f(int., int);
int f(int, int); // !
,
:
...
void f ( int=0); //
void f ();
...
f();
// ?
...
, print 10
. , ++ , , ,
- , . ,
,
. (name mangling). , , ++ , -, . ++, ,
extern "":
extern "" int funcl(int); //
extern "" //
{
14

++.

void func2(int) ;
int func3 ( ) ;
double func4(double);
};
extern "" , :
extern "" int my_func(int i)
// my_func ,
{
// ...
}
, , extern "", .

.
++
. template
(), , , .
, swap, (. 11). , , ,
, max.
11. .
#include <string.h>
#include <stdio.h>
// ""
// . "class":
// .
template <class T> void swap( &, &b)
{
; //
c=b; b=a; =; //
};
// ";"
int main()
{
int i=0, j=l;
double x=0.0, =1.0;
char *sl="Hi, I am the first string!",
*s2="Hi, I am the second string!";
printf (" : \n"
"i=%d j=%d\n"
"x=%f y=%\n"
"sl=%s s2=%s\n",
i, j, x, y, si, s2) ;
swap(i,j);
swap(x,);
swap(s1,s2) ;
printf (" :\n"
"i=%d j=%d\n"
15

++.

"x=%f y=%f\n"
"sl=%s s2=%s\n",
i, j, x, y, si, s2) ;
return 0;
}
, template, . , ,
, , .
swap , ,
. . , .
.
- , . ,
++ .

.
++ , , ,
,
? ! @ ++, :
. .* : : ? :
operator@
, (. 12).
12. .
#include <stdio.h>
#include <string.h>
const MAX_STR_LEN=80;
struct String // ( )
{
char s [MAX_STR_LEN] ; // ""
int ) str_len;
//
};
// ("") String
String operator+ (String s1, String s2)
String TmpStr; // ...
// - .
// , .
If ((TmpStr.str_len=sl.str_len+s2.str_len)
>=MAX_STR_LEN)
{
TmpStr.s[0]='\x0' ;
TmpStr.str_len=0 ;
return TmpStr;
}
//
strcpy(TmpStr.s, sl.s);
strcat(TmpStr.s, s2.s);
16

++.

return TmpStr; //
int main()
{
String strl, str2, str3 ; // ,
// struct struct . "" strl str2
// .
strcpy(strl.s, " - ");
strl.str_len=strlen(strl.s) ;
strcpy(str2.s, " !");
str2.str_len=strlen(str2.s) ;
printf (" : =%d, coepoe=%s\n",
strl.str_len, strl.s);
printf (" : =%d, coepoe =%s\n",
str2.str_len, str2.s);
str3=strl+str2;
// !
// , , ,
// str3=operator+(str1, str2);
printf (" : =%d, coepoe=%s\n",
str3.str_len, str3.s):
return 0 ;
}
++, , .
, - .
. - ++: , const #define,
inline , new delete
malloc,calloc free ..

17

++.

2
-
, , , , , . ,
, , , .
, - . , , , ,
. .
- (),
. , , . , , , .
, ,
, .
.
, , ,
. , , .
,
" , . , , , .

, : , , , . .
.
. ,
: ?
? ? ?
?
, . , ++.

. .
= + .
(encapsulation) ,
, (abstract userdefined data types). ++
(classes). - (data members),
, , . . (member functions) . . , (object) , , - 18

++.

. - , , -
,
.
class Anylass
{
private:
int x;
// -
double ,z;
// ...
void f1();
// -
int f2(int) ;
public:
char ch, chl;
// -
int f3(int, int.); // -
int GetX() {return x;}
// ...
};
private: public: (access mode) :
, , , , , .
.
(private) ( , )
- ( - , ).
, (public), , . ,
, , .
:
, , , , .
class
. struct union. struct,
,
private:. union .
,
. , (local class), ,
.
-
( ), . ++ ,
- GetX() {return X;}, IsErrorQ {return ErrorState;},
AssignValue(int InitValue) {Value=lnitValue;} . .,
.
.
- -
. :: (scope access operator, resolution operator), ,
, . :
int MyClass :: f(int i) // f MyClass
{
// ...
19

++.

}
,
inline.
inline void MyClass::funci(int i, int j)
{
// ..,
}
.
class
{ public:
//...
void f();
};
int main ()
{
c_obj ;
c_obj . f ( ) ; // ...
//...
}
inline void :: f() //... , .
// !
{
//
}
, , !
, :
MyClass Obj1, Obj2, ObjArray[10] ;
: ,
-.
, #include , . - , , (
). ,
.

, , (direct component selector. and indirect component selector ->),
13.
13. .
class MyClass
{
// . . .
int i;
int j;
public:
int state;
int Get_i();
int Get_j();
//
//...
};
// MyClass
// , .
20

++.

Int MyClass::Get_i () {return i;}


int MyClass::Get_j () {return j;}
int main()
{
...
int m, n, nl ;
MyClass obj, objl; // obj obj1 MyClass...
MyClass *obj_ptr=&obj ; // ... obj_ptr - MyClass,
// obj.
m=obj.i;
// ! i - !
m=obj_ptr -> i; // !
n=obj.state;
// ,
// -, , .
m=obj_ptr-estate;
// ...
...
m=obj .Get_i( );
// OK!
n=obj_ptr -> Get_j(); // OK!
nl=objl.Get_j();
//OK!
...
}
:
- , ? , 13
j Get_j? : , Get_j
,
. , obj1.Get_j() &obj1
:
return j ;
:
return (&obj1)->j;
- , ,
this ( ). ,
<, ,
-, - ,
(early binding).
14 WiseString ().
WiseString
.
14. .
#include <stdio.h>
#include <string.h>
// WiseString, class.
class WiseString
{
// - , WiseString:
const char *s; //
int len;
//
// public ( ,
// ), - ,
// - ,
21

++.

// .
public:
// - ,
// WiseString
void Assign(const char *String)
// Assign ,
//
{
s=String;
len=strlen(s);
}
void TellAboutYourself() const; //
// . const
// - ,
// - , .
// WiseString
};
int main()
{
WiseString str1, str2; // ""
str1.ssign (", !"); // -, .
str2.ssign (" !");
// !
str1.TellAboutYourself();
// !
str2.TellAboutYourseif();
return 0;
}
// TellAboutYourself WiseString.
void WiseString::TellAboutYourself() const
{
// const
// - , , ,
// len++ !
const char *fmt=" - ! . \n"
" , :\n%s\n"
" %d ().\n";
printf(fmt, s, len);
}


,
- -
. , -
( non-static), ,
-
.
, ? !
static. 14 ,
.

22

++.

15. .
#include <stdio.h>
#include <string.h>
class VeryWiseString
{
const char *s; //
int len;
//
int my_number; //
static int counter; // , ...
public:
// Assign ,
//
void Assign(const char * String)
{
my_number=++counter ;
s=String;
len=strlen(s);
}
void TellAboutYourself() const;
// - -
// // , .
static int HowManyStrings() {return counter;}
};
// VeryWiseString
/***************************************************************************
- - , ,
, .
-, , .
, static HE .
****************************************************************************/
int VeryWiseString::counter=0; // .
// VeryWiseString::counter .
int main()
{
// - -
// . ::
printf (" - %d\n",
VeryWiseString::HowManyStrings());
VeryWiseString str1, str2, str3; // ...
str1.Assign(" , !");
str2.Assign(" a !");
str3.Assign(" M !");
printf (" Bcero - %d\n",
VeryWiseString::HowManyStrings()) ;
// - :
// str1.HowManyStrings();
// str2.HowManyStrings():
// str3.HowManyStrings():
// , , .
23

++.

str1 .TellAboutYourself();
str2.TellAboutYourself ;
str3.TellAboutYourself() ;
return 0 ;
}
void VeryWiseString::TellAboutYourseif() const
{
const char *fmt=
" - ! . \n"
" %d\n"
" , :\n%s\n"
" %d ().\n";
printf(fmt, my_number, s, len);
}
, , -
.


++ ( , 1/92, cc. 117-123).

.
, , , . ++ ,
( , ?)
friend. . ,
, 16.
16. .
...
class MyClass1; // "tentative" .
class MyClass
{
// . . .
int j;
//
// . . .
friend void IncJ (MyClass &); // : public: private:
// ,
// - MyClass1
// MyClass.
friend MyClass1;
//
// "" MyClass1 - ,
// .
// . . .
};
class MyClass1
{
// . . .
int j;
public:
void MakeJEqual(MyClass &);
// . . .
24

++.

};
// IncJ.
// , , MyClass IncJ - ,
// .
void IncJ(MyClass &obj)
{
obj.j ++;
}
// MakeJEqual MyClass1 , ,
// MyClass.
void MyClass1::MakeJEqual (MyClass &obj )
{
// j MyClass1 j
// MyClass.
j =obj.j ;
}
// ...
int main()
{
// ...
MyClass obj;
MyClass1 obj1;
// ...
// IncJ HE MyClass obj.lncJ().
// - - . ,
// j obj, obj.

IncJ(obj);
// obj1.j obj.j
obj1.MakeJEqual(obj);
// . . .
}

.
. .
, , , , @
++ - ,
operator@ , . , ( ) . , 17, Vector, : , , , ,
.
, ++ operator@ @, , . ++ AT&T 2.1 .
- Class :: operator@() @, Class :: operator@(int) . , .

25

++.

17. .
#include <stdio.h>
class Vector
{
int X, Y; // () .
friend Vectors& operator--(Vect&); //
//
// , .
friend Vector operator-(const VectorS&, const Vector&);
// . , !
public:
// .
void Assign(int x, int ) {=; Y=y;}
Vector operator++(); //
// .
Vector operator++(int); //
// .
Vector operator+(const Vector&) nst; //
// . , !
void print() const // , .
{
printf (" X=%d, Y=%d\n", X, Y) ;
}
};
// - . - operator++
// , , .
Vectors& Vector::operator++()
{
++;
Y++;
// this - , operator++,
// , (*this).
// !
return *this;
}
Vector Vector::operator++(int)
{
//
Vector vec_tmp=*this ;
X++;
Y++;
return vec_tmp; // ++
// .
}
// - , - operator+
// (this) .
Vector Vector::operator+(const Vector& vec) const
{
//
// ...
Vector vec_tmp=*this;
// ...
26

++.

vec_tmp.X+=vec.X;
vec_tmp.Y+=vec.Y;
return vec_tmp;
// ( *this).
}
// -
// this, c
// , - . , . .
// .

Vector& operator--(Vector&; vec) // .


{
vec.--;
vec.Y--;
return vec;
// vec .
}
Vector operator-(const Vector& vecl, const Vector& vec2)
{ Vector vec_tmp=vecl;
vec_tmp.X-=vec2.X;
vec_tmp.Y-=vec2.Y;
return vec_tmp;
// .
}
const NUMBER_OF_VECTORS=5;
int main()
{
Vector v_array[NUMBER_OF_VECTORS], vl; // ...
//... .
for (int i=0; i<NUMBER_OF_VECTORS; i++).
{
v_array[i].Assign(i, i+1);
rintf (" #%d\n", i);
v_array[i].print();
}
for (i=0; i<NUMBER_OF_VECTORS ; i++).
{
--v_array[i] ; // operator- - (v_array[i])
printf (" #%d .\n", i);
v_array[i].print() ;
}
vl.Assign(0, 0) ;
printf (" : ");
for (i=0; i<NUMBER_OF_VECTORS ; i++)
{
vl=v1+v_array[i] ; //
// v1 .operator+(v_array[i])
}
v1.print () ;
return 0 ;
}
, .
--.
, + -
Vector. , , , :
// . . .
27

++.

Vector v1, v2, v3, v4 ;


// . . .
vl=v2+v3+v4;
// v2 v3
// ! v2+v3+v4 :
(v2.operator+(v3)).operator+(v4) ;
// . . .
-,
( ) - ++, -
. :
cin - . , stdin;
cout - , , stdoirt;
- , , stderr;
clog - .
- .
:
iostream.h ++
(, , ). , (manipulators),
iostream.h, - iomanip.h, -. ,
-, ,
(. 18}. .
,
, ++ ( int, double . .),
, . ++ , ,
iostream.h ostream, operator<< ostream. , , ,
operator<< : ostream, , .
18.
-.
#include <iostream.h>
#include <string.h>
struct ComputerInfo {
char* type;
char* processor;
char* coprocessor;
int RAM;
char* operating_systern;
};
// << CompulerInfo
ostream& operator<< (ostream& os, const ComputerInfo& ci)
{
return os<< "Computer System Information:\n"<<
"Type :............ "<< ci.type <<endl
"CPU: ............. "<<ci.processor<<
"Co-processor:..... "<<ci.coprocessor <<endl<<
"RAM: ............. "<<ci.RAM<<" KB"<<endl<<
28

++.

"Operating System:.."<<ci.operating_system<<endl;
}
int main() {
//
ComputerInfo MyComputerInfo=
{
"IBM PC AT",
"Intel 80386",
" NO ",
2048,
"DOS 5.0"
};
char name[80];
int age;
// endl .
cout << "Hello, world! "<< "! ? ";
cin>>name>>age ;
// dec, oct hex
// , .
cout<<name<< " Ba, , , \n"<<
" ?\n"<<
" : "<<dec<<age<<endl<<
" : "<<oct<<age<<endl<<
" : "<<hex<<age<<endl<<
// , "" <<
" 2 , "<<endl<<:
" "<< (g<<2) << "."<<endl<<
" "<<dec<< (age<<2)<< " ! "<<endl ;
cout<< " : "<<
setprecision(4)<< 3.1415926 << endl;
// , Computerlnfo.
ut<< " , , , ? \n"<< MyComputerInfo<< " , "<<name<< ". !";
return 0 ;
}

istream& operator>> ( istream& os, ComputerInto& ci)
,
18.
: , - ( , ), ,
, inline! , ,
.
, , , .
.
object2=object1, object1 object2 , , -, object1, object2, , , object1 object2.
29

++.

. ,
(. 19)!
19. .
#include <iostream.h>
#include <string.h>
// :
// .
#define OVERLOAD_ASSIGNMENT
class Str
{
char *s;
//
int len;
//
public:
void Initialize (const char*);
//
void Initialize() {s=NULL; len=0;} // -
void ToUpper () {strupr(s);} //
void Destroy () {delete s; s=NULL; len=0;} //
void print!) (cout << s << endl;} //
#ifdef OVERLOAD_ASSIGNMENT
Str& operator=(const Str&); //
#endif
};
// - .
void Str::Initialize(const char* InitStr)
{
len=strlen(InitStr);
s=new char[len+l]; //
strcpy(s, InitStr); //
}
#ifdef OVERLOAD_ASSIGNMENT
// Str
// Str, , -,
// Str operator=, , -,
// str1=str2=str3 ..
Str& Str::operator=(const Str& OtherStr)
{
len=OtherStr.len;
delete s; // , .
// , s==NULL delete .
s=new char[len+i];
strcpyfs, OtherStr. s); // .
Return *this;
// .
}
#endif
int main()
{
Str strl, str2, str3;
Str1.Initialize("Hi! I am a string!!!");
// str2 str3 !
Str2.Initialize();
str3.Initializer;
out<< " : " << endl;
cout<< " : "; strl.print();
30

++.

cout<< " : "; str2.print();


ut<< " : "; str3.print();
str3=str2=strl;
cout<< " npcaa:" << endl;
ut<< " : "; strl.print();
cout<< " : "; str2.print();
ut<< " : "; str3.print();
str3.ToUpper() ;
cout<< " Bce ." << endl <<
" ?" << endl ;
cout<< " : "; strl.print();
cout<< " : "; str2.print();
cout<< " : "; str3.print();
strl.Destroy(); str2.Destroy(); str3.Destroy ();
cout<< " Destroy. " endl;
cout<< " : "; strl.print();
cout<< " : "; str2.print();
cout<< " : "; str3.print();
return 0 ;
}
19 , #define OVERLOAD_ASSIGNMENT . .

. .
19 ,
, - Str, Initialize Destroy,
, Str , ,
, , ?
++
- , , , , (constructors) (destructors).
, , :
, , .
,
, , ( ), , , - .
- ,
. ~ () .
. ,
, e -. , . , ,
: .
, , ( !), .

31

++.

, 20 , ,
. , !
: 14 , more :
14 | more
20. .
#include <stream.h>
#include <stdlib.h>
// *****************************************************************************
// ,
// .
#define DEFINE_DEFAULT_CONSTRUCTOR // ,
// .
#define DEFAULT_CONSTR_WITH_NO_ARG // ,
// , - , - , .
#define DEFINE_COPY_GENERATOR // - .
#define UNEXPECTED_EXIT // .
#define UNEXPECTED_RETURN // main.
// *****************************************************************************
typedef char Boolean;
const Boolean NO=0;
const Boolean YES=1;
class SimpleClass
{
static int ObjCounter; //
int PrivateNumber; // " "
Boolean IsCopy; // - ?
friend void WhoAreYou(const SimpleClass&);
//
// ,
// ! ""
public:
#ifdef DEFINE_DEFAULT_CONSTRUCTOR
#ifdef DEFAULT_CONSTR_WITH_NO_ARG
//
// .
SimpleClass(); // .
#else
SimpleClass(const char* DefArg=" !");
#endif
#endif
SimpleClass(int);
SimpleClass(char);
#ifdef DEFINE_COPY_GENERATOR
SimpleClass (const Simple-Classic); // -
#endif
~SimpleClass () ; //
);
void WhoAreYou(const SimpleClassSc obj)
{
char *ms;
if (obj.IsCopy)
32

++.

ms="Bac ! #";
else
ms="Bac ! #";
cout << ms << obj.PrivateNumber << endl ;
}
#ifdef DEFINE_DEPAULT_CONSTRUCTOR
#ifdef DEFAULT_CONSTR_WITH_.NO_ARG
// -
// , .
// ,
// , .
// IsCopy NO
SimpleClass::SimpleClass(): IsCopy(NO) (
cout << " ! - # "<<
(PrivateNumber=++ObjCounter) << endl <<
" !" << endl;
}
#else
SimpleClass::SimpleClass(const char* Arg): IsCopy(NO)
{
cout<< "! - # " <<
(PrivateNumber=++ObjCounter) << endl <<
" char* !" << endl <<
" : " << Arg << endl;
}
#endif
#endif
SimpleClass::SimpleClass(int Arg): IsCopy(NO)
{
cout<< "! - # " <<
(PrivateNumber=++ObjCounter) << endl <<
" int !"
" Arg = " << Arg << endl;
}
SimpleClass::SimpleClass(char Arg): IsCopy(NO)
{
cout<< "! - # " <<
(PrivateNumber=++ObjCounter) << endl <<
" "
" char! Arg =" << Arg << endl;
}
#ifdef DEFINE_COPY_GENERATOR
SimpleClass::SimpleClass(const SimpleClass&
OtherObject):
IsCopy (YES), // .
PrivateNumber(OtherObject.PrivateNumber) //
// " ", .
{
ut << " ! - #"
<< PrivateNumber << endl ;
}
#endif
SimpleClass::~SimpleClass()
{
33

++.

char* ms ;
if (IsCopy)
ms= " - # ";
else
ms= " - # ";
cout << ms << PrivateNumber << "Pa ! " << endl ;
}
int SimpleClass::ObjCounter=0; //
SimpleClass global_object; // .
// main, - main.
int main()
{
ut << ">>> main. ...\n";
SimpleClass obj1, // " " (default), . .
// , , ,
// " ".
obj2(1992), // int.
// : obj2=1992,
obj3('A');
// : bj3='':
ut << ">>> obj1...\n";
SimpleClass objla=objl;
// : SimpleClass obj1a(obj1);
cout << ">>>A ! \n";
WhoAreYou(SimpleClass(''));
cout << ">>> , ? \n":
cout << ">>> - obj1? \n";
WhoAreYou(SimpieClass(obj1)) ;
cout << ">>> ... \n":
SimpleClass obj_array1[2];
cout << ">>> new...\n";
// " ".
SimpleClass* obj_array2=new SimpleClass[2];
cout << ">>>0 . ..\n";
{
//
cout << ">>> " "...\n";
SimpleClass obj_array3 [2];
cout << ">>> new...\n";
SimpleClass* obj_array4=new SimpleClass[2];
cout << ">>> . ..\n";
static SimpleClass stat_obj;
cout << ">>>3 ... \n";
}
//
#ifdef UNEXPECTED_EXIT
cout << ">>> exit \n";
exit(O) ;
#endif
#ifdef UNEXPECTED_RETURN
cout << ">>> main return \n";
return 0;
#endif cout << ">>>Pa6oae delete...\n";
// ! !
Delete[2] obj_array2;
34

++.

// delete[2] obj_array4; ! obj_array4 !


Cout << ">>> main...\n";
return 0 ;
}
: , delete
, , delete
[ ], ( ) . , ,
, . delete .
. .
: , .
new, delete (,
).
, new, (, , ),
delete, , , , , . : , - ,
, , !
() #define DEFINE_DEFAULT_CONSTRUCTOR . . ( , ) (default constructor) . -,
, , , , . , ++ , , ! , . , , 17 Vector, , ? : ( !)
.
#define DEFINE_DEFAULT CONSTRUCTOR #define DEFAULT_CONSTR_WITH_NO_ARG. ,
. ( ).
:
#define UNEXPECTED_EXIT

#define UNEXPECTED_RETURN
, exit
abort,
, , , . , , , , main return.
, - . #define DEFINE_COPY_GENE-RATOR , , .
35

++.

, , ,
.
new , () :: operator new. , , ,
,
this -
. this
. :
class
{
int array [10] ;
public:
() {this=my_allocator (sizeof());}
~() { my_deallocator (this); this=0;}
};
this ,
( auto static , ), . , , ,
, .
, this, new delete. ' new void* , size_t ( size_t stdlib.h, stdio.h
., unsigned int). operator new -
, - ( , - ), , static, . : operator new ,
. delete . delete void*, () size_t.
, , ?

, , , .
. 21, int.
21. .
#include <stream.h>
#include <stdio.h>
// size_t
#define FIRST_VERSION //
7const DEF_SIZE=10;
//
#ifdef FIRST_VERSION
// () .
class Stack
{
int* top_ptr;
//
int* start_ptr;
//
public:
// sz int.
Stack(int sz=DEF_SIZE)
36

++.

{
top_ptr=start_ptr=new int[sz];
}
void push(int i)
{
*top_ptr++ = i;
// "" .
}
int pop()
{
return *--top_ptr; // .
}
~Stack() {delete start_ptr;}
};
#else
// .
class Stack
{
// ,
int top_num;
public:
Stack () {top_num=l;} // ...
void push(int i) {*((int*)this+top_num++) = i;}
int pop() {return *((int*)this+ --top_num);}
/* new Stack def_sz+sz*sizeof(int) , def_sz - , operator new - Stack
(, "" , sizeof(int)), a sz - int,
. , new, operator new, ::operator new.
- operator new , new ,
.*/
void* operator new(size_t def_sz, int sz=DEP_SIZE)
{return new char[def_sz+sz*sizeof(int)];}
// , Stack
, delete stk_ptr!
};
#endif
int main()
{
int st_size;
ut<<" Int ? ";
cin>>st_size;
#ifdef FIRST_VERSION
ut<<" . \n";
Stack* stk_ptr=new Stack(st_size) ;
#else
ut<<" .\n";
// new
// c ( , ).
// st_size - .
Stack* stk_ptr=new(st_size) Stack;
#endif
ut<<"3a ...\n";
37

++.

for (int counter=l; counter<=st_size; counter++)


{
cout<<counter<<' ';
stk_ptr->push(counter) ;
}
cout<<"\n ...\n";
for (counter=l; counter<=st_size; counter++)
cout<<stk_ptr->pop()<<' ';
delete stk_ptr;
return 0;
}
1 Stack
. , .
Puc. 1. Stack.
.


Top ptr


top num

Start ptr

...

...

Stack, new, ?
. ,
push pop :
(stk_ptr->top_ptr) ->,
:
((int*)stk_ptr+top_num)->
, Stack .
, . , , ,
. Stack ,
(, , Stack, !); , :
...
Stack global_stk (st_size) ;
int main()
{
...
Stack local_stk(st_size);
Stack stk_array[100] ;
38

++.

Stack* dynair_ic_stk_array=new Stack[100];


...
}
Stack ,
, , . , ,
Stack:
...
Stack *stk_ptr_array[100];
for (int count=0; count<=100; count++)
stk_ptr_array[count]=new(st_size) Stack;
...
, , ,
, .
, , Stack
. Stack , . .

.
, ,
, (class generators, or generic classes,
or parameterized types). ,
, ,
, .
(container) , ,
.
22.
#include <iostream.h>
template <class , int size> // size : -
// , size -

class Vector
{
*elements; //
public:
Vector() ;
~Vector() {delete elements;}
T& operator[](int i) {return elements[i];}
void print_contents() ;
};
// -
template <class , int size>
Vector<T, size>::Vector()
{
elements= new T[size];
for (int i=0; i<size; elements[i]=(T)0, i++);
};
template<class T, int size>
void Vector<T, size>::print_contents()
{
39

++.

cout<<"Bcero :"<<size<<":";
for (int. i=0; i<size; i++)
cout<<' ' <<elements [i];
cout<<'\n' ;
};
int main()
{
// int, double char, 10 .
// .
Vector <int, 10> i;
Vector <double, 10> x;
Vector <char, 10> ch;
//
for (int count=0; count<10; count++)
{
i[count]=count ;
x[count]=0.1+count ;
ch[count]='a'+count ;
}
//
i.print_contents() ;
x.print_contents() ;
ch.print_contents() ;
return 0 ;
}

. .
(inheritance) ++.
, , , , .
, ,
. ,
, , , (inherit)
(. . - -), .
.
, , (base classes) (derived class),
( - ancestor
classes - descendant classes). 23 , ;
. struct, , ,
, .
. , union, , .
, , .
. , , , 40

++.

, ,
23.
23. .
#include <iostream.h>
// :
//
//
//
//
//
struct Name
{
char* Firstname; //
char* Secondname; //
char* Surname;
//
Name (char* FN, char* SN, char* SurN)
{
cout<< " Name"<<endl;
Firstname=FN; Secondname=SN; Surname=SurN;
}
~Name () { ut<<" Name"<<endl;}
};
struct Job //
{
char* Company; //
char* Position; //
Job(char* C, char* P)
{
ut<< " Job "<< endl;
Company=C; Position=P;
}
Job() {ut<< " Job" << endl; }
};
struct Person: Name, Job // . Person
// Name Job.
{
int Age;
//
char* Sex; //
Person (char* IFirstname, char* ISecondname, char* ISurname,
int IAge, char* ISex, char* Icompany, char* Iposition) ;
~Person()
{
cout<<" Person"<<endl;
}
};
Person::Person(char* iFirstname, char* Isecondname, char*Isurname, intIiAge, char*
ISex,
char* ICompany, char* Iposition):
// .
// , 14, !
Name(IFirstname, Isecondname, I Surname),
Job(ICompany, IPosition)
41

++.

{
cout << " Person" << endl ;
Age=IAge;
Sex=ISex;
}
ostream& operator<<(ostream& os, Person& p)
{
return os<<" Firstname. .... " << p.Firstname << endl <<
"Secondname. . . . " << p.Secondname << endl <<
"Surname. ..... . " << p.Surnam << endl <<
"Age........... " << p.Age << endl <<
"Sex.. ......... " << p.Sex << endl <<
"Company. ...... " << p.Company << endl <<
" Position. ..... " << p.Position << endl ;
}
int main()
{
Person p1("Ivan", "Ivanovitch", "Sidorov", 30, "Male",
"SoftSci", "Programmer");
cout << " Employ : \n" << p1 << "OK! \n" ;
return 0 ;
}

.
, , , . ,
? 24, :: .
24. .
#include <iostream.h>
void f()
(
cout << "\n f !\n";
}
struct Base1
{
int a;
void f(int i) {a+=i;}
int get_a() {return a;}
void print_a()
{cout << "Base1::a=" << a << endl;}
Base1(): a(0) {;}
};
struct Base2
{
int a;
void f(char c) {a+=int(c);}
void print_a() {cout << " Base2 : :a=" << a << endl; }
Base2(): a(0) (;}
};
struct Deriv: Base1, Base2
42

++.

{
int a;
// Deriv :
// - Basel,
// - Base2,
// - Deriv.
void f()
{
++ ;
:: f () ; // -- ,
// -. : - ,
// open, seek, write . .,
// - // :: . .
}
void print_a()
{cout << "Deriv::a =" << a << endl;}
Deriv() : a (0) {;}
};
int main()
{
Deriv obj
obj.Base1 :print_a();
obj.Base2 :print_a();
obj.print_a () ; // Deriv::print_a
obj.Base1 :a=obj.Base2::a=obj.a=1 ;
obj.Base1 :prinfc_a();
obj.Base2 :print_a();
obj.print_a() ;
// , , f,
// Basel, Base2 Deriv, , , ,
// , ,
// ::, . .
// obj.Base1::f(1).
// get_a Base1,
// ! ,
// obj.Basel::a
ut << " get_a: " << obj.get_a() << endl;
obj.Base2::f('\x1');
obj.f();
obj.Base1::print_a();
obj.Base2::print_a();
obj.print_a();
return 0;
}
, -
- , , ,
.
. 23,
, struct,
.
, 25.
43

++.

25. .
class Bl
{
private: // private , ,
// class, . ,
// (private) -
// .
int i1;
protected: // protected? Protected .
// -
// .
int j1;
public:
int k1;
};
class B2
{
int i2;,
protected:
int j2;
public:
int k2 ;
};
// ,
// , private public.
// class,
// private, struct - public.
class D: private B1, public B2
{
// . . .
// i1 i2 - D .
// j1 k1, 1, D "".
// j2 k2 B2, D // , , "" "".
// . . .
};
private
public protected - . , , , . 25 ,
(access modifiers) private public,
, ,
. 1.
, , , , . , .
1. .
.


44

++.

public
public
public
private
private
private

.
++
, . . :
class A {...};
class : , {...};
// !
,
,
(, ):
class A
{
public:
int i ;
// . . .
};
class B: public A {/*...*/};
class : public A, public {/*...*/};
,
, , , . ? , . :
obj_c;
obj_c.i=l;
// i :
// ?!
// ...

.
, , (virtual base classes).
,
. -, . . 2
.
, , ,
, , .
. 2. .
class A {...};
class : public A {...};
class : public A, public {...};
45

++.

class A {...};
class : virtual public A {...};
class : virtual public A, public {...};

26 , .
:
1. virtual . .
2. , . , ( ) .
26. .
#include <iostream.h>
class A
{
long i;
public:
long get_i () {return i;}
A(long init_i) {ut << " . "; i=init_i;}
~A() {ut << " . ";}
};
class : virtual public A
46

++.

{
public:
B(long i): A(i) {cout << " ";}
~B() {cout << " . ";}
};
class B1: public A
{
public:
B1(long i):A(i) {ut << " . B1";}
~B1() {ut << " . B1";}
};
class : public , virtual public A
{
public:
(long j): B(j), A(j)
{ut << " . ";}
~C() {cout << " . ";}
};
int main()
{
out << " A\n";
(i);
out << "\n Paep a=" << sizeof (a) << " . \n";
out << "\ 1\n";
1 b1(1) ;
out << "\n Paep b1=" << sizeof (b1) << " . \n";
out << "\ \n";
b (1) ;
out << "\n Paep b=" << sizeof (b) << " . \n";
out << "\n \n";
(1) ;
out << "\n c=" << sizeof (c) << " . \n";
return 0 ;
}

47

++.

.
Derived Base, Derived Base .
Base Derived . :
class Base { ... };
class Derived : public Base { ... };
Derived d;
Base* b_ptr = &d; // Derived* d_ptr;
d_ptr = b_ptr;
// !
d_ptr = (Derived*) b_ptr; //

, , (, , , )
, . .
.
++ , . , 2.
2. .

aType

aType

--

aType*

PaType

Const aType*

PCaType

aType&

RaType

Const aType&

PCaType

aType*&

RP aType

#define_PTRDEF(name)\
typedef name * P##name;
#define_PTRCONSTDEF(name)\
typedef const name & RC##name;
#define_REFDEF(name)\
typedef name & R##name;
#define_REFCONSTDEF(name)\
typedef const name & RC##name;
#define_REFPTRDEF(name)\
typedef name * & RP##name;

defs.h Borland C++ 3.0 , .


_CLASSDEF, _defs.h,
, .
#define _CLASSDEF(name) class name;\
_PTRDEF (name)\
_REFDEF(name)\
_REFPTRDEF(name)\
_PTRCONSTDEF(name)\
_REFCONSTDEF(name)
,
, _defs.h
_CLASSDEF . :
#include <_defs.h>
48

++.

_CLASSDEF(MyClass)
class MyClass {/* ...*/};
// . . .
// , MyClass:
RMyClass func(int n, RMyClass obj);
// . . .
MyClass obj;
PMyClass obj_ptr=bobj;
RCMyClass ref:_const_obj=obj ;
// . . .
, ,
.

.
(polymorphism) ,
- .
. ++
(. . -)
, , ,
. ++ -.

-.
-

. , - ,
, , , . 27
, , :
27. - .
class Base
{
void f();
/*...*/
};
class Deriv1: public Base
{
void f() ;
/*...*/
};
class Deriv2: public Base
{
void f() ;
/*...*/
};
class Deriv3: public Deriv2
{
void f() ;
/*...*/
};
49

++.

// . . .
Base b_ptr[3] ;
b_ptr[0]=new Deriv1
b_ptr[1]=new Deriv2
b_ptr[2]=new Deriv3
//...
for (int count=0; count<3; count++)
b_ptr -> f(); // Base::f()

: , , . . -
, , , -
. .
: , . , ,
, . ,
, , .
,
, ( , , ).
: , .
, , , ,
, , (. 28).
28. .
//
enum CLASS_ID (ID_Base, ID_Deriv1, ID_Deriv2);
class Base
{
protected:
CLASS_ID class_TD;
public:
void f () ;
Base() {class_ID=ID_Base; /*...*/}
};
class Derivl: public Base
{
// . . .
friend void Base:: f();
Deriv1() {class_ID=ID_Derivl; /*...*/}
};
class Deriv2: public Base
{
// . . .
int some_ipember;
friend void Base::f();
Deriv2() {class_ID=ID_Deriv2; /*...*/}
};
void Base::f()
{
switch (class_ID)
{
50

++.

case ID_Base:
// ...
break;
case ID_Deriv1:
// ...
break;
case ID_Deriv2:
// Deriv2:
((Deriv2*)this) -> some_member++ ;
// ...
break;
default:
// ...
;
}
}
, ... , ,
f . , f - Base.
, ,
, (
!) -, .
, virtual, , , ,
, , ( !). ,
, (late binding). , ,
, : ?
, , .
,
, , , , virtual
.
, -
. , - , -.
,
, .
, ,
- , ,
.
29 , , .

51

++.

29. .
// :
//
//
Base
//
//
//
Deriv 1
//
//
//
//
Deriv 2
//
//

Deriv 2

#include <iostream.h>
const char *const f_msg =" ";
class Base
{
int dummy;
public:
//
void f() {cout << f_msg << "Base :: f()\n";}
virtual void f(int i)
{cout << f_msg << " Base:: f (int)\n";}
void f(int i, int j)
{cout << f_msg << "Base:: f(int, int)\n";)
};
class Derivl: public Base
{
char dummy1;
public:
void f() {cout << f_msg << "Deriv1:: f()\n";}
virtual void f(int i)
{cout << f_msg << "Deriv1::f(int)\n";}
};
class Deriv2: public Base
{
public:
void f() (cout << f_msg << "Deriv2:: f()\n";}
virtual void f(int i)
{cout << f_msg << "Deriv2::f(int)\n";}
};
class Derivl2: public Deriv1
{int dummy;
// f() f(int) .
};
typedef Base* PBase;
typedef Deriv1* Pderiv1;
int main()
{
cout << "\n************************************\n";
Base b_obj;
Deriv1 d1_obj;
52

++.

Deriv2 d2_obj;
Deriv12 d12_obj;
cout << " - : \n";
b_obj.f();
b_obj.f(1);
d2_obj.f(1);
d2_obj.Base::f(1) ;
// d2_obj.f(1,1); ! : . 24.
d2_obj.Base::f(1,1) ; // !
PBase b_ptr[4]; // Base
b_ptr[0]=&b_obj ;
b_ptr[1]=&dl_obj; // Denv1* Base*
b_ptr[2]=&d2_obj; // Denv2* Base*
b_ptr[3]=&dl2_obj; // Deriv12* Base*
//
char* types[4]={"Base", "Deriv1", "Deriv2", "Deriv12"} ;
char* msg = "T :";
cout << "- :\n";
cout << " f() *******************\n";
int count ;
ut << " Base*\n";
// ,
// .
for (count=0; count<4; count++)
{cout << msg << types[count]; b_ptr[count]->f();}
cout << "T Deriv1 *\n";
for (count=0; count<4; count++)
{
cout << msg << types[count];
(PDerivl(b_ptr[count]))->f();
}
cout << " f(int)************************\n";
// b_ptr[3]?
cout << "T Base*\n";
for (count=0; count<4; count++)
{
cout << insg << types[count] ;
b_ptr[count]->f(1) ;
}
ut << " Deriv1 *\n";
for (count=0; count<4; count++)
{
cout << msg << types[count];
(PDerivl(b_ptr[count]))->f(1);
}
cout << "*************************************\n";
// - ,
// , !
(Pderivl(b_ptr[2])) -> Deriv1::f(1);
return 0;
}
, , : , 29
53

++.

? ++
:
, ( ,
). , 29 .
, -. ,
:
class Base
{
// . . .
public:
virtual void f();
// . . .
};
class Deriv: public Base
{
// . . .
private:
virtual void f() ; // !
// . . .
};
// . . .
Deriv d_obj;
Base* b_ptr=&d_obj ; // .
b_ptr->f () ; // Deriv::f()!


, , ,
, , , . : , delete ,
.
, ?
. 30 , Figure.
30. .
Class Figure
{
public:
Figure() ;
virtual ~Figure();
};
typedef Figure* PFigure;
class Circle: public Figure
{
public:
Circle(int CenterX, int CenterY, int Radius);
virtual ~Circle(); // .
// virtual .
};
class Rectangle: public Figure
{
54

++.

public:
Rectangle(int Left, int Top, int Right, int Bottom) ;
~Rectangle(); // .
};
int main()
{
// . . .
// . :
const NallFigures=2
PPigure figures[NAllFigures];
figures [0] =new CircledOO, 100, 10);
figures[1]=new Rectangle(100, 100, 200, 250);
// . . .
// :
for (int count=0; count<NAllFigures; count++)
delete figures[count];
// ,
// ~Figured!
// . . .
}
, delete, .

.

, -, (pure function) =0.
class Base
{
virtual void f(int) =0;
// . . .
};
, , .
.
, . . (
) ,
, .
- -
( , ope
). .
,
. , , , , - , .
class Figure
{
// . . .
public:
55

++.

Figure();
virtual void Show() =0;
virtual void Hide() =0;
virtual void Expand(int ExpandBy) =0;
void Contract(int ContractBy)
{
Expand(-ContractBy); // OK!
}
virtual ~Figure() {Hide();} // !
};

-.
++, , -.
++ -, . - (stdio),
.
- stdio.h.
++, ++ - iostreams,
- , .
- - cin, cout, cerr clog, ,
iostreams.
, , - (input-output streams)? ++ , , - (source, or
producer) (consumer, or sink). (length), (current position). (access
mode):

.
, iostreams, ,
.

, - , .
iostreams . streambuf, ios. streambuf ,
filebuf strstreambuf, -
-. ios ,
- -
. ios
ios::bp streambuf, . , ios, :
istream . ;
ostream .
;
56

++.

iostream istream ostream;


iostream_withassign iostream .
- ++ cin, cout,
cerr clog.
ifstream ;
ofstream ;
istrstream ;
ostrstream .
- , ++ , , - ios.
- ios ,
18, .
31 , , . , .
31. , .
#include <fstream.h> // iostream.h
#include <string.h>
int main()
{
const MaxLineLength=255; // ,
char inbuf [MaxLineLength+1]; //
ut << " a , \n"
" .\n";
ut << " :";
char fname[80];
in >> fname;
if stream ifs(fname); // ,
// filename.
if (!ifs) // ?
{rr << " !"; return 1;}
ut << " :";
char SearchString[MaxLineLength+1] ;
cin >> SearchString;
unsigned line_counter=0,
//
str_counter=0; //
int SearchStrLen=strlen(SearchString) ;
while (!ifs.eof()) // ...
{
ifs.getline (inbuf, MaxLineLength, '\n'); //
// inbuf. , '\n'
// , MaxLineLength-1 .
line_counter++ ;
char* TmpStr=inbuf;
// strstr , NULL
// . string.h. while
// SearchString , .
while ((TmpStr=strstr(TmpStr, SearchString))i=NULL)
{
str_counter++ ;
if (strlen(TmpStr)< SearchStrLen*2) break;
// . ,
57

++.

// '\0'.
TmpStr+=SearchStrLen; // .
}
}
cout << " " << line_counter << "() .\n";
cout<<"Cpoa \"<<SearchString<<"'\ "<<str_counter<< "().";
return 0;
}

58

Вам также может понравиться