.
// #include <iostreara> using namespace std; inline int even (int x) { return! (x%2); int main () { if (even (10)) cout ≪ "10 \"; if (even (11)) cout ≪ "11 \"; return 0; } </iostreara> |
even(), , . ,
if (even(10)) cout ≪ "10 \n"; |
if (! (10%2)) cout ≪ "10 \n"; |
. : . , , . even() main().
13:
1: . .
- char - ;
- int - ;
- float - ;
- double - ;
- void - .
, , .
. :
.
.
, .
, .
,
,
.
()
- .
- , .
- ( ), .
- , - , .
, , . . ().
, 32 , a - z, A - Z, 0 - 9 (_). .
, , 32 , 8 . , . , , .
|
|
.
int a = 2, b; b = a+3; | Int a=2; // int INT a=2; |
int a = 2, b; b = A + 3; // | |
int a = 2; b = a + 3; // b |
.
0 2n-1, n- .
-2n-1...+2n-1-1. (0 , 1 ).
:
unsigned char 0...255 | char -128...127 | |
unsigned short 0...65535 | short -32768...32767 | |
unsigned int | int | |
unsigned long int | long int |
. . ( 0) . . , 12345,678
12345,678 = 1,2345678104
0,009876
0,009876 = 9,87610-3
, , 1. (, 0), 0.
, , .
4 .
- , (0 , 1 - ).
- 2, . 2 , , 2 ,
N-1
n - , .
- , 1, 1.
- , .
:
- | - | ||||
float | 30...23 | 22...0 | |||
double | 62...52 | 51...0 | |||
- | long double | 78...64 | 62...0 |
, float double . , . , float :
: -178,125 32- ( float).
:
17810 = 101100102.
0,12510 = 0,0012. 178,12510 = 10110010,0012=1,01100100012111
( 7 ).
:
|
|
0111111+00000111 = 10000110.
, -178,125
. . char.
: - , .
. .
int n; // n
double a; // a
2: C++..
, , . , . .
. ,
;
;
.
.
static, - -.
, , .
:
::
x - cl,
cl::x
cl.
-:
class X {
public:
int n;
static void func(int v);
void f(int v);
...
};
int main() {
X obj;
obj.f(10) //
X:: func(10); //
return 0;
}
- . private public. private, .
, .
-
- , -. :
class example {
public:
static int mask;
};
int example::mask = 0; //
#include <iostream>
using namespace std;
class temp {
static int number;
static char name[30];
int id;
public:
void setid(void);
int getid(void);
static char* getname(void);
};
int temp:: number = 0;
char temp:: name[] = "ClassName";
|
|
char* temp:: getname() {return(name); }
void temp:: setid(void) {
number++;
id = number;
}
int temp:: getid(void) {return(id);}
int main() {
temp a, b;
a.setid();
b.setid();
cout << "a.id = " << a.getid() << endl;
cout << "b.id = " << b.getid() << endl;
cout << temp::getname();
cin.get();
return 0;
}
14:
1: C.
:
4
. . .
malloc() free() . malloc() , free() . stdlib.h :
void *malloc(size_t size);
void *free(void *p);
malloc() void; . size. , 0. , , sizeof().
:
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
int *p, i;
p = (int *) malloc(100 * sizeof(int)); /* 100
*/
if (!p)
{
printf(" \n");
exit(1);
}
for (i = 0; i < 100; ++i) *(p+i) = i; /* */
for (i = 0; i < 100; ++i) printf("%d", *(p++));
free(p); /* */
}
, malloc(), , ( ).
2: C++..
- , . date:
class date {
int day, month, year;
public:
set(int, int, int);
};
, , .
, . , . , . , .
class date {
int day, month, year;
public:
date(int, int, int); //
};
, :
date today = date(6,4,2014);
date xmas(25,12,0); // (xmas - )
// date my_burthday; // ,
, :
class date {
int month, day, year;
public:
date(int, int, int); //
date(char*); //
date(); // :
};
, . , :
|
|
date july4(" 27, 2014");
date guy(27, 2, 2014);
date now; //
( ) .
, , . , .
, .
class date {
int month, day, year;
public:
date(int, int, int);
date(char*);
date(); //
};
, , , :
date date2 = date1;
, :
, , ;
, , return , .
:
date2 ;
;
, , .
-:
date1 ;
;
- return.
, - , ,
, -. , . - -:
class String {
char *str;
int size;
public:
String(String&); //
};
String::String(String& right) { //
//
s = new char[right->size];
strcpy(str,right->str);
}
, . . . "" ~. , X ~X(). , , .
class date {
int *day, *month, *year;
public:
date(int d, int m, int y) {
day = new int;
month = new int;
year = new int;
*day = d;
*month = m;
*year = y;
}
~date() { delete day; delete month; delete year; }
};