0-课程特点
1. 基础知识:以点带面
2. 重点:指针和内存管理
3. 特色:
- 程序效率,提速几十倍
- 介绍OpenCV采用C++特性设计cv::Mat类
- 介绍ARM开发
1-第一个例子
Hello.cpp
//C++ example in C++11
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
return 0;
}
Compile and run the program
- Compile hello.cpp
g++ hello.cpp
- Initialization of msg is a C++11 extension. We need
g++ hello.cpp --std=c++11
- Executable file can be generates as a.out. Change the output filename by -o option
g++ hello.cpp --std=c++11 -o hello
- Execute
./hello
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 <iostream>
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;
}
Function prototypes and definitions
- function prototypes are put into head files (*.hpp)
int mul(int a, int b);
- function definitions normally are in source files (*.c, *.cpp)
int mul(int a, int b)
{
return a * b;
}
Separate the source codes into multiples files
# main.cpp
#include <iostream>
#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;
}
# mul.cpp
#include "mul.hpp"
int mul(int a, int b)
{
int c = a / b;
return c * b * b;
}
# mul.hpp
#pragma once
int mul(int a, int b);
Compile and link
g++ -c main.cpp. // -c 只编译不链接
g++ main.o mul.o -o mul // 两个文件并起来
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 <iostream>
#define PI
3.1415926535
#if defined(_OPENMP)
#include <omp.h>
#endif
include directive
Macros
PI不是一个变量,直接替换
Simple input and output
C++ Style Output
- What is cout?
std::ostream cout;
- cout is an object of data type stream in namespace std
cout << "hello." << endl;
- << 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;
- Similarly, cin is an object of type std::instream
- “>>” is an operator
C Style Output
int v = 100;
printf("Hello, value = %d\n", v);
- 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);
- 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
- 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)
{
}
//argc: argument count
//argv[]: 具体的参数列表
# argument.cpp
#include <iostream>
using namespace std;
int main(int argc, char * argv[])
{
for (int i = 0; i < argc; i++)
cout << i << ": " << argv[i] << endl;
}
g++ argument.cpp
./a.out hello.cpp -o hello
./a.out
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