C++ Programming Language Tutorial
Master C++ programming from basic syntax to advanced Object-Oriented Programming (OOP), STL, and modern C++ features with practical examples.
Object-Oriented
Learn OOP concepts
STL Included
Standard Template Library
Modern C++
C++11/14/17/20 Features
1. Introduction to C++ Programming
C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". It has imperative, object-oriented and generic programming features, while also providing facilities for low-level memory manipulation.
History of C++
- 1979: Bjarne Stroustrup begins work on "C with Classes"
- 1983: Renamed to C++
- 1985: First commercial release
- 1998: First ISO standard (C++98)
- 2011: Major update (C++11)
- 2014, 2017, 2020: Subsequent updates
Why Learn C++?
- High performance and efficiency
- Object-Oriented Programming
- Standard Template Library (STL)
- Used in game development, system software
- Foundation for understanding other languages
- Cross-platform development
First C++ Program
Every C++ program must have a main() function. Here's the traditional "Hello, World!" program:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
2. Basic Syntax and Structure
C++ programs are composed of various tokens including keywords, identifiers, literals, operators, and punctuators.
| Component | Description | Example |
|---|---|---|
| Preprocessor Directives | Instructions for the preprocessor | #include <iostream> |
| Namespace | Prevents naming conflicts | using namespace std; |
| Functions | Blocks of code that perform tasks | int main() { ... } |
| Variables | Named storage locations | int count = 10; |
| Statements | Instructions that perform actions | cout << "Hello"; |
| Comments | Explanatory text ignored by compiler | // Single line comment |
3. Running C++ Programs
Windows Setup
- Download Visual Studio Community Edition
- Install "Desktop development with C++" workload
- Create a new C++ Console Application project
- Write code and press F5 to run
Using MinGW (Alternative)
# Install MinGW
# Add to PATH: C:\MinGW\bin
# Compile: g++ hello.cpp -o hello.exe
# Run: hello.exe
Linux Setup
# Install g++
sudo apt install g++
# Compile
g++ hello.cpp -o hello
# Run
./hello
30-Day C++ Learning Roadmap
Week 1: C++ Fundamentals
Syntax, Data Types, Operators, Basic I/O
Week 2: Control Flow & Functions
Conditionals, Loops, Functions, Arrays
Week 3: Object-Oriented Programming
Classes, Objects, Inheritance, Polymorphism
Week 4: Advanced Topics
STL, File Handling, Modern C++
4. Variables and Data Types
C++ supports various data types including primitive types, derived types, and user-defined types.
#include <iostream>
using namespace std;
int main() {
// Primitive data types
int age = 25;
float salary = 45000.50f;
double pi = 3.1415926535;
char grade = 'A';
bool isEmployed = true;
// Auto keyword (C++11)
auto score = 95.5; // double
// Display values
cout << "Age: " << age << endl;
cout << "Salary: " << salary << endl;
cout << "Grade: " << grade << endl;
cout << "Is Employed: " << boolalpha << isEmployed << endl;
return 0;
}
| Data Type | Size (Bytes) | Range | Example |
|---|---|---|---|
int | 4 | -231 to 231-1 | int x = 10; |
float | 4 | ±3.4e-38 to ±3.4e38 | float f = 3.14f; |
double | 8 | ±1.7e-308 to ±1.7e308 | double d = 3.14159; |
char | 1 | -128 to 127 | char c = 'A'; |
bool | 1 | true/false | bool flag = true; |
wchar_t | 2 | 0 to 65,535 | wchar_t wc = L'A'; |