CPP
CPP
C++ is a general-purpose programming language that supports both procedural and object-oriented programming
paradigms.
A class is a blueprint for creating objects; an object is an instance of a class containing data and functions.
C is procedural; C++ supports both procedural and object-oriented programming, offering features like classes and
objects.
Operators perform operations on variables and values, such as arithmetic (+, -, *, /), relational (==, !=), and logical
(&&, ||).
Virtual functions allow derived classes to override functions in base classes, enabling dynamic polymorphism.
6. What is Inheritance?
Inheritance allows a class (derived) to acquire properties and behaviors from another class (base).
7. What is Polymorphism?
Polymorphism allows functions or methods to behave differently based on the object that invokes them.
8. What is Encapsulation?
Encapsulation is the bundling of data and methods that operate on that data within a single unit, like a class.
9. What is Abstraction?
Abstraction hides complex implementation details and shows only the necessary features of an object.
Overloading allows multiple functions with the same name but different parameters; overriding allows a derived
class to provide a specific implementation of a function already defined in its base class.
Constructors initialize objects; destructors clean up resources when objects are destroyed.
12. What is the 'this' Pointer?
Operator overloading allows customizing the behavior of operators for user-defined types.
'new' initializes objects and calls constructors; 'malloc' allocates memory without initialization.
A friend function can access private and protected members of a class, even though it is not a member of the class.
The Standard Template Library (STL) provides a set of common classes and interfaces for data structures and
algorithms.
Templates allow functions and classes to operate with generic types, enabling code reuse.
Exception handling manages errors through 'try', 'catch', and 'throw' blocks to maintain normal program flow.
In C++, structs have public members by default; classes have private members by default.
A virtual destructor ensures that the destructor of the derived class is called when deleting an object through a base
class pointer.
Function overloading allows multiple functions with the same name but different parameters.
23. What is the Difference between Shallow Copy and Deep Copy?
A shallow copy copies object references; a deep copy copies the actual objects.GeeksforGeeks+4BTree+4PREP
INSTA+4
The 'static' keyword can define class-level variables, functions, or restrict variable scope within a file.
In C++, '==' compares values; 'equals()' is not standard in C++ (more common in Java).
Run-Time Type Information (RTTI) allows the type of an object to be determined during program execution.
'delete' deallocates memory for a single object; 'delete[]' deallocates memory for an array of objects.
A pure virtual function is a function declared in a base class that must be overridden in derived classes.
29. What is the Difference between 'public', 'private', and 'protected' Access Specifiers?
'public' members are accessible from anywhere; 'private' members are accessible only within the class; 'protected'
members are accessible within the class and its derived classes.
The 'mutable' keyword allows a member of an object to be modified even if the object is declared as const.
Stack memory is used for static memory allocation; heap memory is used for dynamic memory allocation.
The 'const' keyword declares variables as constant, meaning their value cannot be changed after initialization.
'const' defines a constant variable with a specific type; '#define' creates a macro without type checking.
34. What is the Use of 'volatile' Keyword?
The 'volatile' keyword tells the compiler that a variable's value may change at any time, preventing certain
optimizations.
In a 'struct', each member has its own memory; in a 'union', all members share the same memory location.PREP
INSTA
'try' defines a block of code to test for errors; 'catch' defines a block of code to handle errors; 'throw' is used to
signal the occurrence of an exception.
The 'explicit' keyword prevents the compiler from using implicit conversions for constructors.
Overloading allows multiple functions with the same name but different parameters; overriding allows a derived
class to provide a specific implementation of a function already defined in its base class.
An 'inline' function suggests to the compiler to insert the function's code at the point of call to reduce function call
overhead.
42. What is the Difference between 'call by value' and 'call by reference'?
'Call by value' passes a copy of the variable; 'call by reference' passes the variable itself, allowing modifications.
'break' exits the current loop; 'continue' skips the current iteration and proceeds to the next.
45. What is the Use of 'goto' Statement?
The 'goto' statement transfers control to a labeled statement; its use is generally discouraged due to readability
concerns.
'++i' increments the value before use; 'i++' increments the value after use.
The 'sizeof' operator returns the size, in bytes, of a data type or object.
In a 'union', all members share the same memory location; in a 'structure', each member has its own memory
location.
The 'auto' keyword allows the compiler to automatically deduce the type of a variable from its initializer.
50. What is the Difference between 'public', 'private', and 'protected' Inheritance?
In 'public' inheritance, public and protected members of the base class remain public and protected in the derived
class; in 'private' inheritance, they become private; in 'protected' inheritance, they become protected.
Lambda functions are anonymous functions used for short operations, often passed to algorithms like std::sort.
If you define a custom destructor, copy constructor, or copy assignment operator, you should usually define all three.
In modern C++, the rule of five includes move constructor and move assignment operator.
53. What is the difference between deep copy and move semantics?
Deep copy duplicates objects and their contents; move semantics transfers resources from one object to another,
avoiding copies.
Smart pointers (unique_ptr, shared_ptr, weak_ptr) automatically manage memory and prevent leaks.
55. What is std::move and when is it used?
std::move casts an object to an rvalue to enable move semantics (e.g., for performance).
std::vector is a dynamic array provided by the STL, offering efficient insertions and deletions at the end.
Containers include vector, list, deque, map, set, unordered_map, etc., used to store and manipulate data.
RAII ensures resource allocation and deallocation are tied to object lifetime using constructors/destructors.
map is ordered and uses a red-black tree; unordered_map is faster but unordered and uses hash tables.
vector is a dynamic array; list is a doubly linked list. Use list for frequent insertions/deletions.