C++ Interview Questions – OOPs

1. [Asked in Infosys] What is Object-Oriented Programming (OOP) in C++?

Answer:
OOP (Object-Oriented Programming) is a programming paradigm based on the concept of objects, which contain data (attributes) and functions (methods) that operate on the data.

Key Features of OOP in C++:

  • Encapsulation โ€“ Wrapping data and methods together in a single unit (class).
  • Abstraction โ€“ Hiding implementation details from the user.
  • Inheritance โ€“ Acquiring properties of one class in another.
  • Polymorphism โ€“ Using a single interface to represent different types.

Example of OOP in C++:

#include <iostream>
using namespace std;

class Car {
public:
    string brand;
    void show() {
        cout << "Car brand: " << brand;
    }
};

int main() {
    Car c1;
    c1.brand = "Toyota";
    c1.show();
    return 0;
}

Output:

Car brand: Toyota

2. [Asked in TCS] What is a class and an object in C++?

Answer:

  • Class: A blueprint for creating objects. It defines attributes (data members) and methods (member functions).
  • Object: An instance of a class that contains real data.

Example:

class Employee {
public:
    string name;
    int id;
    
    void display() {
        cout << "Name: " << name << ", ID: " << id;
    }
};

int main() {
    Employee e1;
    e1.name = "John";
    e1.id = 101;
    e1.display();
    return 0;
}

Output:

Name: John, ID: 101

3. [Asked in Cognizant] What is the difference between structure and class in C++?

Answer:

FeatureStructure (struct)Class (class)
Access SpecifierMembers are public by defaultMembers are private by default
EncapsulationNot supportedFully supports encapsulation
InheritanceNot supportedSupported
Use CaseData storageOOP concepts

Example:

struct Student {
    string name;  // Public by default
};

class Teacher {
    string subject;  // Private by default
};

4. [Asked in Wipro] What is encapsulation in C++?

Answer:
Encapsulation is the process of hiding data and restricting direct access to it. This protects data from unintended modifications.

Example:

#include <iostream>
using namespace std;

class BankAccount {
private:
    double balance;

public:
    void setBalance(double b) {
        if (b >= 0)
            balance = b;
    }
    double getBalance() {
        return balance;
    }
};

int main() {
    BankAccount acc;
    acc.setBalance(1000);
    cout << "Balance: " << acc.getBalance();
    return 0;
}

Output:

Balance: 1000

Encapsulation ensures that data is accessed only through controlled methods.


5. [Asked in IBM] What is inheritance in C++? Explain types of inheritance.

Answer:
Inheritance allows a class (child) to acquire properties and methods from another class (parent).

Types of Inheritance in C++:

  1. Single Inheritance โ€“ One class inherits from another.
  2. Multiple Inheritance โ€“ A class inherits from multiple base classes.
  3. Multilevel Inheritance โ€“ A class inherits from another derived class.
  4. Hierarchical Inheritance โ€“ Multiple classes inherit from a single base class.
  5. Hybrid Inheritance โ€“ Combination of multiple inheritance types.

Example of Single Inheritance:

class Vehicle {
public:
    void run() {
        cout << "Vehicle is running";
    }
};

class Car : public Vehicle {};

int main() {
    Car c;
    c.run(); // Inherited from Vehicle
    return 0;
}

Output:

Vehicle is running

6. [Asked in Deloitte] What is polymorphism in C++? Explain types.

Answer:
Polymorphism allows one interface to have multiple implementations.

Types of Polymorphism:

  • Compile-time Polymorphism (Function Overloading, Operator Overloading)
  • Run-time Polymorphism (Method Overriding using Virtual Functions)

Example of Function Overloading:

class Math {
public:
    int add(int a, int b) {
        return a + b;
    }
    double add(double a, double b) {
        return a + b;
    }
};

int main() {
    Math obj;
    cout << obj.add(5, 10) << endl;     // Calls int version
    cout << obj.add(3.5, 2.5);          // Calls double version
    return 0;
}

Output:

15
6.0

7. [Asked in Amazon] What is abstraction in C++?

Answer:
Abstraction is hiding implementation details and exposing only necessary functionality.

Example Using Abstract Class:

class Shape {
public:
    virtual void draw() = 0; // Pure virtual function
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing a Circle";
    }
};

int main() {
    Circle c;
    c.draw();
    return 0;
}

Output:

Drawing a Circle

Abstraction allows designing flexible and reusable code.


8. [Asked in Microsoft] What is a virtual function in C++?

Answer:
A virtual function is a function that is overridden in derived classes to support dynamic binding.

Example:

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

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

int main() {
    Base *ptr;
    Derived d;
    ptr = &d;
    ptr->show();  // Calls Derived class function
    return 0;
}

Output:

Derived class

Virtual functions enable runtime polymorphism.


9. [Asked in Google] What is a constructor and a destructor in C++?

Answer:

  • Constructor โ€“ A special function that initializes an object.
  • Destructor โ€“ A special function that destroys an object when it goes out of scope.

Example:

class Demo {
public:
    Demo() { cout << "Constructor Called"; }
    ~Demo() { cout << "Destructor Called"; }
};

int main() {
    Demo d;
    return 0;
}

Output:

Constructor Called
Destructor Called

10. [Asked in Flipkart] What is an interface in C++?

Answer:
An interface in C++ is implemented using pure virtual functions in an abstract class.

Example:

class Interface {
public:
    virtual void display() = 0; // Pure virtual function
};
class Implement : public Interface {
public:
    void display() override { cout << "Interface Implemented"; }
};

int main() {
    Implement obj;
    obj.display();
    return 0;
}

Output:

Interface Implemented