C++ Practice Problems
1. Smart Inventory System (Encapsulation + STL)
Problem:
Design a class Inventory that stores items (string name, int quantity).
Support operations:
• Add item
• Update quantity
• Remove item
• Print items sorted by quantity (descending)
Input Format:
n
operation item_name quantity
Operations: ADD, UPDATE, REMOVE, PRINT
Output:
Print items as:
name quantity
Constraints:
• 1 ≤ n ≤ 10^5
Sample Input:
5
ADD apple 10
ADD banana 5
UPDATE apple 20
REMOVE banana 0
PRINT
Sample Output:
apple 20
2. Student Ranking System (Constructors + Sorting)
Problem:
Create a Student class with:
• name
• marks
Sort students:
1. Highest marks
2. Lexicographically smaller name
Input:
n
name marks
Output:
Sorted list
Sample Input:
3
Aman 90
Riya 95
Aman 95
Output:
Aman 95
Riya 95
Aman 90
3. Bank Account Simulator (Encapsulation + Exception Handling)
Problem:
Class BankAccount:
• deposit
• withdraw
• throw exception if insufficient balance
Input:
n
operation amount
Output:
• Print updated balance
• Print "Insufficient Balance" if exception occurs
Sample Input:
3
deposit 1000
withdraw 500
withdraw 600
Output:
1000
500
Insufficient Balance
4. Shape Area Calculator (Runtime Polymorphism)
Problem:
Base class Shape, derived:
• Circle
• Rectangle
Use virtual function area()
Input:
n
type dimensions
Sample Input:
2
circle 2
rectangle 2 3
Output:
12.56
6
5. Custom Vector Class (Dynamic Memory Allocation)
Problem:
Implement your own MyVector class:
• dynamic resizing
• push_back
• pop_back
• print
Input:
n
operation value
Output:
Final vector
Sample Input:
5
push 1
push 2
push 3
pop 0
print 0
Output:
12
6. Operator Overloading – Complex Numbers
Problem:
Create class Complex:
• real, imag
• overload +, -, *
Input:
ab
cd
Output:
(a+bi) + (c+di)
(a+bi) - (c+di)
(a+bi) * (c+di)
Sample Input:
12
34
Output:
4 + 6i
-2 - 2i
-5 + 10i
7. Multi-Level Inheritance – Employee System
Problem:
Classes:
• Person → Employee → Manager
Manager gets bonus:
salary + (level * 1000)
Input:
name salary level
Output:
Final salary
Sample Input:
John 5000 3
Output:
8000
8. Exception Handling – Matrix Division
Problem:
Divide two matrices element-wise.
Throw exception if division by zero occurs.
Input:
nm
matrixA
matrixB
Output:
• Result matrix
• OR "Division by zero error"
Sample Input:
22
46
82
23
40
Output:
Division by zero error
9. File Logger Simulation (Destructor Concept)
Problem:
Simulate logging system:
• Constructor → open log
• Destructor → print "Log closed"
Input:
n
messages
Output:
Log started
messages...
Log closed
Sample Input:
2
Hello
World
Output:
Log started
Hello
World
Log closed
10. Polymorphic Payment System (Advanced OOP)
Problem:
Base class Payment
Derived:
• CreditCard
• UPI
Each has:
processPayment(amount)
Input:
n
type amount
Output:
Payment method processed: <type> amount
Sample Input:
2
card 1000
upi 500
Output:
Payment method processed: card 1000
Payment method processed: upi 500