CS106L(1): Introduction to C++
Core Idea
- C++: ubiquitous, important, C with classes & many features, safe, efficient
- how to compile C++ program
Lecture 1 Introduction - Note
View Lecture Note
C++ Philosophy
-
Different C++ codes
- std::cout, std::endl
- printf <- a C function
- asm <- assembly code
-
Assembly
- pros: simple, fast, complete control
- cons: lot of code, hard to understand, unportable for different systems
-
C & C++
- C: no object/classes, no generical functional code, tedious writing large programs
- C++: C with classes, fast, simple, cross-platform, high-level features, safety, efficiency, abstraction
Compilation
Compiling Process
Table: 4 Components
| Description | |
|---|---|
| Preprocessor | Deals with #include, #define, etc directives |
| Compiler | Converts C++ source code into assembly. This process is localised to each .cpp file, output .s files. |
| Assembler | Turns assembled code into object code (.o files). Still no intercommunication between separate cpp files. |
| Linker | Object files are linked together to make an executable program. The first place where files are combined. Check that every declared function has an implementation. |
Compiling Ways
IDE
Others not listed in lecture notes:
- Dev C++
- Microsoft VS
- Eclipse
- Xcode
See more at Best C++ IDEs or Source Code Editors for Programming. For some reasons, I gonna use Vim + extensions in this C++ journey.
Command Line Compilation
- Try Compile C++ Program Compiler: g++ (builtin for Mac, download for Windows)
Basic Usage:
$ g++ main.cpp otherFile.cpp -o execFileName
3 common compiler flags:
| Meaning | |
|---|---|
| -std=c++2a | Enable ISO C++ 2020 support |
| -g | Add debugging information to the output |
| -Wall | Turn on most compiler warnings |
Typical First C++ Program for beginners
Source Code:
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
}
Compile:
$ g++ hw.cpp -std=c++2a -g -Wall -o hw
$ ./hw
Output:
Hello World!
POC:

Haha, congrats, first lecture finished!