C++
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented C++
-
Section 4: Memory Management
-
Section 5: Templates and Generic Programming
-
Section 6: The Standard Template Library
-
Section 7: Modern C++ Features
-
Section 8: Error Handling
-
Section 9: Multithreading
-
Section 10: Operator Overloading and Type Conversion
-
Section 11: Advanced Topics
-
Section 12: Tooling and Build Systems
-
Section 13: Design Patterns in C++
-
Section 14: Interfacing with C and Systems Programming
-
Section 15: Networking and IPC Basics
-
Section 16: Graphics and Game Programming Basics
-
Section 17: The Boost Libraries
-
Section 18: Data Structures and Algorithms in C++
-
Section 19: Practical Projects
-
Section 20: More Concurrency Patterns
-
Section 21: More OOP and Design Practice
-
Section 22: File I/O and Streams
-
Section 23: More Standard Library
-
Section 24: Practice Exercises
-
Section 25: Interview and Algorithm Practice
-
Section 26: Compiler and Language Internals
-
Section 27: GUI and Application Frameworks Overview
-
Section 28: Testing and Quality Practices
-
Section 29: Numerics Library
-
Section 30: Concepts Library (C++20)
-
Section 31: Ranges Library (C++20) In Depth
-
Section 32: More Utility Library
3: Your First C++ Program
Alright, you've got your environment set up. Now comes the part where most people just copy-paste a "Hello World" snippet and move on. We aren't doing that. I want to show you how C++ actually thinks, because the first time you hit a compiler error, it's going to feel like the computer is shouting at you in a language you don't speak. Let's walk through this together.
Wait, where is cout?
I'm going to start by writing the absolute bare minimum I think I need to print a message to the screen. Here is my first attempt:
int main() {
cout << "Welcome to the machine!";
return 0;
}
If I try to run this, the compiler is going to lose its mind. It'll give me a wall of text, but the key phrase is 'cout' was not declared in this scope. Essentially, the compiler is saying, "I have no idea what a 'cout' is. Is that a variable? A function? Who is she?"
In C++, the core language is actually very small. Basic input and output aren't built into the language itself; they live in a library. To use them, I have to explicitly tell the compiler to bring in the Input/Output Stream library.
#include <iostream>
int main() {
cout << "Welcome to the machine!";
return 0;
}
The Namespace Hurdle
I'll bet you're thinking, "Great, fixed it." But if I compile this now, I'll get another error: 'cout' is not a member of 'std' (or something very similar). This is a quirk of C++ that trips up everyone.
C++ uses "namespaces" to prevent naming collisions. Imagine if you and I both wrote a function called print() and then tried to use both our libraries in one project. The computer wouldn't know which one to use. To solve this, almost everything in the standard library is wrapped in a namespace called std (short for standard).
I have two choices here. I could put using namespace std; at the top, but honestly? Don't do that. It's a bad habit that leads to massive headaches in larger projects. Instead, I'll just be explicit and use the scope resolution operator :: to tell the compiler exactly where cout lives.
#include <iostream>
int main() {
std::cout << "Welcome to the machine!";
return 0;
}
Now it works. The std::cout is the "character output stream," and those arrows << are the insertion operators. I'm essentially "pushing" the text string into the output stream.
Making it Interactive
Printing text is fine, but a program that doesn't take input is just a fancy typewriter. Let's try to make this program ask for the user's name. I'll need a place to store that name, so I'll use a std::string. But wait—if I just add std::string, I'll probably get another error because strings aren't part of the basic iostream library.
Let's adjust the code to include the string library and use std::cin (character input) to grab a value from the keyboard.
#include <iostream>
#include <string>
int main() {
std::string userName;
std::cout << "Enter your name: ";
std::cin >> userName;
std::cout << "Hello, " << userName << "! You've just written your first C++ program.";
return 0;
}
Notice how I used std::cin >> userName;. Notice the arrows? They point the opposite way of cout. We are now "extracting" data from the input stream and pushing it into our variable. I also chained multiple items together in the final cout line—C++ lets you string together as many pieces of data as you want in one line, as long as they are separated by those insertion operators.
📋 Practical Task
Build a Simple Age-in-Days Calculator
Write a program that asks the user for their age in years (as an integer) and then calculates and prints out approximately how many days they have been alive.
- Include both
<iostream>and<string>(though you might only need the former for this specific task). - Use
std::cinto capture the user's age. - Perform the math (Age * 365) inside the
std::coutstatement or in a separate variable. - Ensure you use the
std::prefix for all standard library components.
There are no comments for now.