C++ — Get Started
What is C++?
C++ is a powerful general-purpose language built on top of C. Created by Bjarne Stroustrup in 1983, it adds object-oriented programming, templates, and the Standard Template Library (STL) while retaining C's performance.
C++ powers game engines (Unreal), browsers (Chrome), databases (MySQL), operating systems (Windows parts), and high-performance computing.
Install a compiler
You need a C++ compiler. The most common is g++ (part of GCC):
Windows
- Install MinGW-w64
- Add the
binfolder to your PATH - Verify:
g++ --version
Mac
xcode-select --install
Linux
sudo apt install g++
Your first program
Create hello.cpp:
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
Compile and run:
g++ hello.cpp -o hello
./hello
Output:
Hello, world!
Understanding the code
#include <iostream> // includes the I/O stream library
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
std::cout— standard output (console)<<— stream insertion operator (sends data to the output)std::endl— newline and flushreturn 0— success
C++ vs C
C++ builds on C but adds significant features:
| Feature | C | C++ |
|---|---|---|
| Output | printf() | std::cout |
| Input | scanf() | std::cin |
| Memory | malloc/free | new/delete, smart pointers |
| Classes | No | Yes |
| Templates | No | Yes |
| STL | No | Yes |
Using namespace std
#include <iostream>
using namespace std;
int main() {
cout << "No std:: prefix needed!" << endl;
return 0;
}
using namespace std brings all standard names into scope. Fine for learning; avoid in large projects to prevent naming conflicts.
Variables
#include <iostream>
using namespace std;
int main() {
int age = 36;
double pi = 3.14159;
char letter = 'A';
string name = "Ada"; // C++ string
bool active = true;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Pi: " << pi << endl;
cout << "Letter: " << letter << endl;
cout << "Active: " << active << endl;
return 0;
}
C++ adds string, bool, and other types beyond C's basic set.
Input with cin
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter your age: ";
cin >> age;
cout << "Hello, " << name << "! You are " << age << " years old." << endl;
return 0;
}
cin >> reads until whitespace. getline(cin, name) reads an entire line.
auto keyword
C++11 introduced auto for type inference:
auto x = 42; // int
auto pi = 3.14; // double
auto name = string("Ada"); // string
auto active = true; // bool
Use auto when the type is obvious from context.
constexpr
constexpr int MAX_SIZE = 100;
constexpr double PI = 3.14159265358979;
constexpr values are computed at compile time — they're constants that the compiler can optimize.
C++11/14/17/20 features
Modern C++ is much easier to learn than old C++:
// Range-based for loop (C++11)
vector<int> nums = {1, 2, 3, 4, 5};
for (auto n : nums) {
cout << n << " ";
}
// Structured bindings (C++17)
auto [name, age] = make_pair("Ada", 36);
// Optional (C++17)
optional<int> maybe = 42;
if (maybe.has_value()) {
cout << maybe.value() << endl;
}
Useful compiler flags
g++ -Wall -std=c++17 -o program source.cpp
-Wall— enable all warnings-std=c++17— use C++17 standard-O2— optimize for speed-g— include debug information
Mini Practice
- Install g++ and verify with
g++ --version - Write a program that reads two numbers and prints their sum
- Use
autoto declare variables of different types - Create a range-based for loop that prints elements of a vector
- Use
constexprfor a mathematical constant
Next: C++ syntax rules →
Related Topics
Frequently Asked Questions about Get Started
What is Get Started in C++?
Get Started is a fundamental concept in C++. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Get Started?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Get Started.
Why is Get Started important in C++?
Get Started is essential for C++ development. Understanding this concept will help you write better code and solve real-world problems more effectively.