{"id":20345813,"url":"https://github.com/izzypt/cpp-module-04","last_synced_at":"2026-05-10T10:49:44.902Z","repository":{"id":181219702,"uuid":"666421869","full_name":"izzypt/CPP-Module-04","owner":"izzypt","description":"Subtype polymorphism, abstract classes, interfaces","archived":false,"fork":false,"pushed_at":"2023-07-18T17:41:09.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-14T21:46:37.796Z","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-14T13:28:27.000Z","updated_at":"2023-07-17T22:01:25.000Z","dependencies_parsed_at":"2025-01-14T21:34:46.577Z","dependency_job_id":"94481b00-791c-47ac-8f79-7f23a6a28ff1","html_url":"https://github.com/izzypt/CPP-Module-04","commit_stats":null,"previous_names":["izzypt/cpp-module-04"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-04","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-04/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-04/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP-Module-04/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/izzypt","download_url":"https://codeload.github.com/izzypt/CPP-Module-04/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241876623,"owners_count":20035396,"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:52.317Z","updated_at":"2026-05-10T10:49:39.875Z","avatar_url":"https://github.com/izzypt.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CPP-Module-04\n\n### Subtype polymorphism, abstract classes, interfaces\n\n----------------------------------------------------------------------------------------------\n\n\nIn C++, abstract classes and interfaces are both used to define common behaviors and create a blueprint for derived classes. \n\nThey are similar in concept but differ in their implementation and purpose.\n\n# Abstract Classes:\n\n  - An abstract class is a class that cannot be instantiated directly.\n  - It serves as a base class for other classes and provides a common interface for derived classes to inherit from.\n  - An abstract class typically contains one or more pure virtual functions, which are declared using the syntax \"= 0\" at the end of the function declaration.\n  - These pure virtual functions have no implementation in the abstract class and must be overridden by any derived class that inherits from the abstract class.\n\n    Here's an example of an abstract class in C++:\n    \n    ```cpp\n    class Shape {\n    public:\n        virtual double calculateArea() const = 0; // Pure virtual function\n        virtual void print() const = 0; // Pure virtual function\n    \n        // Concrete member function\n        void displayArea() const {\n            std::cout \u003c\u003c \"Area: \" \u003c\u003c calculateArea() \u003c\u003c std::endl;\n        }\n    };\n    ```\n    \n    In this example, the `Shape` class is an abstract class that declares two pure virtual functions: `calculateArea()` and `print()`.\n    \n    Any derived class inheriting from `Shape` must provide implementations for these functions. The `displayArea()` function is a concrete member function that can be used by any derived class and calls the `calculateArea()` function.\n    \n    Abstract classes are useful when you want to define a common interface and provide some default behavior, while leaving certain methods to be implemented by the derived classes. They are often used to create hierarchies and define common characteristics for a group of related classes.\n\n# Interfaces:\n\n- In C++, interfaces are not explicitly defined as a language construct, unlike some other programming languages like Java. \n\n- However, the concept of an interface can be achieved by using abstract classes with pure virtual functions only, similar to the example above. An interface is essentially an abstract class with only pure virtual functions and no concrete member functions.\n\n    Here's an example of an \"interface\" using an abstract class:\n    \n    ```cpp\n    class Printable {\n    public:\n        virtual void print() const = 0; // Pure virtual function\n    };\n    ```\n    \n    In this example, the `Printable` class acts as an interface, providing a common behavior of being printable.\n\n    Any class that wants to be printable can inherit from this interface and must provide an implementation for the `print()` function.\n    \n    Interfaces are useful when you want to define a contract or a set of methods that a class must implement, without providing any default behavior. By enforcing the implementation of specific functions, interfaces allow for polymorphism and loose coupling between different components of a system.\n    \n    It's important to note that in C++, a class can inherit from multiple interfaces or abstract classes, but it can only inherit from a single concrete (non-abstract) class.\n\n\n# Override keyword\n\n\u003e The `override` keyword in C++ is used to explicitly indicate that a member function in a derived class is intended to override a virtual function from its base class.\n\nIt serves as a safety mechanism to ensure that the intended function overriding is indeed taking place and helps catch potential bugs during compilation.\n\nWhen you mark a function with the `override` keyword, the compiler will check if the function is actually overriding a virtual function in the base class. \n\n\u003e If the function does not match the signature of any virtual function in the base class, the compiler will generate an error. This helps prevent accidental mistakes, such as typos in function names or mismatched function signatures.\n\n```cpp\nclass Base {\npublic:\n    virtual void foo() const {\n        std::cout \u003c\u003c \"Base::foo()\\n\";\n    }\n};\n\nclass Derived : public Base {\npublic:\n    void foo() const override {\n        std::cout \u003c\u003c \"Derived::foo()\\n\";\n    }\n};\n```\n\n  In this example, the `Base` class has a virtual function `foo()`. \n  \n  The `Derived` class inherits from `Base` and overrides the `foo()` function using the `override` keyword.\n  \n  Now, if you accidentally make a mistake in the `Derived` class, such as misspelling the function name or using the wrong function signature, the compiler will catch it and issue an error. For example:\n\n```cpp\nclass Derived : public Base {\npublic:\n    void fooo() const override { // Incorrect function name\n        std::cout \u003c\u003c \"Derived::foo()\\n\";\n    }\n};\n```\n\nWith the `override` keyword, the compiler will raise an error like:\n\n```\nerror: 'void Derived::fooo() const' marked 'override', but does not override\n```\n\nIn summary, you should use the `override` keyword whenever you want to override a virtual function from the base class in a derived class. It's a good practice to explicitly specify your intent to override, which helps improve code readability and maintainability while catching potential errors early in the development process.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizzypt%2Fcpp-module-04","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fizzypt%2Fcpp-module-04","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizzypt%2Fcpp-module-04/lists"}