. , . .
- .
:
private () - ;
protected () private, - ;
public () , .
, , .
.
class Derived: _ Base1 [, _ Base2,...] {...};
. , .
. - , , (private < protected < public).
. private public .
private, private . , public .
. .
ß
class Base {
public:
void f1();
};
class Derived: private Base {
public:
Base::f1; // f1()
};
private, public .
, .
. .
date
ß
class date {
protected:
int day, month, year;
public:
date(int,int,int);
void set_year(int y);
void print();
};
date::date(int d, int m, int y)
{
day = d; month = m; year = y;
}
void date::set_year(int y)
{
year = y;
print();
}
void date::print()
{
printf("%d-%d-%d\n", day, month, year);
}
birthday:
ß
class birthday: public date {
public:
char name[80];
birthday(int d, int m, int y, char* n);
void print();
};
birthday::birthday(int d, int m, int y, char* n): date(d, m, y)
{
strcpy(name, n);
}
void birthday::print()
{
printf("%d-%d-%d, dear %s\n", day, month, year, name);
}
ß
date* d = new date(19, 10, 1951);
|
|
birthday* b = new birthday(19, 10, 1951, "Bond");
d->set_year(2001);
b->set_year(2001);
: "19-10-1951" .
date::print() ,
ß
virtual void print(int y);
:
"19-10-1951"
"19-10-1951, dear Bond".
, virtual.
:
static;.
virtual , .
, , .. .
virtual void print() = 0;
, , , . .
, . , , (vtable), . vtable.
, , vptr . , vptr .
vtable, vptr , . ( ), ( ). set_year (), , print(), .
Birthday Birthday*, Date*. .
.
ß
Date *pd;
pd = new date(10, 10, 2000);
pd->print(); // date
delete pd;
pd = new birthday(10, 10, 2000, Peter);
pd->print(); // birthday
, - (pd->print() print() set_year() ), . , vptr , , .
. ++ .
++ , .
class AB: public A, public B {...};
AB A B. , :
|
|
A:: B::.
, . .
, A B R ( ).
R R R
| | / \
A B A B
\ / \ /
AB AB
) )
AB R R AB , R :
A:: B::.
AB , R, A B.
ß
class A: virtual public R {...
class B: virtual public R {...
, .1-, .
. .
ß
class C0 {
public:
void f() { cout << "f from C0" << endl; };
};
class C1: public C0 {
public:
void f() { cout << "f from C1" << endl; };
};
class C2: public C0 {
public:
void f() { cout << "f from C2" << endl; };
};
class C3: public C1, public C2 {
public:
void f() { cout << "f from C3" << endl; };
};
///////////////////////// ///////////////
C3 c;
c.C1::f(); // uses vtbl for C1
c.C2::f(); // uses vtbl for C2
// ((C1)c).f(); - !
((C0)(C1)c).f();
, .
. .
ß
//
class Complex {
public:
Complex(float re, float im)
{real = re; imag = im;};
}
//
class Triplex: public Complex {
public:
Triplex(float re, float im, int co):
Complex(re, im) { color = co;};
}
- , .
ß
Triplex(float re, float im, int co):Complex(re, im), color(co) {};
-, - -, .
:
1-3 ;
4-7 -;
: , -.
2 ++
3.1.
. . , ( , , ). :
vector<> (, vector<int> vector<string>)
<vector> std:
using std::vector;
, . :
vector<int> v(n);
n , .
: v[3] = 100;
, - size().
for (int i=0; i < v.size(); i++)
cout << v[i];
|
|
. , . :
vector<int> v;
-
push_back(), , :
int elem;
for (int i=0; i<n; i++) {
cin >> elem;
v.push_back(elem);
}
- . , , , , :
// n m -
vector<vector<int> > ma(m); //
for (int i=0; i<m; i++) {
vector<int> line(n);
ma[i] = line;
// : ma[i] = vector<int>(n);
//
for (int j=0; j<n; j++)
cin >> ma[i][j];
}
:
for (int i=0; i<ma.size(); i++)
for (int j=0; j<ma[i].size(); j++)
cout << ma[i][j];
sort() reverse(). ( ) ( ). . - begin(), - end(). <algorithm>:
#include <algorithm> // sort() reverse()
using std::sort;
using std::reverse;
vector v(10);
// v
sort (v.begin(), v.end()); //
reverse (v.begin(), v.end()); //
max_element(). , ( ). max_element() *.
int max_from_vector = *max_element(v.begin(), v.end());
min_element().
.
1
, c ( ) ,
a[1,1] a[2,1] a[3,1]
a[1,2] a[2,2]...
2
, c ( ) , , ,
a[3,1] a[2,1] a[1,1]
a[3,2] a[2,2]...
3
, c ( ) , ( , .. ) .
4
, c ( ) a b, c d a b, c d .
|
|
5
, c ( ) , , .
6
, c ( ) a, b:
- a, , ;
- , ;
- ,
b
7
, c ( ) , , , , . .
8
, c ( ) , , , .
9
, c ( ) ,
(1 1, 2 2 ..) : . " - "
:
: 1 1 1 2 2 2 2 3 4 4 4 5 5 5
: 1 3 2 4 3 4 3 5 3
1 - 1
2 - 0
3 - 3
4 - 1
10
, c ( ) , ( , ) , , . " - "
:
: 1 2 3 4 1 3 5 2 1
: 1 1 3 3 3 3 1 1 1 5 5
1 - 0
2 - 2
3 - 1
4 - 1
4 ++
4.1. .
- (string-). , .
string std:
#include <string>
using std::string;
, :
string s = "test";
string s2 = s;
string s5; //
:
s[2] = 'k'; // s == "tekt"
,
():
string s3 = s+s2;
s += " end of string";
, :
s2 = s;
s = "test2";
:
cout << s << endl;
, - length():
cout << s3.length();
:
if (s == s2) { }
-. :
1) substr(), :
s = "this is a string";
s2 = s.substr(0, 4); // s2 == "this"
2) insert(),
s.insert(10, "new "); // s == "this is a new string"
3) find(),
int i = s.find ('a'); // 8
i = s.find ("is"); // 2
i = s.find ('w'); // - -1
,
i = s.find ("is",3); // 5 - , 3
4) rfind() - ,
i = s.rfind ("is"); // 5
5) replace() -
s.replace(0, 4, "that"); // "that is a new string"
|
|
6) erase() -
s.erase(0, 5); // "is a new string"
4.2. .
:
vector<string> vs;
:
vs.push_back("string 1");
vs.push_back("string 2");
sort (vs.begin(), vs.end());
Visual C++ 6.0 , , . - , . , , #pragma
#pragma warning (disable: 4786)
4786 - ( -
). .
4.3. -.
( string) , ( ). >> (, ):
string s;
cin >> s;
.
, , getline(). , - string, , .
string s;
getline(cin, s, '\n');
- - s.length().
Visual C++ 6.0 getline() , . ( , lab-3.1bug.txt).
>>, getline() , . , .
, ,
cin.get().
char ch = cin.get();
( ) ch.
cin.get();
, .
1
:
a) .
, c
, ,
( ).
""-" ".
2
:
a) .
, c ,
( ).
.
3
:
a) .
, c ,
"" - ", ":
1 4 12
2 5 14
4
:
a) .
, c ,
. " " - "
". .
5
:
a) .
, c ,
(a a, b b b ..)
:
.
" " - "
".
6
:
a) .
, c ,
"- ". .
7
:
a) .
, c ,
(, 70)
, ( "
" (word wrapping).
. .
8
:
a) .
, c ,
(, 70)
, (text
justification). .
.
9
:
a) .
, c
" " (dissociated text), ..
, ,
(-,
- ..)
+ (, ..).
. ,
.
10
:
a) .
, c
( grep).
, , ,
.
'*'.
5 ++
1.
1.
- , std <algorithm>, , . . , - , . , , ( , ). - begin() end(), .