Once you’ve learned the basics of C++, it’s time to go deeper. These advanced C++ interview questions help you understand concepts that real companies test during coding interviews.
Let’s learn them in simple and clear language 👇
1. What are pointers in C++ and why are they used?
Answer:
A pointer is a variable that stores the memory address of another variable.
It helps in dynamic memory allocation, passing large data efficiently, and building data structures like linked lists.
Example:
int a = 10;
int* ptr = &a;
cout << "Value: " << *ptr; // Output: 10
2. What is dynamic memory allocation in C++?
Answer:
Dynamic memory allocation means assigning memory at runtime instead of compile-time.
In C++, we use new and delete operators for this.
Example:
int* num = new int(5);
cout << *num; // 5
delete num; // free memory
3. What is the difference between shallow copy and deep copy?
| Type | Description |
|---|---|
| Shallow Copy | Copies only the pointer, not the actual data. |
| Deep Copy | Creates a new copy of the data as well. |
Example:
class Demo {
int* data;
public:
Demo(int val) { data = new int(val); }
// Deep copy constructor
Demo(const Demo &d) { data = new int(*d.data); }
};
4. What is the difference between reference and pointer in C++?
| Feature | Pointer | Reference |
|---|---|---|
| Reassignment | Can be reassigned | Cannot be reassigned |
| Null | Can be null | Cannot be null |
| Syntax | Uses * and & | Uses & only |
Example:
int a = 5;
int* ptr = &a;
int& ref = a;
5. What are virtual destructors in C++?
Answer:
A virtual destructor ensures the derived class destructor runs when deleting an object through a base class pointer.
Without it, only the base destructor executes — causing memory leaks.
Example:
class Base {
public:
virtual ~Base() { cout << "Base destroyed\n"; }
};
class Derived : public Base {
public:
~Derived() { cout << "Derived destroyed\n"; }
};
6. What is the difference between abstract class and interface in C++?
Answer:
C++ doesn’t have a direct “interface” keyword, but we can create it using pure virtual functions.
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | Can have normal and pure virtual functions | All pure virtual functions |
| Data | Can have data members | No data members |
Example:
class Interface {
public:
virtual void show() = 0; // pure virtual
};
7. What are pure virtual functions?
Answer:
A pure virtual function is declared but not defined in the base class.
It forces derived classes to provide their own implementation.
Syntax:
virtual void functionName() = 0;
8. What is multiple inheritance in C++?
Answer:
Multiple inheritance allows a class to inherit from more than one base class.
Example:
class A { };
class B { };
class C : public A, public B { };
👉 However, it may cause ambiguity, known as the Diamond Problem, which can be resolved using virtual inheritance.
9. What is the Diamond Problem in C++?
Answer:
When two base classes inherit from the same grandparent class, and a derived class inherits from both, it creates ambiguity.
To fix this, use virtual base classes.
Example:
class A { public: void show() { cout << "A"; } };
class B : virtual public A { };
class C : virtual public A { };
class D : public B, public C { };
10. What are friend functions in C++?
Answer:
A friend function is not a member of a class but can access its private and protected data.
It’s declared using the friend keyword.
Example:
class Box {
private:
int width;
public:
friend void printWidth(Box b);
};
void printWidth(Box b) {
cout << b.width;
}
11. What is function overriding?
Answer:
Function overriding allows a derived class to redefine a base class method.
The function must have the same name, parameters, and return type.
Example:
class Base {
public:
virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived"; }
};
12. What is the difference between ‘new/delete’ and ‘malloc/free’?
| Feature | new/delete | malloc/free |
|---|---|---|
| Type Safety | Type safe | Not type safe |
| Constructor/Destructor | Called automatically | Not called |
| Operator/Function | Operator | Function |
13. What is an inline function?
Answer:
Inline functions suggest the compiler to insert the function code directly at the place of the function call to reduce function call overhead.
Example:
inline int square(int x) { return x * x; }
14. What is the difference between stack and heap memory?
| Feature | Stack | Heap |
|---|---|---|
| Managed by | Compiler | Programmer |
| Lifetime | Ends when function ends | Until manually freed |
| Speed | Faster | Slower |
15. What is an iterator in C++ STL?
Answer:
An iterator is an object that points to elements inside containers like vector, list, or map.
It behaves like a pointer.
Example:
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
for (auto it = v.begin(); it != v.end(); ++it)
cout << *it << " ";
}
16. What is a lambda function in C++?
Answer:
Lambda functions are anonymous functions defined within code blocks.
They’re useful for short tasks.
Example:
auto add = [](int a, int b) { return a + b; };
cout << add(3, 5);
17. What is smart pointer in C++?
Answer:
Smart pointers are objects that automatically manage memory. They delete the object when no longer used.
Types:
unique_ptr— owns one object onlyshared_ptr— shared ownershipweak_ptr— non-owning reference
Example:
#include <memory>
using namespace std;
int main() {
unique_ptr<int> p = make_unique<int>(10);
cout << *p;
}
18. What is move semantics in C++11?
Answer:
Move semantics allow resources to be transferred from one object to another instead of copying, improving performance.
Example:
string s1 = "Hello";
string s2 = move(s1);
19. What are function templates and class templates?
Answer:
Templates help write generic code.
Function Template:
template <typename T>
T add(T a, T b) { return a + b; }
Class Template:
template <class T>
class Box {
T value;
public:
Box(T v) : value(v) {}
};
20. What are STL containers?
Answer:
STL containers store collections of data.
Main types:
- Sequence Containers: vector, list, deque
- Associative Containers: map, set
- Unordered Containers: unordered_map, unordered_set
21. What is the difference between map and unordered_map?
| Feature | map | unordered_map |
|---|---|---|
| Order | Sorted | Unsorted |
| Time Complexity | O(log n) | O(1) average |
| Implementation | Red-black tree | Hash table |
22. What is the difference between const and constexpr?
| Keyword | Meaning |
|---|---|
| const | Value can’t change after initialization |
| constexpr | Evaluated at compile-time |
23. What is RTTI (Run-Time Type Information)?
Answer:
RTTI allows finding the type of an object at runtime using operators like typeid and dynamic_cast.
Example:
Base* b = new Derived();
cout << typeid(*b).name();
24. What is the difference between pass by value and pass by reference?
| Type | Description |
|---|---|
| Pass by Value | A copy of data is passed; changes don’t affect original |
| Pass by Reference | Original data is passed; changes affect original |
25. What is the difference between static and dynamic binding?
| Binding Type | When Decided | Example |
|---|---|---|
| Static | Compile-time | Function overloading |
| Dynamic | Run-time | Virtual functions |
✅ Final Tips for Freshers
- Practice STL and smart pointers.
- Revise OOPs and templates carefully.
- Understand memory management concepts.
- Write small code snippets to test yourself daily.