Top 15 C++ Interview Questions for Freshers with Answers
15 most-asked C++ questions: C vs C++, class vs struct, virtual functions, new vs malloc, references, constructors/destructors, OOPs pillars and tricky outputs.
How to use this list
Language basics (1–5)
Q1. What is the difference between C and C++?
C is procedural; C++ adds object-oriented programming (classes, inheritance, polymorphism) plus references, overloading, STL and exceptions on top of C. C++ is practically a superset of C.
Q2. What is a class and an object?
A class is a blueprint defining data and functions together; an object is a real instance with its own memory. One class, many independent objects — like one house map, many houses.
Q3. What is the only difference between struct and class in C++?
Default access: struct is public by default, class is private by default (same for default inheritance). Everything else — constructors, functions, inheritance — works identically in both.
Q4. What are the 4 pillars of OOP?
Encapsulation (data + functions bundled, private by default), Inheritance (reuse via base→derived), Polymorphism (overloading at compile time, virtual overriding at runtime), Abstraction (expose what, hide how — abstract classes, interfaces).
Q5. Reference vs pointer?
Reference: permanent alias, must be initialized, never null, never re-seated. Pointer: separate variable holding an address — nullable, reassignable, supports arithmetic.
Object lifecycle (6–10)
Q6. What are constructor and destructor?
Constructor (class name, no return type) runs automatically at object creation to initialize; destructor (~ClassName, no parameters, only one) runs at destruction to release resources. Destruction order is reverse of creation (LIFO).
Q7. What is a copy constructor?
A constructor taking a reference to the same class — ClassName(const ClassName &other) — called when an object is initialized from another object, passed by value, or returned by value. Needed for deep copy when the class owns pointers.
Q8. new vs malloc?
new = operator, allocates AND calls constructor, typed pointer, throws bad_alloc. malloc = C function, raw bytes only, void*, returns NULL. Pairs never mix: new↔delete, new[]↔delete[], malloc↔free.
Q9. What is a virtual function?
A function whose call is resolved at runtime by the actual object (via vtable), not at compile time by pointer type — enabling runtime polymorphism. Without virtual, a base pointer to a child object calls the base version.
Q10. Why should a base class destructor be virtual?
So delete on a base pointer runs the derived destructor too; otherwise only the base part is destroyed and derived resources leak.
Deeper + tricky (11–15)
Q11. What is a pure virtual function and abstract class?
virtual void f() = 0; makes a function pure virtual; a class with at least one becomes abstract — cannot be instantiated, exists to force derived classes to implement the function.
Q12. What is function overloading and its biggest rule?
Same name, different parameter lists, resolved at compile time. Biggest rule: return type alone cannot distinguish overloads — that is a compile error.
Q13. What does inline do?
Requests the compiler to paste the function body at the call site, removing call overhead for tiny functions. It is only a hint — the compiler can refuse (recursion, loops), and functions defined inside a class are implicitly inline.
Q14. Predict the output:
class A { public: A(){cout<<"A";} ~A(){cout<<"~A";} };
class B { public: B(){cout<<"B";} ~B(){cout<<"~B";} };
int main() { A a; B b; return 0; }
Output: AB~B~A — constructors in creation order, destructors in reverse (LIFO). The classic order question.
Q15. What is this pointer?
A hidden pointer available inside every non-static member function, holding the address of the calling object — it is how the function knows whose data to use, and enables method chaining via return *this.
Last-minute revision line
इस list को कैसे use करें
Language basics (1–5)
Q1. C और C++ में अंतर क्या है?
C procedural है; C++ उसके ऊपर object-oriented programming (classes, inheritance, polymorphism) plus references, overloading, STL और exceptions जोड़ता है. C++ practically C का superset है.
Q2. Class और object क्या हैं?
Class data और functions को साथ define करने वाला blueprint है; object अपनी memory वाला real instance. एक class, कई independent objects — जैसे एक नक्शा, कई घर.
Q3. C++ में struct और class में इकलौता अंतर?
Default access: struct default में public, class default में private (default inheritance भी वैसी ही). बाकी सब — constructors, functions, inheritance — दोनों में identical.
Q4. OOP के 4 pillars?
Encapsulation (data + functions bundled, default private), Inheritance (base→derived से reuse), Polymorphism (compile time पर overloading, runtime पर virtual overriding), Abstraction (क्या दिखाओ, कैसे छुपाओ — abstract classes).
Q5. Reference vs pointer?
Reference: permanent alias, initialize ज़रूरी, कभी null नहीं, कभी re-seat नहीं. Pointer: address रखने वाला अलग variable — nullable, reassignable, arithmetic supported.
Object lifecycle (6–10)
Q6. Constructor और destructor क्या हैं?
Constructor (class name, no return type) object बनते ही initialize के लिए automatically चलता है; destructor (~ClassName, no parameters, सिर्फ एक) destroy होते ही resources release के लिए. Destruction order creation का उल्टा (LIFO).
Q7. Copy constructor क्या है?
Same class का reference लेने वाला constructor — ClassName(const ClassName &other) — तब चलता है जब object दूसरे object से initialize हो, by value pass हो, या by value return हो. Class में pointers हों तो deep copy के लिए ज़रूरी.
Q8. new vs malloc?
new = operator, allocate करके constructor भी call, typed pointer, fail पर bad_alloc throw. malloc = C function, सिर्फ raw bytes, void*, fail पर NULL. जोड़े कभी mix नहीं: new↔delete, new[]↔delete[], malloc↔free.
Q9. Virtual function क्या है?
ऐसा function जिसकी call runtime पर असली object से resolve होती है (vtable से), compile time पर pointer type से नहीं — इसी से runtime polymorphism. Virtual के बिना child object का base pointer base version call करता है.
Q10. Base class का destructor virtual क्यों होना चाहिए?
ताकि base pointer पर delete करने से derived destructor भी चले; वरना सिर्फ base हिस्सा destroy होता है और derived के resources leak.
Deeper + tricky (11–15)
Q11. Pure virtual function और abstract class क्या हैं?
virtual void f() = 0; function को pure virtual बनाता है; कम से कम एक वाली class abstract बन जाती है — instantiate नहीं हो सकती, derived classes को function बनाने पर मजबूर करने के लिए होती है.
Q12. Function overloading और उसका सबसे बड़ा rule?
Same नाम, अलग parameter lists, compile time पर resolve. सबसे बड़ा rule: सिर्फ return type से overloads अलग नहीं हो सकते — यह compile error है.
Q13. inline क्या करता है?
Compiler से request करता है कि function body call site पर paste हो, जिससे छोटे functions का call overhead हटे. सिर्फ hint है — compiler मना कर सकता है (recursion, loops), और class के अंदर defined functions implicitly inline होते हैं.
Q14. Output predict करें:
class A { public: A(){cout<<"A";} ~A(){cout<<"~A";} };
class B { public: B(){cout<<"B";} ~B(){cout<<"~B";} };
int main() { A a; B b; return 0; }
Output: AB~B~A — constructors creation order में, destructors उल्टे (LIFO). Classic order question.
Q15. this pointer क्या है?
हर non-static member function के अंदर available hidden pointer, जिसमें calling object का address है — इसी से function को पता चलता है किसका data use करना है, और return *this से method chaining होती है.
Last-minute revision line
Frequently Asked Questions
What are the most asked C++ interview questions for freshers?
C vs C++, class vs struct, the 4 OOP pillars, reference vs pointer, new vs malloc, constructor/destructor order, virtual functions with the virtual destructor rule, and pure virtual/abstract classes.
What is the output order of constructors and destructors?
Constructors run in creation order; destructors run in exact reverse (LIFO) — the last object created is the first destroyed when scope ends.
What is the this pointer in C++?
A hidden pointer inside every non-static member function holding the calling object address, letting the function operate on that specific object and enabling chaining via return *this.