How C++ manage its memory? Explain new & delete operators for variables, objects and arrays.
new
and delete
operators. The new
operator allocates memory on the heap for variables, objects, or arrays and returns a pointer to the allocated memory. The delete
operator deallocates memory previously allocated by new
.
int* p = new int; delete p;
MyClass* obj = new MyClass(); delete obj;
int* arr = new int[10]; delete[] arr;
Is it possible to delete storage acquired by a local variable in static memory using delete keyword in C++?
delete
keyword is used to deallocate memory that was dynamically allocated using new
. Local variables in static memory are automatically managed and should not be manually deleted.What is the use of destructors? Write a legal example of a destructor.
Example:
class MyClass {
public:
~MyClass() {
// Cleanup code
}
};
What are the OOP concepts present in C++/Java? Explain with one example each.
class Dog : public Animal {};
How to create an abstract class in C++? How to write a pure virtual function? Can we write a body of pure virtual function?
An abstract class is created by declaring at least one pure virtual function. A pure virtual function is declared using = 0
.
Example:
class AbstractClass {
public:
virtual void pureVirtualFunction() = 0;
};
A pure virtual function can have a body, but it must be defined outside the class.
What is getter and setter? Why do we use that? Write down a small code example.
Example:
class MyClass {
private:
int value;
public:
int getValue() { return value; }
void setValue(int v) { value = v; }
};
Create a spiral matrix.
Here is a C++ implementation:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> matrix(n, vector<int>(n));
int num = 1;
int left = 0, right = n - 1, top = 0, bottom = n - 1;
while (left <= right && top <= bottom) {
for (int i = left; i <= right; i++) matrix[top][i] = num++;
top++;
for (int i = top; i <= bottom; i++) matrix[i][right] = num++;
right--;
for (int i = right; i >= left; i--) matrix[bottom][i] = num++;
bottom--;
for (int i = bottom; i >= top; i--) matrix[i][left] = num++;
left++;
}
return matrix;
}
What is "this" pointer? Is it available for static, virtual, const and friend functions?
this
pointer is a pointer to the current object instance. It is available for non-static member functions, including virtual and const functions, but not for static or friend functions.What is the need to write a user defined destructor? When should it be declared as "virtual"?
virtual
when the class is intended to be a base class in an inheritance hierarchy to ensure proper cleanup of derived class objects.How does virtual function affect the size of an object? How are they executed at runtime?
What is the diamond problem? How to solve it?
Example:
class A {};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};
What is shallow copy and deep copy? How is it implemented in C++? Explain with examples.
Example:
class MyClass {
public:
int* data;
MyClass(const MyClass& other) { // Deep copy
data = new int(*other.data);
}
};
What is a smart pointer? Which are smart pointers in C++?
std::unique_ptr
, std::shared_ptr
, and std::weak_ptr
.What is STL? Explain different components in STL with examples.
vector
, list
, map
, etc.sort
, find
, binary_search
, etc.