A

@Adarsh1999

Diamond problem

C++
4 weeks ago
Without virtual, you get: Animal Animal | | dog cat \ / dogcat ← two copies of Animal → ambiguous speak() With virtual:

Singleton

C++
3 months ago
// Online C++ compiler to run C++ program online #include <iostream> class Add{ public: static Add& getInstance(){ static Add instance; // Thread-safe in C++11 and later return instance; }

thread_local

C++
1 year ago
#include <iostream> #include <thread> //thread_local int var; //int var; class A{ public: void fun () {

Mutable

C++
1 year ago
#include <iostream> class A{ public: mutable int a; void update(int t) const{ a=t; std::cout<<"count: "<<a<<std::endl; }; };

Singleton

C++
1 year ago
#include <iostream> #include <memory> using namespace std; class Counter { private: static unique_ptr<Counter> instance; // Singleton instance int count; // Simple counter // Private constructor

Factory Method

C++
1 year ago
#include <iostream> #include <string> #include <memory> class Animal { // Capital letter class name public: virtual std::string speak() const = 0; // Pure virtual function virtual ~Animal() = default; // Virtual destructor };

vector to map

C++
1 year ago
#include <iostream> #include <vector> #include <map> #include <algorithm> int main() { std::vector<int> a{9, 5, 3, 1}; std::vector<int> b{0, 8, 6, 4}; std::map<int, int> arr;

String all fun code

C++
1 year ago
#include <iostream> #include <string> #include <algorithm> int main() { // Creating and initializing strings std::string str1 = "Hello, World!"; std::string str2 = "Goodbye!"; char char_array[20];

String Built in Functions

C++
1 year ago
Function Description Syntax size() Returns the size of the string in bytes.

Shared, Weak, Unique

C++
1 year ago
#include <iostream> #include <memory> class Entity { public: Entity(int val) : value(val) { std::cout << "Entity Created with value: " << value << "\n"; } ~Entity() { std::cout << "Entity Destroyed with value: " << value

Diamond Inheritance

C++
1 year ago
/* By making the inheritance virtual, Derived3 will have only one copy of Base, which eliminates the ambiguity. Here's how this works: Virtual inheritance ensures that only one copy of Base exists, even though Derived3 inherits Base through both De

Multiple Inheritance

C++
1 year ago
#include <iostream> class cat{ public: cat(){ std::cout<<"cat"<<std::endl; } ~cat(){ std::cout<<"cat distructor"<<std::endl; }

Rule of 3, 5 Modern C++ IMP

C++
1 year ago
#include <iostream> #include <algorithm> // For std::copy class MyResource { public: // Constructor MyResource() : data(new int[100]) {//data is initialized using the member initializer list (:) std::cout << "Constructor called\n";

unique_ptr vs shared_ptr

C++
1 year ago
#if 0 #include <iostream> #include <memory> void fourthFunction(std::unique_ptr<int32_t> responseCode) { *responseCode = 500; // unique_ptr goes out of scope here, releasing the resource } void thirdFunction(std::unique_ptr<int32_t> respon

Raw pointer vs Smart pointer

C++
1 year ago
Normal Pointers In C++ or similar languages, a normal pointer is a simple variable that stores the memory address of another variable. Example: cpp Copy code int* ptr = new int(10); // ptr points to a dynamically allocated integer with value 10 Wh

unique_ptr vs raw pointer

C++
1 year ago
/*std::unique_ptr is a smart pointer provided by the C++ Standard Library. It is a template class that is used for managing single objects or arrays. unique_ptr works on the concept of exclusive ownership - meaning only one unique_ptr is allowed to

week ptr

C++
1 year ago
#include <iostream> #include <memory> // For shared_ptr and weak_ptr class B; // Forward declaration of class B class A { public: std::shared_ptr<B> ptrB; // A holds a shared_ptr to B ~A() { std::cout << "A destroyed\n"; } };

Move Constructor

C++
1 year ago
#include <iostream> using namespace std; class MyClass { public: int* x; // Using a pointer to demonstrate move semantics // Constructor MyClass(int value) { x = new int(value); // Dynamic memory allocation

Copy constructor

C++
1 year ago
#include <iostream> class updater{ int x; public: updater(int value) : x(value){ std::cout << "Constructor called! Value: " << x << std::endl; }

Basics of Cons and Dist(default)

C++
1 year ago
#include <iostream> class Download{ private: int a = 7; int *ptr; public: Download();