Introduction to Programming in C++

C++ is a versatile and powerful programming language that builds on the foundations of C. It introduces object-oriented programming (OOP) features, making it ideal for developing complex systems, games, and high-performance applications. Whether you’re new to programming or transitioning from another language, this guide will help you understand the core concepts of C++ through clear explanations and practical examples.

C++ is widely used in industries ranging from game development to finance, and mastering it opens up a world of opportunities for software development.


Table of Contents

  1. What is C++?
  2. Setting Up Your Environment
  3. Variables and Data Types
  4. Operators and Expressions
  5. Conditional Statements
  6. Loops
  7. Functions
  8. Arrays
  9. Pointers
  10. Object-Oriented Programming

What is C++?

C++ is an extension of the C programming language with added features like classes, objects, and inheritance. It combines the low-level capabilities of C with high-level abstractions, making it both powerful and flexible.

// Example: Your first C++ program
#include <iostream>

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

Setting Up Your Environment

To write and run C++ programs, you need a C++ compiler like GCC (GNU Compiler Collection) or Clang, and a text editor or IDE (Integrated Development Environment).

# Example: Compiling and running a C++ program
g++ program.cpp -o program
./program

Variables and Data Types

Variables are used to store data in C++. You must declare the type of a variable before using it. Common data types include int, float, double, char, and bool.

// Example: Declaring variables
#include <iostream>

int main() {
    int age = 25;
    float height = 5.9f;
    char grade = 'A';
    bool isStudent = true;

    std::cout << "Age: " << age << ", Height: " << height << ", Grade: " << grade << std::endl;
    return 0;
}

Operators and Expressions

C++ includes arithmetic, relational, and logical operators to perform operations on data.

// Example: Using operators
#include <iostream>

int main() {
    int a = 10, b = 5;
    int sum = a + b; // 15
    bool isGreater = a > b; // true

    std::cout << "Sum: " << sum << ", Is Greater: " << isGreater << std::endl;
    return 0;
}

Conditional Statements

Conditional statements like if, else if, and else allow you to execute code based on certain conditions.

// Example: Conditional statement
#include <iostream>

int main() {
    int age = 18;

    if (age >= 18) {
        std::cout << "You are an adult." << std::endl;
    } else {
        std::cout << "You are a minor." << std::endl;
    }

    return 0;
}

Loops

Loops like for, while, and do-while help you repeat a block of code multiple times.

// Example: For loop
#include <iostream>

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

    return 0;
}

Functions

Functions are reusable blocks of code that perform a specific task. You can define functions using the return_type function_name() syntax.

// Example: Function
#include <iostream>

// Function to add two numbers
int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(5, 10);
    std::cout << "Result: " << result << std::endl; // 15
    return 0;
}

Arrays

Arrays are used to store multiple values of the same type in a single variable. You can access and manipulate array elements using their index.

// Example: Array
#include <iostream>

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};

    for (int i = 0; i < 5; i++) {
        std::cout << "Number " << i << ": " << numbers[i] << std::endl;
    }

    return 0;
}

Pointers

Pointers are variables that store memory addresses. They are a powerful feature of C++ that allows direct memory manipulation.

// Example: Pointers
#include <iostream>

int main() {
    int num = 10;
    int *ptr = &num; // Pointer to num

    std::cout << "Value: " << *ptr << ", Address: " << ptr << std::endl;
    return 0;
}

Object-Oriented Programming

C++ supports object-oriented programming (OOP), which allows you to model real-world entities using classes and objects.

// Example: Class and Object
#include <iostream>

// Define a class
class Person {
public:
    std::string name;
    int age;

    void display() {
        std::cout << "Name: " << name << ", Age: " << age << std::endl;
    }
};

int main() {
    // Create an object
    Person person1;
    person1.name = "Alice";
    person1.age = 25;

    person1.display(); // Name: Alice, Age: 25
    return 0;
}