:
// Worker
Worker *pw;
:
pw = &worker; // worker Worker
, , :
Worker *pw = &worker;
() -> (, ):
pw->salary = 150000;
1.2.2.3. . >
> . , :
(*pw).salary = 150000;
. >, [], (, ).
, ,
Worker *pw = staff;
++pw->code;
code ( ) staff, pw.
:
Worker *pw = new Worker; //
pw->age=28;
pw->code=3983;
Worker *pwd = new Worker[k]; // k
delete:
delete pw;
delete [] pwd;
,. ->, :
pwd[2].age=28;
(pwd+2)->code=3983;
(*(pwd+2)).salary=35000;
( , . > , *).
, , C++ :
Worker staff[100];
...
int nAge = staff[1].age;
int iCode = (staff+2)->code;
int iAge = (*(staff+2)).age;
.
, .
C++ , , .
, , , . .
, () (), ( ).
- () .
|
|
, , .
(encapsulation) .
, , .
.
2. C++: -
, - - . C/C++ - . C, C++, - stdio. C++ , - iostream.
C++ C, - C.
C/C++ -, , . , - . , , , . , . . - , . , . , .
, .
, ( , , ). , () . , , . , .
( ), ( ) ( , ).
, , , .
, , (, ), .
|
|
C++ , ios streambuf. ios , streambuf . istream ostream . iostream, . (ifstream, ofstream, fstream) .
istream , ostream . iostream.h. - : cin, cout, cerr clog. cin , cout, cerr clog . cout, cerr clog ostream, cin istream. , . , , (cerr clog ).
cout cerr Unix . .
, , >>, <<.
2.3. >> <<
>> istream << ostream C++ (char*). , ("") . , C++, . , , ("") .
>> , ( ). , .
>> (int, long, short, double, float), (, ). (double, float) , . .
<< , .
<< . , , :
cout << x << ' ' << y;
:
((cout << x) << ' ') << y;
<<, :
cin >> x >> y;
>> , , , , , char. , >>, , .
|
|
C++ - ifstream ( ), ofstream ( ) fstream ( ). , , istream, ostream iostream, , , ios. , - -, .. - fstream.h.
:
- ;
- ;
- (/);
- ;
- .
- (ifstream, ofstream fstream):
ifstream f1; // f1
ofstream f2; // f2
, ( f1 f2 ) ( new):
ofstream *f3 = new ofstream; // f3
//
- , , , , - . - (ios, istream, ostream). :
ofstream f4("d:\\test4.txt"); // f4 ,
//
open:
f1.open("d:\\test5.txt"); // f1
//
, , , 11.4.3.
>> << -, 11.4.5. close:
f4.close();
f3->close();
( delete):
delete f3;