CPP Interview Questions
CPP Interview Questions
22. What is the difference between `private`, `protected`, and `public` access
specifiers?
Answer:
- Private: Accessible only within the class.
- Protected: Accessible within the class and by derived classes.
- Public: Accessible from anywhere.
26. What is the difference between shallow copy and deep copy in C++?
Answer:
- Shallow copy: Copies the object's values as they are (including pointers).
- Deep copy: Creates a new copy of dynamically allocated objects, ensuring
independent memory allocation.
be invoked.
37. What is the difference between function overloading and function overriding?
Answer:
- Function Overloading: Multiple functions with the same name but different
parameters in the same scope.
- Function Overriding: Redefining a base class function in a derived class with
the same signature.
---
42. What is the difference between function templates and class templates in C++?
Answer:
- Function Template: Defines a template for a function to work with any data
type.
- Class Template: Defines a template for a class to work with any data type.
61. What is the difference between `#include <filename>` and `#include "filename"`?
Answer:
- #include <filename> is used for system or standard library headers, which are
searched in standard directories.
- #include "filename" is used for user-defined headers, which are searched in
the current directory first.
68. What is the default access specifier for class members in C++?
Answer: The default access specifier for class members in C++ is private.
69. What is the default access specifier for struct members in C++?
Answer: The default access specifier for struct members in C++ is public.
71. What is the difference between an enum and a `#define` constant in C++?
Answer:
- An enum is a type-safe way of defining constants, whereas #define is not type-
safe.
- Enum variables can be debugged, whereas #define values are replaced by their
literal values in the code.