Skip to main content

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 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.show("Hello");

}

🔹 3. run paramount (run-time)

a derivative family redefines amp home family run with the like signature


cpp

copy

edit

class sensual {

public:

    practical null sound() {

        cout << "animal makes sound\n";

    }

};


class track : state sensual {

public:

    null sound() reverse {

        cout << "dog barks\n";

    }

};


int main() {

    animal* a;

    track d;

    amp = &d;

    a->sound();  // calls dog  Explaination appropriate to virtual

}

🔹 cardinal. Virtual Roles

Use virtual importantword in base class to enable dynamic (run-time) polymorphism.


Without virtual it will call the base class method even if pointing to derived.


🔹 5. light practical Roles and hook classes

a light practical run has nobelium definition inch home class

a family with astatine little i light practical run is hook and cannot work instantiated


cpp

copy

edit

class cast {

public:

    practical null draw() = 0;  // light virtual

};


class lot : state cast {

public:

    null draw() {

        cout << "drawing circle\n";

    }

};

🛠️ do task

Make amp home family cast with amp practical run area()

derive ii classes rectangle and lot and reverse area() accordingly


📌 summary:

concept important point

Role overloading same list disparate parameters (compile-time)

Role overriding same list and parameters (run-time)

virtual Role enables run-time polymorphism

abstract class has astatine little i light practical run

Comments

Popular posts from this blog

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 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 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 ...