, , ' '.
, . () , . , .
, -, ' ' , -, .
++.
2. .
, .
++ operator, - (operator function).
-:
__ operator _(_)
{__}
- Å : , . Å Z Z.operatorÅ (), - operatorÅ (Z).
, , . ' .
, ( ), . ' .
:
) ( ):
__ operator _
) ( ):
__ operator _(_)
.
1)class person | 2)class person |
{ int age; | { int age; |
... | ... |
public: | public: |
... | |
void operator++(){ ++age;} | friend void operator++(person&); |
}; | }; |
void main() | void person::operator++(person& ob) |
{class person jon; | {++ob.age;} |
++jon;} | void main() |
{class person jon; | |
++jon;} |
- Å : , ( ) . xÅy x.operatorÅ(y), operatorÅ(x,y).
, , . ' .
, , , .
|
|
.
1)class person{...};
class adresbook
{
// '
//person, ,
public:
person& operator[](int); // i- '
};
person& adresbook:: operator[](int i){...}
void main()
{class adresbook persons;
class person record;
record = persons[3];
}
2) class person{...};
class adresbook
{ // ' //person, ,
public:
friend person& operator[](const adresbook&,int); // i- '
};
person& operator[](const adresbook& ob,int i){...}
void main()
{class adresbook persons;
class person record;
record = persons[3];
}
:
;
', , ', .
. , .
:
'_ & operator= ('_ &);
³ operanor=. -, --. ﳿ ', . ﳿ, . ', ', . -- .
-, operator=() ', . , --. ', . , , '. '. , ', , , .
3. Borland C++ 5.02.
* .
- , : , . ide.
', . . , , , , . . , .ide. , ' .
* . .
1. , , File|Open. - File|New, Text Edit. , File|Save.
2. , ( Alt+F10) Target Expert. ' Target Expert.
|
|
3. Ok. , DOC, EasyWin Windows 3.x(16- ) Console Win32. EasyWin .
4. Debug () Sun (). , .
5. . Options|Project|Compiler Precompiled Headers Generate and use.
* . .
, .
:
1. File|New Project. ' New Target. Target Expert.
2. ' (Project Path and Name), ' (Target Name) ++.
3. Advanced, . ' Advanced Options.
4. . def, .rc .def.
5. Advanced Options New Target ( Ok).
6. , ', Project , . , Add Node. ' Add to Proect List, , .
7.ϳ , , . , Make Node.
8.ϳ , IDE Open Project Project. ' .
* . .
1. Project New Target. ' Add Target.
2. ' , (Standard) Ok. ' New Target.
3. New Target .
4. , , .
5. Build Node, .
4. EasyWin ?
(IDE) BC++5.02 , EasyWin , , Windows. . Windows, .
* EasyWin .
1. IDE BC++5.02.
2. File.
3. New/Project, New Target.
4. ' .ide ( Project Path and Name). ' ( Target Name ), ' . Brows , .
5. EasyWin[.exe] Target Type.
6. Advanced, Advanced Options. , . cpp Node. IDE .cpp . .rc .def. Windows-, EasyWin, , . (k).
7. k, .
|
|
8.IDE Project, . , Project .
9. EasyWin - . . ., .
10. .
11.³ (F9).
12.
13. 12, .
14. (Ctrl+F9).
.
1. .
2. , , Input( ) Print( ), .
3. .
4. ( ).
5. . .
.
1. . :
- ;
- ;
- .
2. ' . , .
3. , '. :
* :
MyClass(const MyClass& ob);
* :
MyClass& operator = (const MyClass& ob);
4. - private (protected) , . , - :
- ;
- ;
- ;
- , .
- (operator).
.
1. .
2. , .
3. .
4. , .
5. ' ' .
6. (- , , ).
7. .
.
1. ?
2. .
3. / --?
4. -?
5. , ? ?
6. ?
7. ?
8. ?
9. , , ?
10. ++ ?
11. ?
12. ?
13. ?
14. - ?
15. ' operator ?
|
|
16. ?
17. , ?
18. -- ?
19. .
20. + .
21. <,>,== .
. .
1.- char. :
+ - ( char + set);
+ - ' ;
== - .
2.- char. :
- - ( set-char);
* - ;
< - .
3.- char. :
- - ( set-char);
> - ;
!= - .
4.- char. :
+ - ( set+char);
* - ;
int() - .
5.- char. :
() - ( );
+ - ' ;
<= - .
6.- char. :
> - (char in set );
* - ;
< - .
7.- char. :
+ ᒺ (list+list);
-- - ( list);
== .
8.- char. :
+ - (char+list);
-- - ( list);
== - .
9.- char. :
+ - (list+char);
- - ( list-);
!= - .
10.- char. :
[] - , :
int i; char c;
list L;
c=L[i];
+ - ' ;
== - .
11.- char. :
[] - , :
int i; char c;
list L;
c=L[i];
+ - ' ;
!= - .
12.- char. :
() - , :
int i;
list L;
L[i];
() - , :
int i; char c;
list L;
L[c,i];
!= .
13.-. :
+ - ;
- ;
bool() - ?
14.-. :
+ - ;
- ;
bool() - ?
15.- () . :
+ - (a[i]+b[i] i);
[] - ;
+ - (double+vector).
16.- () . :
- - (a[i]-b[i] i);
[] - ;
- - (vector-double).
17.- () . :
* - (a[i]*b[i] i);
[] - ;
* - (vector*double).
18.- () . :
int() - ;
() - ;
- - (vector-double);
[] - ;
19.- () . :
= - (vector=double);
[] - ;
== - ;
!= - ;
20.- () . :
|
|
() - ;
* - ;
* - ;
* - .
21.- () . :
() - ;
- - ;
- - ;
== - .
22.- () . :
() - ;
= - (matr=double);
+ - ;
+ - (matr+double).
23.- () . :
() - ;
== - .
++ - ;
6.
.
. ++.
.
.
.
.
( ) (), . , , 䳿 .
++ template. :
template <class _> __ '_(_){_}
.
* .
* .
* class.
* ' .
* - . , .
* ' .
* , , , .
.
( ) . , , - . , , , ', . , ', . , , ' ' , ' .
:
template <class _> class '_ {... };
.
* . ' template.
* , , , , .
* friend- , friend-.
* friend- ( ).
* () , , , .
* , , virtual.
* .
, , :
* template, __ , .
* '_, (::), __ .
template<_>__ '_<__ >:: '_(_){... }
.
1. . , , (=) , .
2. , .
3. .
4. , . .
5. , .
6. .
.
1. . :
- ;
- ;
- .
2. input print.
3. , '. :
* :
MyTmp (const MyTmp& ob);
* :
MyTmp& operator = (const MyTmp& ob);
4. , , , . 1.
5. . complex.
class complex{
int re; //
int im; //
public;
//
};
6. .
7. EasyWin- Borland C++5.02.
8. .
.
1. : , , , ', , .
2. .
, , , -, ..
.
3. .
4. .
5. , , , .
6. .
7. . ( ).
.
1. ?
2. / ?
3. / ?
4. .
5. - .
6. ?
7. .
8. ?
9. ?
10. .
11. ? .
12. ?
13. ?
14. , , ?
15. ?
16. ?
.
1.- . :
* - ;
[] - ;
2.- . :
int() - ;
[] - ;
3.- . :
[] - ;
== - ;
!= - ;
4.- set. :
+ - ( set+item);
+ - ' .
* - ;
5.- set. :
+ - ( item + set);
+ - ' ;
== - .
6.- set. :
- - ( set-item);
* - ;
< - .
7.- set. :
- - ( set-item);
>- ;
!= - .
8.- set. :
+ - ( set+item);
* - ;
int() - .
9.- set. :
() - ( Pascal);
+ - ' ;
<= - .
10.- set. :
> - ( in Pascal);
* - ;
< - .
11.- list. :
+ - (list+item);
-- - (--list);
== - .
12.- list. :
+ - (item+list);
- - (-list);
!= - .
13.- list. :
+ - (list+item);
- - ( list-);
!= - .
14.- list. :
[] - , :
Type c;
int i;
list L;
c=L[i];
+ - ' ;
== - .
15.- list. :
[] - , :
int i; Type c;
list L;
c=L[i];
+ - ' ;
!= .
16.- list. :
() - , :
int i;
list L;
L(i);
() - , :
int i;
Type c;
list L;
L(c,i);
!= - .
17.- stack. :
+ - ;
- ;
bool() - ?
18.- queue. :
+ - ;
- ;
bool() - ?
19.- . :
+ - ;
[] - ;
+ - .
20.- . :
- ;
[] - ;
- - .
7.
.
. ++, ' ++.
.
, ' , , , , .
.
.
'- ANSI-C. :
- ' .
- - .
- - / , - ++ .
- ( )
basic_istream, basic_ostream , ' - ';
basic_ifstream, basic_iostream ;
basic_istringstream, basic_ostringstream '-.
- ', ' , / .
- basic_ios ( ) basic_streambuf ( ) :
(char) ;
(traits) ' ios_traits ( ), ;
char wchar_t ' ios_traits .
.
template <class char, class traits = ios_traits <char>> class basic_istream: virtual public basic_ios <char, traits>;
++.
++ : ios streambuf.
streambuf ' -, ', -. streambuf . -. ³ .
ios - .
istream ;
ostream ;
iostream -;
istrstream ;
ifstream ..
, , .
iostream.h - ios, ostream, istream.
strstream.h - strstream, istrstream, ostrstream
fstream.h - fstream, ifstream, ofstream
-
' istream. - ' ostream.
istream .
istream& get (char* buffer, int size, char delimiter='\n');
istream . , size , . streambuf. .
istream& read(char* buffer,int size);
, . ʳ ' istream::gcount_ (private).
istream& getline(char* buffer,int size, char delimiter='\n');
, . . .
istream& get(streambuf& s,char delimiter='\n');
istream streambuf , -. istream. s .
istream get (char& C);
istream . , 0XFF.
int get();
istream . EOF.
int peek();
istream, istream.
int gcount();
, .
* istream& putback()
get ' streambuf , .
* istream& ignore(int count=1,int target=EOF);
istream, :
- count ;
- target;
- .
ostream .
* ostream& put(char C);
ostream .
* ostream& write(const char* buffer,int size);
ostream . , size . . . ( ) ostream.
* ostream& flush();
streambuf.
- .
* istream& seekg(long p);
get ( ) .
* istream& seekg(long p,seek_dir point);
* enum seek_dir{beg,curr,end}
get ( ), ( ).
* long tellg();
get.
*ostream& seekp(long p);
put streambuf streambuf.
* ostream& seekp(long p,seek_dir point);
.
* long tellp();
put.
istream >>, ostream <<. << >> ˳ ' istream (ostream), , .
<< >> operator<< operator>>. - operator.
ϳ : , , (char*). void* ( , char*, void*). >> << .
operator<< operator>> ', . , .
cout << a << b << c;
cin >> i >> j >> k;
- .
>> << , , , -. - ' , ' .
iostream.h ', ' -.
cin ' istream, ' .
cout ' ostream, ' .
cerr .
clog .
<< >> cout, cin, cerr, clog , , , .
. ֳ ios. protected long x_flags. public .
protected ios.
int x_width .
int x_precision ( ) .
int x_fill , .
()
int width();
int width(int);
int precision();
int precision(int);
char fill();
char fill(char);
ios, . .
, . , >> <<. , , ( ), .
istream ostream operator.
istream& operator>>(istream&(*_f)(istream&));
ostream& operator<<(ostream&(*_f)(ostream&));
<iomanip.h>, .
, :
1. (my_manip) : +
ostream& (*f)(ostream&,< >);
2. (my_manip) .
3. operator<<. ' (my_manip) (), ostream ostream *f. ,
typedef far ostream&(far *PTF)(ostream&,int,int,char);
class my_man{
int w;int n;char fill;
PTF f;
public:
//
my_man(PTF F,int W,int N,char FILL):f(F),w(W),n(N),fill(FILL){}
friend ostream& operator<<(ostream&,my_man&);
};
ostream& operator<<(ostream& out,my_man& my)
{return my.f(out,my.w,my.n,my.fill);}
4. *f (fmanip), . . ,
ostream& fmanip(ostream& s,int w,int n,char fill)
{s.width(w);
s.flags(ios::fixed);
s.precision(n);
s.fill(fill);
return s;}
5. (wp) , ' my_manip, f fmanip. ,
my_man wp(int w,int n,char fill)
{return my_man(fmanip,w,n,fill);}
, <iomanip.h>:
OMANIP(int)
IMANIP(int)
IOMANIP(int)
' . ios enum.
public:
enum io_state{
goodbit, // 000
eofbit, // 001
failbit, // 002
badbit, // 004
hardfail // 008
};
, ' ios, state. int rdstate().
, :
int bad(); 1, badbit hardfail
int eof(); 1, eofbit
int fail(); 1, failbit, badbit hardfail
int good(); 1, goodbit
>> , .
-
' :
ofstream ;
ifstream ;
fstream /.
.
* fstream();
, .
* fstream(const char* name,int mode,int p=filebuf::openprot);
, ' name, , mode p. , . mode=ios::out, , .
ios .
in -
out -
ate - . , .
app - . seekp()
trunc -
nocreate - ,
noreplace - ,
binary -
,
void open(const char* name,int mode,int p=filebuf::openprot);
void fstreambase::close();
, ' . . .
' :
1.
' filebuf
filebuf fbuf;
' filebuf ' ()
fbuf.open(',ios::in);
' filebuf
istream stream(&fbuf);
2.
' fstream (ifstream, ofstream)
fstream stream;
³ , ' filebuf
stream.open(',ios::in);
3.
' fstream, , '
fstream stream(',ios::in);
.
1. () . , , , .
2. 1 ' ( >>) (). ' .
3. .
4. .
5. 2 ' , . ' cout << . ' .
6. ' .
7. 3 ' .
8. , ' .
9. 4 ' .
10. , ' .
11. 5 ( ) .
12. .
.
1. EasyWin- Borland C++5.02.
5 ( ).
2. 1. char* char[].
3. : istream, ostream, fstream, ifstream, ofstream.
4. (. ).
5. read/write, >> <<.
6. , .
7. h-.
8. cpp-.
9. h-.
:
) ;
) :
) - :
) ( ). ..
10. 5- '.
11.ϳ ' ' .
' :
) - seekp(),seekg().
) - tellp(),tellg().
) ' - '.
12. . operator!(), operator void*() bad(), good().
13. , , ', 2 .
14. find(), ' ' . / ' .
15. ' del() repl(), , , , ( find), .
.
1. .
2. .
3. .
4. .
5. find(), del() repl().
6. . , ' , ,
8
. (STL) C++.
.
STL. STL, STL.
.
(STL).
STL , , .
STL .
STL.
: , .
(containers) ', . : , , .
(associative containers) , .
- . : , .
(algorithms) . , , , . (sequence), .
(iterators) ', . , .
, . *, , . iterator, .
' :
1. (input_iterator) , .
==,!=, *i, ++i, i++, *i++
istream_iterator.
2. (output_iterator) , .
++i, i++, *i=t, *i++=t
ostream_iterator.
3. (forward_iterator) / , , :
==,!=, =, *i, ++i, i++, *i++
4. (biderectional_iterator) forward-, (--i, i--, *i--), .
5. (random_access_iterator) biderectional-, , .
i+=n, i+n, i-=n, i-n, i1-i2, i[n], i1<i2, i1<=i2, i1>i2, i1>=i2
STL (reverse iterators). , , .
, STL . ', .
' (allocator), ' .
' ' allocator. .
, . . , : . . UnPred, BinPred. ', .
. ³ (comparison function). , . Comp.
STL '-.
'- - , (). ' - . ' , operatot().
1.
class less{
public:
bool operator()(int x,int y)
{return x<y;}
};
-.
STL .
, , . , . , . map ( ). , , , list. , queue, deque, stack. vector; , .
, , , . STL