Comprehensive C++ Concepts and Examples
1. What is Polymorphism & Types of Polymorphism?
Polymorphism is an object-oriented programming concept that allows a single function or method to behave
differently based on the object it is acting upon. It promotes flexibility, maintainability, and reusability in code.
Types of Polymorphism:
1. Compile-time Polymorphism (Static): Resolved at compile time, achieved through function and operator
overloading.
2. Run-time Polymorphism (Dynamic): Resolved at runtime, achieved through virtual functions and
inheritance.
2. What is Understood by Object-Oriented?
Object-oriented programming (OOP) is a paradigm that structures software around objects that contain data
and methods. OOP concepts include Encapsulation, Abstraction, Inheritance, and Polymorphism. It models
real-world entities and makes programs modular and easier to debug and maintain.
3. What Do You Understand by Overloading?
Overloading means having multiple definitions for the same function name or operator with different
parameter types or counts.
Function Overloading: Same function name with different parameter types.
Operator Overloading: Redefining operators for user-defined classes.
4. What is File Processing System in C++ and Advantage of File Processing System?
File processing refers to reading from and writing data to files using streams like ifstream, ofstream, and
fstream.
Advantages:
- Data persistence
- Data sharing
- Efficient large data handling
- Helps in logs, configs, and data backups.
Comprehensive C++ Concepts and Examples
5. Difference between Static Binding & Dynamic Binding
Static Binding:
- Resolved at compile time
- Uses normal or overloaded functions
- Faster
Dynamic Binding:
- Resolved at runtime
- Uses virtual functions and inheritance
- More flexible for polymorphic behavior.
6. Write a Program Which Demonstrates Function Overloading
class Calculator {
public:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
string add(string a, string b) { return a + b; }
};
7. Write a Program Which Demonstrates Switch Statement
switch (option) {
case 1: cout << 'Start'; break;
case 2: cout << 'Pause'; break;
case 3: cout << 'Exit'; break;
default: cout << 'Invalid';
8. Write a Program Which Demonstrates for/while & do-while Loop
FOR loop: for(int i=0;i<5;i++)
Comprehensive C++ Concepts and Examples
WHILE loop: while(i<5)
DO-WHILE loop: do { ... } while(i<5);
9. What Do You Understand by File Processing System & Advantages of File System?
A file processing system allows you to store, access, and manipulate data on the disk. C++ provides streams
like ifstream and ofstream.
Advantages:
- Data storage beyond program runtime
- Handling large records
- Useful for logs, backups, and persistent state.
10. Define Inline Function
An inline function is a function defined with the 'inline' keyword, which suggests the compiler to insert the
function's code directly at the call site to reduce function call overhead.
11. Write a Program Which Demonstrates Friend Class
class Box { friend class Display; private: int length; };
class Display { void show(Box b) { cout << b.length; } };
12. Write a Program Which Demonstrates If-Else Statement
if (marks >= 90) cout << 'A';
else if (marks >= 75) cout << 'B';
else cout << 'F';
13. Write a Program Which Demonstrates One Statement
int main() { cout << 'Hello'; return 0; }