{"id":20345810,"url":"https://github.com/izzypt/cpp-module-01","last_synced_at":"2025-06-25T06:39:23.603Z","repository":{"id":179513369,"uuid":"662141652","full_name":"izzypt/CPP-Module-01","owner":"izzypt","description":"This module is designed to help you understand the memory allocation, reference, pointers to members and the usage of the switch in CPP.","archived":false,"fork":false,"pushed_at":"2023-08-24T15:55:01.000Z","size":42,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-04T15:48:02.355Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"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":null,"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-04T12:51:08.000Z","updated_at":"2023-07-07T17:52:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"ccb0f73e-31df-497e-a9b9-10d2e6ba6d8b","html_url":"https://github.com/izzypt/CPP-Module-01","commit_stats":null,"previous_names":["izzypt/cpp-module-01"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/izzypt/CPP-Module-01","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-01","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-01/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-01/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-01/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/izzypt","download_url":"https://codeload.github.com/izzypt/CPP-Module-01/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-01/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261821399,"owners_count":23214886,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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.338Z","updated_at":"2025-06-25T06:39:23.570Z","avatar_url":"https://github.com/izzypt.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CPP-Module-01\nThis module is designed to help you understand the memory allocation, reference, pointers to members and the usage of the switch in CPP.\n\n# Exercise 02 Notes :\n\n```cpp\n#include \u003ciostream\u003e\nusing namespace std;\n\nint main() {\n    string str = \"HI THIS IS BRAIN\";\n    string* stringPTR = \u0026str;\n    string\u0026 stringREF = str;\n\n    cout \u003c\u003c \"Memory address of the string variable: \" \u003c\u003c \u0026str \u003c\u003c endl;\n    cout \u003c\u003c \"Memory address held by stringPTR: \" \u003c\u003c stringPTR \u003c\u003c endl;\n    cout \u003c\u003c \"Memory address held by stringREF: \" \u003c\u003c \u0026stringREF \u003c\u003c endl;\n\n    cout \u003c\u003c \"Value of the string variable: \" \u003c\u003c str \u003c\u003c endl;\n    cout \u003c\u003c \"Value pointed to by stringPTR: \" \u003c\u003c *stringPTR \u003c\u003c endl;\n    cout \u003c\u003c \"Value pointed to by stringREF: \" \u003c\u003c stringREF \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n- We define a string variable `str` and initialize it with the value \"HI THIS IS BRAIN\".\n- `stringPTR` is a pointer to the string, and we initialize it with the memory address of `str` using the `\u0026` operator.\n- `stringREF` is a reference to the string, and we initialize it to `str` directly.\n- The memory addresses of `str`, `stringPTR`, and `stringREF` are printed using the address-of operator `\u0026`.\n- The values of `str`, `*stringPTR` (value pointed by `stringPTR`), and `stringREF` are printed.\n\n\n# Exercise 03 Notes:\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.\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# Exercise 04 Notes:\n\nA brief introduction to the basics of `std::fstream` in C++, including how to include the necessary headers, open a file, read from a file, and write to a file.\n\n1. Including the necessary headers:\nTo use `std::fstream`, you need to include the `\u003cfstream\u003e` header file:\n\n```cpp\n#include \u003cfstream\u003e\n```\n\n2. Opening a file:\nTo open a file for reading, writing, or both, you can create an instance of `std::ifstream`, `std::ofstream`, or `std::fstream`, respectively. Then, you can use the `open()` member function to open the file:\n\n```cpp\n#include \u003cfstream\u003e\n\nint main() {\n    std::ifstream inputFile;\n    inputFile.open(\"input.txt\"); // Open the file for reading\n\n    std::ofstream outputFile;\n    outputFile.open(\"output.txt\"); // Open the file for writing\n\n    std::fstream file;\n    file.open(\"data.txt\", std::ios::in | std::ios::out); // Open the file for reading and writing\n\n    // ...\n\n    return 0;\n}\n```\n\n3. Reading from a file:\nTo read from a file, you can use the `\u003e\u003e` operator or the `getline()` function to extract data from the file stream:\n\n```cpp\n#include \u003cfstream\u003e\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\n\nint main() {\n    std::ifstream file(\"data.txt\"); // Open the file for reading\n\n    if (file.is_open()) {\n        int number;\n        file \u003e\u003e number; // Read an integer from the file\n\n        std::string line;\n        std::getline(file, line); // Read a line from the file\n\n        // Process the read data\n        std::cout \u003c\u003c \"Number: \" \u003c\u003c number \u003c\u003c std::endl;\n        std::cout \u003c\u003c \"Line: \" \u003c\u003c line \u003c\u003c std::endl;\n\n        file.close();\n    }\n    else {\n        // Failed to open the file\n        // Handle the error accordingly\n    }\n\n    return 0;\n}\n```\n\n4. Writing to a file:\nTo write to a file, you can use the `\u003c\u003c` operator or the `write()` function to insert data into the file stream:\n\n```cpp\n#include \u003cfstream\u003e\n\nint main() {\n    std::ofstream file(\"output.txt\"); // Open the file for writing\n\n    if (file.is_open()) {\n        int number = 42;\n        file \u003c\u003c number \u003c\u003c std::endl; // Write an integer to the file\n\n        std::string line = \"Hello, World!\";\n        file.write(line.c_str(), line.size()); // Write a string to the file\n\n        file.close();\n    }\n    else {\n        // Failed to open the file\n        // Handle the error accordingly\n    }\n\n    return 0;\n}\n```\n\nThese examples demonstrate the basic usage of `std::fstream` for file input and output operations in C++. Remember to handle any potential errors when opening or operating on files and to close the file when you're done working with it using the `close()` member function.\n\n# Exercise 05 Notes\n\nIn C++, there are several ways to use pointers to member functions. Here are the main ways to work with pointers to member functions:\n\n1. \u003cins\u003eFunction Pointers to Static Member Functions\u003c/ins\u003e:\n\n   - C++ allows you to declare function pointers that point to static member functions.\n   - Static member functions are associated with the class itself rather than specific instances of the class. The syntax for declaring a function pointer to a static member function is as follows:\n   \n   ```cpp\n   returnType (*functionPointer)(arguments);\n   ```\n   \n   Here's an example:\n   \n   ```cpp\n   class MyClass {\n   public:\n       static void staticMemberFunction(int arg) {\n           // Code here\n       }\n   };\n\n   int main() {\n       void (*functionPtr)(int) = \u0026MyClass::staticMemberFunction;\n       // Use functionPtr to call staticMemberFunction\n\n       return 0;\n   }\n   ```\n\n2. \u003cins\u003ePointers to Non-Static Member Functions\u003c/ins\u003e:\n   \n   - Pointers to non-static member functions in C++ are a bit more complex than function pointers to static member functions.\n   - Since non-static member functions are associated with specific instances of a class, you need an instance (object) of the class to call the member function via the pointer. The syntax for declaring a pointer to a non-static member function is as follows:\n\n   ```cpp\n   returnType (className::*functionPointer)(arguments);\n   ```\n\n   Here's an example:\n\n   ```cpp\n   class MyClass {\n   public:\n       void memberFunction(int arg) {\n           // Code here\n       }\n   };\n\n   int main() {\n       void (MyClass::*functionPtr)(int) = \u0026MyClass::memberFunction;\n       MyClass obj;\n       // Call memberFunction using the object and functionPtr\n       (obj.*functionPtr)(42);\n\n       return 0;\n   }\n   ```\n\n4. \u003cins\u003estd::function with Member Functions\u003c/ins\u003e:\n\n   - The C++ Standard Library provides `std::function`, which is a general-purpose polymorphic function wrapper.\n   - It can be used to store and invoke member functions. `std::function` provides more flexibility and type safety compared to function pointers. Here's an example:\n\n   ```cpp\n   #include \u003ciostream\u003e\n   #include \u003cfunctional\u003e\n\n   class MyClass {\n   public:\n       void memberFunction(int arg) {\n           // Code here\n           std::cout \u003c\u003c \"Member function called with argument: \" \u003c\u003c arg \u003c\u003c std::endl;\n       }\n   };\n\n   int main() {\n       MyClass obj;\n       std::function\u003cvoid(MyClass*, int)\u003e functionWrapper = \u0026MyClass::memberFunction;\n       functionWrapper(\u0026obj, 42);  // Call memberFunction via the functionWrapper\n\n       return 0;\n   }\n   ```\n\n   In this example, `std::function` is used to store a member function pointer along with the object pointer (`MyClass*`). The stored function can then be invoked later by calling `functionWrapper(\u0026obj, 42)`.\n\nThese are the main ways to work with pointers to member functions in C++. Depending on your requirements and use case, you can choose the appropriate method that suits your needs.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizzypt%2Fcpp-module-01","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fizzypt%2Fcpp-module-01","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizzypt%2Fcpp-module-01/lists"}