. , , , , , , . , : , , ; . "" , .
, . , , . , , .
, , :
1. , , , ..
, .
, : , , "". , , . , , , , , ..
, . , : , .. , ? .
2. , , .
. , , .
3. , .
.
4. , - .
, . , , . , , , .
|
|
5. .
.
, , . .
, , , .
- , .
;
;
;
.
, -. , :
;
;
.
, , , .
. . , . , .
, , , ..
, , , , .
. . . , .
. , , , . . , "" " ". , , , . , .
, . , , , .
, , ( , " , "), "" . , , "" .
|
|
, . , , . . : , .
, , . , , , . . : , ( ). , - -. , , .
, , . , , , , . , . .
. , , , . . , . : , , , . , . , .
. . , , . , , , , , .
, , . , .
, . , , . , , .
++.
typedef float Temperature; //
typedef unsigned int Location; // ,
//
Temperature Location , . Temperature . Location , .
|
|
. . : . , " " : "" " ".
struct TemperatureSensor {
Temperature curTemperature; //
//
Location loc; //
void calibrate (Temperature actualTemperature); //
Temperature currentTemperature (); //
};
TemperatureSensor. , , -, , , , -, , .
, :
TemperatureSensor TSensors[100]; //
// TemperatureSensor
, , -. . , :
TSensors [3]. calibrate (0.); // 3
, -, , TemperatureSensor, , . this. , calibrate
curTemperature = actualTemperature;
this -> curTemperature = actualTemperature;
. , ( ) . (.. , ) (.. , ).
, currentTemperature. , , .
, . , . , , .
++ assert.h.
- . , , .
++ , . try ( ). , try, , , catch try. catch . try , .
|
|
throw -. catch, throw. try (, - ) . , throw, , try .. , catch.
. , .
int stack[100]; //
int top=-1; //
void push (int el) {
if(top == 99) throw (1); //
else stack[++top] = el; //
}
int pop () {
if(top == -1) throw (0); //
else return stack[top--]; //
}
main () {
int i = 0, k;
...
try{ //
push (i);
...
k = pop ();
...
}
catch(int error){...} // error = 0, ;
// error = 1,
}
, . , .
, , . , .
. . pop ( ) push ( ). , , , . , . , , . .
: , . , .. , . , . - .
. , , . . .
pop push, .
. , , .
struct Stack {
int s[100];
int top;
void push(int el);
int pop();
};
pop push . . . , Stack.
Stack Stack. , Stack. :
class Stack {
private:
int s[100];
int top;
public:
void push(int el);
int pop();
bool isFull () const;
bool isEmpty () const;
};
Stack , private public. (public) . (private) - .
, . friend.
Stack , . , ++ , , , . , class, .
|
|
, , . , top, , . const. , .
, .
, . , - , , - , . . , , . .
: , , . , , . , . ++ , . .
, .
, , . , .
C++ , .
. , .
.
, , , , , . , . . .
, stack.h.
, ( ), () , . , stack.h
void push(int el);
int pop ();
, , user.c:
#include "stack.h" //
main (void)
{
push (1);
if (pop ()!= 1)...; //???
...
}
, Stack, , , stack.c:
#include "stack.h" //
int stack [100]; //
int top;
void push (int el){...}
int pop (){...}
user.c stack.c , stack.h. . . 2.1.
. 2.1 ,
Stack, , , , ( ) ++.
,
Stack My_stack;
, . , , . .
, ++ , . - -. .
stack.h Stack, . 2.2.
, , .
- .
. . . , . , , .
, :
;
;
;
, .
: . :
, , ;
, ;
; ;
.
, .
. , , . , , . , .
: , , ().
:
, , .
, , . . . .
, . .
, .. . .
. , , . - , . , . .
.
, .
"" "".
"" , , , , .
, ( ), - 6L ( - 6L ). "" , , , , , .
- "" () ( -).
, - -. - -, . , . .
" " . , .
. . -. , , , , .
, . " ". , -. .
, , . , ; , .
. . " " ( ).
" " "". " " "" , . , , , .
. . " " "". . " " . .
. , , . " ": , .
"" (/). "" , .
, , . - : , .
. ( ). () , , . , , . , , . , .
() , , , .
. , , . , , . , . , .
:
typedef char* Pchar;
Pchar p1, p2;
char *p3 = p1;
, typedef, , . C++ .
C++ . , , .
- ++ .
() . .
.
. , . ,
int i = 1;
double a, b = 4.5;
a = b + i;
i double double. , i , i, .
. . ,
double a;
a = 1/2; // , a = 0
a = 1./2.; // , a = 0.5
- "operator ", , , . . :
class complex {
double re, im;
public:
...
complex operator+(complex);
complex operator*(complex);
};
: , + *. , b c complex, b+c, ( ) b.operator+(c). , .
. 4.3 4.5 .
++ .
static_cast , . ,
int i;
double a, b;
...
= static_cast < double > (i);
= static_cast < double > (static_cast < int > (b) + 1);
reinterpret_cast () . ,
i = reinterpret_cast < int > (&x);// -
const , . , const_cast.
static_cast , , , . , dynamic_cast. . 4.3.
, . . .
( ) . , .
, .
.
, , (.. ). .
, - , , . .
, .
.
, . (, ), . :
;
;
;
, ;
, ;
, .
, , . , , . , . , - .
. , . , . , .
.
, , () , .
, . . , ; ; " " "" .
. , . , . , , . , . ( ), , . , " , " . .
: . , . , , , . . :
( ) ( ) . .
. () , , . , , . , .
(, ), " ".
, , , . , , , .
, , , . " ", . . , , , . .
. , . 3, , . , . , .
, .
, ; . .
, .
, push pop , - ( ).
- . C++ , - . .
, . , .
. , . , . , ( ).
. , .
, . :
, ;
, , ;
/ ; ++ , ;
, , , / ; ++ , "" "~".
. , . , , . , , .
:
, , , ;
, , , ;
new delete;
- .
. Stack, ( s).
class Stack {
public:
Stack (int n); // , n
~Stack (); //
void push (int el); //
int pop (); //
bool isFull () const; //
bool isEmpty () const; //
...
};
.
. complex.
class complex {
double re, im;
public:
complex (double r, double i);
complex (double r);
...
};
, ( ) .
complex a = complex (1);
complex a = 1;
, explicit.
class Stack {
public:
explicit Stack (int n); // ,
... // ,
};
. , , . :
class {
const int i;
Stack &ps;
X(int ii, Stack &s): i(ii), ps(s){...} // i ii, ps s
};
- . , , C++, .
, , . C++ -.
bool check_stack (Stack & my_stack, int el)
{
Stack temp_stack;
... // temp_stack, ,
// my_stack el
}
, . , .
, .
- .
. .
struct Point {
int ; //
int ; //
Point (void); // (0,0)
Point (int xValue, int yValue); //
};
Point (,). : , .
, (DisplayPoint). , . C++:
class DisplayPoint {
public:
DisplayPoint (); // (0,0)
DisplayPoint (const Point& location); //
~DisplayPoint (); //
void draw (); //
void move (const Point& location); //
Point location (); //
...
};
const. , , , . , , , const&- const &-.
DisplayPoint:
DisplayPoint Item1;
DisplayPoint * Item2 = new DisplayPoint (Point (75,75));
DisplayPoint * Item3 = new DisplayPoint (Point (100,100));
DisplayPoint * Item4 = 0;
(. 3.1 ). Item1, Item2, Item3, Item4. Item1 DisplayPoint, . , Item2 Item3 . , Item2 Item3, , "" (, *Item2). , Item2 , *Item2.
, . .
(. 3.1, ):
Item1.move (Item2 -> location ());
Item4 = Item3;
Item4 -> move (Point(38, 100));
Item1 , Item2, . Item4 , Item3. Item1 , Item2, , . , *Item3, Item4.
. 3.1
, , .
- . , , , . , Item3, Item4 : . (. 3.1, ):
Item2 = &Item1;
Item4 -> move (Item2 -> location());
: Item2 , Item1. Item1 . , : , Item2, , .
C++ , , . , , . . .
, , , , :
DisplayPoint (const DisplayPoint &); //
, , . . , .
. DisplayPoint , :
class DisplayPoint {
...
Point * DPoint
...
};
...
DisplayPoint Item1;
...
DisplayPoint Item2(Item1);
Item1 , Point Item1 Item2 , (. 3.2). , . ?
C++ . , DisplayPoint :
DisplayPoint operator= (const DisplayPoint &);
. 3.2
DisplayPoint Item5;
Item5 = Item1;
, , .
. . -, . -, . , . 3.1, , Item1 Item2 . Item2 Item3 .
++ , , :
int operator == (Point&);
int operator!= (Point&);
: . , " , , ". .
, : , , . - : .
. , , . , - .
. , - .
:
;
( ) ;
;
- .
"-" , , , , (), ().
. , Controller. h Heater ().
class Controller {
Heater h;
...
};
Controller , h ( ). Controller, . h ( ), h.
, . , , . , , . , .
, . .