#include <stdio.h>
#include <stdlib.h>
int main() {
int a, *b;
system("chcp 1251");
system("cls");
a=134;
b=&a;
printf("\n a %d.", a);
printf("\n a %d.", &a);
printf("\n b %d.",*b);
printf("\n b %d.",b);
printf("\n b %d.", &b);
getchar();
return 0;
}
:
a b:
, : , .
2: ++:,
1)
++ class. , private,
class s {...}
struct s { private:...}
, ++ . ++ , struct , ,
struct s {...};
class s { public:...}
, .
, . : private public.
:
struct complex
{
void assign(double r, double i);
void print() { cout << real << "+"<< imag << "i"; }
private:
double real;
double imag;
};
void complex:: assign(double r, double i=0.0)
{
real = r;
imag = i;
}
:
class complex
{
double real;
double imag;
public:
void assign(double r, double i);
void print() { cout << real << "+"<< imag << "i"; }
};
void complex:: assign(double r, double i=0.0)
{
real = r;
imag = i;
}
2)
, , , . . . . C++ , , , . , . , .
. . , , , . : . . , :
|
|
- ( ).
- extern, ( ) (). , , .
- - .
- , , . ( . .)
- , class T;.
- typedef.
, .
// Declare and define int variables i and j.
int i;
int j = 10;
// Declare enumeration suits.
enum suits { Spades = 1, Clubs, Hearts, Diamonds };
// Declare class CheckBox.
class CheckBox: public Control
{
public:
Boolean IsChecked();
virtual int ChangeState() = 0;
};
, , .
extern int i;
char *strchr(const char *Str, const char Target);
, () . . .
. , ( ). , , . .
, , extern, . , . . .
3)
++ : (public), (private) (protected). . - - . . .
, . . , :
class X {
int i;
int j;
public:
void get_ij();
void put_ij();
};
class Y: public X {
int k;
public:
int get_k();
void make_k();
};
Y get_ij() put_ij() X, i j, X. , . . , . , .
|
|
, , : ? protected (). , . . , protected, . :
class X {
protected:
int i;
int j;
public:
void get_ij();
void put_ij();
};
class Y: public X {
int k;
public:
int get_k();
void make_k();
};
Y i j, . , , . .
private, protected public , . , :
class my_class {
protected:
int i;
int j;
public:
void f1();
void f2();
protected:
int a;
public:
int b;
};
, .
7
1: C.
, .
, .
, . . , , .
:
while - ;
do...while - ;
for - ( ).
while
while() {
;
}
( ), , , . , , , ( ). , , .
int k=5;
int i=1;
int sum=0;
while(i <=k) {
sum = sum + i;
i++;
}
while, , , ( ). ( ),
while(1) {
;
}
while , , .
int k=5;
int n=10;
while(k>n) {
printf(" k=%d n=%d \n", k, n);
k = k + 2;
}
do...while
do {
;
} while();
do...while , , , . , , .
|
|
do...while , , , , .
. 0 10
#include <stdio.h>
#include <stdlib.h>
int main() {
int num;
system("chcp 1251");
system("cls");
do {
printf(" 0 10: ");
scanf("%d", &num);
} while((num < 0) || (num > 10));
printf(" %d", num);
getchar(); getchar();
return 0;
}
:
for
for ( ; ; ) {
;
}
for ( ). :
- ;
- ;
- .
(;). , .
for . . ( ), . . , .
#include <stdio.h>
int main() {
int num;
for(num = 1; num < 5; num++)
printf("num = %d\n",num);
getchar();
return 0;
}
, , .
#include <stdio.h>
int main() {
int num = 1;
for(; num < 5; num++)
printf("num = %d\n",num);
getchar();
return 0;
}
, .
for (,) . . , , .
#include <stdio.h>
int main() {
int i,j;
for(i = 1, j=2; i < 5; i++, j=j+2)
printf("i = %d j = %d\n",i,j);
getchar();
return 0;
}
, :
for (i=0; i<n; i++) { // 1
for(j=0; j<n; j++) { // 2
// 2;
}
// 1;
}
: 0 99
#include <stdio.h>
#include <stdlib.h>
int main() {
unsigned int i,j;
for(i=0; i<10; i++) {
for(j=0; j<10; j++) {
printf("%2d ",i*10+j);
}
printf("\n"); //
}
getchar();
return 0;
}