zoukankan      html  css  js  c++  java
  • C++ 梳理:跑通简单程序

    C++ 结合了三个编程流派:

    1. 过程式编程:C 特性,结构化语言,强调过程
    2. 面向对象编程:C++ 对于 C 语言的补充,一切皆对象,强调数据
    3. 泛型编程(generic programming):由 C++ 模板支持,强调代码与数据类型无关(type independent)。

    C++ 常用标准:

    • C98
    • C03
    • C11
    • C14

    C++ 程序创建的机制

    1. 使用文本编辑器编写源代码
    2. 编译源代码。编译器(compiler)会将源代码转换为平台相关的机器语言/目标代码
    3. 目标代码的链接。由于往往存在多个 C++ 库,需要链接器(linker)把这些目标代码以某种方式整合起来,最终形成可执行代码(executable code)

    第一个 C++ 程序

     1 // myfirst.cpp -- displays a message
     2 
     3 #include <iostream>                           // a PREPROCESSOR directive
     4 int main()                                    // function header
     5 {                                             // start of function body
     6     using namespace std;                      // make definitions visible
     7     cout << "Come up and C++ me some time.";  // message
     8     cout << endl;                             // start a new line
     9     cout << "You won't regret it!" << endl;   // more output
    10     return 0;                                 // terminate main()
    11 }                                             // end of function body

    编译源代码:

    g++ myfirst.cpp

    运行二进制文件:

    ./a.out

    注意:

    • 避免使用 void main() 进行主函数的声明,这不是 C++ 标准格式。不同编译器有可能通过,有可能报错。
    • C++ 标准允许省略 return 0; ,如果不写编译器会自动添加。
    • iostream 称为包含文件(include file)或者头文件(head file)。iostream 提供输入输出相关的功能。
    •  #include <iostream> 是一个预处理命令,本质上是把 iostream 的内容替换在这一行,接着由编译器进行编译。
    • 命名空间(namespace):多个头文件可能有相同的函数名,命令空间可以避免函数名冲突。 using namespace std; 表示使用命名空间声明为 std 的头文件,里面的函数或者变量可以直接使用。如果没有这条语句,则需要使用 std::cout 这样的形式进行调用。
    • cout 是一个对象,知道如何显示出各种各样类型的数据。<< 是一个插入运算符(insertion operator),可以将字符串插入到输出流。

    大多数程序员遵循这样的 C++ 源码风格:

    1. 每行一条语句;
    2. 函数的花括号的 { 和 } 分别占一行;
    3. 函数中的语句需要缩进;
    4. 函数名与其对应的括号之间不要加空格(为了区分其他 C++ 结构)。

    简单的声明语句和赋值语句

    // an addition operation
    #include <iostream>
    
    int main()
    {
        using namespace std;
        int x1;
        int x2;
        int ans;
        x1 = 1;
        x2 = 2;
        ans = x1 + x2;
        cout << "x1 + x2 = " << ans << endl;
    }

    编译并运行后的输出:

    x1 + x2 = 3

    简单的输入语句

     1 // double your input number
     2 #include <iostream>
     3 
     4 int main()
     5 {
     6     int now = 2019;
     7     int birth_year;
     8     std::cout << "What's your birth year?" << std::endl;
     9     std::cin >> birth_year;  // using cin
    10     std::cout << "You're " << 2019 - birth_year << " years old." << std::endl;
    11     return 0;
    12 }

    编译运行后会提示输入,输入 1992,得到运算之后的输出:

    What's your birth year?
    1992
    You're 27 years old.

    使用 cmath 头文件:

     1 // using sqrt
     2 
     3 #include <iostream>
     4 #include <cmath>
     5 
     6 int main()
     7 {
     8     using std::cout;
     9     using std::cin;
    10     using std::endl;
    11     using std::sqrt;
    12 
    13     float num;
    14     cout << "Give me a number: ";
    15     cin >> num;
    16     cout << "sqrt(" << num << ") = " << sqrt(num) << endl;
    17 }

    编译运行,输入 25 后输出:

    Give me a number: 2
    sqrt(2) = 1.41421

    简单的自定义函数

    自定义函数需要声明函数原型:

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 int add(int, int);  // function prototype
     6 
     7 int main()
     8 {
     9     int x1 = 3;
    10     int x2 = 4;
    11     int sum = add(x1, x2);
    12     cout << "3 + 4 = " << sum << endl;
    13 }
    14 
    15 int add(int n1, int n2)
    16 {
    17     return n1 + n2;
    18 }

    输出如下:

    3 + 4 = 7

    参考

    • 《C++ Primer Plus》by Stephen Prata
  • 相关阅读:
    leetcode 190 Reverse Bits
    vs2010 单文档MFC 通过加载位图文件作为客户区背景
    leetcode 198 House Robber
    记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
    逆序数2 HDOJ 1394 Minimum Inversion Number
    矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence
    递推DP URAL 1586 Threeprime Numbers
    递推DP URAL 1167 Bicolored Horses
    递推DP URAL 1017 Staircases
    01背包 URAL 1073 Square Country
  • 原文地址:https://www.cnblogs.com/noluye/p/11482746.html
Copyright © 2011-2022 走看看