chapter 3オブジェクト、タイプ、値(reading notes)

8060 ワード

chapter 3オブジェクト、タイプ、値
  • 0. 著作権声明
  • 3.1 input
  • 3.1.1 notation & explanation
  • 3.1.2 function
  • 3.2変数
  • 3.3型セキュリティ
  • 3.3.1明示型変換
  • n. reference

  • 0.著作権声明
  • C++シリーズ読書ノートはBjarne Stroustrup著『C++プログラム設計原理と実践(基礎編)』;1
  • このシリーズのノートは利益を目的とせず、学習過程の知識の要点と心得を記録するためにしか使われていない.
  • 権利侵害がある場合は、本人に連絡してください([email protected])、確認した後、直ちに削除する.
  • 転載は出典を明記してください.

  • 3.1 input
    3.1.1 notation & explanation
  • cin: abbreviation of “character input” ;
  • cin stringを読み取ると、空白文字に遭遇すると終了し、e.g.スペース、改行、tab;
  • カスタム名は、実装およびシステムエンティティのために保持される下線の先頭を使用しないでください.マクロとして通常保持される大文字を使用する名前はすべて使用しないでください.CPPと標準ライブラリでは大文字が使用されないため、カスタムタイプは頭文字を大文字にすることができます.0、o、1、iの命名を避け、混同しないようにする.
  • typedef:タイプ別名を定義します.
  • typedef int INT:定義タイプintの別名はINT;

  • decltype;
  • decltype(i) a:変数aを定義し、その変数タイプはiと同じである.


  • 3.1.2 function
    function
    description
    sqrt()
    平方根を求める
    3.2変数
    // ***********************************************************************************************
    
    string name = "Andrew" + ''Viterbi" ;  
                         
    //            ;
    // string    ' + '       ;
    
    // ***********************************************************************************************
    
    cin >> first_name >> age ;       
                                  
    // cin         ; 
    // Windows   Ctrl+Z,        ;
    // Unix / Linux   Ctrl+D,        ;
    
    // ***********************************************************************************************
    
    #include 
    #include              
    // If you want to use std::string reliably, you must #include  ;
    
    using namespace std;
    int main()
    {
    	cout << "Please input your first name(followed by 'enter'):
    "
    << endl; string first_name; cin >> first_name; cout << "Hello, " << first_name << " !
    "
    ; return 0; } // --------------------------------------------------------------------------------------------------------------- remark: - error: no operator ">>" matches these operands; solution: #include <string> ; // --------------------------------------------------------------------------------------------------------------- // << ; cout << "Hello, " << first_name << " !
    "
    << endl; cout << "Hello, " << first_name << "!
    "
    ; // ---------------------------------------------------------------------------------------------------------------

    3.3タイプのセキュリティ
  • CPPでは、変数を初期化する習慣を身につける.
  • セキュリティ変換;安全でない変換;
  • ‘a’+1はint{‘a’}+1に等しい.
  •   int a = 20000;
      char c = a;   
    
  • remark:安全でない変換:intは4バイトを占め、charは1バイトを占め、intをcharに付与し、数値が変更される(狭化、narrowingとも呼ばれる);
  • 
     //        ,   narrowing   ;
     //                             ;
     // {},       ,      ' = ' ;
     double x {2.7};         
     // true;
     int y {x};                  
     // false, narrowing;
     int a {1000};
     // true;
     char b {a};
     // false, narrowing;
     
    

    3.3.1明示的なタイプ変換
  • 明示的なタイプ変換:読者にコードの考え方を見やすくする.
  • int x1 = int(x);
    int x2 = static_cast<int>(x); 
    
  • タイプ変換;
  • 明示的なタイプ変換;

  • n. reference
    Bjarne S.C++プログラム設計原理と実践(基礎編)[M].任明,王剛,李忠偉,訳.北京:機械工業出版社,2017.↩︎