.


:




:

































 

 

 

 


C++ , - . .

, -, . .

, ( ):

/ private- protected- public-
private- private private
protected- protected protected
public- protected public

, , , , , .

 

, . .

, public,private protected. , ( ) private, . , struct public.

, :

private , .

protected , .

public , .

21.

K , . , , , :

DName(DBList): BName(BList)

{ // }.

DName , DBList , BName , BList .

DBList, .

.

class base {

int i;

public:

base(int n) {cout << ʔ<< endl; i = n;}

~base() {cout << ʔ<< endl;}

void showi() {cout << << i << endl;}

};

 

class derived: public base {

int j;

public:

derived(int n, int m): base(m)

{cout << ʔ<< endl; j = n;}

~derived() {cout << ʔ<< endl;}

void showj() {cout << << j << endl;}

};

 

int main()

(

derived ob(10, 20); //

ob.showi(); //

ob.showj(); //

return 0;

)

? .

, .

1. . , .

2. . . , .

:

class DName: Spec1 BName1, , SpecN BNameN

{ // };

DName , Spec1 BName1 ..

, :

DName(DBList): BName1(BList1), , BNameN(BListN)

{ // }.

DName , DBList , BName1 BList1 ..

 

22.

(polymorphism) ( polymorphos) - , , . , - , . . , , : abs(), labs() fabs(). , . ++ abs(). , , , . ++ . (function overloading).

, " , ". , . , , . , , . , , . . , , , , .

. , , . , , + , , . , . ++ , , . (operator overloading).

, .

, -, , , . C++ ,
( vtable) , . , . .

, . , .

, = 0.

class Shape { public: virtual void draw() = 0; ... };     //  

, , . , , .

Shape s; Shape *s; Shape f(); void f(Shape s); Shape& f(Shape &s); // : // // // //

, , , , .

23.

́ (. template) C++, , (, , , ).

(). S E. D S D S. . . Rn . L2-, . , , , , . , 20 .

, (classdef*, int, char* ..) , nlist: slist. ( ), . , C (#4.7 #.11.1), . .

, (generic) slist, gslist, . generic.h:

#include slist.h

#ifndef GENERICH #include generic.h #endif

#ifndef , , generic.h . GENERICH generic.h.

name2(), generic.h ,

:

#define gslist(type) name2(type,gslist) #define gslist_iterator(type) name2(type,gslist_iterator)

, , gslist() gslist_iterator():

#define gslistdeclare(type) \ struct gslist(type): slist (* \ int insert(type a) \ (* return slist::insert(ent(a)); *) \ int append(type a) \ (* return slist::append(ent(a)); *) \ type get() (* return type(slist::get()); *) \ gslist(type)() (* *) \ gslist(type)(type a): (ent(a)) (* *) \ ~gslist(type)() (* clear(); *) \ *); \ \ struct gslist_iterator(type): slist_iterator (* \ gslist_iterator(type)(gslist(type) amp; a) \: ((slist amp;)s) (**) \ type operator()() \ (* return type(slist_iterator::operator()()); *)\ *)

\ , .

, nlist, :

#include name.h

typedef name* Pname; declare(gslist,Pname); // gslist(Pname)

gslist(Pname) nl; // gslist(Pname)

declare () generic.h. , gslistdeclare, . declare . name*, typedef.

, . , , . gslist #7.6.2.

24.

, , . , , , , . , catch- , try-. , , . , , . C++ catch. - try- , catch , .
Catch- : catch, , ( ), . catch-, . catch- pushOnFull popOnEmpty main() :

catch (pushOnFull) { cerr << "trying to push value on a full stack\n"; return errorCode88;}catch (popOnEmpty) { cerr << "trying to pop a value on an empty stack\n"; return errorCode89;}

catch- ; pushOnFull, popOnEmpty. , . ( 19 , : .) , - pop() iStack popOnEmpty, . cerr, main() errorCode89.
catch- return, ? , catch- . return main(). catch- popOnEmpty , main() 0.

int main() { iStack stack(32); try { stack.display(); for (int x = 1; ix < 51; ++ix) { // , } } catch (pushOnFull) { cerr << "trying to push value on a full stack\n"; } catch (popOnEmpty) { cerr << "trying to pop a value on an empty stack\n"; } // return 0;}

, C++ : , , . - pop(), .

, catch . , catch- , , "" . catch- .

throw;

-. , catch-:

catch (exception eObj) { if (canHandle(eObj)) // return; else // , // catch- throw;}

- . , catch- , . - . ?

enum EHstate { noErr, zeroOp, negativeOp, severeError }; void calculate(int op) {try { // , mathFunc(), zeroOp mathFunc(op); } catch (EHstate eObj) { // - // - eObj = severeErr; // , // severeErr throw; }}

eObj , catch- -, eObj -, . , - zeroOp.
-, catch- :

catch (EHstate &eObj) { // - eObj = severeErr; // severeErr throw;}

eObj -, throw, . .
, catch- , - . ( 19.2, , catch- .)

, , , . , , , :

void manip() { resource res; res.lock(); // // // , res.release(); // , }

, , . , ( , , ), , . , catch, (...) .
:

// catch (...) { // }

catch(...) . catch- , :

void manip() { resource res; res.lock(); try { // // , } catch (...) { res.release(); throw; } res.release(); // , }

, manip() , catch(...) , . . , . ( 19.)
catch(...) catch-. , try-.
Catch- , , . , . , catch(...) catch-, , :

try { stack.display(); for (int ix = 1; ix < 51; ++x) { // , }}catch (pushOnFull) { }catch (popOnEmpty) { }catch (...) { } // catch-

11.4
, C++ .
11.5
. throw, -, :

(a) class exceptionType { }; catch(exceptionType *pet) { }(b) catch(...) { }(c) enum mathErr { overflow, underflow, zeroDivide }; catch(mathErr &ref) { }(d) typedef int EXCPTYPE; catch(EXCPTYPE) { }

11.6
, .
11.7
, catch .
11.8

 

, 11.3, : , operator[](), - catch-. , operator[]() .

25.

(. Run-time type information, Run-time type identification, RTTI) , . C++ [1] dynamic_cast typeid ( typeinfo.h), .

dynamic_cast . , void*.

, .

, .

void*, , , , .

typeid[2] type_info, , .

, , . , . , . , , - .

26.

(STL - Standard Template Library) - , , , , .

STL . .

:

Q ,

Q ,

Q .

, STL ,

Q ,

Q ,

Q .

- , .

STL :

q

q .

( ).

.

STL

- , . .

STL vector, <vector> std.

vector , . , , . vector :

template <class , class = allocator<T>>

Class vector

{

//

}

new() delete (). , . :

//

vector<int> vints;

//

double vector<double> vDbls;

, . , , :

vector<int > int s(50);

- size ():

Size_type size() const;

resize . :



<== | ==>
| ә ә
:


: 2016-12-18; !; : 384 |


:

:

: , , , , .
==> ...

1462 - | 1335 -


© 2015-2024 lektsii.org - -

: 0.073 .