![]() |
VOOZH | about |
C++ has evolved significantly with modern standards (C++11, 14, 17, 20), introducing powerful features like move semantics, concurrency, smart pointers and design patterns. Interviewers often assess not only coding skills but also a candidate’s design thinking, performance awareness and fluency in modern C++ idioms.
A scope resolution operator is denoted by a '::' symbol. Just like its name this operator resolves the barrier of scope in a program. A scope resolution operator is used to reference a member function or a global variable out of their scope furthermore to which it can also access the concealed variable or function in a program.
Scope Resolution is used for numerous amounts of tasks:
An inline function suggests to the compiler that function calls may be replaced with the function body to reduce overhead. Recursive functions can technically be marked inline, but in practice, the compiler will inline only a limited number of calls (if any). For deep recursion, inlining is not practical.
So yes, you can declare a recursive function inline, but compilers usually ignore inlining in such cases.
Just like its name, things can change suddenly and unexpectantly; So it is used to inform the compiler that the value may change anytime. Also, the volatile keyword prevents the compiler from performing optimization on the code. It was intended to be used when interfacing with memory-mapped hardware, signal handlers and machine code instruction.
Here, b takes ownership of the internal buffer of a, avoiding deep copy.
Benefits:
Best Use Cases:
this" pointer is an implicit pointer available in all non-static member functions of a class. It points to the object that invoked the member function, allowing access to that object’s members.
It's uses:
Following are the primary differences between the shallow copy VS deep copy:
Shallow Copy | Deep Copy |
|---|---|
| In Shallow copy, a copy of the original object is stored and only the reference address is finally copied. In simple terms, Shallow copy duplicates as little as possible | In Deep copy, the copy of the original object and the repetitive copies both are stored. In simple terms, Deep copy duplicates everything |
| A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share individual elements. | A deep copy of a collection is two collections with all of the elements in the original collection duplicated. |
| A shallow copy is faster | Deep copy is comparatively slower. |
An overflow occurs when a calculation produces a result outside the range representable by a data type. In C++, signed integer overflow leads to undefined behavior, while unsigned integer overflow wraps around modulo the maximum value.
Example:
int x = INT_MAX;
x = x + 1; // signed overflow → undefined behaviorunsigned int y = UINT_MAX;
y = y + 1; // wraps around to 0
Move semantics allow the resources of a temporary (rvalue) object to be transferred (moved) rather than copied, leading to better performance, especially for large objects like containers or strings. Introduced in C++11, move semantics are enabled through rvalue references (T&&), which bind to temporaries.
Example:
string a = "Hello";
string b = std::move(a); // 'a' is moved, not copied
Perfect forwarding is a technique to pass arguments to another function without losing their value category (lvalue or rvalue). It's essential in generic programming to write functions that forward arguments efficiently.
Achieved using:
Example:
This ensures:
Use Case: Constructors in wrapper classes, factory functions, etc.
A lambda expression is an anonymous function that can capture variables from its enclosing scope. It's a concise way to write small functions, especially for STL algorithms or callbacks.
Syntax:
[ capture ] (parameters) -> return_type { body }
Capture Modes:
Example:
15
Closures are the underlying objects generated from lambdas that store captured variables. Lambdas simplify code in loops, sorting, threading and event handling.
unique_ptr, shared_ptr and weak_ptr differ?Smart pointers are template classes in <memory> that automate memory management and prevent leaks by destroying objects when they go out of scope.
Types:
shared_ptr without affecting the count (avoids cyclic references).Example:
unique_ptr<int> up = make_unique<int>(10);
shared_ptr<int> sp = make_shared<int>(20);
Best Practices:
unique_ptr by defaultshared_ptr only when ownership must be sharedweak_ptr to break cycles in graphs, trees, etc.Smart pointers ensure exception-safe and clean RAII-based memory management.
Modern C++ (from C++11) provides a full-fledged standard threading library with:
Key Features:
Example:
These tools help in building concurrent and parallel programs that are safe and portable, avoiding race conditions and deadlocks.
The Singleton Pattern ensures that only one instance of a class is created throughout the program and it provides a global point of access to that instance.
Example:
Use Cases:
Caution: Must be made thread-safe in multithreaded environments.
The Observer Pattern defines a one-to-many dependency so that when one object (subject) changes, all dependent objects (observers) are notified.
Real-world Example: GUI frameworks where clicking a button updates multiple widgets.
Components:
Example Sketch:
This pattern promotes loose coupling and event-driven design.
Template Metaprogramming (TMP) is a technique where templates are used to compute values at compile time, enabling optimization and static checks.
Example Factorial at compile time:
Use Cases:
TMP makes C++ powerful for low-level systems and high-performance code.
C++20 introduces concepts, ranges, coroutines and more, enhancing code expressiveness and safety.
Example Concepts:
Benefits:
Other features include:
ranges::views::filter and transformco_await, co_yield for asynchronous programming