• 【CPP】Introduction


    0-课程特点

    1. 基础知识:以点带面

    2. 重点:指针和内存管理

    3. 特色:

    • 程序效率,提速几十倍
    • 介绍OpenCV采用C++特性设计cv::Mat类
    • 介绍ARM开发

    1-第一个例子

    Hello.cpp

    //C++ example in C++11
    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main()
    {
        vector<string> msg {"Hello", "C++", "World", "!"};
        
        for (const string& word : msg)
        {
            cout << word << " ";
        }
        cout << endl;
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Compile and run the program

    • Compile hello.cpp
    g++ hello.cpp
    
    • 1
    • Initialization of msg is a C++11 extension. We need
    g++ hello.cpp --std=c++11
    
    • 1
    • Executable file can be generates as a.out. Change the output filename by -o option
    g++ hello.cpp --std=c++11 -o hello
    
    • 1
    • Execute
    ./hello
    
    • 1

    2-Different Programming Languages

    Assembly Languages

    • List item

    High Level Languages

    • C: 1973
    • C++: 1979
    • Java:1995
    • Python: 1990
    • Scratch:2002

    Advantages of C/C++

    Development languages of most fundamental systems:

    • linux
    • MySQL
    • OpenCV

    High Effiiency

    • Widely optimized compilers
    • Access memory directly
    • Excellent on computing

    3-Complie and Link

    Example

    #include 
    
    using namespace std;
    
    int mul(int a, int b)
    {
        return a * b;
    }
    int main()
    {
        int a = 0, b = 0;
        int result = 0;
    
        cout << "Pick two integers:";
        cin >> a;
        cin >> b;
    
        result = mul(a, b);
    
        cout << "The result is " << result << endl;
        return 0;
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    Function prototypes and definitions

    • function prototypes are put into head files (*.hpp)
    int mul(int a, int b);
    
    • 1
    • function definitions normally are in source files (*.c, *.cpp)
    int mul(int a, int b)
    {
      return a * b;
    }
    
    • 1
    • 2
    • 3
    • 4

    Separate the source codes into multiples files

    # main.cpp
    #include 
    #include "mul.hpp"
    
    using namespace std;
    int main()
    {
        int a = 0, b = 0;
        int result = 0;
    
        cout << "Pick two integers:";
        cin >> a;
        cin >> b;
    
        result = mul(a, b);
    
        cout << "The result is " << result << endl;
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    # mul.cpp
    #include "mul.hpp"
    
    int mul(int a, int b)
    {
        int c = a / b;
        return c * b * b;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    # mul.hpp
    #pragma once
    
    int mul(int a, int b);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Compile and link

    在这里插入图片描述

    g++ -c main.cpp. // -c 只编译不链接
    
    • 1
    g++ main.o mul.o -o mul  // 两个文件并起来
    
    • 1

    Compilation errors

    • Normally caused by grammar
    • Please check the source code

    在这里插入图片描述

    Link errors

    • Symbol not found
    • Function mul() is misspelled to Mul()

    在这里插入图片描述

    Runtime errors

    • The source code can be successfully complied and linked
    • The floating point exception (divided by 0) will kill the program.
    • It is a typical runtime error.

    在这里插入图片描述

    Preprocessor and Macros

    Preprocessor

    • The preprocessor is executed before the compilation.
    • Preprocessing directives begin with a # character
    • Each directive occupies one line
    • preprocessing instruction
      (define, undef, include, if, ifdef, ifndef, else, elif, endif, line, error, pragma)
    #include 
    #define PI
    3.1415926535
    
    #if defined(_OPENMP)
    #include 
    #endif
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    include directive

    在这里插入图片描述

    Macros

    在这里插入图片描述

    PI不是一个变量,直接替换

    Simple input and output

    C++ Style Output

    • What is cout?
    std::ostream cout;
    
    • 1
    • cout is an object of data type stream in namespace std
    cout << "hello." << endl;
    
    • 1
    • << is an operator which is defined as follows

    在这里插入图片描述

    • end, an output-only I/O manipulator. It will output a new line character and flushes
    int a;
    float b;
    cin >> a;
    cin >> b;
    
    • 1
    • 2
    • 3
    • 4
    • Similarly, cin is an object of type std::instream
    • “>>” is an operator

    C Style Output

    int v = 100;
    printf("Hello, value = %d\n", v);
    
    • 1
    • 2
    • int printf(const char * format, …); is a function
    • format: a string specifying how to interpret the data
    • %d will let the function interpret v as an integer

    C Style Input

    int v;
    int ret = scanf("%d", &v);
    
    • 1
    • 2
    • scanf reads data from stdin, and interpret the input as an integer and store it into v

    Why the examples have no GUI?

    • The programs I used all have GUI. Why the examples have no GUI?
    • GUI (graphical user interface) is not mandatory
    • GUI is for human begins to interact with computers
    • No all programs interact with human beings
    • We can also interact with the program in a command line window
    • We can call a GUI library to create a graphic window by many programming languages. Surely C/C++ can create a GUI window

    Command line arguments

    g++ hello.cpp -o hello
    
    • 1
    • g++ is an executable program/line
    • There are three command line arguments
    int main()
    {
        /* ... */
    }
    
    int main(int argc, char *argv[])
    {
    }
    
    int main(int argc, char **argv)
    {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    //argc: argument count
    //argv[]: 具体的参数列表

    # argument.cpp
    #include 
    
    using namespace std;
    int main(int argc, char * argv[])
    {
        for (int i = 0; i < argc; i++)
            cout << i << ": " << argv[i] << endl;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    g++ argument.cpp
    ./a.out hello.cpp -o hello
    
    • 1
    • 2

    在这里插入图片描述

    ./a.out
    
    • 1

    在这里插入图片描述

    But

    • I don’t like to compile a program in a command window
    • IDE: Integrated development evvironment
      Visual Studio
      Apple Xcode
      Eclipes
      Clion
    • Visual Srudio Code(VSCode) is an integrated development environment made by Microsoft for Windows, Linux and macOS
  • 相关阅读:
    dubbo项目整合nacos注册中心问题记录
    mac 下载、安装、配置mysql详细教程
    为什么网站页面没有被百度搜索收录?是网站被攻击了?
    Java中的接口与抽象类:区别与联系
    TF卡格式化了怎么办?tf卡数据恢复,看这3个方法
    【总结】I/O接口中的数据线,地址线,控制线,状态线传输什么信息?
    【MySQL】基本查询(表的增删改查)-- 详解
    JavaScript重点知识总结二
    混沌系统在图像加密中的应用(分数阶Lorenz混沌系统数值求解)
    【SpringCloud】API网关(Spring Cloud Gateway)
  • 原文地址:https://blog.csdn.net/weixin_38362786/article/details/133879237