C Plus Plus Interview Questions

1. What is C++?

C++ is a general-purpose programming language that supports both procedural and object-oriented programming. It is an extension of C with features like classes, objects, inheritance, and polymorphism.

2. What are the key features of C++?

  • Object-Oriented
  • Faster than most high-level languages
  • Supports OOP concepts (Encapsulation, Inheritance, Polymorphism)
  • Rich Standard Template Library (STL)
  • Memory management using pointers

3. What is the difference between C and C++?

C is procedural, while C++ supports both procedural and object-oriented programming (OOP). C++ also supports features like classes, objects, and function overloading.

4. What is a class and an object?

  • Class: Blueprint or template.
  • Object: Real-world instance of a class.
class Car { 
public:     
     void drive() { cout << "Driving"; } 
}; 

Car myCar; 
MyCar.drive(); 

5. What is a constructor?

A constructor is a special function that runs when an object is created. It initializes the object.

class Car { 
public:     
    Car() { cout << "Car created"; } 
}; 

6. What is a destructor?

A destructor runs when an object is destroyed. Used to release resources like memory or files.

~Car() {     
    cout << "Car destroyed"; 
} 

7. What is function overloading?

Function with the same name but different parameters.

void show(int a); 
Void show(double b); 

8. What is operator overloading?

Giving new meaning to existing operators like + or ==.

class A { 
public:     
     int x;     
     A(int val) : x(val) {}     
     A operator+(A obj) {         
            return A(x + obj.x);     
      } 
}; 

9. What is inheritance?

One class (child) inherits features of another class (parent).

class Animal { 
public:     
     void sound() { cout << "Some sound"; }
}; 
Class Dog : public Animal {}; 

10. What is polymorphism?

"Many forms" – same function behaves differently in different classes (runtime and compile-time).

11. What is a virtual function?

Allows function overriding in inheritance for runtime polymorphism.

class Base {
public:     
     virtual void show() { cout << "Base"; } 
}; 
class Derived : public Base { 
public:     
     void show() override { cout << "Derived"; } 
}; 

12. What is the use of this pointer?

this is a pointer that refers to the current object in a class.

13. What is a smart pointer (C++11)?

Automatically manages memory. Types:

  • unique_ptr
  • shared_ptr
  • weak_ptr
#include <memory> 
Std::unique_ptr<int> ptr(new int(10)); 

14. What is the difference between new and malloc()?

new calls constructor, supports OOP, type-safe.

malloc() doesn’t call constructor and is from C.

15. What is a template?

Used to write generic functions or classes.

template <typename T> 
T add(T a, T b) {     
    return a + b; 
} 

16. What is the Standard Template Library (STL)?

Ready-made templates like:

  • Vector – Dynamic array
  • Map – Key-value
  • Set – Unique values
  • Stack/Queue – Data structures

17. What is the difference between struct and class in C++?

In C++:

  • struct: Members are public by default
  • class: Members are private by default

18. What is nullptr (C++11)?

It replaces NULL. It is type-safe.

int* p = nullptr;

19. What is lambda expression (C++11)?

Inline anonymous function.

auto add = [](int a, int b) { return a + b; }; 
Cout << add(3, 4);  // Outputs 7 

20. What are move semantics and rvalue references (C++11)?

Used to avoid unnecessary copying and improve performance.

std::string a = "hello"; 
Std::string b = std::move(a);  // Moves instead of copying