C++ OOPS Interview Questions
Last updated 2026-11-19
Object-oriented programming in C++ is tested in almost every campus technical round, usually through short code snippets rather than definitions. Interviewers like C++ OOPS because the language lets you get things subtly wrong: slicing, constructor order, hidden virtual functions and copy semantics all produce output that surprises people who only memorised the four pillars.
The questions below pair each concept with the exact behaviour you are expected to predict. Where a question has a famous trap, the answer names it. If you need the machinery behind dynamic dispatch, continue to C++ virtual functions; for the general language set see C++ interview questions.
| Topics | Constructors, inheritance, polymorphism, abstraction |
|---|---|
| Format | 15 questions with code and expected output |
| Standard | C++17 |
The order rules worth memorising
- Construction runs base first, then members in declaration order, then the constructor body.
- Destruction runs in exactly the reverse order.
- Member initialiser lists run in declaration order, not the order you write them.
- A virtual call inside a constructor or destructor resolves to the class being constructed, not the most derived class.
Common ways answers go wrong
Candidates say "polymorphism means many forms" and stop. Interviewers want the split between compile-time polymorphism (overloading, templates) and run-time polymorphism (virtual functions through a base pointer or reference), plus the cost: a vtable pointer per object and an indirect call.
The other frequent miss is access control versus visibility. private members are still inherited and still occupy space in the derived object; they are just not accessible by name.
C++ OOPS Interview Questions: questions with worked answers
1 What are the four pillars of OOP, and how does C++ express each?
easyEncapsulation: bundle data with the functions that act on it and restrict access with private and protected. Abstraction: expose an interface and hide detail, using abstract classes (pure virtual functions) or simply a small public API. Inheritance: derive a class from another to reuse and specialise behaviour. Polymorphism: use one interface for many types, at compile time through overloading and templates and at run time through virtual functions. The C++ nuance is that it is a multi-paradigm language, so none of these is forced on you.
2 What is the difference between public, protected and private inheritance?
easyThe inheritance mode caps the access of the base members inside the derived class. Public inheritance keeps public as public and protected as protected, and models "is-a". Protected inheritance makes base public members protected. Private inheritance makes them all private and models "implemented in terms of", where composition is usually clearer. Private members of the base are never accessible to the derived class in any mode. Only public inheritance allows a Derived* to convert implicitly to Base* outside the class.
3 In what order are constructors and destructors called? Predict the output.
mediumThe output is B() D() ~D() ~B(). The base subobject is built first so that it is complete before the derived constructor body runs, and destruction reverses this. With multiple bases, constructors follow the order of the base list; with members, the order of declaration.
cppstruct B { B() { std::cout << "B() "; } ~B() { std::cout << "~B() "; } }; struct D : B { D() { std::cout << "D() "; } ~D() { std::cout << "~D() "; } }; int main() { D d; } // B() D() ~D() ~B()4 What is object slicing and how do you avoid it?
mediumCopying a derived object into a base object by value copies only the base subobject, discarding the derived part and its vptr, so virtual calls on the copy use the base class. It happens when passing by value, storing derived objects in std::vector<Base>, or assigning. Avoid it by passing by reference or pointer, storing std::unique_ptr<Base>, and deleting the copy operations of polymorphic base classes.
cppstruct A { virtual void f() { std::cout << "A"; } }; struct B : A { void f() override { std::cout << "B"; } }; void byValue(A a) { a.f(); } // prints A: sliced void byRef(A& a) { a.f(); } // prints B int main() { B b; byValue(b); byRef(b); } // AB5 What is the diamond problem and how does virtual inheritance solve it?
mediumIf B and C both derive from A and D derives from both B and C, a D object contains two separate A subobjects, so D::x is ambiguous and A is duplicated. Declaring B and C as "virtual public A" makes them share one A subobject. The most derived class (D) is then responsible for constructing A, which is a common follow-up: any A constructor arguments must be supplied in D's initialiser list. The cost is an extra indirection to reach the shared base and a slightly larger object.
cppstruct A { int x = 0; }; struct B : virtual A {}; struct C : virtual A {}; struct D : B, C {}; int main() { D d; d.x = 1; } // unambiguous: one shared A6 What is the difference between overloading, overriding and hiding?
mediumOverloading means several functions with the same name and different parameter lists in the same scope, resolved at compile time. Overriding means a derived class provides its own version of a virtual function with the same signature, resolved at run time. Hiding happens when a derived class declares any function with the same name as a base function: all base overloads of that name are hidden, even with different parameters, unless you add "using Base::f;". The override keyword makes the compiler reject a signature mismatch that would otherwise silently create a new function.
7 What is a pure virtual function and an abstract class? Can a pure virtual function have a body?
mediumA pure virtual function is declared with "= 0" and makes the class abstract, so it cannot be instantiated; derived classes must override it to become concrete. It can still have a definition outside the class, which derived classes may call explicitly as Base::f(). This is mandatory for a pure virtual destructor: "virtual ~Base() = 0;" needs a body because destructors always chain up. An interface in C++ is simply an abstract class with only pure virtual functions and a virtual destructor.
8 Why should a member initialiser list be preferred to assignment in the constructor body?
mediumMembers are initialised before the body runs. Assigning in the body means each member was first default-constructed and then assigned, which is wasteful for non-trivial types and impossible for const members, reference members and members without a default constructor. The list also runs in declaration order, so if you write it in a different order the compiler may warn, and an initialiser depending on a later-declared member reads garbage.
cppstruct P { const int id; std::string& log; P(int i, std::string& l) : id(i), log(l) {} // required };9 What does the copy constructor do, and when is it called?
mediumIt creates a new object as a copy of an existing one, with signature T(const T&). It is called when initialising from an existing object, passing by value and returning by value (unless elided, which C++17 guarantees for prvalues). The compiler-generated version does a member-wise copy, which for a raw owning pointer gives a shallow copy: two objects pointing at one buffer and a double free on destruction. Write a deep-copy constructor, or better, use a member that owns itself.
cppstruct Buf { int* p; size_t n; Buf(size_t n) : p(new int[n]()), n(n) {} Buf(const Buf& o) : p(new int[o.n]), n(o.n) { std::copy(o.p, o.p + n, p); } Buf& operator=(Buf o) { std::swap(p, o.p); std::swap(n, o.n); return *this; } ~Buf() { delete[] p; } };10 What are explicit and delete used for on constructors?
mediumexplicit stops a single-argument constructor being used for implicit conversion, so "void f(Str); f(5);" does not compile when Str(int size) is explicit. "= delete" removes a function entirely, which is the modern way to make a class non-copyable: "T(const T&) = delete;". A deleted function still takes part in overload resolution, so calling it is a compile error rather than silently picking another overload, which is useful for banning particular conversions.
11 Can a constructor be virtual? Can a destructor?
mediumA constructor cannot be virtual: when it runs, the object has no vptr yet and its dynamic type is not complete. The factory pattern or a virtual clone() function gives the same effect. A destructor can and usually should be virtual in a polymorphic base so that deleting through a base pointer runs the full destructor chain. A virtual call from inside a constructor does not reach the derived override, because the derived part is not constructed yet.
12 Predict the output of the virtual call in a constructor.
hardIt prints "Base", not "Derived". While Base's constructor runs, the object's dynamic type is Base, so the virtual call binds to Base::name. The vptr is switched to Derived's vtable only after the Base constructor finishes. The same applies in destructors. Calling a pure virtual function from a constructor is undefined behaviour and typically aborts with a "pure virtual method called" message.
cppstruct Base { Base() { std::cout << name() << "\n"; } virtual std::string name() const { return "Base"; } virtual ~Base() = default; }; struct Derived : Base { std::string name() const override { return "Derived"; } }; int main() { Derived d; } // prints Base13 What is the difference between composition and inheritance, and which would you pick?
mediumInheritance models "is-a" and ties the derived class to the base layout and behaviour. Composition models "has-a": the object holds another as a member and forwards what it needs. Prefer composition when you only need to reuse implementation, because it keeps coupling low and avoids exposing base members that do not make sense on the new type. Use public inheritance when substitutability holds: anywhere a Base is expected, a Derived must behave correctly (the Liskov substitution principle). A Square deriving from Rectangle is the standard counterexample.
14 What does friend do, and does it break encapsulation?
mediumA friend function or class is granted access to the private and protected members of the class that declares it. It is not a member, is not inherited, and friendship is not transitive or mutual. Used sparingly it strengthens encapsulation, for example operator<< for stream output or a tightly coupled helper class, because the alternative is exposing public getters to everyone. Overusing it indicates the design is leaking.
15 What is the difference between static binding and dynamic binding?
easyStatic (early) binding resolves the function at compile time using the static type of the expression: non-virtual functions, overloaded functions and templates. Dynamic (late) binding resolves at run time using the dynamic type of the object, through the vtable, and applies only to virtual functions called via a pointer or reference. A call on an object by value is always statically bound, because the compiler knows its exact type.
16 What does the compiler generate for an empty class, and what is sizeof it?
hardAn empty class has implicitly declared default constructor, copy constructor, copy assignment and destructor, and C++11 adds move operations when no user-declared copy or destructor suppresses them. Its sizeof is 1, because distinct objects must have distinct addresses. When used as a base the empty base optimisation lets it take zero bytes, which std::unique_ptr with a stateless deleter relies on. C++20 adds [[no_unique_address]] for the same effect on members.
Frequently asked questions
Are C++ OOPS questions mostly theory or code?
Both. Expect a definition followed by a short snippet where you predict the output, usually covering constructor order, virtual dispatch or slicing.
What is the difference between a class and an object?
A class is the type definition: members and member functions. An object is an instance of it occupying memory. sizeof applies to the class, addresses to objects.
Does C++ support multiple inheritance?
Yes. It can cause ambiguity and the diamond problem, which virtual inheritance solves. Many styles avoid it except for implementing several pure interfaces.
How is C++ OOPS different from Java OOPS in interviews?
C++ adds manual memory management, copy and move semantics, multiple inheritance and non-virtual-by-default methods. Java asks more about interfaces and garbage collection.
Related guides
More in Programming Language Interview Questions by Language
- C++ Pointers and References Interview Questions
- C++ Smart Pointers Interview Questions
- C++ STL Interview Questions
- C++ Templates Interview Questions
- C++ Virtual Functions Interview Questions
- Core Java Interview Questions for Freshers
- Entity Framework Core Interview Questions
- Java 8 Streams and Lambdas Interview Questions
- Java Coding Interview Questions
- Java Collections Interview Questions
- Java Concurrency Utilities Interview Questions
- Java Exception Handling Interview Questions