C++小題(十四)
13006 ワード
/*
#include ()。
: C
#include , <> , ,
#include
*/
//////////////////////////////////////////////////////////////////////////////////
/*
,
: B : B ( )
A for(int a=1;a<=10;a++);
B int a=1;
do
{
a++;
}while(a<=10)
C int a=1;
while(a<=10)
{
a++;
}
D for(int a=1;a<=10;a++)a++;
do while “;”
*/
//////////////////////////////////////////////////////////////////////////////////
/*
myClass , :
myClass::~myClass(){
delete this;
this = NULL;
}
: C : B ( )
a ,
b
c
d , 。
this const, , this = NULL 。
*/
//////////////////////////////////////////////////////////////////////////////////
/*
:
#include "stdio.h"
class Base
{
public:
Base()
{
Init();
}
virtual void Init()
{
printf("Base Init
");
}
void func()
{
printf("Base func
");
}
};
class Derived: public Base
{
public:
virtual void Init()
{
printf("Derived Init
");
}
void func()
{
printf("Derived func
");
}
};
int main()
{
Derived d;
((Base *)&d)->func();
return 0;
}
: B : B ( )
a
Base Init
Derived func
b
Base Init
Base func
c
Derived Init
Base func
d
Derived Init
Derived func
。 , , , 。
: , , , , ,
, , ,
C++ 。
, , 。
, 。
。 , ,
, ,
, 。
*/
//////////////////////////////////////////////////////////////////////////////////
/*
“=” ,
: A : A ( )
“ ”
,
*/
//////////////////////////////////////////////////////////////////////////////////
/*
C++ , ( )
: B : B ( )
A'\\'
B'074'
C'\t'
D'\0'
A , \
B , 3 , '\074' 8 '\074'
C
D
*/
//////////////////////////////////////////////////////////////////////////////////
/*
fclose FILE *fp ?
: A : A ( )
A ,
B , ,
C
D
fclose , 。 fclose() ,
。 :int fclose( FILE *fp ).
fclose FILE 。 fclose FILE ,
fclose() null
*/
//////////////////////////////////////////////////////////////////////////////////
/*
+、=、<<、= [] ,
: D : D ( )
A+ =
B= <<
C== <<
D= []
:
(1) :=、()、[]、->、new、delete。
(2) 。
(3) +=、-=、*=、/=、&=、!=、~=、%=、>>=、<<= 。
(4) , 。
, , , 。
, 。 。 :
operator ( )
{
}
= -1( ++、-- )
= ,
*/
//////////////////////////////////////////////////////////////////////////////////
/*
?
: A : A ( )
100 , 100 , 100 。 1MB
*/
//////////////////////////////////////////////////////////////////////////////////
/*
?
#include<stdio.h>
int main(void)
{
int n;
char y[10] = "ntse";
char *x = y;
n = strlen(x);
*x = x[n];
x++;
printf("x=%s
",x);
printf("y=%s
",y);
return 0;
}
: B : B ( )
x=atse,y=
x=tse,y=
x=atse,y=e
x=tse,y=e
n = strlen(x), n=4, x y , x[4] y[4]='\0',
*x=x[n] x '\0',x++ x t,
x=tse, y '\0', ,y
*/
//////////////////////////////////////////////////////////////////////////////////
/*
const char *ptr;ptr ()
: C : C ( )
;
;
;
;
*/
//////////////////////////////////////////////////////////////////////////////////
/*
,a b ?
class Test{
public:
int a;
int b;
virtual void fun() {}
Test(int temp1 = 0, int temp2 = 0)
{
a=temp1 ;
b=temp2 ;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
};
int main()
{
Test obj(5, 10);
// Changing a and b
int* pInt = (int*)&obj;
*(pInt+0) = 100;
*(pInt+1) = 200;
cout << "a = " << obj.getA() << endl;
cout << "b = " << obj.getB() << endl;
return 0;
}
: A
200 10
5 10
100 200
100 10
32 4 , ,
a 、b 。 test obj pInt ,
pInt , *(pInt+0)=100 ,
*(pInt+1)=200 a , b 。
*/
//////////////////////////////////////////////////////////////////////////////////
/*
static ?
: B C D : B C D ( )
1: , ,
2: ,
3: ,
*/
//////////////////////////////////////////////////////////////////////////////////
/*
, mock 。Mock 。 ?
: A : A ( )
mock : , mock , 。
: , , 。
: , , ; ,
*/
//////////////////////////////////////////////////////////////////////////////////
/*
include<stdio.h>
int fgetc(FILE * stream);
fgetc() stream , 。 , EOF, ferror feof 。
fgetc() , EOF , 。
*/
//////////////////////////////////////////////////////////////////////////////////
/*
,
: B : B ( )
virtual ,
,
,
*/
//////////////////////////////////////////////////////////////////////////////////
/*
:
void test1()
{
unsigned char array[MAX_CHAR+1],i;
for(i=0;i<=MAX_CHAR;i++){
array[i]=i;
}
}
char*test2()
{
char p[] = "hello world";
return p;
}
char *p =test2();
void test3(){
char str[10];
str++;
*str='0';
}
: A : A ( )
0
1
2
3
:
CHAR_MAX , i 。
char -128 127, u char 0~255, i 0~255.
CHAR_MAX 255 , i++ ,i 256 , 。
:
p , , ;
p, p , p , ( )。
。
p, p , p p, p , 。
p , ,p 。
char p[]="hello"; char *p="hello"; 。
:
str++; str=str+1; str , , 。( str+1, str=.)
*/
//////////////////////////////////////////////////////////////////////////////////
/*
c++ ,
const int i = 0;
int *j = (int *) &i;
*j = 1;
printf("%d,%d", i, *j)
?
: A : A ( )
0,1
1,1
1,0
0,0
C++ : const ( ) , , , .
i :
const volatile int i = 0;
int *j = (int *) &i;
*j = 1;
printf("%d,%d", i, *j);
1,1
*/
//////////////////////////////////////////////////////////////////////////////////
/*
:
int a = 5, b = 4, c = 3, d = 2;
if (a>b>c)
printf("%d
", d);
else if ((c - 1 >= d) == 1)
printf("%d
", d + 1);
else
printf("%d
", d + 1);
: B : B ( )
2
3
4
*/
//////////////////////////////////////////////////////////////////////////////////
/*
:
: B : B ( )
char str[2] = {"a","b"};
char str[2][3]={"a","b"};
char str[2][3]={{'a','b'},{'e','d'},{'e','f'}};
char str[] = {"a", "b"};
*/
//////////////////////////////////////////////////////////////////////////////////
/*
struct T {
char a;
int *d;
int b;
int c:16;
double e;
};
T *p;
64 64 ,
: C : C ( )
sizeof(p) == 24
sizeof(*p) == 24
sizeof(p->a) == 1
sizeof(p->e) == 4
1) ;
2) , ;
3) , 。
, ,
【2】 :
P ,64 8
32 a_ _ _ _ _ _ _ | * d 8 | | b4 | |c2 |_ _ |e8 |
double 8
*/
//////////////////////////////////////////////////////////////////////////////////
/*
B A , B A ( ) ?
: A C : A C ( )
public
private
protected
*/
//////////////////////////////////////////////////////////////////////////////////
/*
:
: C D
C++
n , 0-n-1
*/
//////////////////////////////////////////////////////////////////////////////////
/*
x=2014, ()
int fun(unsigned int x)
{
int n=0;
while((x+1))
{
n++;
x=x|(x+1);
}
return n;
}
: C : C ( )
20
21
23
25
x&(x-1) 1 ,x|(x+1) 0
*/
//////////////////////////////////////////////////////////////////////////////////
/*
int a[3][4], a[1][1]?
: C : C ( )
*(&a[0][0]+5)
*(*(a+1)+1)
*(&a[1]+1)
*(a[1]+1)
*/
//////////////////////////////////////////////////////////////////////////////////
/*
Windows 32 , 4, A,sizeof(A) ()?
: B : B ( )
0
1
2
4
, 。
, , , 。
, sizeof 1, 0. 1.
*/
//////////////////////////////////////////////////////////////////////////////////
/*
,
: D : D ( )
A
B
C
D
, 。
, , 。
, 。
, 。
this , this 。
, 。
*/
//////////////////////////////////////////////////////////////////////////////////
/*
#include<stdio.h>
main( )
{
int a=-2,b=0;
while(a++) ++b;
printf("%d,%d
",a,b);
}
?
: B : B ( )
1,1
1,2
2,1
2,2
while a++, , -2 0 , a ++b ,a -1,b 1。
while ,a 0,b 2。 while , , 1, 。 1 2。
*/
/////////////////////////////////////////////////////////////////////
/*
?
main() {
int i=0;
switch(i) {
case 0:
i++;
printf("%d..",i);
case 1:
printf("%d..",i);
case 2:
printf("%d..",i);
}
}
: C : C ( )
..1..1..0
0..1..1
1..1..1..
1
break, switch。
case , , “ ”。
, switch , ;
case , break , case , ,
break , switch 。
, case switch , break 。
*/
/////////////////////////////////////////////////////////////////////
/*
?
: C : C ( )
*/
/////////////////////////////////////////////////////////////////////
/*
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
int m=0123, n = 123;
printf("%o %o
", m, n);
return 0;
}
()
: C : C ( )
0123 0173
0123 173
123 173
173 173
*/
/////////////////////////////////////////////////////////////////////
/*
? ?
template<class T> class Foo{
T tVar;
public:
Foo(T t) : tVar(t) { }
};
template<class T> class FooDerived:public Foo<T>
{
};
int main()
{
FooDerived<int> d(5);
return 0;
}
: D : D ( )
A 。
B ,FooDerived , 。
C ,tVal 。
D , FooDerived 。
, , ;
, 。
template<class T> class Foo{
T tVar;
public:
Foo(T t) : tVar(t) {}
};
template<class T> class FooDerived:public Foo<T>
{
public:
FooDerived(T a):Foo<T>(a){}
};
*/
/////////////////////////////////////////////////////////////////////