C++復習1

4731 ワード

C++復習1
ヘッダファイル
1.  C++  ,     C 
2.C++       .h,
3. C++     C           .h    c

しゅつりょくしゅつりょく
cin      ,   
cout        ,   
      hex/oct/dec         ,
                             ,
            cin/cout(    )  
cerr      ,   

PS:
------------------------------------------------------------------------ c cin istream_withassign stdin cout ostream_withassign stdout cerr ostream_withassign stderr clog ostream_withassign stderr ------------------------------------------------------------------------ << >> ------------------------------------------------------------------------ C , printf strlen / setw( ): , , cout << "hello" << setw(10) << "1234567890" << endl;

ネームスペース:
std        
::       

using     ::       ;
using namespace     ;

変数の定義と割り当て:
1.          
       :         
     :for(int i=0; i<10; i++){}
2.    
    int n = 250;
    int n(250);

stringクラス:
1.        
    string str("hello");
    string str = "hello";
    string str(10,'b');//   “bbbbbbbbbb”
    string str = str1 + str2;
    string str = str1 + "hello";
    string str[2] = {"hello","world"}; 
2.   
    str.size()
3.  
    if(str.empty() == 1)
4.      
    string str("hello");
    string str[2] = {"hello","world"};
    cout << str1[0] << endl;//   h
    cout << str2[0][0] << endl;//   h

bool:
1.     
    bool i = true;
    bool i = false;
2.bool           false
3.     1   
    sizeof(bool)

ポインタと参照の違い:
(1)  :       ,               ,           ;
                      ,              。
     :
        int a=1;int *p=&a;
        int a=1;int &b=a;
                      p,       a     , p   a       。
       2          a     a   b,   a b      ,            。
(2)       ,         (int **p;     int &&a     )
(3)        ,         NULL,               ;
(4)             ,          ,                 。
(5)"sizeof  "          (  )   , "sizeof  "           ;
(6)        (++)       ;

const:
1.C C++  const  
    C    const     ,     ,      ,          
    C++  const     ,     ,    ,        
2.const    
    const int *p    //*p         
    int * const p   //p         
    const int * const p //p         ,*p          
3.const    
    “  ”       (          ):
                           ( )  
        const              ( )  ,    ( )  
4.const    
                            
    const                            

関数:
1.  :
    (1)  
    (2)  
    (3)   
            ,          (    )
2.    
      :
        int fun(int,int){}
      :
        fun(  1,  2)
                 ,     ,      。  
3.    
      :
        int fun(int i=0,int c=0){}
             ,         。
                       ,         。
      :
        fun();
        fun(1);
        fun(1,2);
                   
4.      
    int& fun(void){}
          
        fun() = 100;
5.     
      const  ,     
     const  ,       

関数の再ロード:
                                          

    (    )    
    1)     ,     
    2)                 
    3)      
PS:
    Main      
              

インライン関数:
1)inline        
2)__attribute__((always_inline))        
PS:
             ,         
               :
            i)         ,      
                #define MUL(a,b) a*b
                MUL(2,2+3);
            ii)          
                #define MAX(a,b) (a>b)?a:b
                MAX(++a,--b);
              

強制型変換:
1)  C   
    1.1)    
        char b;
        int a = b;
    1.2)    
        int b;
        char a = (char)b;
2) C++        
    reinterpret_cast(expression)
    dynamic_cast(expression)
    static_cast(expression)
    const_cast(expression)

struct構造体
C++            
C++        ,      struct
 :
    struct msg{
        int a;
        int (*fun1)(int i);
        void fun2(){
        };
    };
    msg a;

新キーワードとdeleteキーワード
new        malloc(   );
delete     free(    );
 :
    int *i = new int(0);
    delete i;
    int *p = new int[10]{(0)};
    delete p;