0% found this document useful (0 votes)
10 views8 pages

Oop CPP Interview Notes With Code

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts in C++, including definitions, characteristics, and examples of key features such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation. It also highlights differences between C and C++, access specifiers, constructors, destructors, namespaces, and inline functions, along with relevant code snippets for illustration. The notes emphasize the importance of understanding OOP principles and practicing code examples for interview preparation.

Uploaded by

amankrc2025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Oop CPP Interview Notes With Code

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts in C++, including definitions, characteristics, and examples of key features such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation. It also highlights differences between C and C++, access specifiers, constructors, destructors, namespaces, and inline functions, along with relevant code snippets for illustration. The notes emphasize the importance of understanding OOP principles and practicing code examples for interview preparation.

Uploaded by

amankrc2025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

### OOP with C++ Interview Questions and Answers Notes - August 13, 2025, 08:46 PM

IST

#### 00:00 - Introduction


- Host: Nishant from Career Right.
- Topic: Object-Oriented Programming (OOP) with C++ interview questions and
answers.
- Objective: To cover essential OOP concepts for interviews.

#### 00:18 - Why Do We Need Object-Oriented Programming?


- **Reason:** Developed to overcome limitations of procedural languages (e.g., C,
Pascal, Fortran).
- **Procedural Approach:** Code written top-down, divided into functions; uses
local and global data.
- **Limitations:**
- Unrestricted access: Many functions and global data complicate modifications
(e.g., changing global data type from int to float requires updating all
functions).
- Poor real-world modeling: Doesn’t reflect real-life objects well.

#### 02:00 - What Is Object-Oriented Programming?


- **Definition:** A style where data items (data members) and functions (member
functions) are grouped into units called objects.
- **Approach:** Objects communicate via member function calls.
- **Languages:** C++, Java, C#, Python, PHP, etc.

#### 02:47 - What Are the Characteristics of Object-Oriented Programming?


- **Key Features:** Class, Objects, Inheritance, Polymorphism, Abstraction,
Encapsulation.

#### 03:06 - What Are Classes and Objects?


- **Class:** A blueprint defining data and functions (e.g., doesn’t create objects
automatically).
- **Object:** An instance of a class, reserves memory, mirrors real-life entities
(e.g., car with properties like model, color, and functions like start, drive).
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Car {
public:
string model;
void start() { cout << "Car started" << endl; }
};

int main() {
Car car1; // Object creation
car1.model = "Toyota";
car1.start(); // Output: Car started
return 0;
}
```

#### 04:55 - What Is Inheritance?


- **Definition:** A mechanism where a derived class acquires attributes and
functions from a base class.
- **Terms:** Base class (inherited from), Derived class (inherits).
- **Process:** Uses colon `:` for inheritance.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Vehicle {
public:
void drive() { cout << "Vehicle is driving" << endl; }
};

class Car : public Vehicle {


public:
void honk() { cout << "Car honks" << endl; }
};

int main() {
Car car1;
car1.drive(); // Inherited from Vehicle
car1.honk(); // Specific to Car
return 0;
}
```

#### 05:52 - What Is Polymorphism?


- **Definition:** An entity behaving differently in different situations (means
"many forms").
- **Real-Life Example:** A man as a father, husband, or employee.
- **Types in C++:**
- Function Overloading: Multiple functions with the same name but different
parameters (compile-time).
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Math {
public:
void add(int a, int b) { cout << a + b << endl; }
void add(int a, int b, int c) { cout << a + b + c << endl; }
};

int main() {
Math m;
m.add(2, 3); // Output: 5
m.add(2, 3, 4); // Output: 9
return 0;
}
```
- Operator Overloading: Changes operator behavior for user-defined types
(compile-time).
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Complex {
public:
int real;
Complex(int r = 0) : real(r) {}
Complex operator+(Complex c) { return Complex(real + c.real); }
};

int main() {
Complex c1(5), c2(3);
Complex c3 = c1 + c2;
cout << c3.real << endl; // Output: 8
return 0;
}
```
- Function Overriding: Redefines a base class function in a derived class
(runtime).
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Animal {
public:
virtual void sound() { cout << "Some sound" << endl; }
};

class Dog : public Animal {


public:
void sound() { cout << "Bark" << endl; }
};

int main() {
Animal *a = new Dog();
a->sound(); // Output: Bark (runtime polymorphism)
delete a;
return 0;
}
```

#### 08:22 - Difference Between Compile-Time and Runtime Polymorphism


- **Compile-Time (Static Binding):**
- Compiler selects the function at compile time.
- Achieved via function/operator overloading.
- Faster execution.
- **Runtime (Dynamic Binding):**
- Function call decided at runtime.
- Achieved via virtual functions/function overriding.
- Slower execution.

#### 09:29 - What Is Abstraction in OOP?


- **Definition:** Displaying essential information and hiding background details.
- **Purpose:** Provides an interface, hides implementation.
- **Example:** `log` function in `<cmath>`—users call it without knowing the
algorithm.
- **Example Code:**
```cpp
#include <iostream>
#include <cmath>
using namespace std;

int main() {
double x = log(10); // Abstraction: Details hidden
cout << x << endl; // Output: Approx 2.302
return 0;
}
```

#### 10:26 - What Is Encapsulation?


- **Definition:** Grouping related data members and functions into a single class.
- **Benefits:** Cleaner code, easier to read, better data control.
- **Example:** Class `Rectangle` with length, breadth, and area function.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Rectangle {
private:
int length, breadth;
public:
void setDimensions(int l, int b) { length = l; breadth = b; }
int area() { return length * breadth; }
};

int main() {
Rectangle r;
r.setDimensions(5, 3);
cout << "Area: " << r.area() << endl; // Output: Area: 15
return 0;
}
```

#### 11:19 - What Is Data Hiding in C++?


- **Definition:** Concealing data within a class to prevent accidental access.
- **Mechanism:** Declare data as `private`.
- **Purpose:** Protects from mistakes; private members accessible only within the
class.
- **Example Code:** (Same as Encapsulation example above)

#### 11:53 - What Is the Difference Between C and C++?


- **Paradigm:** C (procedural), C++ (object-oriented).
- **Subset/Superset:** C is a subset, C++ is a superset.
- **Data/Functions:** C separates them, C++ encapsulates them.
- **Access Specifiers:** Absent in C, present in C++.
- **Namespace:** Not in C, available in C++.
- **I/O:** C uses `scanf`/`printf`, C++ uses `cin`/`cout`.
- **Data Hiding:** Not supported in C, supported in C++.

#### 13:00 - What Are Access Specifiers in C++?


- **Definition:** Control access to class members.
- **Types:**
- Public: Accessible outside the class.
- Private: Accessible only within the class.
- Protected: Accessible in inherited classes, not outside.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Example {
private:
int x = 10;
protected:
int y = 20;
public:
void display() { cout << x << " " << y << endl; }
};

int main() {
Example e;
e.display(); // Output: 10 20
// e.x is inaccessible due to private
return 0;
}
```

#### 13:48 - What Are Structures in C++?


- **Definition:** Collection of variables of different data types, user-defined.
- **Keyword:** `struct`.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

struct Citizen {
string name;
int age;
};

int main() {
Citizen c1;
c1.name = "John";
c1.age = 25;
cout << c1.name << " is " << c1.age << endl; // Output: John is 25
return 0;
}
```

#### 14:36 - Difference Between Structures and Classes


- **Similarity:** Both group data and functions.
- **Difference:** Structure members are public by default, class members are
private by default.
- **Usage:** Structures for data, classes for data + functions.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

struct StructExample {
int x = 10; // Public by default
};

class ClassExample {
private:
int y = 20; // Private by default
public:
void show() { cout << y << endl; }
};

int main() {
StructExample s;
cout << s.x << endl; // Accessible
ClassExample c;
c.show(); // Accessible via public method
return 0;
}
```

#### 15:17 - What Are Constructors in C++?


- **Definition:** Specialized member function called automatically when an object
is created, used to initialize data.
- **Rules:**
- Same name as class.
- No return type.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Shoes {
public:
Shoes() { cout << "Shoes created" << endl; } // Constructor
};

int main() {
Shoes s; // Output: Shoes created
return 0;
}
```

#### 16:10 - What Are the Types of Constructors in C++?


- **Default Constructor:** No parameters.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Shoes {
public:
Shoes() { cout << "Default Constructor" << endl; }
};

int main() {
Shoes s; // Output: Default Constructor
return 0;
}
```
- **Parameterized Constructor:** Has parameters.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Shoes {
public:
int size;
Shoes(int s) { size = s; cout << "Size: " << size << endl; }
};

int main() {
Shoes s(42); // Output: Size: 42
return 0;
}
```
- **Copy Constructor:** Copies an existing object.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Shoes {
public:
int size;
Shoes(int s) { size = s; }
Shoes(const Shoes& s) { size = s.size; cout << "Copy: " << size << endl; }
};

int main() {
Shoes s1(42);
Shoes s2 = s1; // Output: Copy: 42
return 0;
}
```

#### 17:06 - What Are Destructors?


- **Definition:** Special member function invoked when an object is destroyed, used
to deallocate memory.
- **Rules:** Same name as class, no arguments/return type, preceded by `~`.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

class Shoes {
public:
Shoes() { cout << "Shoes created" << endl; }
~Shoes() { cout << "Shoes destroyed" << endl; } // Destructor
};

int main() {
Shoes s; // Output: Shoes created
} // Output: Shoes destroyed
return 0;
}
```

#### 17:39 - What Is a Namespace in C++?


- **Definition:** Declarative region providing scope to identifiers, prevents name
conflicts.
- **Purpose:** Resolves errors from duplicate names in large projects.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

namespace A {
int x = 5;
void printX() { cout << "A: " << x << endl; }
}
namespace B {
int x = 10;
void printX() { cout << "B: " << x << endl; }
}

int main() {
A::printX(); // Output: A: 5
B::printX(); // Output: B: 10
return 0;
}
```

#### 18:28 - What Are Inline Functions?


- **Definition:** Function where code is inserted at the call location during
compilation, avoiding jumps.
- **Benefit:** Speeds up execution.
- **Example Code:**
```cpp
#include <iostream>
using namespace std;

inline int add(int a, int b) { return a + b; }

int main() {
cout << add(2, 3) << endl; // Output: 5 (inline replaces call)
return 0;
}
```

---

**Notes:** Focus on understanding OOP principles (inheritance, polymorphism, etc.)


and C++ specifics (constructors, destructors, namespaces). Practice the provided
code examples like `Car` class, `Vehicle` inheritance, and `Shoes`
constructors/destructors for interviews.

You might also like