• 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

     

  • 相关阅读:
    MySQL的用户名不记得了怎么办?
    辨析:机器字长、存储字长、指令字长和操作系统位数
    基于Flask框架搭建视频网站的学习日志(六)之数据库
    基于Flask框架搭建视频网站的学习日志(三)之原始web表单
    基于Flask框架搭建视频网站的学习日志(二)
    Docker 容器的网络连接 & 容器互联
    Docker DockerFile文件指令 & 构建
    linux :没有找到 ifconfig netstat
    ubuntu : 无法安全地用该源进行更新,所以默认禁用该源。
    Docker 守护进程的配置和操作 & 远程访问
  • 原文地址:https://www.cnblogs.com/xin-le/p/4083900.html
Copyright © 2020-2023  润新知