If you’re preparing for a C++ interview, this guide will help you understand the most common questions with easy explanations and examples. These are simple, beginner-friendly answers — written like how a human would explain in a real interview.
1. What is C++?
Answer:
C++ is an object-oriented programming language created by Bjarne Stroustrup. It is an extension of the C language with added features like classes, objects, inheritance, polymorphism, and encapsulation.
It is widely used for system software, game development, and competitive programming.
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, C++!" << endl;
return 0;
}
2. What are the main features of C++?
Answer:
Some of the important features of C++ are:
- Object-Oriented Programming (OOP)
- Platform Independence
- High Performance
- Reusability through Inheritance
- Function Overloading and Operator Overloading
- Rich Library Support
3. What is the difference between C and C++?
| Feature | C | C++ |
|---|---|---|
| Type | Procedural | Object-Oriented |
| Focus | Functions | Objects & Classes |
| Data Security | Less (No encapsulation) | More (Encapsulation & Abstraction) |
| Overloading | Not supported | Supported |
| Inheritance | Not supported | Supported |
4. What are classes and objects in C++?
Answer:
A class is a blueprint for creating objects. An object is an instance of a class that contains real values.
Example:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s1;
s1.name = "Riya";
s1.age = 21;
s1.display();
}
5. What is the difference between a constructor and a destructor?
Answer:
- Constructor: Automatically called when an object is created.
- Destructor: Automatically called when an object is destroyed.
Example:
#include <iostream>
using namespace std;
class Demo {
public:
Demo() { cout << "Constructor called" << endl; }
~Demo() { cout << "Destructor called" << endl; }
};
int main() {
Demo obj;
}
6. What is inheritance in C++?
Answer:
Inheritance allows one class to acquire the properties and methods of another class. It helps in reusing existing code.
Example:
#include <iostream>
using namespace std;
class Parent {
public:
void show() { cout << "This is Parent class" << endl; }
};
class Child : public Parent {
public:
void display() { cout << "This is Child class" << endl; }
};
int main() {
Child obj;
obj.show();
obj.display();
}
7. What is polymorphism?
Answer:
Polymorphism means “many forms”. It allows one function to behave differently based on the object that calls it.
There are two types:
- Compile-time Polymorphism (Static) — using function overloading or operator overloading.
- Run-time Polymorphism (Dynamic) — using virtual functions.
Example:
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() { cout << "Drawing Shape" << endl; }
};
class Circle : public Shape {
public:
void draw() override { cout << "Drawing Circle" << endl; }
};
int main() {
Shape* s = new Circle();
s->draw();
delete s;
}
8. What is encapsulation?
Answer:
Encapsulation means wrapping data and methods together in a single unit. It helps protect data from unauthorized access.
Example:
class Account {
private:
double balance;
public:
void setBalance(double b) { balance = b; }
double getBalance() { return balance; }
};
9. What are access specifiers in C++?
Answer:
Access specifiers define how class members can be accessed.
- Public: Accessible from anywhere.
- Private: Accessible only inside the class.
- Protected: Accessible in the class and derived classes.
10. What is the difference between compile-time and run-time polymorphism?
| Type | Description | Example |
|---|---|---|
| Compile-time | Resolved during compilation | Function overloading |
| Run-time | Resolved during execution | Virtual functions |
11. What is a virtual function in C++?
Answer:
A virtual function is a function in the base class that can be overridden in the derived class. It allows run-time polymorphism.
Syntax:
virtual void functionName();
12. What is the use of ‘this’ pointer in C++?
Answer:this pointer refers to the current object of the class. It helps to distinguish between class variables and parameters when they have the same name.
Example:
class Demo {
int x;
public:
Demo(int x) {
this->x = x;
}
};
13. What is a copy constructor?
Answer:
A copy constructor initializes a new object as a copy of an existing object.
Example:
class Test {
public:
int a;
Test(int x) { a = x; }
Test(const Test &t) { a = t.a; } // Copy constructor
};
14. What is function overloading?
Answer:
Function overloading allows multiple functions with the same name but different parameter lists.
Example:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
15. What is operator overloading?
Answer:
Operator overloading allows us to redefine operators for user-defined data types.
Example:
class Complex {
public:
int real, imag;
Complex operator + (Complex obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
};
16. What is the difference between struct and class in C++?
| Feature | struct | class |
|---|---|---|
| Default Access | public | private |
| Inheritance | Supported | Supported |
| Object-Oriented | Not fully | Fully |
17. What is the use of a namespace in C++?
Answer:
Namespace helps avoid name conflicts by grouping related identifiers under one name.
Example:
namespace MyNamespace {
int x = 100;
}
int main() {
cout << MyNamespace::x;
}
18. What are templates in C++?
Answer:
Templates allow writing generic programs that work with any data type.
Example:
template <typename T>
T add(T a, T b) {
return a + b;
}
19. What is an exception in C++?
Answer:
An exception is an event that occurs during program execution that disrupts the flow.
C++ handles exceptions using try, catch, and throw blocks.
Example:
try {
int x = 5, y = 0;
if (y == 0) throw "Division by zero!";
}
catch (const char* msg) {
cout << msg;
}
20. What is the difference between malloc() and new in C++?
| Feature | malloc() | new |
|---|---|---|
| Syntax | C-style | C++ operator |
| Returns | void pointer | Exact data type |
| Calls constructor | No | Yes |
21. What is STL in C++?
Answer:
STL (Standard Template Library) is a collection of templates for data structures and algorithms like vectors, stacks, queues, maps, and sets.
Example:
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
v.push_back(4);
for (int i : v)
cout << i << " ";
}
✅ Final Tips for Freshers
- Focus on basic concepts (OOP, inheritance, polymorphism, templates).
- Practice writing small programs daily.
- Understand memory management (pointers, new, delete).
- Revise STL and exception handling.