Cpp Basic Level
Cpp Basic Level
ILLINDA CHENNAKESAV
1. What is C++?
Object-Oriented Programming
Operator Overloading
Inheritance
Polymorphism
Encapsulation
Abstraction
Templates
An object is an instance of a class. It can hold both data (attributes) and methods (functions).
A class is a blueprint for creating objects. It encapsulates data for the object and methods to
manipulate that data.
8. What is inheritance?
Inheritance allows one class to inherit attributes and methods from another class, promoting
code reusability.
9. What is polymorphism?
Polymorphism allows objects of different classes to respond to the same function call in
different ways. It can be achieved using function overloading or overriding.
Function overloading is when multiple functions with the same name but different parameters
are defined.
Operator overloading allows C++ operators to be redefined and used with user-defined types.
A virtual function is a member function in the base class that can be overridden in a derived
class. It enables dynamic (run-time) polymorphism.
An abstract class contains at least one pure virtual function. It cannot be instantiated directly
and is intended to be a base class.
Templates allow functions or classes to operate with generic types, enabling code reuse for
any data type.
19. What is the difference between a shallow copy and a deep copy?
Stack: Memory is managed automatically, and variables are removed once the
function call ends.
Heap: Memory is managed manually, and it remains until explicitly freed.
The static keyword makes variables or functions maintain their value across multiple calls.
The const keyword defines a variable whose value cannot be changed after initialization.
Constructor overloading allows a class to have more than one constructor with different
parameter lists.
A friend function can access the private and protected members of a class, though it is not a
member of the class.
In a class, members are private by default, while in a struct, they are public by default.
An inline function is expanded in line when it is called, which may improve performance
by reducing the overhead of a function call.
A virtual destructor ensures that destructors are called in the correct order in a class hierarchy
when deleting objects.
RAII is a programming concept in C++ where resources are acquired and released through
object lifetime, often through constructors and destructors.
A segmentation fault occurs when a program tries to access an invalid memory location.
40. What is multiple inheritance?
Multiple inheritance allows a class to inherit from more than one base class.
Function overriding occurs when a derived class has a definition for one of the member
functions of the base class.
The explicit keyword prevents the compiler from using implicit constructors for type
conversions.
Typecasting is the conversion of one data type into another. There are two types:
An exception is an error-handling mechanism in C++ that handles runtime errors, using try,
catch, and throw blocks.
A try-catch block is used to handle exceptions. Code that may throw an exception is placed
in a try block, and the catch block catches and handles that exception.
Dynamic memory allocation refers to allocating memory during runtime using new and
deallocating it using delete.
Function templates allow you to create a function that can work with any data type. Syntax:
template<typename T>
T add(T a, T b) {
return a + b;
}
template<typename T>
class Box {
T value;
public:
Box(T val) : value(val) {}
T getValue() { return value; }
};
STL is a collection of C++ template classes for data structures and algorithms like vector,
list, map, stack, etc.
Iterators are objects that point to elements in containers like arrays, lists, or maps. They
provide a way to access elements in these data structures sequentially.
A deque (double-ended queue) allows insertion and deletion from both ends, providing more
flexibility than a vector or a list.
A stack is a container that follows the LIFO (Last In, First Out) principle. Elements can only
be inserted or removed from the top.
A queue is a container that follows the FIFO (First In, First Out) principle. Elements are
inserted at the back and removed from the front.
The volatile keyword tells the compiler not to optimize the variable, as its value might
change unexpectedly, often used in multi-threaded programs or with hardware interrupts.
A lambda function is an anonymous function that can capture variables from its surrounding
scope. Syntax:
Smart pointers manage the lifetime of dynamically allocated objects, ensuring proper
memory management. Types include:
std::unique_ptr
std::shared_ptr
std::weak_ptr
A segmentation fault occurs when a program tries to access memory that it is not allowed to.
It usually results from accessing a null or uninitialized pointer.
A dangling pointer refers to memory that has already been freed, yet the pointer still holds the
address of the deleted object.
A memory leak occurs when dynamically allocated memory is not properly deallocated,
leading to wastage of memory resources.
A friend class can access private and protected members of another class.
A pure virtual function is a virtual function with no implementation in the base class, forcing
derived classes to override it.
The copy assignment operator is used to assign one object to another of the same class. It is
implemented using the operator=.
1. Destructor
2. Copy constructor
3. Copy assignment operator
1. Move constructor
2. Move assignment operator
78. What is the difference between shallow copy and deep copy?
Shallow Copy: Copies only the memory address (pointer) of the object, leading to
multiple objects pointing to the same data.
Deep Copy: Copies the actual object data, allocating separate memory for the new
object.
79. What is the difference between early binding and late binding?
A function pointer stores the address of a function. It can be used to call functions indirectly.
void (*funcPtr)(int);
funcPtr = &someFunction;
(*funcPtr)(5);
assert() is used to perform debugging checks. If the expression inside assert() evaluates
to false, the program terminates with an error message.
Single-line comment: //
Multi-line comment: /* ... */
The auto keyword allows the compiler to automatically deduce the type of the variable from
its initializer.
Move semantics is a feature that allows the transfer of resources from one object to another,
eliminating unnecessary copying. It is implemented using move constructors and move
assignment operators.
A virtual destructor ensures that the destructor of a derived class is called when a base class
pointer pointing to a derived class object is deleted.
A friend class can access the private and protected members of another class. It is useful
when two or more classes need to work closely together.
A pure virtual function is a function that must be overridden in derived classes. It is defined
as:
A vtable is a lookup table used by the compiler to resolve function calls in the case of
polymorphism. Each class with virtual functions has its own vtable.
Function overriding allows a derived class to provide a specific implementation for a function
that is already defined in its base class.
A functor (function object) is an object that can be treated like a function. It is created by
overloading the operator().
struct Functor {
};
exit(): Terminates the program immediately, regardless of the point in the code.
return: Exits a function and returns control to the calling function.