Before You Start
Welcome to the C++ Piscine! Before diving into Module 00, let’s make sure you have the foundational knowledge you’ll need to succeed.
Who This Guide Is For
Section titled “Who This Guide Is For”This guide is designed for 42 students who have completed the C Piscine. You should already be familiar with:
- Basic C programming (variables, loops, functions)
- Pointers and memory management (malloc/free)
- Compiling with gcc and using Makefiles
- Working in a terminal environment
If you’re coming from C, you’re in the right place. C++ builds on C, adding powerful new features while remaining compatible with most C code.
What is C++?
Section titled “What is C++?”C++ was created by Bjarne Stroustrup in 1979 as an extension of C. The ”++” in the name comes from the increment operator in C, suggesting that C++ is “C plus more.”
Why C++ Over C?
Section titled “Why C++ Over C?”C++ adds several key features that make programming easier and safer:
| Feature | C | C++ |
|---|---|---|
| Object-Oriented Programming | No | Yes (classes, inheritance) |
| Type Safety | Weak | Stronger |
| Memory Management | Manual only | Manual + RAII |
| Standard Library | Limited | Extensive (STL) |
| Function Overloading | No | Yes |
| References | No | Yes |
| Exceptions | No | Yes |
The Core Philosophy
Section titled “The Core Philosophy”C++ follows the principle of “you don’t pay for what you don’t use.” Features like virtual functions or exceptions only add overhead when you actually use them. This makes C++ suitable for performance-critical applications.
Key Concepts You’ll Learn
Section titled “Key Concepts You’ll Learn”Throughout the C++ Piscine, you’ll learn these fundamental concepts:
Object-Oriented Programming (OOP)
Section titled “Object-Oriented Programming (OOP)”OOP is a programming paradigm where you organize code around “objects” rather than functions. Think of it like this:
- In C: You have data (structs) and functions that operate on that data
- In C++: You have objects that contain both data AND the functions that operate on it
This bundling of data and behavior is called encapsulation.
Classes and Objects
Section titled “Classes and Objects”A class is like a blueprint for creating objects. An object is an instance of a class.
Analogy: A class is like an architectural blueprint for a house. The actual houses built from that blueprint are objects. Each house has the same structure (defined by the blueprint) but can have different contents (furniture, paint colors, etc.).
Memory Management
Section titled “Memory Management”C++ gives you more tools for managing memory:
- Stack allocation: Automatic, fast, limited size (like C’s local variables)
- Heap allocation: Manual with
new/delete(replacesmalloc/free) - RAII: A pattern where objects automatically clean up their resources
The Standard Template Library (STL)
Section titled “The Standard Template Library (STL)”The STL provides ready-to-use containers (like dynamic arrays and hash maps) and algorithms. You won’t have to implement linked lists from scratch anymore!
C vs C++: What Changes?
Section titled “C vs C++: What Changes?”Here’s a quick preview of the differences you’ll encounter:
Output
Section titled “Output”C: printf("Hello %s\n", name);C++: std::cout << "Hello " << name << std::endl;Memory Allocation
Section titled “Memory Allocation”C: int *arr = malloc(10 * sizeof(int)); free(arr);
C++: int *arr = new int[10]; delete[] arr;Strings
Section titled “Strings”C: char str[100]; strcpy(str, "Hello"); strcat(str, " World");
C++: std::string str = "Hello"; str += " World";Data Structures
Section titled “Data Structures”C: struct Point { int x; int y; }; struct Point p; p.x = 10;
C++: class Point { int x, y; public: Point(int x, int y) : x(x), y(y) {} }; Point p(10, 20); // Constructor callThe 42 C++ Module Structure
Section titled “The 42 C++ Module Structure”The C++ Piscine consists of 10 modules (00-09) plus an exam:
| Module | Topic | Key Concepts |
|---|---|---|
| 00 | C++ Basics | Namespaces, classes, iostream, strings |
| 01 | Memory | References, pointers to members, file streams |
| 02 | OCF & Operators | Orthodox Canonical Form, operator overloading |
| 03 | Inheritance | Base/derived classes, access specifiers |
| 04 | Polymorphism | Virtual functions, abstract classes, interfaces |
| 05 | Exceptions | Try/catch/throw, custom exceptions |
| 06 | Casts | static_cast, dynamic_cast, const_cast, reinterpret_cast |
| 07 | Templates | Function templates, class templates |
| 08 | STL Containers | vector, list, map, stack, queue, iterators |
| 09 | STL Practical | Algorithms, container selection, practical applications |
Each module builds on the previous ones, so complete them in order.
How to Use This Guide
Section titled “How to Use This Guide”This documentation is organized into Guides - each guide combines theory and practical exercises for a module.
What Each Guide Contains
Section titled “What Each Guide Contains”- Concepts: What the new syntax means, why patterns exist, common pitfalls
- Exercises: Step-by-step walkthroughs and line-by-line explanations
- Visual diagrams and practical examples
- Testing strategies for each exercise
Recommended Workflow
Section titled “Recommended Workflow”- Read the Guide for the current module (theory section first)
- Read the subject PDF carefully
- Attempt the exercises yourself
- Check the exercise walkthroughs if stuck
- Review Common Pitfalls before evaluation
Development Environment
Section titled “Development Environment”You’ll need these tools:
Compiler
Section titled “Compiler”Use c++ or clang++ with C++98 standard:
c++ -Wall -Wextra -Werror -std=c++98 main.cpp -o programThe -std=c++98 flag is important! 42 requires C++98 compliance.
Editor
Section titled “Editor”Use any editor you’re comfortable with. Popular choices:
- vim/neovim
- VS Code
- CLion
Terminal
Section titled “Terminal”You’ll compile and run programs from the terminal, just like in C.
42 Subject Rules (Applies to All Modules)
Section titled “42 Subject Rules (Applies to All Modules)”These rules come from the official 42 subject PDFs. If a guide ever conflicts with the subject, follow the subject.
- Compile with
c++and-Wall -Wextra -Werror, and keep C++98 compatibility (-std=c++98) - No external libraries (C++11 and Boost are forbidden)
- Forbidden C functions:
*printf(),*alloc()(malloc/calloc/realloc), andfree()(grade 0) using namespace ...andfriendare forbidden unless explicitly allowed (grade -42)- STL containers/algorithms are forbidden until Modules 08 and 09 (grade -42)
- Unless specified otherwise, output goes to stdout and ends with a newline
- No implementation in headers (except templates), and always use include guards
From Module 02 onward, classes must follow Orthodox Canonical Form (OCF), unless explicitly stated otherwise.
Common Beginner Mistakes to Avoid
Section titled “Common Beginner Mistakes to Avoid”Before you start, be aware of these common issues:
-
Using
using namespace std;- 42 forbids this. Always usestd::prefix. -
Mixing C and C++ styles - Don’t use
printfwhen you should usestd::cout. Don’t usemallocwhen you should usenew. -
Forgetting the semicolon after class definition - Classes end with
};not just} -
Not understanding references vs pointers - Module 01 covers this in detail.
-
Ignoring the Orthodox Canonical Form - From Module 02 onwards, every class needs proper constructors, destructor, and assignment operator.
Ready to Begin?
Section titled “Ready to Begin?”You now have the background knowledge to start the C++ Piscine. Head to Module 00: C++ Fundamentals to begin your journey!
Remember:
- Take your time with each module
- Practice by writing code, not just reading
- Use the Guides as reference
- Don’t hesitate to re-read previous modules
Good luck! 🚀