An Introduction to C++ Programming

Available in: Tiếng Việt
Reading time 3 min read
c++

Introduction to C++

C++ is a powerful general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language.

Why Learn C++?

C++ remains one of the most widely used programming languages in the world for several compelling reasons:

  • Performance: C++ provides high performance and efficiency
  • Control: Direct memory manipulation and low-level control
  • Versatility: Used in game development, operating systems, browsers, and more
  • Foundation: Understanding C++ makes learning other languages easier

Basic Syntax Example

Here’s a simple “Hello, World!” program in C++:

cpp
#include <iostream>

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

Key Features of C++

Object-Oriented Programming

C++ supports the four main principles of OOP:

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

Example of a simple class:

cpp
class Rectangle {
private:
    double length;
    double width;
    
public:
    // Constructor
    Rectangle(double l, double w) : length(l), width(w) {}
    
    // Method to calculate area
    double area() {
        return length * width;
    }
};

Memory Management

C++ gives you direct control over memory allocation and deallocation:

cpp
#include <iostream>

int main() {
    // Dynamic memory allocation
    int* arr = new int[5];
    
    // Use the memory
    for (int i = 0; i < 5; i++) {
        arr[i] = i * 10;
        std::cout << arr[i] << " ";
    }
    
    // Free the allocated memory
    delete[] arr;
    
    return 0;
}

Templates

C++ templates enable generic programming:

cpp
template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}

int main() {
    // Works with different types
    std::cout << max(10, 20) << std::endl;      // int
    std::cout << max(10.5, 20.5) << std::endl;  // double
    std::cout << max('A', 'B') << std::endl;    // char
    
    return 0;
}

Modern C++ Features

Modern C++ (C++11 and beyond) introduced many features that make the language more powerful and easier to use:

  • Auto type deduction
  • Lambda expressions
  • Smart pointers
  • Range-based for loops
  • Move semantics

Example of modern C++ features:

cpp
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>

int main() {
    // Auto type deduction
    auto x = 10;
    
    // Vector with initialization list
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    
    // Range-based for loop
    for (const auto& num : numbers) {
        std::cout << num << " ";
    }
    
    // Lambda expression
    std::for_each(numbers.begin(), numbers.end(), [](int n) {
        std::cout << n * 2 << " ";
    });
    
    // Smart pointer
    std::shared_ptr<int> ptr = std::make_shared<int>(100);
    
    return 0;
}

Conclusion

C++ continues to evolve and remain relevant despite being over 40 years old. Its combination of performance, control, and modern features makes it an excellent language for many applications, from system programming to game development.

Whether you’re a beginner or an experienced programmer looking to expand your skills, learning C++ will provide you with valuable insights into how computers work and how software is built.