Welcome to the C++ Basics Tutorial! If you're new to programming or looking to expand your knowledge of C++, this guide will help you get started.

Introduction to C++

C++ is a powerful, general-purpose programming language. It's widely used for developing applications in various domains, including system/software applications, game development, and high-performance server and client applications.

Key Features of C++

  • High Performance: C++ offers high performance and is often used in performance-critical applications.
  • Portability: C++ programs can be run on various platforms.
  • Object-Oriented Programming (OOP): C++ supports OOP, which helps in organizing and structuring code efficiently.
  • Rich Standard Library: C++ provides a rich standard library with a wide range of functionalities.

Getting Started

Before diving into the basics, make sure you have a C++ compiler installed. We recommend using Visual Studio or Code::Blocks.

Writing Your First C++ Program

Here's a simple C++ program that prints "Hello, World!" to the console:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

To compile and run this program, follow these steps:

  1. Save the code in a file named hello.cpp.
  2. Open the command prompt and navigate to the directory where you saved the file.
  3. Compile the program using the following command: g++ -o hello hello.cpp
  4. Run the program using the following command: ./hello

You should see "Hello, World!" printed to the console.

Variables and Data Types

In C++, variables are used to store data. Here are some common data types:

  • int: Integer values.
  • float: Floating-point numbers.
  • double: Double-precision floating-point numbers.
  • char: Single characters.

Example

int age = 25;
float pi = 3.14159;
char grade = 'A';

Control Structures

Control structures are used to control the flow of execution in a program. Here are some common control structures:

  • Conditional statements: if, else if, else
  • Loops: for, while, do-while

Example

#include <iostream>

int main() {
    int number = 10;

    if (number > 5) {
        std::cout << "Number is greater than 5" << std::endl;
    } else {
        std::cout << "Number is not greater than 5" << std::endl;
    }

    for (int i = 0; i < 5; i++) {
        std::cout << "Loop iteration: " << i << std::endl;
    }

    return 0;
}

Functions

Functions are blocks of code that perform a specific task. They help in organizing and reusing code.

Example

#include <iostream>

void greet() {
    std::cout << "Hello, World!" << std::endl;
}

int main() {
    greet();
    return 0;
}

Next Steps

To learn more about C++, explore the following resources:

By following this tutorial, you should have a basic understanding of C++. Happy coding! 🚀