unit 5 opps
1️⃣ Strings in C++
🔹 Definition (Easy Words)
A string is a collection of characters used to store text like name, address, etc.
Example:
-
"Ujjwal"
-
"ATM Machine"
-
"Library Book"
🔹 String in C++
C++ provides string class in <string> library.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "Ujjwal";
cout << name;
}
🔹 Common String Operations
string s1 = "Hello";
string s2 = "World";
cout << s1 + " " + s2; // Concatenation
cout << s1.length(); // Length
2️⃣ Exception Handling in C++
🔹 Definition (Easy Words)
Exception handling is a method to handle errors during program execution so the program does not crash.
In simple words:
👉 If an error happens, we handle it safely.
🔹 Keywords Used
-
try -
catch -
throw
🔹 Example
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 0;
try {
if (b == 0)
throw "Division by zero not allowed";
cout << a / b;
}
catch (const char* msg) {
cout << msg;
}
}
Here:
If error happens → program shows message instead of crashing.
3️⃣ Introduction to Multithreading
🔹 Definition (Easy Words)
Multithreading means running multiple tasks at the same time in a program.
Example:
-
ATM processing multiple users
-
Browser downloading and playing video together
🔹 Thread in C++
C++ provides <thread> library.
#include <iostream>
#include <thread>
using namespace std;
void display() {
cout << "Thread running\n";
}
int main() {
thread t1(display);
t1.join();
}
🔹 Why Multithreading?
✅ Faster execution
✅ Better CPU usage
✅ Performs multiple tasks together
4️⃣ Data Collections in C++
🔹 Definition (Easy Words)
Data collection means storing multiple data items together.
C++ provides STL (Standard Template Library):
-
Vector
-
List
-
Map
-
Set
🔹 Example: Vector
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {10, 20, 30};
for (int n : numbers) {
cout << n << " ";
}
}
🔹 Example: Map
#include <map>
#include <iostream>
using namespace std;
int main() {
map<int, string> student;
student[1] = "Ujjwal";
student[2] = "Rahul";
cout << student[1];
}
5️⃣ Case Study 1: ATM System (Using OOP Concepts)
🔹 Classes Required
-
Account
-
ATM
-
User
🔹 Simple ATM Example
#include <iostream>
using namespace std;
class ATM {
private:
double balance;
public:
ATM(double b) {
balance = b;
}
void withdraw(double amount) {
try {
if (amount > balance)
throw "Insufficient Balance";
balance -= amount;
cout << "Withdraw Successful\n";
}
catch (const char* msg) {
cout << msg << endl;
}
}
void checkBalance() {
cout << "Balance: " << balance << endl;
}
};
int main() {
ATM user1(5000);
user1.withdraw(6000);
user1.checkBalance();
}
Concepts Used:
✅ Encapsulation
✅ Exception Handling
✅ Strings
✅ OOP
6️⃣ Case Study 2: Library Management System
🔹 Classes Required
-
Book
-
Student
-
Library
🔹 Example
#include <iostream>
#include <vector>
using namespace std;
class Book {
public:
string title;
bool issued;
Book(string t) {
title = t;
issued = false;
}
};
class Library {
private:
vector<Book> books;
public:
void addBook(string title) {
books.push_back(Book(title));
}
void showBooks() {
for (Book b : books) {
cout << b.title << endl;
}
}
};
int main() {
Library lib;
lib.addBook("C++ Programming");
lib.addBook("Data Structures");
lib.showBooks();
}
Concepts Used:
✅ Strings
✅ Data Collections (vector)
✅ OOP
✅ Objects and Classes
7️⃣ Quick Revision Table
| Topic | Simple Meaning |
|---|---|
| String | Text storage |
| Exception Handling | Handling errors safely |
| Multithreading | Running tasks at same time |
| Data Collection | Storing multiple data together |
| ATM Case Study | Real-world banking system |
| Library Case Study | Book management system |
Comments
Post a Comment