zoukankan      html  css  js  c++  java
  • 1.Basics of C++

    From: 

    http://www.cplusplus.com/doc/tutorial/

    /*

    C++ Language Tutorial

    1.Basics of C++
    2.Control Structures
    3.Compound data types
    4.Object Oriented Programming
    5.Advanced concepts
    6.C++ Standard Library

    */

    Table of contents

    /*
    
    Basics of C++
    Structure of a program 
    Variables and types 
    Constants 
    Operators 
    Basic Input/Output 
    
    
    Program structure
    Control Structures 
    Functions 
    Overloads and templates 
    Name visibility 
    
    
    Compound data types
    Arrays 
    Character sequences 
    Pointers 
    Dynamic Memory 
    Data structures 
    Other data types 
    
    
    Classes
    Classes (I) 
    Classes (II) 
    Special members 
    Friendship and inheritance 
    Polymorphism 
    
    
    Other language features
    Type conversions 
    Exceptions 
    Preprocessor directives 
    
    
    C++ Standard Library
    Input/Output with files 
    
    */
    View Code

    1.Basics of C++

    The codes:

    // 1. Basics of C++
    
    
    /***********************************
    1
    Structure of a program
    */
    
    // my first program in C++
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      cout << "Hello World!";
      return 0;
    }
    
    
    // my second program in C++
    
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
      cout << "Hello World! ";
      cout << "I'm a C++ program";
      return 0;
    }
    
    
    /* my second program in C++
       with more comments */
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      cout << "Hello World! ";     // prints Hello World!
      cout << "I'm a C++ program"; // prints I'm a C++ program
      return 0;
    }
    
    
    /***********************************
    2
    Variables. Data Types.
    */
    
    // operating with variables
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      // declaring variables:
      int a, b;
      int result;
    
      // process:
      a = 5;
      b = 2;
      a = a + 1;
      result = a - b;
    
      // print out the result:
      cout << result;
    
      // terminate the program:
      return 0;
    }
    
    
    
    // initialization of variables
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int a=5;               // initial value = 5
      int b(2);              // initial value = 2
      int result;            // initial value undetermined
    
      a = a + 3;
      result = a - b;
      cout << result;
    
      return 0;
    }
    
    
    
    // my first string
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string mystring = "This is a string";
      cout << mystring;
      return 0;
    }
    
    
    
    // my first string
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string mystring;
      mystring = "This is the initial string content";
      cout << mystring << endl;
      mystring = "This is a different string content";
      cout << mystring << endl;
      return 0;
    }
    
    
    /*********************************
    3
    Constants
    */
    
    // test escape codes
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string s;
      s="one
    two
    three
    ";
      cout << s;
      s="string expressed in 
    two lines";
      cout << s << endl;
      s="string expressed in 
        two lines";  //space...
      cout << s << endl;
      s="b,c want:142143"; //octal
      cout << s << endl;
      s="b,c want:x62x63"; //hexadecimal
      cout << s << endl;  
      return 0;
    }
    
    
    // defined constants: calculate circumference
    
    #include <iostream>
    using namespace std;
    
    #define PI 3.14159
    #define NEWLINE '
    '
    
    int main ()
    {
      double r=5.0;     // radius
      double circle;
    
      circle = 2 * PI * r;
      cout << circle;
      cout << NEWLINE;
    
      return 0;
    }
    
    // test constants
    #include <iostream>
    using namespace std;
    
    #define PI 3.14
    const n=10;
    
    int main ()
    {
      PI=3.14159; //error 
      n=100; //error
    
      return 0;
    }
    
    
    /******************************************
    4
    Operators
    */
    
    // assignment operator
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int a, b;         // a:?,  b:?
      a = 10;           // a:10, b:?
      b = 4;            // a:10, b:4
      a = b;            // a:4,  b:4
      b = 7;            // a:4,  b:7
    
      cout << "a:";
      cout << a;
      cout << " b:";
      cout << b;
    
      return 0;
    }
    
    
    // test rvalue
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int a, b;
      a=2+(b=5);
      cout<<a<<" "<<b<<endl;
      a=b=a;
      cout<<a<<" "<<b<<endl;
    
      return 0;
    }
    
    
    
    // compound assignment operators
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int a, b=3;
      a = b;
      a+=2;             // equivalent to a=a+2
      cout << a;
      return 0;
    }
    
    
    // more...
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int a, b=3;
      a = b;
      a+=2; //5
      cout << a << endl;
      a-=2; //3
      cout << a << endl;
      a*=2; //6
      cout << a << endl;
      a/=3; //2;
      cout << a << endl;
      a%=b; // 2 mod 3=2
      cout << a << endl;
      a>>=1; // shift right 1, 10->1
      cout << a << endl;
      a<<=3; // shift left 3, 1->1000
      cout << a << endl;
      a&=15; // 1000 and 1111 -> 1000
      cout << a << endl;
      a^=15; // 1000 xor 1111 -> 0111
      cout << a << endl;
      a|=15; // 0111 or 1111 -> 1111
      cout << a << endl;  
      
      return 0;
    }
    
    // difference of: ++a a++
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int a, b=3;
      a=++b; // increased befor...
      cout << a << endl;
      b=3;
      a=b++; // include after...
      cout << a << endl;
      
      return 0;
    }
    
    
    
    // conditional operator
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int a,b,c;
      a=2;
      b=7;
      c = (a>b) ? a : b;
      cout << c;
      return 0;
    }
    
    
    // comma operator
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int a,b,c;
      c = ( a=2, b=7, a>b?a:b ); //
      cout << c;
      return 0;
    }
    
    
    // type casting operator
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int i;
      double f=3.14;
      i=(int)f;
      cout << i << endl;
      i=int(f);
      cout << i << endl;
      i=98;
      cout << char(i) << endl; // same as pascal
      return 0;
    }
    
    
    /*************************************
    5
    Basic Input/Output
    */
    
    // cout
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      cout << "First sentence.
    ";
      cout << "Second sentence.
    Third sentence.";
      return 0;
    }
    
    
    // i/o example
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int i;
      cout << "Please enter an integer value: ";
      cin >> i;
      cout << "The value you entered is " << i;
      cout << " and its double is " << i*2 << ".
    ";
      return 0;
    }
    
    
    // cin with strings
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string mystr;
      cout << "What's your name? ";
      getline (cin, mystr);
      cout << "Hello " << mystr << ".
    ";
      cout << "What is your favorite team? ";
      getline (cin, mystr);
      cout << "I like " << mystr << " too!
    ";
      return 0;
    }
    
    
    
    // stringstreams
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main ()
    {
      string mystr;
      float price=0;
      int quantity=0;
    
      cout << "Enter price: ";
      getline (cin,mystr);
      stringstream(mystr) >> price; //convert strings to numerical values 
      cout << "Enter quantity: ";
      getline (cin,mystr);
      stringstream(mystr) >> quantity;
      cout << "Total price: " << price*quantity << endl;
      return 0;
    }
    
    
    
    // more
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      int i,j;
      char c;
      cin>>i>>j;
      cin>>c;
      cout<<i<<" "<<j<<endl;
      cout<<c<<endl;
      return 0;
    }
    // cin always uses blank space character?

    TOP

     

  • 相关阅读:
    typecho开启pjax,ajax,无刷新
    typecho 调用评论最多热门文章
    typecho 文章归档调用
    剑指offer解题报告(Java版)——翻转单词顺序 左旋字符串 42
    剑指offer解题报告(Java版)——和为s的两个数,一串连续数 41
    剑指offer解题报告(Java版)——数组中只出现一次的数字 40
    J2EE——J2EE的十三种技术
    剑指offer解题报告(Java版)——二叉树的深度 判断二叉树是否平衡 38
    剑指offer解题报告(Java版)——找到两个链表的第一个公共节点 37
    剑指offer解题报告(Java版)——求逆序对 36
  • 原文地址:https://www.cnblogs.com/xin-le/p/4083900.html
Copyright © 2011-2022 走看看