Courses/C++/Introduction to C++

    Lesson 1 • Beginner

    Introduction to C++

    Write your first C++ program and understand the building blocks of one of the world's most powerful languages.

    What You'll Learn

    • What C++ is and why it matters
    • The structure of a C++ program
    • How to use cout for output and cin for input
    • Comments and escape sequences

    What is C++?

    C++ is a high-performance, compiled programming language created by Bjarne Stroustrup in 1979 at Bell Labs. It extends the C language with object-oriented features, making it ideal for building everything from operating systems to video games.

    Think of C++ like a professional race car — it gives you incredible speed and control, but you need to learn how the engine works. Languages like Python are more like automatic cars — easier to drive, but less control under the hood.

    Where is C++ used?

    • Game Engines: Unreal Engine, Unity (core), CryEngine
    • Operating Systems: Windows, macOS, Linux kernels
    • Browsers: Chrome, Firefox, Edge
    • Databases: MySQL, MongoDB, PostgreSQL
    • Embedded Systems: IoT devices, automotive software, robotics
    • Finance: High-frequency trading systems

    Your First C++ Program

    Every C++ program follows a specific structure. Let's break down the classic "Hello, World!" program:

    #include <iostream>    // 1. Include the I/O library
    using namespace std;   // 2. Use the standard namespace
    
    int main() {           // 3. Main function — entry point
        cout << "Hello, World!" << endl;  // 4. Print output
        return 0;          // 5. Return success
    }

    Line by line:

    • #include <iostream> — Includes the input/output stream library. Without this, you can't use cout or cin.
    • using namespace std; — Tells the compiler to use the standard namespace so you can write cout instead of std::cout.
    • int main() — The main function where every C++ program starts executing. The int means it returns an integer.
    • cout << "..." — The console output stream. The << operator sends data to the output.
    • endl — Inserts a newline and flushes the output buffer.
    • return 0; — Tells the OS the program finished successfully (0 = no error).

    Hello World Program

    Write your very first C++ program

    Try it Yourself »
    C++
    #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Hello, World!" << endl;
        cout << "Welcome to C++ programming!" << endl;
        cout << "C++ is powerful and fast." << endl;
        return 0;
    }

    Comments — Documenting Your Code

    Comments are notes for humans that the compiler ignores. Good comments explain why you did something, not what the code does.

    // Single-line comment — everything after // is ignored
    
    /* Multi-line comment
       spans multiple lines
       useful for longer explanations */
    
    int speed = 120;  // km/h — max allowed on this road segment

    Comments Example

    Practice using single-line and multi-line comments

    Try it Yourself »
    C++
    #include <iostream>
    using namespace std;
    
    int main() {
        // This is a single-line comment
        cout << "Comments are ignored by the compiler" << endl;
        
        /* This is a
           multi-line comment.
           It can span several lines. */
        cout << "Use comments to explain your code!" << endl;
        
        // Good practice: explain WHY, not WHAT
        int maxRetries = 3;  // Limit retries to prevent server overload
        cout << "Max retries: " << maxRetries << endl;
        
        return 0;
    }

    Escape Sequences

    Escape sequences are special character combinations that represent non-printable characters:

    SequenceMeaningExample Output
    \nNewlineMoves to next line
    \tTabAdds horizontal tab
    \\BackslashPrints a \
    \"Double quotePrints a "

    Escape Sequences

    Experiment with newlines, tabs, and special characters

    Try it Yourself »
    C++
    #include <iostream>
    using namespace std;
    
    int main() {
        // Newline character
        cout << "Line 1\nLine 2\nLine 3" << endl;
        
        // Tab character for alignment
        cout << "Name\tAge\tCity" << endl;
        cout << "Alice\t25\tLondon" << endl;
        cout << "Bob\t30\tParis" << endl;
        
        // Backslash and quotes
        cout << "She said \"Hello!\"" << endl;
        cout << "File path: C:\\Users\\Documents" << endl;
        
        return 0;
    }

    Input & Output

    Learn to read user input with cin and display output with cout

    Try it Yourself »
    C++
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        // Output with cout
        cout << "=== C++ Input & Output ===" << endl;
        
        // You can chain multiple outputs
        int year = 2024;
        string language = "C++";
        cout << "Learning " << language << " in " << year << "!" << endl;
        
        // Input with cin
        string name;
        int age;
        cout << "Enter your name: ";
        cin >> name;
        cout << "Enter your age: ";
        cin >> age;
        
        cout << "Hello, " << name 
    ...

    Common Mistakes

    ⚠️ Forgetting semicolons: Every statement in C++ must end with ;. Missing one causes a compilation error.

    ⚠️ Case sensitivity: Cout and cout are different. C++ is case-sensitive — use lowercase.

    ⚠️ Missing #include: If you use cout without #include <iostream>, the compiler won't recognize it.

    ⚠️ Using = instead of <<: The insertion operator is <<, not =. Write cout << "text".

    Pro Tips

    💡 Use '\n' over endl: For performance, '\n' is faster than endl because endl flushes the buffer every time.

    💡 Compile often: Don't write 100 lines before compiling. Write a few lines, compile, test, repeat.

    💡 Read error messages: Compiler errors always include a line number. Start fixing from the first error — later errors are often caused by earlier ones.

    📋 Quick Reference

    ConceptSyntax
    Include library#include <iostream>
    Outputcout << "text" << endl;
    Inputcin >> variable;
    Main functionint main() { ... return 0; }
    Single comment// comment
    Multi comment/* comment */

    Lesson Complete!

    You've written your first C++ program and learned about output, input, comments, and escape sequences. In the next lesson, you'll learn how to store and manipulate data with Variables & Data Types.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service