1. Introduction & Compilation
C++ is a very powerful, statically typed, object-oriented programming language. Unlike Python or Java, C++ is compiled directly into machine code, making it extremely fast.
You need a compiler like G++ (on Linux) or MinGW (on Windows). The standard 'Hello World' code uses the `iostream` library.
#include <iostream>
int main() {
// std::cout is the object for printing to the console
std::cout << "Hello, C++!" << std::endl;
return 0;
}# Compile the code
g++ main.cpp -o program
# Run the program
./program2. Variables & Data Types
Variable declarations in C++ MUST include their data type. Common primitive types are `int`, `double` (for decimals), `char` (character), and `bool` (true/false).
Use `const` to make a variable a constant (it cannot be changed).
#include <string>
int main() {
int age = 25;
double price = 10.99;
bool isComplete = true;
std::string name = "Budi"; // String is part of the standard library
const double PI = 3.14159;
// PI = 3; // This will cause an error (because of const)
}3. Control Flow (If, For, While)
Control flow syntax in C++ is identical to Java or C. It uses `if-else` and `switch` for conditionals, and `for` and `while` for loops.
int num = 10;
if (num > 5) {
std::cout << "Greater than 5";
} else {
std::cout << "5 or smaller";
}
// Standard 'for' loop
for (int i = 0; i < 3; i++) {
std::cout << i << std::endl;
}4. Functions
Functions in C++ must be defined with a return type. If a function returns nothing, use `void`.
In C++, there is often a separation between Declaration (in a `.h` file) and Definition (in a `.cpp` file), though for simple programs they can be in one file.
// Function declaration & definition
int add(int a, int b) {
return a + b;
}
void greet(std::string name) {
std::cout << "Hello, " << name;
}
int main() {
int result = add(5, 3);
greet("Alex");
}5. Pointers & References
This is the most important concept in C++. A Pointer is a variable that stores the memory address of another variable.
1. `&` (Address-of): Gets the memory address. `int x = 10; int* ptr = &x;`
2. `*` (Dereference): Gets the value *at* that address. `std::cout << *ptr; // Will print 10`
Pointers are crucial for manual memory management (`new` and `delete`) and for 'pass-by-reference'.
// This function changes the original 'value'
void addTen(int* numPtr) {
*numPtr = *numPtr + 10;
}
int main() {
int myValue = 20;
addTen(&myValue); // Send its address
std::cout << myValue; // Output: 30
}6. OOP: Classes & Objects
C++ is an OOP language. You use the `class` keyword to create a 'blueprint'. By default, all properties in a class are `private`.
`public:`: Can be accessed from outside. `private:`: Can only be accessed from within the class.
class Dog {
private:
int age;
public:
// Constructor
Dog(int startAge) {
age = startAge;
}
// Method
void bark() {
std::cout << "Woof!";
}
};
int main() {
Dog myDog(5); // Create an object
myDog.bark();
// myDog.age = 10; // Error! 'age' is private
}7. STL (Standard Template Library)
Don't create your own data structures. C++ provides the STL, a rich collection of templates.
1. `std::vector`: Dynamic array (most common).
2. `std::map`: Key-value store (like a Dictionary).
3. `std::string`: Text (you've already used it).
#include <vector>
#include <map>
#include <string>
int main() {
// Vector (Dynamic array)
std::vector<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
// Map (Key-Value)
std::map<std::string, int> scores;
scores["Alice"] = 100;
}Practice Set
Reinforce basic C++ concepts, from syntax, pointers, to OOP.
Question #1 - C++ Introduction
Which stream object is used to print output to the console in C++?
Question #2 - Variables & Data Types
Which is the correct way to declare a constant (fixed value) in modern C++?
Question #3 - Pointers
Which operator is used to get the memory address of a variable?
Question #4 - Pointers
Which operator is used to access the value at a memory address (dereferencing a pointer)?
Question #5 - OOP
In C++, how does a Class (e.g., 'Sedan') inherit from another Class (e.g., 'Car')?
Question #6 - OOP
The `private` access modifier means...
Question #7 - STL
Which data structure in the C++ Standard Template Library (STL) functions as a dynamic array (like ArrayList in Java)?
Final Exam
Test your C++ knowledge, including memory management and the STL.
Question #1 - C++ Introduction
Which stream object is used to print output to the console in C++?
Question #2 - Variables & Data Types
Which is the correct way to declare a constant (fixed value) in modern C++?
Question #3 - Pointers
Which operator is used to get the memory address of a variable?
Question #4 - Pointers
Which operator is used to access the value at a memory address (dereferencing a pointer)?
Question #5 - OOP
In C++, how does a Class (e.g., 'Sedan') inherit from another Class (e.g., 'Car')?
Question #6 - OOP
The `private` access modifier means...
Question #7 - STL
Which data structure in the C++ Standard Template Library (STL) functions as a dynamic array (like ArrayList in Java)?
Question #8 - Memory Management
What keyword is used to dynamically allocate memory on the 'heap'?
Question #9 - OOP
What is a 'Constructor'?