Posts

Object slicing

Object slicing is a concept in C++ that occurs when an object of a derived class is assigned to an object of its base class. In this process, the portion of the derived class (i.e., the members and functionalities specific to the derived class) is "sliced off," leaving only the part of the base class. This happens because the base class object cannot hold the additional members and methods of the derived class. Why Does Object Slicing Occur? Object slicing occurs because of value assignment or passing objects by value . When a derived class object is copied into a base class object, only the base class part is copied. The derived class members, which extend the base class, are lost during this assignment. #include <iostream> using namespace std ; class Base { public:     int baseVar ;     virtual void show () {         cout << "Base class variable: " << baseVar << endl ;     } }; class Derived : ...

std::reduce

  Leveraging std::reduce for Custom Operations in C++ Introduction: In modern C++ (C++17 and later), the Standard Template Library (STL) introduced parallel algorithms, allowing developers to harness multi-core processors easily. One such powerful function is std::reduce , which performs reduction operations on a range of elements. This blog will guide you through using std::reduce for custom operations like finding the maximum element and multiplying values in an array. What is std::reduce ? std::reduce is a function that combines elements in a range using a binary operation. It supports parallel execution, making it suitable for performance-critical applications. Syntax: cpp template<class ExecutionPolicy, class ForwardIt, class T, class BinaryOperation> T reduce(ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, T init, BinaryOperation binary_op); ExecutionPolicy: Determines the execution strategy (e.g., std::execution::par for parallel). first, last...