OOP Questions 1 To 20 Notes
OOP Questions 1 To 20 Notes
Example:
#include <iostream>
int main() {
int a = 10, b = 0;
try {
if(b == 0)
cout << a / b;
return 0;
2. Explain the concept of virtual function and pure virtual function with the help of example.
Virtual Function: A member function that can be overridden in derived classes using the 'virtual' keyword.
Pure Virtual Function: A virtual function with no definition, used in abstract classes.
Example:
class Base {
public:
};
3. What is polymorphism? Explain its two types and difference between compile-time and run-time polymorphism.
Types:
Example:
Compile-time:
Run-time:
4a. How will you create a text file and perform read/write operations on it?
Example:
ofstream fout("file.txt");
fout.close();
ifstream fin("file.txt");
string s;
fin >> s;
fin.close();
class Complex {
int a;
public:
Complex(int x) { a = x; }
};
5. What is polymorphism? Explain the different ways in which we can implement compile-time and run-time
polymorphism.
6. Create a base class DMA and a class that uses DMA to store strings. Use deep copy constructor and destructor.
class DMA {
char* str;
public:
DMA(const char* s) {
strcpy(str, s);
DMA(const DMA& d) {
strcpy(str, d.str);
}
};
7. Create a class and inherit two derived classes. Use base class pointer. Explain runtime polymorphism.
class Base {
public:
};
public:
};
public:
};
Function Overriding: Derived class redefines base class function using virtual keyword.
class A {
};
class B : public A {
};
10. What is Constructor and Destructor? How are they used in inheritance?
Types:
12. What do you mean by Constructor in C++? Explain all its types and also explain parameterized & copy constructor
with examples.
Types:
- Default
- Parameterized
- Copy
Example:
class A {
public:
A() {} // Default
A(int x) {} // Parameterized
A(A &obj) {} // Copy
};
1. Game Development
2. GUI Applications
3. Real-Time Systems
4. Database Systems
5. Web Development
6. Network Simulations
7. CAD/CAM Systems
8. Embedded Systems
9. Robotics
10. AI Applications
Static Binding:
- Resolved at compile-time.
Dynamic Binding:
- Resolved at run-time.
Examples:
16. Define class? How member function define inside and outside the class? Give example.
Inside class:
class A {
};
Outside class:
class A {
};
17. What is the role of this pointer in C++? Explain it with programming example.
Example:
class A {
int a;
public:
};
Structure:
- Members are public by default.
Class:
19. What are the different features and applications of OOP? Explain in detail.
Features:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Example:
class A {
private: int x;
public: int y;
protected: int z;
};