VOOZH about

URL: https://www.geeksforgeeks.org/interview-prep/oops-interview-questions-c-programming/

⇱ C++ OOP Interview Questions - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C++ OOP Interview Questions

Last Updated : 11 Mar, 2026

C++ supports Object-Oriented Programming through features like classes, inheritance, polymorphism, and encapsulation. These concepts help build reusable, maintainable, and efficient code, which is why they are often a key focus in interviews.

1. What is a class and object in C++?

  • A class in C++ is a user-defined data type that groups data members (variables) and member functions (methods) into a single unit. It acts as a blueprint for creating objects and helps implement encapsulation in Object-Oriented Programming.
  • An object is an instance of a class. It represents a real-world entity and allows access to the data members and member functions defined inside the class. Multiple objects can be created from the same class, and each object has its own copy of data members.

2. What is encapsulation in C++?

Encapsulation is the concept of wrapping data members and member functions together inside a class and restricting direct access to the data. It is usually implemented using access specifiers such as private, protected, and public. Sensitive data is kept private and accessed through public member functions.

👁 Encapsulation
Encapsulation

3. What is inheritance in C++?

Inheritance is a mechanism in which a class (derived class) acquires the properties and behaviors of another class (base class). It promotes code reuse and helps represent relationships between classes.

👁 inheritance
Different classes inheriting from Animal Class

4. What is polymorphism in C++?

Polymorphism means one interface, multiple implementations.
It allows the same function or operator to behave differently depending on the context.

In C++, polymorphism can be achieved through:

  • Compile-time polymorphism (function overloading, operator overloading)
  • Run-time polymorphism (function overriding using virtual functions)
  • 👁 polymorphism
    Polymorphism

5. What is the difference between struct and class?

structclass
Members are public by default.Members are private by default.
Public inheritance by defaultSupports inheritance (with public, protected, or private access).
Used for Plain Old Data (POD) structures, or simple data grouping.Suitable for complex objects that may include methods, constructors, and destructors.

Example:

6. What are the C++ access modifiers?

The access restriction specified to the class members (whether it is member function or data member) is known as access modifiers/specifiers. 

C++ provides three access modifiers:

  1. Private: It can neither be accessed nor be viewed from outside the class 
  2. Protected: It can be accessed if and only if the accessor is the derived class
  3. Public: It can be accessed or be viewed from outside the class 

Example:

7. What is the difference between virtual functions and pure virtual functions?

FeatureVirtual FunctionPure Virtual Function
ImplementationProvided in the base class.No implementation in the base class (typically).
Syntaxvirtual void func() { ... }virtual void func() = 0;
Class TypeThe class remains concrete.The class becomes abstract.
InstantiationYou can create objects of the base class.You cannot create objects of the base class.
ObligationDerived classes can override it (optional).Derived classes must override it (mandatory) to be concrete.

Virtual Function

Pure Virtual Function

A virtual function has an implementation in the base class and can be overridden in derived classes.A Pure Virtual Function is a member function of a base class that is only declared in a base class and defined in a derived class to prevent it from becoming an abstract class.
A virtual Function has its definition in its respective base class.              There is no definition in Pure Virtual Function and is initialized with a pure specifier (= 0).
The base class has a virtual function that can be represented or instanced; In simple words, its object can be made.A base class having pure virtual function becomes abstract that cannot be represented or instanced; In simple words, it means its object cannot be made.

Example:

8. Can a virtual function be called from a constructor? What happens when we do it?

Yes, a virtual function can be called from a constructor in C++, but the behavior is not what most people expect:

  • When a constructor is running, the derived part of the object is not yet constructed.
  • So, if you call a virtual function inside a constructor, the call will resolve to the base class version, not the derived one.
  • This happens because during base class construction, the object is treated as a base-class object only.

Example:


Output
Base constructor
Base show()
Derived constructor

Notice that even though Derived overrides show(), the base version runs inside the base constructor.

9. What is Function Overriding?

  • Function overriding occurs when a derived class defines a function with the same name, parameters, and return type as in the base class.
  • It allows the derived class to provide its own implementation of the base function.
  • It is an example of runtime polymorphism (late binding), where the function executed is decided at runtime based on the object type.

Example:

10. Explain the constructor & destructor in C++ .

  • A constructor is a special member function in C++ that has the same name as the class and is automatically called when an object is created. It is used to initialize the object’s data members.
  • A destructor is a special member function with the same name as the class, preceded by a tilde (~). It is automatically called when an object goes out of scope or is deleted, and is used to release resources or perform cleanup.
  • Constructors can be overloaded (multiple constructors with different parameters), but a class can only have one destructor, which cannot be overloaded.
  • Both constructors and destructors do not have a return type, not even void.

Output
Constructor called, value = 10
Constructor called, value = 20
Destructor called for value = 20
Destructor called for value = 10

11. What happens if we define a virtual destructor but forget to mark the base class destructor as virtual?

If a base class destructor is not virtual, deleting a derived object via a base class pointer causes undefined behavior: only the base destructor runs, and the derived destructor is skipped. This leads to resource leaks.

To ensure proper cleanup, always make base class destructors virtual when using inheritance.

Example:

It's a silent issue no compile error, no crash, just leaked resources

12. Can constructors be private in C++? If yes, how are they used?

  • Yes, constructors can be private in C++.
  • When a constructor is private, objects of the class cannot be created directly outside the class.
  • Such constructors are usually used in:
    • Singleton design pattern: to restrict object creation and ensure only one instance exists.
    • Factory methods: object creation is controlled by static functions within the class.
    • Utility classes: where you don’t want anyone to instantiate the class (only static members are used).

Example (Singleton Pattern):


Output
Private constructor called
Singleton instance working

Private constructors control instantiation, commonly used in design patterns.

13. When should we use multiple inheritance?

  • Use multiple inheritance when a class logically needs to derive features or behavior from more than one base class.
  • It is useful when a class needs to combine independent functionalities (e.g., a class that is both Printable and Serializable).
  • It can model real-world scenarios where an object belongs to multiple categories (e.g., Teacher who is also a Researcher).
  • However, it should be used carefully because it can lead to issues like the diamond problem, so prefer it only when there’s a clear and justifiable relationship.
👁 Multiple_inheritance
Multiple Inheritance

Example:


Output
Function from class A
Function from class B
Function from class C

This shows how a derived class C can reuse functionality from two parent classes A and B.

14. What is virtual inheritance?

  • Virtual inheritance is a C++ mechanism used to solve the diamond problem in multiple inheritance.
  • It ensures that when a class is derived from two classes that have a common base, only one copy of the common base is inherited, avoiding ambiguity and duplication.
  • Without virtual inheritance, each path in the inheritance hierarchy brings its own copy of the base class, leading to multiple instances and conflicts.

Without Virtual Inheritance – Diamond Problem:

With Virtual Inheritance- Solves Ambiguity:

15. Can a derived class access private members of the base class? How?

A derived class cannot directly access private members of a base class. Access is possible only through:

  1. protected members (directly accessible in derived).
  2. public or protected getters/setters in base.
  3. Declaring the derived class (or function) as a friend.

Example:

16. What are the different types of polymorphism in C++?

There are two types of polymorphism

1. Compile Time Polymorphism or Static Binding: This type of polymorphism is achieved during the compile time of the program which results in it making a bit faster than Run time. Also, Inheritance is not involved in it. It is comprised of 2 further techniques:

  • Function Overloading: When there are multiple functions with the same name but different parameters then this is known as function overloading.
  • Operator Overloading: It is basically giving practice of giving a special meaning to the existing meaning of an operator or in simple terms redefining the pre-redefined meaning

2. Run-Time Polymorphism or Late Binding: Run-time polymorphism takes place when functions are invoked during run time. 

  • Function Overriding: Function overriding occurs when a base class member function is redefined in a derived class with the same arguments and return type.

Output:

Function of derived class

17. What happens when we override a function but forget to use virtual in the base class?

  • If you override a function in the derived class but the base class function is not marked virtual, then function overriding won’t work as runtime polymorphism.
  • Instead, function hiding occurs when the base class function is hidden by the derived class function if called through a derived object.
  • But if you use a base class pointer or reference, the base version is always called (compile-time binding), not the derived one.

Example:


Output
Base

There is no runtime failure, just static binding to the base version.

18. What is object slicing in C++? How does it occur?

  • Object slicing happens in C++ when a derived class object is assigned to a base class object by value.
  • In this process, only the base part of the derived object is copied, and the derived-specific members (extra data or overridden behavior) are “sliced off”.

This usually occurs when:

  • Passing a derived object by value to a function that takes a base class parameter.
  • Assigning a derived object to a base class variable (not pointer or reference).
  • To avoid slicing, use pointers or references to base instead of objects by value.

Example:

19. What is a virtual destructor?

  • A virtual destructor in C++ makes sure that when a base class pointer deletes a derived class object, the derived destructor is called first, followed by the base destructor.
  • Without it, only the base destructor executes, which can lead to incomplete cleanup and resource leaks.
  • It is mainly used in polymorphic base classes where objects may be deleted through a base pointer.

Example:

20. Is destructor overloading possible? If yes then explain and if no then why?

  • Destructor overloading is not possible in C++. A class can have only one destructor, and it cannot take parameters or have a return type.
  • The reason is: the destructor is called automatically by the compiler when an object goes out of scope or is deleted, so it must have a fixed, predictable signature.
  • If overloading were allowed, the compiler wouldn’t know which destructor to call during cleanup, leading to ambiguity.

Example:

21. What do you know about friend class and friend function?

  • A friend function is a non-member function that has access to the private and protected members of a class. It is declared using the keyword friend inside the class.
  • A friend class is a class that can access the private and protected members of another class. The friendship is declared inside the class using friend class.
  • Friendship is not mutual (if A is a friend of B, B isn’t automatically a friend of A) and not inherited (derived classes don’t inherit friendship).
  • They are mainly used when two classes or functions need close cooperation and direct access to each other’s internals.

Example:


Output
Friend function accessed secret: 42
Friend class accessed secret: 42

This shows that both a friend function and a friend class can directly access private members of another class.

22. What is an abstract class and when do you use it?

  • A class with at least one pure virtual function (virtual void func() = 0;).
  • Cannot be instantiated directly.
  • Provides a common interface for derived classes.
  • Enforces implementation of specific functions in subclasses.
  • Enables runtime polymorphism.

When to use an abstract class?

  • When you want to define a base class that outlines a common interface.
  • To ensure derived classes implement certain functions.
  • When you need to share some common functionality among related classes.
  • To enable polymorphic behavior through base class pointers or references.

Example:


Output
Drawing Circle

23. What are the static data members and static member functions?

A static data member belongs to the class, not to individual objects. Only one copy exists, shared by all objects.

Syntax:

static Data_Type Data_Member; 

A static member function can access only static members of the class and can be called using the class name.

Syntax:

classname::function name(parameter);

Example:

24. Can we call a virtual function from a constructor?

Yes, we can call a virtual function from a constructor, but during base class construction the derived part of the object is not yet initialized. Hence, the base class version of the function is called, not the derived version.

Example:

During Base constructor execution, Derived is not yet constructed, so virtualFunc() is not overridden yet.

Comment