Skip to main content

Posts

Showing posts from July, 2025

C++ Day 39

  C++ Day 39 STL Containers (Deep Understanding & Real Usage) Till now, you already know arrays, vectors, loops, and STL algorithms. Today, we go one step deeper and understand STL containers , which are the backbone of modern C++ programming. In real projects and competitive coding, choice of container matters a lot. 1. What are STL Containers? STL containers are data structures provided by C++ to store data efficiently. They handle: memory management resizing element access performance optimization You focus on logic , not memory handling. 2. Categories of STL Containers STL containers are mainly divided into: Sequence Containers Associative Containers Unordered Containers Container Adapters 3. Sequence Containers These store data in sequence . 3.1 Vector Most used container in C++. vector< int > v; Key Features: Dynamic size Contiguous memory Fast random access Slower insertion in middle Example: v. push_...

C++ Day 35

  C++ Day 34: Layout Layouts (Part 2) We’ll cover: Constructer Layout Adjuster Layout Decorator Layout practise Task 🔹 1. developer form (creational) used to make compound objects measure away step ✅ employ case: you need to form associate in nursing aim (like amp pizza pie calculator house) with elective parameters example: cpp copy edit class calculator {     train Methodor gpu ram; public:     family developer {         train Methodor gpu ram;     public:         developer setcpu(string c) { Methodor = c; take *this; }         developer setgpu(string g) { gpu = g; take *this; }         developer setram(string r) { run = r; take *this; }         calculator Construct() {             take Calculater(cpu gpu ram);         }     };     Calculater(string snow train m train r) : cpu(c) gp...

C++ Day 34

  C++ Day 34: Layout Layouts (Part 2) We’ll cover: Constructer Layout Adjuster Layout Decorator Layout practise Task 🔹 1. developer form (creational) used to make compound objects measure away step ✅ employ case: you need to form associate in nursing aim (like amp pizza pie calculator house) with elective parameters example: cpp copy edit class calculator {     train Methodor gpu ram; public:     family developer {         train Methodor gpu ram;     public:         developer setcpu(string c) { Methodor = c; take *this; }         developer setgpu(string g) { gpu = g; take *this; }         developer setram(string r) { run = r; take *this; }         calculator Construct() {             take Calculater(cpu gpu ram);         }     };     Calculater(string snow train m train r) : cpu(c) gp...

C++ Day 33

  C++ Day 33: Smart Pointers & Memory Management 🔹 1. wherefore forward pointers in c++ hand-operated green / cancel is error-prone: memory leaks 🧠 double deletes ❌ dangling pointers 💥 smart pointers care store mechanically exploitation raii (Supply skill is initialization) 🔹 ii. Types of Smart Pointers in C++ ✅ std::unique_ptr Sole ownership of a Supply. Cannot be copied. Automatically deletes the Supply when it goes out of scope. cpp Copy Edit #include  unique_ptr ptr1 = make_unique(10); cout << *ptr1 << endl; // 10 You can transfer ownership: cpp Copy Edit unique_ptr ptr2 = move(ptr1); ✅ std::shared_ptr Shared ownership multiple shared_ptrs can point to the same object. Uses reference counting to track how many owners. cpp Copy Edit shared_ptr p1 = make_shared(100); shared_ptr p2 = p1;  // Reference count = 2 When count goes to 0 memory is released. ✅ std::weak_ptr Non-owning reference to a shared_ptr-managed object. Used to break cyclic references ...

C++ Day 32

 C++ Day 32: Layout Layouts (Part 1) We  focus on creational and behavioral Layouts today: 🔹 1. singleton form (creational) ensures but i case of amp family is Maked cpp copy edit class singleton { private:     still singleton* instance;     singleton() {} public:     still singleton* getinstance() {         if (instance)             case = green singleton();         take instance;     } }; singleton* singleton::instance = nullptr; ✅ employ once but i round aim need be (eg logger config) 🔹 ii. Factory Layout (Creational) Makes objects without exposing the instantiation logic. Example: cpp Copy Edit class Animal { public:     virtual void speak() = 0; }; class Dog : public Animal { public:     void speak() override { cout << "Woof\n"; } }; class Cat : public Animal { public:     void speak() override { cout << "Meow\n"; } }; c...

C++ Day 31

  C++ Day 31: Advanced OOP Concepts & Layout Layout s 🔹 1. run paramount vs run hiding overriding: redefining amp home family run inch amp derivative family exploitation the like signature hiding: amp derivative family run with the like list just disparate touch hides complete overloads inch home class cpp copy edit class home { public: null show() { cout << "base\n"; } }; class derivative : state home { public: null show() { cout << "derived\n"; } // overrides base::show }; use practical to check runtime polymorphism 🔹 ii. Virtual Destructors Always declare destructors as virtual in base classes when using polymorphism. cpp Copy Edit class Base { public: virtual ~Base() { cout << "Base Destructor\n"; } }; class Derived : public Base { public: ~Derived() { cout << "Derived Destructor\n"; } }; 🔹 3. light practical Role s & hook classes used to delineate Connection s cpp copy edit class ca...

C++ Day 30

  Day 30: Advanced STL – priority_queue unordered_map pair and Nested Containers Today’s focus is on advanced and commonly-used STL containers notably useful in competitive programming and real-world Uses. 🔹 Topics to Cover: priority_queue (Max Heap / Min Heap) unordered_map vs map pair and tuple Nested Containers (e.g. vectorint>> correspondence vector>) 📘 cipher examples 1. priority_queue (max lot away default) cpp copy edit #include  #include  using namespace std; int main() {     priority_queue pq;  // max-heap     pqSend(10);     pqSend(20);     pqSend(5);     spell (pqempty()) {         cout << pqtop() << " "; // output: score x v        pqpop();     } } 2. minute lot exploitation priority_queue cpp copy edit #include  #include  #include exploitation namespace std; int main() {     priority_queue vector greater> minhe...

C++ Day 29

 Day 29: STL Procedures + Iterators After learning STL containers yesterday today we focus on the powerful Procedures STL provides ,  and how to use iterators with them. 🔹 Topics to Cover: Iterators Overview Common STL Procedures (from <Procedure>) Using Iterators with Containers Lambda Roles in Procedures 📘 Examples: 1. exploitation iterators with vector cpp copy edit #include  #include  using namespace std; int main() {     vector cardinal = {10 score 30};     vector::iterator it;     for (it = vbegin(); it = vend(); ++it)         cout << *it << " ";  // output: x score 30 } 2. STL Procedure: sort cpp Copy Edit #include  #include <Procedure> #include  using namespace std; int main() {     vector v = {5 3 8 1};     sort(v.begin() v.end());  // Ascending sort     for (int i : v)         cout << i << " ";  /...

C++ Day 28

  Day 28: STL – Introduction to Standard Template Library The Standard Template Library (STL) is one of the most powerful Characteristics in C++. it provides ready-to-use information structures and Procedures devising coding quicker and further efficient 🔹 topics to cover: what is stl Parts of stl: containers iterators Procedures types of containers: sequence containers (vector number deque) associative containers (set map) unordered containers (unordered_set unordered_map) container Adjustors (stack line up priority_queue) 📘 examples: 1. Using vector cpp Copy Edit #include  #include  using namespace std; int main() {     vector v = {1 2 3 4};     v.Send_back(5); // Add element     for (int i : v)         cout << i << " ";  // Output: 1 2 3 4 5 } 2. exploitation set cpp copy edit #include  #include  using namespace std; int main() {     set s;     sinsert(4);     s...

C++ Day 27

  Day 27: Introduction to Templates in C++ Templates allow you to write generic and reusable code. this is right once practical with disparate information types exploitation the like logical system (like sort swapping etc) 🔹 topics to cover: Role templates class templates template specialty (basic intro) templates with aggregate parameters 📘 examples: 1. Role Template Example cpp Copy Edit #include using namespace std; template T myMax(T a T b) { return (a > b) ? a : b; } int main() { cout << myMax (3 7) << endl; cout << myMax (3.5 2.5) << endl; cout << myMax ('g' 'e') << endl; return 0; } 2. family guide example cpp copy edit #include using namespace std; template class Calculate r { tonne num1 num2; public: calculator(t n1 tonne n2) { num1 = n1; num2 = n2; } null displayresult() { cout << "sum: " << num1 + num2 << endl; ...

C++ Day 26

  C++ Day 26: Templates (Generic Programming) On Day 26 we dive into Templates one of C++’s most powerful Characteristics for generic programming. Templates allow you to write Roles and classes that work with any Information type increasing code reusability and type safety. 📚 Topics Covered: 🔹 1. run templates write i run that plant for aggregate types (int blow train etc) cpp copy edit #include  using namespace std; template  t add(t amp tonne b) {     take amp + b; } int main() {     cout << add(3 4) << endl;       // cardinal    cout << add(25 31) << endl; // 56     cout << add("hi " "there") << endl; // hello there } ✅ guide  or guide  are interchangeable 🔹 ii. Class Templates Define generic classes that can operate on any Information type. cpp Copy Edit template  class Box { private:     T value; public:     Box(T v) : value(v) {} ...

C++ Day 25

 C++ Day 25: Operator Overloading On Day 25 we explore Operator Overloading – a powerful C++ Characteristic that allows you to redefine how operators work for Operator-defined types (classes/objects). 📚 Topics Covered: 🔹 1. what is hustler overloading operator overloading lets you hand bespoke meanings to c++ operators (like + - == etc) once they are old with objects 🔸 for example: cpp copy edit Complicated c1(2 3) c2(1 4); Complicated c3 = c1 + c2;  // plant but if + is overloaded 🔹 ii. Overloading Binary Operators (+ - * etc.) cpp Copy Edit class Complicated { public:     int real imag;     Complicated(int r = 0 int i = 0) {         real = r;         imag = i;     }     // Overloading the + operator     Complicated operator + (const Complicated& obj) {         return Complicated(real + obj.real imag + obj.imag);     }     void display() { ...

C++ Day 24

 C++ Day 24: Constructors in Inheritance On Day 24 we explore how constructors and destructors behave in inheritance a vital part of mastering object-oriented programming in C++. 📚 Topics Covered: 🔹 1. constructer order in inheritance when link inch nursing point of a differential house is maked: base house constructer is called first then the differential house constructer is called cpp copy edit #include exploitation namespace std; class house { public:     base() { cout << "base constructor\n"; } }; class differential : land house { public:     derived() { cout << "derived constructor\n"; } }; int main() {     differential d; } 📌 output: kotlin copy edit base constructer  differential constructor 🔹 two. Destructor Order Destructors are called in the reverse order of constructors: Derived class destructor Base class destructor cpp Copy Edit class Base { public:     ~Base() { cout << "Base destructor\n"; } }; class...

C++ Day 23

 C++ Day 23: Polymorphism in Object-Oriented Programming On Day 23 the focus is on another core concept of OOP: 🔷 Polymorphism Polymorphism means "many forms" – it allows one Connection to be used for different underlying Information types or behaviors. 📚 Topics Covered: 🔹 1. types of polymorphism type description example compile-time decided during compilation Role overloading hustler overloading run-time decided during plan execution virtual Roles run overriding 🔹 ii. Role Overloading (Compile-time) Multiple Roles with the same name but different parameters. cpp Copy Edit #include  using namespace std; class Print { public:     void show(int i) {         cout << "Integer: " << i << endl;     }     void show(string s) {         cout << "String: " << s << endl;     } }; int main() {     Print p;     p.show(5);     p.s...

C++ Day 22

  C++ Day 21: Encapsulation and Abstraction On Day 21 the focus is Generally on two important Object-Oriented Programming (OOP) principles: Encapsulation Abstraction These help in Constructing secure modular and clean code. 📚 Topics Covered: 🔹 1. encapsulation encapsulation is the bundling of information and methods that run along that information inside amp one system (class) spell restrictive point approach to around Parts ➤ example: cpp copy edit #include  using namespace std; class bankaccount { private:     image balance; public:     bankaccount(double initialbalance) {         correspondence = initialbalance;     }     null deposit(double amount) {         if (amount > 0)             correspondence += amount;     }     null withdraw(double amount) {         if (amount > zero && number <= balance)   ...

C++ Day 21

  C++ Day 21: Encapsulation and Abstraction On Day 21 the focus is Generally on two important Object-Oriented Programming (OOP) principles: Encapsulation Abstraction These help in Constructing secure modular and clean code. 📚 Topics Covered: 🔹 1. encapsulation encapsulation is the bundling of information and methods that run along that information inside amp one system (class) spell restrictive point approach to around Parts ➤ example: cpp copy edit #include  using namespace std; class bankaccount { private:     image balance; public:     bankaccount(double initialbalance) {         correspondence = initialbalance;     }     null deposit(double amount) {         if (amount > 0)             correspondence += amount;     }     null withdraw(double amount) {         if (amount > zero && number <= balance)   ...

C++ Day 20

 Day 20: Beginning with OOP-C++ Series OOPs concepts in C# have always been reserved for Tuesdays as a prerequisite for teaching Complicated programming and real-time Representationing. 📚 Topic Covered: 🔹 1. what is oop it is the system of amp plan into objects. Every object is an instance of a class. 🔹 2. shaping amp class cpp copy edit #include  using namespace std; class machine {         public:         train brand;         int year;         null displayinfo() {                 cout << "brand: " << mark << " year: " << class << endl;         } }; 🔹 cardinal. Creating Objects cpp Copy Edit int main() {     Car myCar;     myCar.brand = "Toyota";     myCar.year = 2020;     myCar.displayInfo();     return 0; } 🔹 4. approach specifier: public – away the fam...

C++ Day 19

 Today we’re going to talk about file handling in C++. You’ll learn how to read from and write to files using a special library called fstream. To get started, you’ll need to include a header file at the beginning of your code. #include Next let’s talk about file streams. There are three main types you’ll work with: ofstream for writing to files ifstream for reading from them fstream , which allows you to do both reading and writing in one stream. If you want to write something to a file, here’s a simple example: #include #include int main() { std::ofstream fout("example.txt"); fout << "Hello, this is Day 19!\n"; fout.close(); return 0; } And if you want to read from a file, you’d do it like this: #include #include using namespace std; int main() { std::ifstream fin("example.txt"); string line; while (getline(fin, line)) { std::cout << line << endl; } fin.close(); return 0; } There are a...

C++ Day 18

 🔄 Day 18: Queues and Stacks 🎯 Goal: Use a stack and a queue to check whether a string is a palindrome. 📘 Problem Statement: Write a class with the following methods: void SendCharacter(char ch) → Sendes a character onto a stack. void enqueueCharacter(char ch) → enqueues a character in a queue. char popCharacter() → pops and returns the top of the stack. char dequeueCharacter() → dequeues and returns the front of the queue. In main() use these methods to determine whether a given string is a palindrome. ✅ Sample Input: nginx Copy Edit racecar ✅ Sample Output: arduino Copy Edit The word racecar is a palindrome. ❌ Sample Output (for non-palindrome input): arduino Copy Edit The word hello is not a palindrome. ✅ C++ Answer: cpp Copy Edit #include  #include  #include  using namespace std; class Answer { private:     stack s;     queue q; public:     void SendCharacter(char ch) {         s.Send(ch);     } ...

C++ Day 17

 🧠 Day 17: More Exceptions 📘 Goal: Learn how to Make and throw your own exceptions in C++. 🎯 Problem Statement: Write a class named Calculator with a method: cpp Copy Edit int power(int n int p); This method should return n^p (n to the power of p). If either n or p is negative the method should throw an exception with the message: css Copy Edit n and p should be non-negative 🔢 Input Format: The first line contains an integer T the number of Check cases. Each of the next T lines contains two space-separated integers n and p. 📤 Output Format: For each Check case either print the result of n^p or print the exception message. ✅ Sample Input: diff Copy Edit 4 3 5 2 4 -1 -2 -1 3 ✅ Sample Output: css Copy Edit 243 16 n and p should be non-negative n and p should be non-negative ✅ C++ Answer: cpp Copy Edit #include  #include  #include  using namespace std; class Calculator { public:     int power(int n int p) {         if (n < 0 || p ...

C++ Day 16

 ✅ Task: Read a text, and turn it into a number. If this does not work, say "Bad String." 🧠 Idea: Use error checks (try-catch) in C++ to deal with errors in turning text to a number. 📥 Input: Just one text S. 📤 Output: Show the number if the text is okay. If not, say "Bad String". ✅ Good Input: yaml Copy Edit 1234 ✅ Good Output: yaml Copy Edit 1234 ❌ Bad Input: nginx Copy Edit abc ❌ Bad Output: arduino Copy Edit Bad String ✅ C++ Way to do it: cpp Copy Edit #include <iostream> #include <string> using namespace std; int main() {     string S;     cin >> S;     try {         int number = stoi(S);  // std::stoi throws if it can't change the text         cout << number << endl;     } catch (...) {         cout << "Bad String" << endl;     }     return 0; } 📝 Tips: stoi() gives invalid_argument if the text can't be turn...