Getting Started with C++
What is C++?
C++ is a general-purpose, compiled, statically-typed programming language created by Bjarne Stroustrup in 1979 at Bell Labs as an extension of C. It adds object-oriented programming, generic programming (templates), and the Standard Template Library (STL) on top of C's procedural foundation.
C++ is used in operating systems, game engines, browsers, databases, embedded systems, and high-frequency trading - anywhere performance and control matter.
Why Learn C++?
- Combines low-level memory control (like C) with high-level OOP abstractions
- Powers game engines (Unreal Engine), browsers (Chrome), and databases (MySQL)
- Foundation for understanding how languages like Java and C# work under the hood
- Essential for competitive programming and systems programming
- Modern C++ (C++11/14/17/20) is expressive and safe while remaining fast
Setting Up the Compiler
| Platform | Compiler | Install | Verify |
|---|---|---|---|
| Windows | GCC (MinGW-w64) | mingw-w64.org or via VS Code C++ extension | g++ --version |
| Linux | GCC | sudo apt install g++ | g++ --version |
| macOS | Clang (via Xcode) | xcode-select --install | g++ --version |
Structure of a C++ Program
#include <iostream>- includes the standard I/O libraryusing namespace std;- avoids writingstd::prefix everywhereint main()- entry point of every C++ programcout <<- outputs to console (C++ stream syntax)return 0;- signals successful execution
#include <iostream> // standard input/output stream library
using namespace std; // use std namespace - avoids std::cout
int main() {
cout << "Hello, World!" << endl; // endl = newline + flush
cout << "Welcome to C++!" << "\n"; // \n is faster than endl
return 0; // 0 = success
}
Compile and Run
| Step | Command | Description |
|---|---|---|
| Compile | g++ hello.cpp -o hello | Compile to executable named hello |
| Compile (C++17) | g++ -std=c++17 hello.cpp -o hello | Use C++17 standard |
| Run (Linux/Mac) | ./hello | Run the program |
| Run (Windows) | hello.exe | Run on Windows |
| With warnings | g++ -Wall -Wextra hello.cpp -o hello | Enable all warnings (recommended) |
#include <iostream>
#include <string> // C++ string class (not char array)
using namespace std;
int main() {
// C++ uses cin/cout instead of scanf/printf
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Hello, " << name << "!" << endl;
// C++ has bool type natively
bool isLearning = true;
cout << "Learning C++: " << boolalpha << isLearning << endl;
// C++ supports // single-line comments (C99 also does)
/* and multi-line comments */
// C++ has references (C does not)
int x = 10;
int &ref = x; // ref is an alias for x
ref = 20;
cout << "x = " << x << endl; // 20
return 0;
}
Related C++ Topics