{"id":20345811,"url":"https://github.com/izzypt/cpp","last_synced_at":"2026-02-01T09:33:40.467Z","repository":{"id":177839704,"uuid":"660974055","full_name":"izzypt/CPP","owner":"izzypt","description":"This is a repo that holds the basics and functionalities of CPP. It serves as the foundation for the future CPP modules repos.","archived":false,"fork":false,"pushed_at":"2023-09-04T17:46:30.000Z","size":180,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-01T12:05:29.178Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/izzypt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-01T11:43:17.000Z","updated_at":"2023-07-01T11:43:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"37b6bb3d-44c1-4ee8-8cca-9283d7d78374","html_url":"https://github.com/izzypt/CPP","commit_stats":null,"previous_names":["izzypt/cpp"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/izzypt/CPP","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/izzypt","download_url":"https://codeload.github.com/izzypt/CPP/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28975259,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-01T08:16:14.655Z","status":"ssl_error","status_checked_at":"2026-02-01T08:06:51.373Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T22:09:51.479Z","updated_at":"2026-02-01T09:33:40.452Z","avatar_url":"https://github.com/izzypt.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# CPP\nThis is a repo that holds the fundamentals to start working on the CPP modules ahead.\n\nhttps://roadmap.sh/cpp\n\n# Intro\n\n![image](https://github.com/izzypt/CPP/assets/73948790/279b4bc2-f369-44dc-95e3-4828bb67460d)\n\nC++ is a programming language that has evolved over time, and different versions, or standards, have been released to introduce new features, improve the language, and ensure compatibility across different implementations. Here's an overview of the major C++ standards:\n\n- C++98: The first standardized version of C++ was known as C++98, released in 1998. This version introduced features like namespaces, exceptions, templates, and the Standard Template Library (STL).\n\n- C++11: Released in 2011, C++11 was a major update to the language. It introduced numerous new features, including lambda expressions, smart pointers, range-based for loops, type inference (using the auto keyword), and the constexpr keyword. C++11 aimed to modernize the language and make it more expressive and efficient.\n\n- C++14: Was released in 2014 as a minor update to C++11. It added several small features and improvements, such as binary literals, generic lambdas, relaxed constexpr restrictions, and improved support for variable templates. The focus of C++14 was on refining and enhancing the language rather than introducing major changes.\n\n- C++17: C++17  was released in 2017. It brought several significant features, including structured bindings, if constexpr, constexpr lambdas, inline variables, and a simplified std::variant type. C++17 also introduced parallel algorithms in the STL, filesystem support, and other library enhancements.\n\n- C++20: Released in 2020, C++20 introduced many new features and enhancements. Some notable additions include concepts (a way to specify type requirements), ranges (a new library for working with sequences of values), coroutines, three-way comparisons, and modules (a new way to organize and manage code). C++20 aimed to improve code readability, simplify common tasks, and provide better support for modern programming techniques.\n\nThe evolution of C++ standards is driven by the ISO C++ committee, which consists of experts from academia, industry, and the broader C++ community.\n\n![image](https://github.com/izzypt/CPP/assets/73948790/fc93368b-1e7b-4e5d-bf66-b3e9825c0a3a)\n\n\n# Table of Contents\n\n- [NameSpaces](#namespaces)\n- [Const](#const)\n- [Static](#static)\n- [Typedef and aliases](#typedef)\n- [Stdio Streams](#iostreams)\n- [Containers](#containers)\n- [Arrays and Vectors](#arraysandvectors)\n- [Classes](#oop)\n- [Member Attribute and Member Functions](#members)\n- [Initialization Lists](#initializationlist)\n- [Class Acess Specifiers](#accessspecifiers)\n- Strings in C++\n- [Pointers and References](#pointersandreferences)\n- [Passing by value or reference](#valueorreference)\n- Standard Template Library (STL)\n\n# Usefull references\n\n- https://www.youtube.com/watch?v=-TkoO8Z07hI\u0026t=3178s\u0026ab_channel=BroCode\n\n\u003ca id=\"namespaces\"\u003e\u003c/a\u003e\n# NameSpaces \n\n- NameSpaces provides a solution for preventing name conflicts.\n- Each entity needs a unique name.\n- A namespace allows for identically named entities as long as the namespaces are different.\n\nExamples :\n\n![image](https://github.com/izzypt/CPP/assets/73948790/69f7a8ef-3126-4467-93da-76a25e2e66af)\n\nIt will return 0. If I dont specify the namespace, we will use the local version of an entity.\n\nBut if I specify the namespace of the variable, like this ```\u003cnamespace\u003e::variable``` it will specify the value of the variable within that namespace :\n\n![image](https://github.com/izzypt/CPP/assets/73948790/5d07a8a3-b3d1-4bef-b84f-a851764dbc8b)\n\nIt will return 1 for ```first::x``` and 2 for ```second::x```.\n\n\u003ca id=\"const\"\u003e\u003c/a\u003e\n# Const keyword\n- The const keyword specifies that a variable's value is constant.\n- It tells the compiler to prevent anything from modifying it.\n- https://www.geeksforgeeks.org/const-keyword-in-cpp/\n\nTo create a constant variable, just preceed the datatype declaration of the variable with the ```const``` keyword:\n\n![image](https://github.com/izzypt/CPP/assets/73948790/97418b8e-581e-415f-b3c0-0d422d6dae7a)\n\n- If at some point you or someone else tries to change the value of ```PI```, the compier will throw an error.\n![image](https://github.com/izzypt/CPP/assets/73948790/b20b25d3-a640-4b04-b619-4ffb120c1ad0)\n\n![image](https://github.com/izzypt/CPP/assets/73948790/f4d89687-ea21-45fd-8f7a-6c63f59bd3f1)\n\n**Constant Member Function:**\n\n- When you place `const` after the function name in the function declaration, it indicates that the member function is a constant member function. \n- This means that the function promises not to modify the state of the object on which it is called. In other words, it guarantees that the member function will not modify any non-static data members of the class.\n\n   Example:\n   \n   ```cpp\n   class MyClass {\n   public:\n       void foo() const; // Constant member function\n   };\n   ```\n   \n   In this example, `foo()` is a constant member function of the class `MyClass`.\n\n\n\u003ca id=\"static\"\u003e\u003c/a\u003e\n# Static\n\nIn C++, the keyword \"static\" has multiple uses and can be applied to variables, functions, and class members. Here are the main uses of the \"static\" keyword:\n\n1. Static Variables:\n   - When applied to a variable within a function or method, the \"static\" keyword gives the variable static storage duration. It means that the variable is initialized only once, and its value persists across multiple function calls. The variable retains its value even after the function or method scope is exited.\n\n      ```cpp\n      void countCalls() {\n          static int counter = 0;\n          counter++;\n          cout \u003c\u003c \"Function called \" \u003c\u003c counter \u003c\u003c \" times.\" \u003c\u003c endl;\n      }\n   \n      int main() {\n          countCalls();  // Function called 1 times.\n          countCalls();  // Function called 2 times.\n          countCalls();  // Function called 3 times.\n          return 0;\n      }\n      ```\n   \n      In the example above, the \"counter\" variable inside the \"countCalls\" function is declared as static. It retains its value between function calls and increments with each call. Static variables are often used for maintaining state across function calls or for caching values.\n\n2. Static Functions:\n   - When applied to a function, the \"static\" keyword restricts the function's scope to the translation unit where it is defined. It means the function is not visible outside its source file. Static functions can only be called from other functions within the same file.\n\n      ```cpp\n      static void helperFunction() {\n          // Function implementation\n      }\n   \n      void publicFunction() {\n          // Can call helperFunction()\n      }\n   \n      int main() {\n          // Cannot call helperFunction() here\n          return 0;\n      }\n      ```\n   \n      In the example above, the \"helperFunction\" is declared as static. It can only be called within the same file, typically for internal implementation details or utility functions not intended to be accessed externally.\n\n3. Static Class Members:\n   - When applied to a class member (variable or function), the \"static\" keyword indicates that the member is associated with the class itself rather than with individual objects of the class. Static class members are shared among all instances of the class.\n\n      ```cpp\n      class MyClass {\n      public:\n          static int count;\n      };\n   \n      int MyClass::count = 0;  // Definition and initialization\n   \n      int main() {\n          MyClass obj1;\n          MyClass obj2;\n   \n          obj1.count = 5;\n          cout \u003c\u003c obj2.count \u003c\u003c endl;  // Output: 5\n   \n          return 0;\n      }\n      ```\n   \n      In the example above, the \"count\" variable is declared as a static member of the \"MyClass\" class. It is shared by all instances of the class, and modifying it in one instance reflects the change in other instances. Static class members are often used to store shared data or provide utility functions associated with the class itself rather than individual objects.\n\nThese are the primary uses of the \"static\" keyword in C++. It provides various capabilities such as persistent local variables, restricted visibility of functions, and shared data among class instances.\n\n\u003ca id=\"typedef\"\u003e\u003c/a\u003e\n# Typedef and aliases \n\n- ```typedef``` is  a reserved keyword used to create a new datatype or an additional name (alias) for an existing data type.\n- It helps with readbility and reduces types.\n\n![image](https://github.com/izzypt/CPP/assets/73948790/7d9b8de9-2ab5-4666-ae27-7c30d18b56d0)\n\n- In the picture above, we create the type ```text_t``` which is the same as referencing ```std::string```.\n\nAnother example would be :\n\n![image](https://github.com/izzypt/CPP/assets/73948790/f1b8f7ee-5d47-4e3c-9203-782d72232676)\n\nNowadays, the keyword ```typedef``` has been largely replaced with the ```using``` keyword. (works bettwer w/ templates)\n\n\n![image](https://github.com/izzypt/CPP/assets/73948790/391964bb-5d1b-4697-bd1a-6a539316fab8)\n\n\n- This would do the same job\n\n\u003ca id=\"iostreams\"\u003e\u003c/a\u003e\n# Stdio Streams\n\nLet's explore the iostream library in more detail, focusing on the stream insertion (\u003c\u003c) and stream extraction (\u003e\u003e) operators, as well as formatting options using manipulators.\n\n1. Stream Insertion (\u003c\u003c) Operator:\nThe stream insertion operator (\u003c\u003c) is used for output operations in C++. It allows us to insert data into an output stream, typically std::cout, to be displayed on the console or redirected to another output destination.\n\nExample:\n```cpp\nint number = 10;\nstd::string message = \"Hello, world!\";\n\nstd::cout \u003c\u003c \"The number is: \" \u003c\u003c number \u003c\u003c std::endl;\nstd::cout \u003c\u003c message \u003c\u003c std::endl;\n```\nIn this example, we use the \u003c\u003c operator to insert the value of `number` and the string `message` into the std::cout stream. The output will be:\n```\nThe number is: 10\nHello, world!\n```\n\n2. Stream Extraction (\u003e\u003e) Operator:\nThe stream extraction operator (\u003e\u003e) is used for input operations in C++. It allows us to extract data from an input stream, typically std::cin, to store it in variables for further processing.\n\nExample:\n```cpp\nint age;\nstd::string name;\n\nstd::cout \u003c\u003c \"Enter your age: \";\nstd::cin \u003e\u003e age;\n\nstd::cout \u003c\u003c \"Enter your name: \";\nstd::cin \u003e\u003e name;\n\nstd::cout \u003c\u003c \"Welcome, \" \u003c\u003c name \u003c\u003c \"! You are \" \u003c\u003c age \u003c\u003c \" years old.\" \u003c\u003c std::endl;\n```\nIn this example, we use the \u003e\u003e operator to extract the user's age and name from the std::cin stream. The input provided by the user is stored in the variables `age` and `name`, which are then used for output.\n\n3. Formatting Options with Manipulators:\nManipulators in C++ allow us to modify the format and behavior of the output stream. The iostream library provides several useful manipulators to control various aspects of output formatting. Here are some commonly used manipulators:\n\n- setw: Sets the width of the field for the next output value.\n```cpp\n#include \u003ciomanip\u003e\n\nint number = 42;\nstd::cout \u003c\u003c \"The number is: \" \u003c\u003c std::setw(5) \u003c\u003c number \u003c\u003c std::endl;\n```\nOutput:\n```\nThe number is:    42\n```\n\n- setprecision: Sets the precision (number of decimal places) for floating-point values.\n```cpp\n#include \u003ciomanip\u003e\n\ndouble pi = 3.14159;\nstd::cout \u003c\u003c \"The value of pi is: \" \u003c\u003c std::setprecision(3) \u003c\u003c pi \u003c\u003c std::endl;\n```\nOutput:\n```\nThe value of pi is: 3.14\n```\n\n- fixed: Forces floating-point values to be displayed in fixed-point notation.\n```cpp\n#include \u003ciomanip\u003e\n\ndouble number = 123.456789;\nstd::cout \u003c\u003c \"The number is: \" \u003c\u003c std::fixed \u003c\u003c number \u003c\u003c std::endl;\n```\nOutput:\n```\nThe number is: 123.456789\n```\n\nThese manipulators, along with many others available in the iostream library, provide greater control over the appearance and formatting of the output, making it easier to present data in a desired format.\n\nBy combining the stream insertion (\u003c\u003c) and stream extraction (\u003e\u003e) operators with manipulators, we can perform a wide range of input and output operations while maintaining control over the format and presentation of data.\n\n\u003ca id=\"containers\"\u003e\u003c/a\u003e\n\n# Containers\n\nIn C++, containers are data structures provided by the Standard Template Library (STL) that store and organize collections of objects. \n\nThey are designed to offer different capabilities for storing, accessing, and manipulating data in a generic and efficient manner.\n\nThe C++ STL provides several container classes, each with its own characteristics and use cases. The main container types in the STL are:\n\n1. Sequence Containers:\n   - `std::vector`: A dynamic array-like container that provides fast random access and efficient element insertion and deletion at the end.\n   - `std::deque`: Similar to a vector, but allows efficient element insertion and deletion at both ends.\n   - `std::list`: A doubly-linked list that supports efficient insertion and deletion at any position, but sacrifices random access performance.\n\n2. Associative Containers:\n   - `std::set`: A container that stores unique elements in a sorted order. It uses a binary search tree (usually implemented as a red-black tree) to provide efficient searching, insertion, and deletion.\n   - `std::map`: A container that stores key-value pairs, where keys are unique and stored in a sorted order. It offers efficient searching, insertion, and deletion based on the key.\n   - `std::multiset` and `std::multimap`: Similar to `std::set` and `std::map`, respectively, but allow duplicate elements.\n\n3. Unordered Associative Containers:\n   - `std::unordered_set`: A container that stores unique elements using a hash table. It provides constant-time average complexity for insertion, deletion, and searching.\n   - `std::unordered_map`: A container that stores key-value pairs using a hash table. It offers constant-time average complexity for insertion, deletion, and searching based on the key.\n   - `std::unordered_multiset` and `std::unordered_multimap`: Similar to `std::unordered_set` and `std::unordered_map`, respectively, but allow duplicate elements.\n\n4. Container Adapters:\n   - `std::stack`: Implements a Last-In-First-Out (LIFO) data structure by using an underlying container (e.g., `std::deque` or `std::list`).\n   - `std::queue`: Implements a First-In-First-Out (FIFO) data structure using an underlying container (e.g., `std::deque` or `std::list`).\n   - `std::priority_queue`: Provides a priority queue using an underlying container (e.g., `std::vector`).\n\nThese container classes provide a wide range of functionalities and allow you to choose the most suitable container for your specific needs. They encapsulate complex data structures and algorithms, making it easier to work with and manipulate data in a generic way. The choice of the container depends on the requirements of your program, including the desired operations, performance characteristics, and memory usage.\n\n\u003ca id=\"arraysandvectors\"\u003e\u003c/a\u003e\n# Arrays and vectors\n\nThose are compound types because they are made of another data types.\n\n\u003cins\u003eArrays\u003c/ins\u003e: A data structure that can hold mutiple values acessed by an index number.\n- Collection of elements.\n- All elements are of the same type.\n- Each element can be accessed directly.\n\n![image](https://github.com/izzypt/CPP/assets/73948790/3f7ea465-6d20-4393-82c4-197c21702c5e)\n\n\n\u003ca id=\"oop\"\u003e\u003c/a\u003e\n# Classes and Objects\n\n\u003cins\u003eClasses\u003c/ins\u003e:\n- Blueprints from which objects are created.\n- A user defined data type.\n- It has attributes (data)\n- It has methods (functions)\n- Able to hide data and methods\n- Provides a publid interface\n\n\u003cins\u003eObjects\u003c/ins\u003e:\n- Created from a class\n- Represents an instance of a class\n\n\n## Declaring a class\n\n```\nclass Class_Name\n{\n   //declartion(s);\n}\n```\n\nLet's say we wanted to create a player class :\n\n```\nclass Player\n{\n    //attributes\n    std::string name;\n    int health;\n    int xp;\n\n    //methods\n    void talk(std::string text_to_say);\n    bool is_dead(void);\n}\n```\n\nAnother example. let's say we wanted to create an Account class:\n\n```\nclass Account\n{\n      // Attributes\n      std::string name;\n      double balance;\n\n      // Methods\n      bool withdraw(double amount);\n      bool deposit(double amount);\n}\n```\n\n## Creating objects\n\nIf we use the class we created above, creeating an object would be something like:\n\n\nApproach 1 : Declared just like primitive data types.\n\n```\n      Player frank;\n      Player hero;\n```\n\nApproach 2 : \n- We can also create pointer to objects using the ```new``` keyword. \n- We declare enemy as a pointer to a Player object.\n- The ```new``` keyword, dynamically allocated memory on the heap for the player object. This memory must be freed.\n- To release the memory allocated with ```new```, you need to use the ```delete``` keyword.\n```\nPlayer *enemy = new Player();\ndelete enemy;\n```\n\nNow, let's say we wanted to create objects for our account class :\n\nApproach 1:\n\n```\n      Account frank_account;\n      Account jim_account;\n```\nApproach 2:\n\n```\n      Account *marry_account = new Account();\n      delete mary_account;\n```\n\n\u003ca id=\"members\"\u003e\u003c/a\u003e\n# Member Attributes and Member Functions\n\n- Member attributes, also known as member variables or instance variables, are the data elements associated with an object.\n- They represent the characteristics or properties that define the object's state.\n- These attributes hold values that are unique to each object of a class.\n\n\u003eFor example, in a Person class, member attributes could include the person's name, age, and address.\n\n\u003eEach instance of the Person class will have its own set of attribute values, allowing you to store and access specific data associated with each person object.\n\n____________________________________________________________________________________________________________\n\n- Member functions, also known as methods, are the actions or behaviors that objects of a class can perform.\n- These functions are associated with the class and operate on its member attributes.\n- They encapsulate the logic and operations that manipulate the data and provide functionality to the objects.\n\n\u003eContinuing with the Person class example, member functions could include methods like setName(), setAge(), and getAddress(). These functions allow you to modify or retrieve the attribute values of a Person object.\n\n\u003eMember functions are defined within the class definition and can access the member attributes of the class. They can perform computations, validate data, modify the object's state, or return information based on the object's attributes.\n\n```\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\n\nclass Person {\nprivate:\n    std::string name;\n    int age;\n    std::string address;\n\npublic:\n    void setName(const std::string\u0026 newName) {\n        this-\u003ename = newName;\n    }\n\n    void setAge(int newAge) {\n        this-\u003eage = newAge;\n    }\n\n    void setAddress(const std::string\u0026 newAddress) {\n        this-\u003eaddress = newAddress;\n    }\n\n    std::string getName() const {\n        return this-\u003ename;\n    }\n\n    int getAge() const {\n        return this-\u003eage;\n    }\n\n    std::string getAddress() const {\n        return this-\u003eaddress;\n    }\n};\n\nint main() {\n    Person person1;\n    person1.setName(\"John\");\n    person1.setAge(30);\n    person1.setAddress(\"123 Main St\");\n\n    std::cout \u003c\u003c \"Name: \" \u003c\u003c person1.getName() \u003c\u003c std::endl;\n    std::cout \u003c\u003c \"Age: \" \u003c\u003c person1.getAge() \u003c\u003c std::endl;\n    std::cout \u003c\u003c \"Address: \" \u003c\u003c person1.getAddress() \u003c\u003c std::endl;\n\n    return 0;\n}\n\n```\n\n\u003ca id=\"initializationlist\"\u003e\u003c/a\u003e\n# Initialization List\n\nIn C++, an initialization list is a syntax used in the constructor definition to initialize the member variables of a class. It allows you to provide initial values to the member variables before the constructor body is executed.\n\nThe syntax of an initialization list is as follows:\n\n```cpp\nConstructorName::ConstructorName(parameters) : \n    memberVariable1(initialValue1),\n    memberVariable2(initialValue2),\n    // ...\n{\n    // Constructor body\n}\n```\n\nHere's an example to illustrate the usage of an initialization list:\n\n```cpp\nclass MyClass {\nprivate:\n    int m_value1;\n    double m_value2;\n    std::string m_value3;\n\npublic:\n    MyClass(int value1, double value2, const std::string\u0026 value3)\n        : m_value1(value1), m_value2(value2), m_value3(value3)\n    {\n        // Constructor body (if needed)\n    }\n};\n```\n\nIn this example, the `MyClass` constructor takes three parameters: `value1` of type `int`, `value2` of type `double`, and `value3` of type `const std::string\u0026`.\n\nThe initialization list follows the constructor's parameter list. Each member variable is initialized using the syntax `memberVariable(initialValue)`. In this case, `m_value1` is initialized with `value1`, `m_value2` is initialized with `value2`, and `m_value3` is initialized with `value3`.\n\nThe advantage of using an initialization list is that it allows for direct initialization of member variables and can improve performance by avoiding unnecessary default constructions and assignments. \n\nIt is particularly useful for initializing member variables that are of non-default constructible types or that are declared as `const` or reference types.\n\nBy using an initialization list, you ensure that the member variables are properly initialized before the constructor body is executed, providing a clean and efficient way to initialize your class's member variables.\n\n\u003ca id=\"accessspecifiers\"\u003e\u003c/a\u003e\n\n# Class access specifiers\n\nIn object-oriented programming (OOP) with C++, the `public`, `private`, and `protected` keywords are used as access specifiers in a class definition to control the visibility and accessibility of class members (variables and functions) from within the class and its derived classes. These access specifiers define the level of encapsulation and inheritance relationships between classes. Here's a brief explanation of each:\n\n1. `public`:\n\n   Members declared as `public` are accessible from anywhere in the program, including outside the class. They have no access restrictions, meaning they can be accessed freely by any part of the program.\n   \n   Example:\n   ```cpp\n   class MyClass {\n   public:\n       int publicVariable;\n       void publicFunction() { /* ... */ }\n   };\n   ```\n   \n   In this example, `publicVariable` and `publicFunction()` are accessible from any part of the program, even outside the class.\n\n2. `private`:\n   \n   Members declared as `private` are only accessible from within the class itself. They are not accessible from outside the class or its derived classes. This provides strong encapsulation, as the implementation details of the class are hidden from external entities.\n   \n   Example:\n   ```cpp\n   class MyClass {\n   private:\n       int privateVariable;\n       void privateFunction() { /* ... */ }\n   };\n   ```\n   \n   In this example, `privateVariable` and `privateFunction()` can only be accessed and used within the class `MyClass`.\n\n3. `protected`:\n\n   Members declared as `protected` are similar to `private` members, but they are also accessible by derived classes. This means that the derived class can access the protected members of its base class.\n   \n   Example:\n   ```cpp\n   class MyBaseClass {\n   protected:\n       int protectedVariable;\n       void protectedFunction() { /* ... */ }\n   };\n   \n   class MyDerivedClass : public MyBaseClass {\n   public:\n       void accessProtectedMember() {\n           protectedVariable = 42; // Can access protected member from the base class.\n           protectedFunction();    // Can call protected member function from the base class.\n       }\n   };\n   ```\n   \n   In this example, `MyDerivedClass` can access `protectedVariable` and `protectedFunction()` from `MyBaseClass`, but these members are still not accessible outside of the `MyBaseClass` or its derived classes.\n\nTo summarize, the access specifiers in C++ allow you to control the visibility of class members, providing a way to enforce data encapsulation and manage class relationships in terms of inheritance. Good design practice often involves making data members private and exposing them through public member functions (getters and setters) to control access to the class's internal state while maintaining data integrity.\n\n\u003ca id=\"pointersandreferences\"\u003e\u003c/a\u003e\n\n# Pointers and References\n\nPointers:\n- Pointers are variables that store memory addresses.\n- They are denoted by the asterisk (*) symbol when declaring the pointer type.\n- Pointers can be assigned the address of another object or null (nullptr) if they don't currently point to any valid memory location.\n- Pointers can be reassigned to point to different objects throughout their lifetime.\n- Dereferencing a pointer (using the asterisk (*) operator) allows access to the value stored at the memory address it points to.\n- Pointers can be explicitly modified to point to a different object or memory location.\n- Pointers can be used for dynamic memory allocation and deallocation using `new` and `delete` operators (in C++).\n\nReferences:\n- References are aliases or alternative names for existing objects.\n- They are declared by using an ampersand (\u0026) symbol after the type name.\n- References must be initialized when declared and cannot be null.\n- Once a reference is bound to an object, it cannot be changed to refer to another object.\n- References provide a convenient and cleaner syntax for accessing and manipulating objects without needing to use pointer dereferencing syntax.\n- They are useful when you want to pass objects to functions by reference, avoiding object copying.\n- References cannot be reassigned to refer to different objects.\n\nWhen to use pointers:\n- When you need the flexibility to dynamically allocate and deallocate memory (e.g., using `new` and `delete`).\n- When you want to represent the absence of an object by assigning null (nullptr) to the pointer.\n- When you need to change the pointed object during runtime.\n- When implementing data structures or algorithms that require fine-grained control over memory and object lifetimes.\n\nWhen to use references:\n- When you want a convenient way to access and modify an existing object without explicitly using pointer dereferencing syntax.\n- When passing objects to functions, particularly large objects, to avoid the overhead of object copying.\n- When implementing operator overloading to provide a more intuitive syntax.\n\nIt's worth noting that references can also be used in certain cases where pointers are used, but references offer a safer and more convenient alternative when there is no need for the flexibility provided by pointers.\n\n\u003ca id=\"valueorreference\"\u003e\u003c/a\u003e\n# Passing by value or reference\n\nPassing data between functions or objects in C++ can be done either by value or by reference.\n\n1. Passing by Value:\n   - When passing data by value, a copy of the data is made and passed to the function or object. Any modifications made to the copied data within the function or object do not affect the original data in the calling context.\n\n      Example:\n   \n      ```cpp\n      void increment(int value) {\n          value++;  // Increment the copied value\n      }\n   \n      int main() {\n          int num = 5;\n          increment(num);\n          // The value of 'num' remains unchanged (still 5)\n          return 0;\n      }\n      ```\n   \n      In the example above, the function `increment` takes an integer parameter `value` by value. Any changes made to `value` inside the function do not affect the original `num` variable in `main()`. This method is useful when you want to work with a local copy of the data without modifying the original value.\n\n3. Passing by Reference:\n   - When passing data by reference, instead of making a copy, a reference or pointer to the original data is passed. This allows the function or object to directly access and modify the original data.\n\n      Example:\n   \n      ```cpp\n      void increment(int\u0026 value) {\n          value++;  // Increment the original value\n      }\n   \n      int main() {\n          int num = 5;\n          increment(num);\n          // The value of 'num' is modified to 6\n          return 0;\n      }\n      ```\n   \n      In this example, the function `increment` takes an integer reference `value`. Any modifications made to `value` inside the function directly affect the original `num` variable in `main()`. This method is useful when you want to modify the original data or avoid making unnecessary copies of large objects.\n\nImplications:\n\n- Passing by value: Creating a copy of the data can have performance implications, especially for large objects or when called frequently. However, it ensures data immutability and avoids unintended side effects on the original data.\n- Passing by reference: It allows direct modification of the original data and can be more efficient for large objects. However, it requires caution since modifications made inside the function affect the original data, potentially leading to unexpected behavior.\n\nIn general, passing by value is suitable for small, immutable data types, while passing by reference is more efficient for larger objects that need modification. The choice between the two methods depends on the specific requirements of the program and the desired behavior when interacting with data.\n\nThis video also explains more about it : https://www.youtube.com/watch?v=wro8Bb6JnwU\u0026ab_channel=LowLevelLearning\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizzypt%2Fcpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fizzypt%2Fcpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizzypt%2Fcpp/lists"}