What is C++?
C++ is a general-purpose, compiled programming language that supports procedural, object-oriented, and generic programming.
Master C++ from fundamentals to advanced enterprise architecture with all 100 comprehensive questions and expert answers, each equipped with dedicated copy-to-clipboard functionality.
Browse through all 100 technical questions and answers below. Use the instant search filter to find specific topics or click the Copy Q&A button on any card to copy the question and answer directly to your clipboard.
C++ is a general-purpose, compiled programming language that supports procedural, object-oriented, and generic programming.
Bjarne Stroustrup developed C++ at Bell Labs, beginning in 1979.
C is primarily procedural, while C++ adds features such as classes, inheritance, polymorphism, templates, and function overloading. C++ also supports procedural programming.
Important features include classes, objects, encapsulation, inheritance, polymorphism, abstraction, templates, exception handling, and the Standard Library.
A compiler translates source code into machine code or another lower-level representation that can be executed.
main() is the entry point of a standard hosted C++ program. Program execution begins there.
#include is a preprocessor directive that makes declarations from a header available to the source file.
A declaration introduces a name and its type. A definition provides the entity itself, such as storage for a variable or the body of a function.
A namespace groups names and helps prevent naming conflicts. For example, the standard library uses the std namespace.
std::cout is the standard output stream, commonly used to print text and values to the console.
std::cin is the standard input stream, commonly used to read input from the console.
A variable is a named object that stores a value of a particular type.
Common fundamental types include bool, char, int, float, double, and void, along with their variations.
int represents whole numbers, while double represents floating-point numbers and can store fractional values.
A constant is a value or object that cannot be modified through the name or access path being used. Use const to declare a const-qualified object.
const prevents modification through that name. constexpr additionally allows a value or function to be used in constant expressions when its requirements are met.
Type conversion changes a value from one type to another. It can happen implicitly or be requested explicitly with a cast.
Implicit conversion is performed automatically by the language. Explicit conversion is requested by the programmer, for example with static_cast.
Operators are symbols or keywords that perform operations on operands, such as +, -, *, /, ==, and &&.
= assigns a value. == compares two values for equality and produces a bool result.
The % operator gives the remainder of integer division. For example, 10 % 3 evaluates to 1.
Operator precedence determines how operators are grouped in an expression when parentheses do not specify the grouping.
The conditional operator ?: chooses one of two expressions based on a condition, as in condition ? value1 : value2.
Both increment i. ++i produces the incremented value; i++ produces the old value before incrementing. The distinction matters when the result is used.
Control statements determine the flow of execution. Examples include if, switch, for, while, and break.
It executes one block when a condition is true and an alternative block when the condition is false.
switch is useful when selecting among multiple cases based on an integral, enumeration, or suitable constant expression value.
A for loop repeats a statement or block, commonly using an initialization, a continuation condition, and an iteration expression.
A while loop repeatedly executes its body as long as its condition evaluates to true. The condition is checked before each iteration.
A do-while loop checks its condition after executing the body, so its body runs at least once.
break exits the nearest enclosing loop or switch. continue skips the rest of the current loop iteration and proceeds to the next iteration.
An array stores a fixed number of elements of the same type in contiguous memory.
Use an index in square brackets, such as values[0]. C++ array indexing starts at zero.
A multidimensional array is an array whose elements are themselves arrays, such as int grid[3][4].
std::string, provided by the
A C-style string is a null-terminated character sequence, often stored in a char array. std::string manages its own size and storage and provides convenient operations.
A function is a named, reusable unit of code that can accept parameters and optionally return a value.
A function prototype is a declaration that specifies a function's name, return type, and parameter types before its definition is needed.
Function overloading allows multiple functions to share a name when their parameter lists differ in type, number, or order.
A default argument is a value supplied automatically for a function parameter when the caller omits that argument.
Recursion occurs when a function calls itself, directly or indirectly. A correct recursive solution needs a base case.
An inline function is a function declared with the inline specifier. The specifier primarily affects linkage rules; it does not guarantee that calls are expanded inline.
Pass by value gives a function its own parameter object initialized from the argument. Changes to that parameter do not change the caller's original object.
Pass by reference binds a parameter to the caller's object, allowing the function to access or modify that object directly.
A pointer is an object that stores the address of another object or function, or a special null pointer value.
A reference is an alias for an existing object or function. A reference must be initialized when declared.
A pointer can be reassigned and can be null. A reference acts as an alias once initialized and cannot be reseated to refer to a different object.
nullptr is the null pointer literal in modern C++. It represents a null pointer and is preferred over using 0 or NULL for pointer values.
Pointer arithmetic moves a pointer among elements of the same array object, such as incrementing a pointer to reach the next element.
A dangling pointer refers to an object whose lifetime has ended or to storage that is no longer valid. Using it to access the object causes undefined behavior.
Dynamic memory allocation obtains storage during program execution. Modern C++ usually favors standard containers and smart pointers over manual new and delete.
new creates an object in dynamically allocated storage and returns a pointer. delete destroys an object created with a matching single-object new expression and releases its storage.
A memory leak occurs when dynamically allocated storage is no longer reachable or released even though the program no longer needs it.
A class is a user-defined type that can combine data members and member functions, with access control for its members.
An object is an instance of a type. An object of a class has the data and behavior defined by that class.
Encapsulation bundles data and the functions that operate on it, while controlling access to internal details through public, protected, and private members.
Abstraction exposes essential operations while hiding implementation details that users of a type do not need to know.
public members are accessible wherever the object is accessible; private members are accessible to the class and its friends; protected members are also accessible to derived classes.
A constructor is a special member function used to initialize objects. It has the class name and no return type.
A destructor is a special member function that runs when an object is destroyed. It is commonly used to release resources owned by the object.
A copy constructor initializes a new object from another object of the same class, typically with a signature like ClassName(const ClassName&).
A move constructor initializes an object using resources from an rvalue or expiring object, often allowing efficient transfer instead of copying.
Inheritance lets a derived class build on an existing base class, reusing and extending its accessible members.
Common structural forms are single, multiple, multilevel, hierarchical, and hybrid inheritance.
Polymorphism allows a common interface to represent different underlying types or behaviors. In C++, it includes compile-time and run-time forms.
Compile-time polymorphism is resolved during compilation. Examples include function overloading, operator overloading, and templates.
Run-time polymorphism commonly uses virtual functions and base-class pointers or references to select an overridden function based on the object's dynamic type.
A virtual function is a member function that supports dynamic dispatch when called through a base-class pointer or reference.
A pure virtual function is declared with = 0 in a class. A class with a pure virtual function is abstract and cannot be instantiated directly.
An abstract class is a class that cannot be instantiated, usually because it has at least one pure virtual function.
Overriding occurs when a derived class provides its own implementation of a virtual function declared in a base class.
Operator overloading defines how an operator works with user-defined types, subject to C++ language rules.
this is a pointer available in non-static member functions that refers to the object on which the function was called.
A static data member belongs to the class rather than to each individual object, so the class has one shared member.
A friend function is not a member of the class, but it is granted access to that class's private and protected members.
A template lets programmers write functions or classes parameterized by types or values, enabling generic code.
A function template defines a family of functions that can operate with different types or values.
A class template defines a family of classes parameterized by types or values, such as std::vector
STL commonly refers to the C++ Standard Library facilities built around generic containers, algorithms, iterators, and related utilities.
std::vector is a dynamic-size sequence container that stores elements contiguously and can grow or shrink as needed.
A built-in array has a fixed extent, while std::vector manages a resizable sequence and provides size and container operations.
std::list is a doubly linked sequence container. It supports efficient insertion and removal at known positions but does not provide constant-time random access.
std::map is an associative container that stores unique keys in sorted order, with each key associated with a mapped value.
std::unordered_map is an associative container that stores key-value pairs using hashing and does not keep keys in sorted order.
An iterator is an object that provides a way to access and traverse elements in a container or range.
A stack follows last-in, first-out (LIFO). A queue follows first-in, first-out (FIFO).
Exception handling provides a mechanism to report and handle exceptional conditions using try, throw, and catch.
Code that may throw an exception is placed in a try block. A matching catch handler can handle an exception thrown from that code.
throw raises an exception, transferring control to a matching handler up the call stack.
File streams such as std::ifstream, std::ofstream, and std::fstream provide input and output operations on files.
Include
Text mode may apply implementation-specific text translations. Binary mode requests byte-oriented input or output without those text translations.
A const member function promises not to modify the observable non-mutable state of the object through this. It can be called on const objects.
Resource Acquisition Is Initialization (RAII) ties resource ownership to object lifetime. Constructors acquire resources and destructors release them.
Smart pointers are class types that manage dynamically allocated objects automatically. The standard library provides std::unique_ptr, std::shared_ptr, and std::weak_ptr.
std::unique_ptr provides exclusive ownership. std::shared_ptr allows shared ownership using a reference-counting control block.
A lambda is an unnamed function object that can be defined inline, optionally capturing variables from its surrounding scope.
auto asks the compiler to deduce a variable's type from its initializer, reducing the need to spell out complex types.
A range-based for loop iterates over elements in a range, for example: for (const auto& item : items).
Both define class types. The default member access is public for struct and private for class; default inheritance access is also different.