Posts

Showing posts from May, 2023

C++11 What is Variadic template and How it works?

Variadic templates are a feature introduced in C++11 that allows functions and classes to take a variable number of arguments of different types. The syntax for a variadic template is to use an ellipsis ... after the type parameter. Here is an example of a variadic function that takes a variable number of arguments and returns their sum: template<typename T> T sum(T value) { return value; } template<typename T, typename... Args> T sum(T value, Args... args) { return value + sum(args...); } The first template function is the base case, which simply returns the value passed in. The second template function is the recursive case, which adds the current value to the sum of the remaining arguments. Variadic templates can also be used to create variadic classes. Here is an example of a variadic tuple class that stores a tuple of values of different types: template<typename... Ts> class Tuple {}; template<typename T, typename... Ts> class Tuple<T,...

C++11 What is Delegating Constructors?

Delegating constructors are a feature introduced in C++11 that allows one constructor of a class to call another constructor of the same class to initialize the object. This feature simplifies code and eliminates the need to repeat the same code multiple times for different constructors. A delegating constructor has the same syntax as a regular constructor, but it calls another constructor of the same class in its member initializer list using the this keyword. Here's an example: class MyClass { public: MyClass(int x, int y) : x_(x), y_(y) {} MyClass(int x) : MyClass(x, 0) {} // delegating constructor private: int x_;  int y_; }; In this example, the MyClass has two constructors: one that takes two int parameters and one that takes one int parameter. The second constructor is a delegating constructor because it calls the first constructor using this . The x parameter is passed to the first constructor, and the y parameter is set to zero. Delegating constructors...

Prepare for your next C++ interview

Preparing for a C++ interview requires a solid understanding of the language, its features, and common programming concepts. Here are some key areas to focus on: C++ basics: Review the fundamentals of C++, such as variables, data types, operators, control structures (if-else, loops), functions, and classes. Pointers and memory management: Understand how pointers work in C++ and be comfortable with concepts like memory allocation and deallocation, dynamic memory allocation (new/delete), and memory leaks. Adopt Smart Pointers. Understand what are they, How they work and know when to use them. Object-oriented programming (OOP): Review the principles of OOP, including encapsulation , inheritance , and polymorphism . Understand the concepts of classes, objects, constructors, destructors, access specifiers, and virtual functions and virtual classes. Templates and generics: Familiarize yourself with C++ templates, including function templates and class templates. Understand how to use the...