{"id":27125058,"url":"https://github.com/georgiirocket/cpp-part-1","last_synced_at":"2025-04-07T14:29:08.144Z","repository":{"id":284931504,"uuid":"956511482","full_name":"georgiirocket/cpp-part-1","owner":"georgiirocket","description":"Class, Template","archived":false,"fork":false,"pushed_at":"2025-04-04T14:48:59.000Z","size":34,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T15:28:30.508Z","etag":null,"topics":["cpp"],"latest_commit_sha":null,"homepage":"","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/georgiirocket.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":"2025-03-28T11:28:22.000Z","updated_at":"2025-04-04T14:49:03.000Z","dependencies_parsed_at":"2025-04-04T15:24:42.810Z","dependency_job_id":null,"html_url":"https://github.com/georgiirocket/cpp-part-1","commit_stats":null,"previous_names":["georgiirocket/cpp-part-1"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/georgiirocket%2Fcpp-part-1","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/georgiirocket%2Fcpp-part-1/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/georgiirocket%2Fcpp-part-1/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/georgiirocket%2Fcpp-part-1/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/georgiirocket","download_url":"https://codeload.github.com/georgiirocket/cpp-part-1/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247669118,"owners_count":20976328,"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":["cpp"],"created_at":"2025-04-07T14:29:07.507Z","updated_at":"2025-04-07T14:29:08.112Z","avatar_url":"https://github.com/georgiirocket.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Class\n\n```c++\n#include \u003ciostream\u003e\nusing namespace std;\n\nclass Student {\npublic:\n    string name;\n    int age;\n\n    Student(string n, int a) {\n        name = n;\n        age = a;\n    }\n\n    ~Student() {\n        cout \u003c\u003c \"Destructor  works\" \u003c\u003c endl;\n    }\n    \n    void talk() {\n        cout \u003c\u003c \"Hello, my name is \" \u003c\u003c name \u003c\u003c endl;\n    }\n\n    int getAge() {\n        return age;\n    }\n};\n\nint main() {\n    Student student1(\"Parker\", 20);\n\n    student1.talk();\n\n    return 0;\n}\n```\n\n## This\n\n```c++\n#include \u003ciostream\u003e\nusing namespace std;\n\nclass Person {\npublic:\n    string name;\n    int age;\n    \n    Person(string name, int age) {\n        this-\u003ename = name;\n        this-\u003eage = age;\n    }\n\n    int getAge() const {\n        return this-\u003eage;\n    }\n\n    void setAge(int age) {\n        this-\u003eage = age;\n    }\n};\n\nint main() {\n    Person person(\"Patrik\", 10);\n\n    cout \u003c\u003c person.getAge() \u003c\u003c endl;\n    \n    person.setAge(15);\n\n    cout \u003c\u003c person.getAge() \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n## Friend function\n\n```c++\n#include \u003ciostream\u003e\nusing namespace std;\n\nclass Person {\nprivate:\n    string name;\n    int age;\n\npublic:\n    Person(string name, int age) {\n        this-\u003ename = name;\n        this-\u003eage = age;\n    }\n\n    int getAge() const {\n        return this-\u003eage;\n    }\n\n    void setAge(int age) {\n        this-\u003eage = age;\n    }\n\n    friend void print(const Person \u0026p);\n};\n\nvoid print(const Person \u0026p) {\n    cout \u003c\u003c p.name \u003c\u003c \" \" \u003c\u003c p.age \u003c\u003c endl;\n}\n\nint main() {\n    Person person(\"Patrik\", 10);\n\n    print(person);\n\n    return 0;\n}\n```\n\n## Friend class\n\n```c++\n#include \u003ciostream\u003e\nusing namespace std;\n\nclass Person {\nprivate:\n    string name;\n    int age;\n\npublic:\n    Person(string name, int age) {\n        this-\u003ename = name;\n        this-\u003eage = age;\n    }\n\n    int getAge() const {\n        return this-\u003eage;\n    }\n\n    void setAge(int age) {\n        this-\u003eage = age;\n    }\n\n    friend class Printer;\n};\n\nclass Printer {\npublic:\n    void print(const Person \u0026p) {\n        cout \u003c\u003c p.name \u003c\u003c \" \" \u003c\u003c p.age \u003c\u003c endl;\n    }\n};\n\n\n\nint main() {\n    Person person(\"Patrik\", 10);\n\n    Printer printer;\n    printer.print(person);\n\n    return 0;\n}\n```\n\n## Static methods\n\n```c++\n#include \u003ciostream\u003e\nusing namespace std;\n\nclass Counter {\nprivate:\n    static int count;\npublic:\n    Counter() {\n        count++;\n    }\n\n    static int getCount() {\n        return count;\n    }\n};\n\nint Counter::count = 0;\n\n\n\nint main() {\n    Counter c1;\n    \n\n    cout \u003c\u003c c1.getCount() \u003c\u003c endl;\n    Counter c2;\n\n    cout \u003c\u003c c2.getCount() \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n## Inline varible\n\n```c++\n#include \u003ciostream\u003e\nusing namespace std;\n\nclass Item {\nprivate:\n    static inline int count = 0;\n    int id;\n\npublic:\n    Item() {\n        count++;\n        this-\u003eid = count;\n    }\n\n    static int getCount() {\n        return count;\n    }\n\n    int getId() const {\n        return this-\u003eid;\n    }\n};\n\nint main() {\n    for(int i = 0; i \u003c 5; i++) {\n        Item item;\n\n        cout \u003c\u003c \"Id: \" \u003c\u003c item.getId() \u003c\u003c endl;\n        cout \u003c\u003c \"Count: \" \u003c\u003c item.getCount() \u003c\u003c endl;\n    }\n\n    return 0;\n}\n```\n\n## Enum\n\n```c++\n#include \u003ciostream\u003e\nusing namespace std;\n\nenum Day {\n    Sunday,\n    Monday\n};\n\nint main() {\n    Day one = Sunday;\n\n    cout \u003c\u003c one \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n## Class extends\n\n```c++\n#include \u003ciostream\u003e\nusing namespace std;\n\nclass Shape {\npublic:\n    string color;\n\n    Shape(string color) {\n        this-\u003ecolor = color;\n    }\n\n    ~Shape() {\n        cout \u003c\u003c \"Descructor shape\" \u003c\u003c endl;\n    }\n    \n    void info() {\n        cout \u003c\u003c \"Color: \" \u003c\u003c color \u003c\u003c endl;\n    }\n};\n\n//public Shape, protected Shape, privat shape\nclass Circle : public Shape {\npublic: \n    int radius;\n\n    Circle(string color, int radius) : Shape(color) {\n        this-\u003eradius = radius;\n    }\n\n    ~Circle() {\n        cout \u003c\u003c \"Descructor circle\" \u003c\u003c endl;\n    }\n\n    void print() {\n        this-\u003einfo();\n        cout \u003c\u003c \"Radius: \" \u003c\u003c this-\u003eradius \u003c\u003c endl;\n    }\n};\n\nint main() {\n    Circle c(\"red\", 10);\n\n    c.print();\n    return 0;\n}\n```\n\n## Virtual, override\n\n```c++\n#include \u003ciostream\u003e\n#include \u003ccmath\u003e\nusing namespace std;\n\n\nclass Shape {\n    private:\n        string color;\n\n    virtual double calculateArea() {\n        return 0.0;\n    }    \n};\n\nclass Circle : public Shape {\n    public:\n        int radius; \n\n    double calculateArea() override {\n        return 3.14 * pow(this-\u003eradius, 2);\n    }       \n};\n\nclass Rectangle : public Shape {\n    public:\n        int width;\n        int height;\n\n    double calculateArea() override {\n        return this-\u003ewidth * this-\u003eheight;\n    }         \n};\n\nint main() {\n    return 0;\n}\n```\n\n## Polymorphism\n\n```c++\n#include \u003ciostream\u003e\n#include \u003ccmath\u003e\nusing namespace std;\n\n\nclass Shape {\n    public:\n        string color;\n\n    virtual double calculateArea() {\n        return 0.0;\n    }    \n};\n\nclass Circle : public Shape {\n    public:\n        int radius = 5; \n\n    double calculateArea() override {\n        return 3.14 * pow(this-\u003eradius, 2);\n    }       \n};\n\nclass Rectangle : public Shape {\n    public:\n        int width = 2;\n        int height = 3;\n\n    double calculateArea() override {\n        return this-\u003ewidth * this-\u003eheight;\n    }         \n};\n\nint main() {\n    Shape* shape1 = new Circle();\n    Shape* shape2 = new Rectangle();\n\n    cout \u003c\u003c shape1-\u003ecalculateArea() \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n## Abstract class, clear virtual method\n\n```c++\n#include \u003ciostream\u003e\n#include \u003ccmath\u003e\nusing namespace std;\n\nclass Shape {\n    public:\n        virtual int calculateArea() = 0;\n    };\n    \nclass Circle : public Shape {\n    private:\n        int radius;\n        \n    public:\n        Circle(int r) {\n            radius = r;\n        }\n        \n        int calculateArea() override {\n            return 3.14 * radius * radius;\n        }\n};\n    \nclass Rectangle : public Shape {\n    private:\n        int a;\n        int b;\n        \n    public:\n        Rectangle(int a, int b) {\n            this-\u003ea = a;\n            this-\u003eb = b;\n        }\n        \n        int calculateArea() override {\n            return a * b;\n        }\n};\n\nint main() {\n    Circle c(10);\n    Rectangle r(2,3);\n\n    cout \u003c\u003c  c.calculateArea() \u003c\u003c endl;\n    cout \u003c\u003c  r.calculateArea() \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n## Interfaces\n\n```c++\n#include \u003ciostream\u003e\n#include \u003ccmath\u003e\nusing namespace std;\n\nclass Shape {\n    public:\n        virtual int calculateArea() = 0;\n        virtual void info() = 0;\n    };\n    \nclass Circle : public Shape {\n    private:\n        int radius;\n        \n    public:\n        Circle(int r) {\n            radius = r;\n        }\n        \n        int calculateArea() override {\n            return 3.14 * radius * radius;\n        }\n\n        void info() override {\n            cout \u003c\u003c \"Radius: \" \u003c\u003c this-\u003eradius \u003c\u003c endl;\n        }\n};\n    \nclass Rectangle : public Shape {\n    private:\n        int a;\n        int b;\n        \n    public:\n        Rectangle(int a, int b) {\n            this-\u003ea = a;\n            this-\u003eb = b;\n        }\n        \n        int calculateArea() override {\n            return a * b;\n        }\n\n        void info() override {\n            cout \u003c\u003c \"A: \" \u003c\u003c this-\u003ea \u003c\u003c endl;\n            cout \u003c\u003c \"B: \" \u003c\u003c this-\u003eb \u003c\u003c endl;\n        }\n};\n\nint main() {\n    Circle c(10);\n    Rectangle r(2,3);\n\n    cout \u003c\u003c  c.calculateArea() \u003c\u003c endl;\n    cout \u003c\u003c  r.calculateArea() \u003c\u003c endl;\n\n    c.info();\n    r.info();\n\n    return 0;\n}\n```\n\n## Typeid info\n\n```c++\n#include \u003ciostream\u003e\n#include \u003ccmath\u003e\n#include \u003ctypeinfo\u003e\nusing namespace std;\n\nclass Animal {\n    public: \n        virtual void print() {\n            cout \u003c\u003c \"Object type: \" \u003c\u003c typeid(*this).name() \u003c\u003c endl;\n        }\n};\n\nclass Dog : public Animal {};\nclass Cat : public Animal {};\n\nint main() {\n    Animal* d = new Dog;\n    Animal* c = new Cat;\n\n    d-\u003eprint();\n    c-\u003eprint();\n\n    return 0;\n}\n```\n\n## Rtti, dynamic_cast\n\n```c++\n#include \u003ciostream\u003e\n#include \u003ccmath\u003e\n#include \u003ctypeinfo\u003e\nusing namespace std;\n\nclass Animal {\n    public: \n        virtual void print() {\n            cout \u003c\u003c \"Object type: \" \u003c\u003c typeid(*this).name() \u003c\u003c endl;\n        }\n};\n\nclass Dog : public Animal {};\nclass Cat : public Animal {};\n\nint main() {\n    Animal* d = new Dog;\n    Animal* c = new Cat;\n\n    Cat* cat = dynamic_cast\u003cCat*\u003e(c);\n    Animal* c = dynamic_cast\u003cAnimal*\u003e(cat);\n\n    d-\u003eprint();\n    c-\u003eprint();\n\n    return 0;\n}\n```\n\n## Composition\n\n```c++\n#include \u003ciostream\u003e\n#include \u003ccmath\u003e\n#include \u003ctypeinfo\u003e\nusing namespace std;\n\nclass Engine {\n    public:\n        void start() {\n            cout \u003c\u003c \"Engine is on\" \u003c\u003c endl;\n        }\n};\n\nclass Car {\n    private: \n        Engine engine;\n\n    public:\n        void startCar() {\n            engine.start();\n            cout \u003c\u003c \"Car is on\" \u003c\u003c endl;\n        }    \n};\n\nint main() {\n    Car car;\n\n    car.startCar();\n\n    return 0;\n}\n```\n\n## Agregate\n\n```c++\n#include \u003ciostream\u003e\n#include \u003ccmath\u003e\n#include \u003ctypeinfo\u003e\nusing namespace std;\n\nclass Author {\n    public:\n        Author(const string\u0026 name) : name(name) {}\n    \n    private: \n        string name;    \n};\n\nclass Book {\n    private:\n        string title;\n        Author\u0026 author;\n    \n    public:\n        Book(const string\u0026 title, Author\u0026 author): title(title), author(author) {}    \n};\n\nint main() {\n    Author author(\"Patrik\");\n    Book book(\"My book\", author);\n\n\n    return 0;\n}\n```\n\n## Reload operators\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\nclass FranctionalNumber {\n    private: \n        int numerator;\n        int denominator;\n\n    public:\n        FranctionalNumber(int num, int den) : numerator(num), denominator(den) {}\n\n    FranctionalNumber operator+(const FranctionalNumber\u0026 other) {\n        int num = this-\u003enumerator * other.denominator + this-\u003edenominator * other.numerator;\n        int den = this-\u003edenominator * other.denominator;\n\n        return FranctionalNumber(num, den);\n    }\n\n    FranctionalNumber operator++() {\n        this-\u003enumerator += this-\u003edenominator;\n\n        return *this;\n    }\n\n    FranctionalNumber operator++(int) {\n        FranctionalNumber temp = *this;\n\n        this-\u003enumerator += this-\u003edenominator;\n\n        return temp;\n    }\n\n    bool operator==(const FranctionalNumber\u0026 f) {\n        if(this-\u003enumerator == f.numerator \u0026\u0026 this-\u003edenominator == f.denominator) {\n            return true;\n        }\n\n        return false;\n    }\n\n    friend ostream\u0026 operator\u003c\u003c(ostream\u0026 out, FranctionalNumber\u0026 f);\n    friend istream\u0026 operator\u003e\u003e(istream\u0026 in, FranctionalNumber\u0026 f);\n};\n\nostream\u0026 operator\u003c\u003c(ostream\u0026 out, FranctionalNumber\u0026 f) {\n    out \u003c\u003c f.numerator \u003c\u003c \" / \" \u003c\u003c f.denominator;\n\n    return out;\n}\n\n//With empty constructor\nistream\u0026 operator\u003e\u003e(istream\u0026 in, FranctionalNumber\u0026 f) {\n    char delim;\n    in \u003e\u003e f.numerator \u003e\u003e delim \u003e\u003e f.denominator;\n\n    return in;\n}\n\nint main() {\n    FranctionalNumber f1(5, 6);\n    FranctionalNumber f2(3, 4);\n\n    FranctionalNumber f3 = f1 + f2;\n\n    cout \u003c\u003c f3 \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n## Templates\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\ntemplate\u003ctypename T\u003e\n\nT Max(T a, T b) {\n    return a \u003e b ? a : b;\n}\n\nint main() {\n    cout \u003c\u003c Max(5, 6) \u003c\u003c endl;\n    cout \u003c\u003c Max(5.25, 5.27) \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n## Template class\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\ntemplate \u003ctypename T\u003e\nstruct Node\n{\n    T val;\n    Node* next;\n};\n\ntemplate \u003ctypename T\u003e\nclass Stack {\n    private: \n        Node\u003cT\u003e* head;\n\n    public:\n        Stack() : head(nullptr) {}\n        \n        void push(T val) {\n            Node\u003cT\u003e* newHead = new Node\u003cT\u003e{val, head};\n            head = newHead;\n        }\n\n        T pop() {\n            T res = head-\u003eval;\n            Node\u003cT\u003e* temp = head-\u003enext;\n\n            delete head;\n            head = temp;\n\n            return res;\n        }\n};\n\nint main() {\n    Stack\u003cdouble\u003e stack;\n\n    for(int i = 0; i \u003c 5; ++i) {\n        stack.push(i / 2.0);\n    }\n\n    for(int i = 0; i \u003c 5; ++i) {\n        cout \u003c\u003c stack.pop() \u003c\u003c endl;\n    }\n\n    Stack\u003cchar\u003e stack1;\n\n    for(int i = 0; i \u003c 5; ++i) {\n        stack1.push(char('a' + i));\n    }\n\n    for(int i = 0; i \u003c 5; ++i) {\n        cout \u003c\u003c stack1.pop() \u003c\u003c endl;\n    }\n}\n```\n\n## Template pair\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\ntemplate \u003ctypename T, typename P\u003e\nstruct Node\n{\n    T val1;\n    P val2;\n    Node* next;\n};\n\ntemplate \u003ctypename T, typename P\u003e\nclass Stack {\n    private: \n        Node\u003cT, P\u003e* head;\n\n    public:\n        Stack() : head(nullptr) {}\n        \n        void push(T val1, P val2) {\n            Node\u003cT, P\u003e* newHead = new Node\u003cT, P\u003e{val1, val2, head};\n            head = newHead;\n        }\n\n        pair\u003cT, P\u003e pop() {\n            pair\u003cT, P\u003e res = {head-\u003eval1, head-\u003eval2};\n            P res1 = head-\u003eval2;\n            Node\u003cT, P\u003e* temp = head-\u003enext;\n\n            delete head;\n            head = temp;\n\n            return res;\n        }\n};\n\nint main() {\n    Stack\u003cchar, int\u003e stack;\n\n    for(int i = 0; i \u003c 5; ++i) {\n        stack.push(char('a' + i), i);\n    }\n\n    for(int i = 0; i \u003c 5; ++i) {\n        pair\u003cchar, int\u003e p = stack.pop();\n\n        cout \u003c\u003c p.first \u003c\u003c p.second \u003c\u003c endl;\n    }\n}\n```\n\n## Template specialization\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\ntemplate \u003ctypename T\u003e\nT Max(T a, T b) {\n    return a \u003e b ? a : b;\n}\n\ntemplate \u003c\u003e\nchar Max\u003cchar\u003e(char a, char b) {\n    return a \u003e b ? toupper(a) : toupper(a);\n}\n\nint main() {\n    cout \u003c\u003c Max(10, 2) \u003c\u003c endl;\n    cout \u003c\u003c Max('a', 'b') \u003c\u003c endl;\n\n    return 0;\n}\n```\n\n## unique_ptr\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\nclass A {\n    public:\n        A() {\n            cout \u003c\u003c \"A created\" \u003c\u003c endl;\n        }\n\n        ~A() {\n            cout \u003c\u003c \"A destroed\" \u003c\u003c endl;\n        }\n};\n\n\n\nint main() {\n    unique_ptr\u003cA\u003e a = make_unique\u003cA\u003e();\n    \n    //Owner will be b;\n    unique_ptr\u003cA\u003e b = move(a);\n\n    return 0;\n}\n```\n\n## shared_ptr\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\nclass A {\n    public:\n        A() {\n            cout \u003c\u003c \"A created\" \u003c\u003c endl;\n        }\n\n        ~A() {\n            cout \u003c\u003c \"A destroed\" \u003c\u003c endl;\n        }\n};\n\n\n\nint main() {\n    shared_ptr\u003cA\u003e a = make_shared\u003cA\u003e();\n    shared_ptr\u003cA\u003e b = a;\n\n    return 0;\n}\n```\n\n## weak_ptr\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\nclass A {\n    public:\n        A() {\n            cout \u003c\u003c \"A created\" \u003c\u003c endl;\n        }\n\n        ~A() {\n            cout \u003c\u003c \"A destroed\" \u003c\u003c endl;\n        }\n};\n\n\n\nint main() {\n    shared_ptr\u003cA\u003e a = make_shared\u003cA\u003e();\n    shared_ptr\u003cA\u003e b = a;\n\n    return 0;\n}\n```\n\n## Stack with clever pointers\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\ntemplate \u003ctypename T\u003e\nstruct Node {\n    T val;\n    unique_ptr\u003cNode\u003cT\u003e\u003e next;\n\n    Node(T v, unique_ptr\u003cNode\u003cT\u003e\u003e n) : val(v), next(move(n)) {\n        \n    }\n};\n\ntemplate \u003ctypename T\u003e\nclass Stack {\nprivate:\n    unique_ptr\u003cNode\u003cT\u003e\u003e head;\n\npublic:\n    Stack() = default;\n\n    void push(T val) {\n        head = make_unique\u003cNode\u003cT\u003e\u003e(val, move(head));\n    }\n\n    T pop() {\n        if (!head) {\n            throw runtime_error(\"Stack underflow: Cannot pop from empty stack\");\n        }\n\n        T res = head-\u003eval;\n        head = move(head-\u003enext);  // this deletes the old head automatically\n        return res;\n    }\n};\n\n\n\nint main() {\n    Stack\u003cdouble\u003e stack;\n\n    for(int i = 0; i \u003c 5; ++i) {\n        stack.push(i / 2.0);\n    }\n\n    for(int i = 0; i \u003c 5; ++i) {\n        cout \u003c\u003c stack.pop() \u003c\u003c endl;\n    }\n\n    return 0;\n}\n```\n\n## Exceptions\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\nint main() {\n    int a = 10, b = 0;\n\n    try {\n        if(b == 0) {\n            throw runtime_error(\"Cannot divide by 0\");\n        }\n\n        cout \u003c\u003c a / b \u003c\u003c endl;\n    } catch(const exception\u0026 e) {\n        cout \u003c\u003c e.what() \u003c\u003c endl;\n    }\n\n    return 0;\n}\n```\n\n## Stack with exception\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\ntemplate \u003ctypename T\u003e\nstruct Node {\n    T val;\n    unique_ptr\u003cNode\u003cT\u003e\u003e next;\n\n    Node(T v, unique_ptr\u003cNode\u003cT\u003e\u003e n) : val(v), next(move(n)) {\n        \n    }\n};\n\ntemplate \u003ctypename T\u003e\nclass Stack {\nprivate:\n    unique_ptr\u003cNode\u003cT\u003e\u003e head;\n\npublic:\n    Stack() = default;\n\n    void push(T val) {\n        head = make_unique\u003cNode\u003cT\u003e\u003e(val, move(head));\n    }\n\n    T pop() {\n        if (!head) {\n            throw overflow_error(\"Stack underflow: Cannot pop from empty stack\");\n        }\n\n        T res = head-\u003eval;\n        head = move(head-\u003enext);  // this deletes the old head automatically\n        return res;\n    }\n};\n\n\n\nint main() {\n    Stack\u003cdouble\u003e stack;\n\n    try\n    {\n        stack.pop();\n    }\n    catch(const std::exception\u0026 e)\n    {\n        std::cerr \u003c\u003c e.what() \u003c\u003c '\\n';\n    }\n    \n    \n\n    return 0;\n}\n```\n\n## My exception\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\nstruct MyException : public exception\n{\n    public:\n        char const* what() const noexcept override {\n            return \"My exception message\";\n        }\n};\n\n\nint main() {\n\n    try\n    {\n        throw MyException();\n    }\n    catch(const MyException\u0026 e)\n    {\n        std::cerr \u003c\u003c e.what() \u003c\u003c '\\n';\n    }   catch (const exception\u0026 e) {\n        std::cerr \u003c\u003c e.what() \u003c\u003c '\\n';\n    }\n    \n    return 0;\n}\n```\n\n## Array with dynamic memory (destructor, smart pointers)\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n\ntemplate \u003ctypename T\u003e\nclass Array {\n    private:\n        size_t size;\n        T* storage;\n\n    public:\n        Array(int size) {\n            this-\u003esize = size;\n            this-\u003estorage = new T[size];\n        }\n\n        ~Array() {\n            delete[] storage;\n        }\n        \n        void setValue(size_t index, T value) {\n            if(index \u003c 0 || index \u003e= this-\u003esize) {\n                throw out_of_range(\"Cannot set element with this index\");\n            }\n\n            this-\u003estorage[index] = value;\n        }\n\n        T getValue(size_t index) const {\n            if(index \u003c 0 || index \u003e= this-\u003esize) {\n                throw out_of_range(\"Cannot set element with this index\");\n            }\n\n            return this-\u003estorage[index];\n        }\n};\n\ntemplate \u003ctypename M\u003e\nclass Array1 {\nprivate:\n    size_t size;\n    unique_ptr\u003cM[]\u003e storage;\n\npublic:\n    // Constructor\n    Array1(size_t size) : size(size), storage(make_unique\u003cM[]\u003e(size)) {}\n\n    // No need for a destructor — handled by unique_ptr\n\n    void setValue(size_t index, const M\u0026 value) {\n        if (index \u003c 0 || index \u003e= this-\u003esize) {\n            throw out_of_range(\"Index out of range in setValue\");\n        }\n        storage[index] = value;\n    }\n\n    M getValue(size_t index) const {\n        if (index \u003c 0 || index \u003e= this-\u003esize) {\n            throw out_of_range(\"Index out of range in getValue\");\n        }\n        return storage[index];\n    }\n};\n\n\nint main() {\n    Array\u003cint\u003e myArr(5);\n\n    myArr.setValue(0, 1);\n    cout \u003c\u003c myArr.getValue(0) \u003c\u003c endl;\n\n    Array1\u003cint\u003e myArr1(2);\n\n    myArr1.setValue(0, 1122);\n    cout \u003c\u003c myArr1.getValue(0) \u003c\u003c endl;\n\n\n\n    \n    return 0;\n}\n```\n\n## Quiz\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\n#include \u003cmemory\u003e \nusing namespace std;\n\nclass Question {\n    private:\n        string question;\n        string answerA;\n        string answerB;\n        string answerC;\n        string answerD;\n        char rightAnswer;\n\n    public:\n        Question(string question, string answerA, string answerB, string answerC, string answerD, char rightAnswer) {\n            this-\u003equestion = question;\n            this-\u003eanswerA = answerA;\n            this-\u003eanswerB = answerB;\n            this-\u003eanswerC = answerC;\n            this-\u003eanswerD = answerD;\n            this-\u003erightAnswer = rightAnswer;\n        }\n\n        friend ostream\u0026 operator\u003c\u003c(ostream\u0026 out, const Question\u0026 q);\n\n        bool checkRightAnswer(char\u0026 ch) const {\n            return this-\u003erightAnswer == ch;\n        }\n};\n\nostream\u0026 operator\u003c\u003c(ostream\u0026 out, const Question\u0026 q) {\n    out \u003c\u003c q.question \u003c\u003c endl;\n    out \u003c\u003c \"a) \" \u003c\u003c q.answerA \u003c\u003c endl;\n    out \u003c\u003c \"b) \" \u003c\u003c q.answerB \u003c\u003c endl;\n    out \u003c\u003c \"c) \" \u003c\u003c q.answerC \u003c\u003c endl;\n    out \u003c\u003c \"d) \" \u003c\u003c q.answerD \u003c\u003c endl;\n\n    return out;\n}\n\nclass Quiz {\n    private:\n        unique_ptr\u003cvector\u003cQuestion\u003e\u003e questions;\n        int questionCount;\n        int size;\n    \n    public:\n        Quiz(int questionCount) {\n            this-\u003equestions = make_unique\u003cvector\u003cQuestion\u003e\u003e();\n            this-\u003equestionCount = questionCount;\n            this-\u003esize = 0;\n        }\n\n        void operator+=(Question q) {\n            if(this-\u003equestionCount == this-\u003equestions-\u003esize()) {\n                throw out_of_range(\"Limit questions is: \" + to_string(this-\u003equestionCount));\n            }\n\n            this-\u003equestions-\u003epush_back(q);\n            this-\u003esize += 1;\n        }\n\n        void start() const {\n            int rightCount = 0;\n            char value;\n\n            for (const Question\u0026 q : *questions) {\n                cout \u003c\u003c q \u003c\u003c endl;\n\n                cin \u003e\u003e value;\n\n                bool isRightAnswer = q.checkRightAnswer(value);\n\n                if(isRightAnswer) {\n                    rightCount += 1;\n                }\n            }\n\n            cout \u003c\u003c \"Total score: \" \u003c\u003c rightCount \u003c\u003c endl;\n        }\n};\n\nint main() {\n    Quiz quiz(3);\n\n    Question q(\"question\", \"answerA\", \"answerB\", \"answerC\", \"answerD\", 'd');\n\n    quiz += q;\n\n    Question q1(\"question1\", \"answerA\", \"answerB\", \"answerC\", \"answerD\", 'a');\n\n    quiz += q1;\n\n    Question q2(\"question2\", \"answerA\", \"answerB\", \"answerC\", \"answerD\", 'c');\n\n    quiz += q2;\n\n    quiz.start();\n\n    return 0;\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeorgiirocket%2Fcpp-part-1","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeorgiirocket%2Fcpp-part-1","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeorgiirocket%2Fcpp-part-1/lists"}