Computational Thinking & Object Modeling
প্রোগ্রামারের মতো চিন্তা করা শিখুন
1. Think First, Code Last
Most beginners open the editor and start typing. Then they get stuck. Then they Google. Then they copy-paste. Professional engineers do the opposite: they decompose, model, then code.
কোর্সের নিয়ম: algorithm এবং data model বুঝিয়ে বলতে না পারলে কোড লেখা শুরু করবেন না।
2. The Four Pillars of Computational Thinking
| Pillar | Meaning | বাংলায় |
|---|---|---|
| Decomposition | Break a big problem into smaller parts. | বড় সমস্যাকে ছোট ছোট অংশে ভাগ করা। |
| Abstraction | Hide details, expose only what matters. | অপ্রয়োজনীয় বিস্তারিত আড়াল করে শুধু গুরুত্বপূর্ণ অংশ দেখানো। |
| Pattern Recognition | Notice when problems share structure. | একই ধরনের গঠন বারবার চিনতে পারা। |
| Algorithmic Design | Write step-by-step instructions a machine can follow. | মেশিন যে ধাপগুলো অনুসরণ করতে পারে এমন instruction তৈরি করা। |
3. Object Modeling — The C++ Way
C++ adds a powerful new way to think: in objects. Before you code, ask: "What real-world things am I modeling?" Each thing becomes a class. Each class has state (data) and behavior (functions).
Example: Modeling a library system:
Book: title, author, ISBN, isAvailable()Member: name, id, borrowedBooksLibrary: collection of Books, list of Members, lend(), return()
4. Pseudocode — Algorithm Before Syntax
Before C++ syntax, write pseudocode. It is language-agnostic.
input: list L of numbers
max ← L[0]
for each element x in L:
if x > max:
max ← x
output: max
Now convert to C++:
#include <iostream>
#include <vector>
int main() {
std::vector<int> L = {3, 1, 4, 1, 5, 9, 2, 6};
int max_val = L[0];
for (int x : L) {
if (x > max_val) max_val = x;
}
std::cout << "Max = " << max_val << "\n";
return 0;
}
5. Loop Invariants — The Math of Loops
A loop invariant is a condition that is true before, during, and after each iteration. Stating the invariant is how we prove a loop is correct.
i elements, max_val equals the largest of those i elements.
This is true at start (i=1, only L[0] seen). If true at step i, it stays true at i+1 (we either keep max or update it). Therefore at the end, max_val = max of all elements.
6. Proof by Induction — In Code
Recursion is induction in code. Base case proves the smallest case; recursive case shows: if true for n-1, true for n.
#include <iostream>
long long factorial(int n) {
if (n <= 1) return 1; // base case
return n * factorial(n - 1); // inductive step
}
int main() {
for (int i = 0; i <= 10; ++i) {
std::cout << i << "! = " << factorial(i) << "\n";
}
return 0;
}
7. Practice Problems
-
Decompose: list the classes you'd design for a ride-sharing app. Mention 3 classes with their state and one behavior each.একটি ride-sharing app-এর জন্য ৩টি class ডিজাইন করুন। প্রতিটির state এবং একটি behavior লিখুন।
✨ Show Answer
Rider: state — name, location, paymentMethod; behavior — requestRide().
Driver: state — name, vehicle, isAvailable; behavior — acceptRide().
Trip: state — pickup, dropoff, fare, status; behavior — calculateFare(). -
Write pseudocode to count even numbers in a list, then translate to C++.একটি লিস্টে কয়টি জোড় সংখ্যা আছে — তা গণনার pseudocode লিখুন এবং C++-এ অনুবাদ করুন।
✨ Show Answer
count_even.cpp#include <iostream> #include <vector> int main() { std::vector<int> v = {2, 3, 4, 7, 10, 11}; int count = 0; for (int x : v) if (x % 2 == 0) ++count; std::cout << "Even count = " << count << "\n"; } -
Write the loop invariant for a function that sums a list.একটি লিস্টের যোগফল গণনার ফাংশনের loop invariant লিখুন।
✨ Show Answer
Invariant: After iteration
i, the variablesumequals the sum of the firstielements of the list. At i=0, sum=0 (empty sum). At i=N, sum equals the total sum. ✓ -
Write a recursive function to compute the sum 1+2+...+n in C++.1 থেকে n পর্যন্ত যোগফল recursive ফাংশন দিয়ে বের করুন।
✨ Show Answer
sum_n.cpp#include <iostream> int sum(int n) { if (n <= 0) return 0; return n + sum(n - 1); } int main() { std::cout << sum(100) << "\n"; // 5050 } -
Identify pattern: what do "find max", "find min", and "find sum" have in common?"find max", "find min", "find sum" — এই তিনটির মধ্যে মিল কী?
✨ Show Answer
All three are fold/reduce operations. They start with an initial accumulator and combine each element with it: max uses
max(acc, x), min usesmin(acc, x), sum usesacc + x. STL hasstd::accumulatefor exactly this pattern. -
Write a program that prints all even numbers from 1 to 20.১ থেকে ২০ পর্যন্ত সব জোড় সংখ্যা প্রিন্ট করুন।
✨ Show Answer
even.cpp#include <iostream> int main() { for (int i = 2; i <= 20; i += 2) { std::cout << i << " "; } std::cout << "\n"; } -
Why is decomposition important when designing software?সফটওয়্যার ডিজাইনে decomposition কেন গুরুত্বপূর্ণ?
✨ Show Answer
Decomposition lets you focus on one small problem at a time, makes code reusable, and allows multiple engineers to work in parallel on different parts. Without it, large systems become unmaintainable.
-
Compute factorial(15) using the program above. Why might it overflow with
int?factorial(15) উপরের প্রোগ্রাম দিয়ে বের করুন। int overflow কেন হতে পারে?✨ Show Answer
15! = 1,307,674,368,000 — this exceeds the max
intvalue (~2.1 billion on 32-bit). Uselong long(as in the example) which holds up to ~9.2 × 1018. Beyond 20! you need big-integer libraries.
Summary — Module 02
Computational thinking has four pillars: decomposition, abstraction, pattern recognition, and algorithmic design. In C++, abstraction takes the form of objects — classes that bundle state and behavior. Always think first, write pseudocode, prove correctness with invariants, and only then translate to syntax.