{"pageProps":{"code":{"CMakeLists.txt":{"name":"CMakeLists.txt","content":"add_executable(const-members const-members.cpp)\n\nadd_executable(construction-basic construction-basic.cpp)\n\nadd_executable(construction-eg construction-eg.cpp)\n\nadd_executable(construction-eg2 construction-eg2.cpp)\n\nadd_executable(constructor-basic constructor-basic.cpp)\n\nadd_executable(constructor-delegate constructor-delegate.cpp)\n\nadd_executable(constructor-init constructor-init.cpp)\n\nadd_executable(default-delete default-delete.cpp)\n\nadd_executable(explicit-1 explicit-1.cpp)\n\nadd_executable(explicit-2 explicit-2.cpp)\n\nadd_executable(lifetime lifetime.cpp)\n\nadd_executable(member-access member-access.cpp)\n\nadd_executable(scope-3 scope-3.cpp)\n\nadd_executable(scope scope.cpp)\n\nadd_executable(static-1 static-1.cpp)\n\nadd_executable(static-2 static-2.cpp)\n\nadd_executable(this-1 this-1.cpp)\n\nadd_executable(this-2 this-2.cpp)\n\n","path":"code/6771/24T2/3.1/CMakeLists.txt","fileext":"txt"},"const-members.cpp":{"name":"const-members.cpp","content":"#include <iostream>\n#include <string>\n\nclass person {\npublic:\n    person(std::string const& name)\n        : name_ { name }\n    {\n    }\n    auto set_name(std::string const& name) -> void\n    {\n        name_ = name;\n    }\n    auto get_name() -> std::string const&\n    {\n        return name_;\n    }\n\nprivate:\n    std::string name_;\n};\n\nauto main() -> int\n{\n    person p1 { \"Hayden\" };\n    p1.set_name(\"Chris\");\n    std::cout << p1.get_name() << \"\\n\";\n\n    person const p2 { \"Hayden\" };\n    // p2.set_name(\"Chris\"); // WILL NOT WORK... WHY NOT?\n    // std::cout << p2.get_name() << \"\\n\"; // WILL NOT WORK... WHY NOT?\n}","path":"code/6771/24T2/3.1/const-members.cpp","fileext":"cpp"},"construction-basic.cpp":{"name":"construction-basic.cpp","content":"#include <vector>\r\n\r\nauto main() -> int\r\n{\r\n    auto v1 = std::vector<int>(1, 2);\r\n    auto v2 = v1;\r\n}","path":"code/6771/24T2/3.1/construction-basic.cpp","fileext":"cpp"},"construction-eg.cpp":{"name":"construction-eg.cpp","content":"#include <vector>\r\n\r\nauto main() -> int\r\n{\r\n    // Always use auto on the left for this course, but you may see this elsewhere.\r\n    std::vector<int> v11; // Calls 0-argument constructor. Creates empty vector.\r\n\r\n    // There's no difference between these:\r\n    // T variable = T{arg1, arg2, ...}\r\n    // T variable{arg1, arg2, ...}\r\n    auto v12 = std::vector<int> {}; // No different to first\r\n    auto v13 = std::vector<int>(); // No different to the first\r\n\r\n    {\r\n        auto v2 = std::vector<int> { v11.begin(), v11.end() }; // A copy of v11.\r\n        auto v3 = std::vector<int> { v2 }; // A copy of v2.\r\n    } // v2 and v3 destructors are called here\r\n\r\n    auto v41 = std::vector<int> { 5, 2 }; // Initialiser-list constructor {5, 2}\r\n    auto v42 = std::vector<int>(5, 2); // Count + value constructor (5 * 2 => {2, 2, 2, 2, 2})\r\n} // v11, v12, v13, v41, v42 destructors called here","path":"code/6771/24T2/3.1/construction-eg.cpp","fileext":"cpp"},"construction-eg2.cpp":{"name":"construction-eg2.cpp","content":"#include <iostream>\n\ndouble f()\n{\n    return 1.1;\n}\n\nint main()\n{\n    // One of the reasons we do auto is to avoid ununitialized values.\n    // int n; // Not initialized (memory contains previous value)\n\n    int n21 {}; // Default constructor (memory contains 0)\n    auto n22 = int {}; // Default constructor (memory contains 0)\n    auto n3 { 5 };\n\n    // Not obvious you know that f() is not an int, but the compiler lets it through.\n    // int n43 = f();\n\n    // Not obvious you know that f() is not an int, and the compiler won't let you (narrowing\n    // conversion)\n    // auto n41 = int{f()};\n\n    // Good code. Clear you understand what you're doing.\n    auto n42 = static_cast<int>(f());\n\n    // std::cout << n << \"\\n\";\n    std::cout << n21 << \"\\n\";\n    std::cout << n22 << \"\\n\";\n    std::cout << n3 << \"\\n\";\n    std::cout << n42 << \"\\n\";\n}","path":"code/6771/24T2/3.1/construction-eg2.cpp","fileext":"cpp"},"constructor-basic.cpp":{"name":"constructor-basic.cpp","content":"#include <iostream>\n\nclass myclass {\npublic:\n    myclass(int i)\n    {\n        i_ = i;\n    }\n    int getval()\n    {\n        return i_;\n    }\n\nprivate:\n    int i_;\n};\n\nint main()\n{\n    auto mc = myclass { 1 };\n    std::cout << mc.getval() << \"\\n\";\n}","path":"code/6771/24T2/3.1/constructor-basic.cpp","fileext":"cpp"},"constructor-delegate.cpp":{"name":"constructor-delegate.cpp","content":"#include <string>\n\nclass dummy {\npublic:\n    explicit dummy(int const& i)\n        : s_ { \"Hello world\" }\n        , val_ { i }\n    {\n    }\n    explicit dummy()\n        : dummy(5)\n    {\n    }\n    std::string const& get_s()\n    {\n        return s_;\n    }\n    int get_val()\n    {\n        return val_;\n    }\n\nprivate:\n    std::string s_;\n    const int val_;\n};\n\nauto main() -> int\n{\n    dummy d1(5);\n    dummy d2 {};\n}","path":"code/6771/24T2/3.1/constructor-delegate.cpp","fileext":"cpp"},"constructor-init.cpp":{"name":"constructor-init.cpp","content":"#include <iostream>\n#include <string>\n\nclass myclass {\npublic:\n    myclass(int i)\n        : i_ { i }\n    {\n    }\n    int getval()\n    {\n        return i_;\n    }\n\nprivate:\n    int i_;\n};\n\nint main()\n{\n    auto mc = myclass { 5 };\n    std::cout << mc.getval() << \"\\n\";\n}","path":"code/6771/24T2/3.1/constructor-init.cpp","fileext":"cpp"},"default-delete.cpp":{"name":"default-delete.cpp","content":"#include <vector>\n\nclass intvec {\npublic:\n    // This one allows the implicit conversion\n    explicit intvec(std::vector<int>::size_type length)\n        : vec_(length, 0)\n    {\n    }\n    // intvec(intvec const& v) = default;\n    // intvec(intvec const& v) = delete;\n\nprivate:\n    std::vector<int> vec_;\n};\n\nauto main() -> int\n{\n    intvec a { 4 };\n    // intvec b{a}; // Will this work?\n}","path":"code/6771/24T2/3.1/default-delete.cpp","fileext":"cpp"},"destructor-1.cpp":{"name":"destructor-1.cpp","content":"class MyClass {\n    ~MyClass();\n};","path":"code/6771/24T2/3.1/destructor-1.cpp","fileext":"cpp"},"destructor-2.cpp":{"name":"destructor-2.cpp","content":"MyClass::~MyClass()\n{\n    // Definition here\n}","path":"code/6771/24T2/3.1/destructor-2.cpp","fileext":"cpp"},"explicit-1.cpp":{"name":"explicit-1.cpp","content":"#include <iostream>\n\nclass age {\npublic:\n    age(int age)\n        : age_ { age }\n    {\n    }\n\n    auto getAge() { return age_; }\n\nprivate:\n    int age_;\n};\n\nauto main() -> int\n{\n    // Explicitly calling the constructor\n    age a1 { 20 };\n\n    // Explicitly calling the constructor\n    age a2 = age { 20 };\n\n    // Attempts to use an integer\n    // where an age is expected.\n    // Implicit conversion done.\n    // This seems reasonable.\n    age a3 = 20;\n\n    (void)a1;\n    (void)a2;\n    std::cout << a3.getAge() << \"\\n\";\n}\n","path":"code/6771/24T2/3.1/explicit-1.cpp","fileext":"cpp"},"explicit-2.cpp":{"name":"explicit-2.cpp","content":"#include <vector>\n\nclass intvec {\npublic:\n    // This one allows the implicit conversion\n    // intvec(std::vector<int>::size_type length)\n    // : vec_(length, 0);\n\n    // This one disallows it.\n    explicit intvec(std::vector<int>::size_type length)\n        : vec_(length, 0)\n    {\n    }\n\nprivate:\n    std::vector<int> vec_;\n};\n\nauto main() -> int\n{\n    int const size = 20;\n    // Explictly calling the constructor.\n    intvec container1 { size }; // Construction\n    intvec container2 = intvec { size }; // Assignment\n\n    // Implicit conversion.\n    // Probably not what we want.\n    // intvec container3 = size;\n}\n","path":"code/6771/24T2/3.1/explicit-2.cpp","fileext":"cpp"},"lifetime.cpp":{"name":"lifetime.cpp","content":"#include <algorithm>\n#include <fstream>\n#include <string>\n#include <vector>\n\nvoid ReadWords(const std::string& filename)\n{\n    std::ifstream f { filename };\n    std::vector<std::string> words;\n    std::copy(std::istream_iterator<std::string> { f }, {}, std::back_inserter(words));\n    f.close();\n}","path":"code/6771/24T2/3.1/lifetime.cpp","fileext":"cpp"},"member-access.cpp":{"name":"member-access.cpp","content":"class foo {\npublic:\n    // Members accessible by everyone\n    foo(); // The default constructor.\n\nprotected:\n    // Members accessible by members, friends, and subclasses\n    // Will discuss this when we do advanced OOP in future weeks.\n\nprivate:\n    // Accessible only by members and friends\n    void private_member_function();\n    int private_data_member_;\n\npublic:\n    // May define multiple sections of the same name\n};","path":"code/6771/24T2/3.1/member-access.cpp","fileext":"cpp"},"namespace-1.cpp":{"name":"namespace-1.cpp","content":"// lexicon.hpp\nnamespace lexicon {\nstd::vector<std::string> read_lexicon(std::string const& path);\n\nvoid write_lexicon(std::vector<std::string> const&, std::string const& path);\n} // namespace lexicon","path":"code/6771/24T2/3.1/namespace-1.cpp","fileext":"cpp"},"namespace-2.cpp":{"name":"namespace-2.cpp","content":"// word_ladder.hpp\nnamespace word_ladder {\nstd::unordered_set<std::string> read_lexicon(std::string const& path);\n} // namespace word_ladder","path":"code/6771/24T2/3.1/namespace-2.cpp","fileext":"cpp"},"namespace-3.cpp":{"name":"namespace-3.cpp","content":"// word_ladder.hpp\nnamespace word_ladder {\nstd::unordered_set<std::string> read_lexicon(std::string const& path);\n} // namespace word_ladder","path":"code/6771/24T2/3.1/namespace-3.cpp","fileext":"cpp"},"namespace-4.cpp":{"name":"namespace-4.cpp","content":"// read_lexicon.cpp\nnamespace word_ladder {\nstd::unordered_set<std::string> read_lexicon(std::string const& path)\n{\n    // open file...\n    // read file into std::unordered_set...\n    // return std::unordered_set\n}\n} // namespace word_ladder","path":"code/6771/24T2/3.1/namespace-4.cpp","fileext":"cpp"},"namespace-fully-qualify.cpp":{"name":"namespace-fully-qualify.cpp","content":"namespace word_ladder {\nnamespace {\n    bool valid_word(std::string const& word);\n} // namespace\n\nstd::vector<std::vector<std::string>>\ngenerate(std::string const& from, std::string const& to)\n{\n    // ...\n    auto const result = word_ladder::valid_word(word);\n    // ...\n}\n} // namespace word_ladder","path":"code/6771/24T2/3.1/namespace-fully-qualify.cpp","fileext":"cpp"},"namespace-nest1.cpp":{"name":"namespace-nest1.cpp","content":"namespace comp6771::word_ladder {\nstd::vector<std::vector<std::string>>\nword_ladder(std::string const& from, std::string const& to);\n} // namespace comp6771::word_ladder","path":"code/6771/24T2/3.1/namespace-nest1.cpp","fileext":"cpp"},"namespace-nest2.cpp":{"name":"namespace-nest2.cpp","content":"namespace comp6771 {\n// ...\n\nnamespace word_ladder {\n    std::vector<std::vector<std::string>>\n    word_ladder(std::string const& from, std::string const& to);\n} // namespace word_ladder\n} // namespace comp6771","path":"code/6771/24T2/3.1/namespace-nest2.cpp","fileext":"cpp"},"namespace-unnamed.cpp":{"name":"namespace-unnamed.cpp","content":"namespace word_ladder {\nnamespace {\n    bool valid_word(std::string const& word);\n} // namespace\n} // namespace word_ladder","path":"code/6771/24T2/3.1/namespace-unnamed.cpp","fileext":"cpp"},"scope-1.h":{"name":"scope-1.h","content":"// foo.h\n\n#include <istream>\n\nclass Foo {\npublic:\n    // Equiv to typedef int Age\n    using Age = int;\n\n    Foo();\n    Foo(std::istream& is);\n    ~Foo();\n\n    void member_function();\n};","path":"code/6771/24T2/3.1/scope-1.h","fileext":"h"},"scope-2.cpp":{"name":"scope-2.cpp","content":"// foo.cpp\n#include \"scope-1.h\"\n\nFoo::Foo()\n{\n}\n\nFoo::Foo(std::istream& is)\n{\n}\n\nFoo::~Foo()\n{\n}\n\nvoid Foo::member_function()\n{\n    Foo::Age age;\n    // Also valid, since we are inside the Foo scope.\n    Age age;\n}","path":"code/6771/24T2/3.1/scope-2.cpp","fileext":"cpp"},"scope-3.cpp":{"name":"scope-3.cpp","content":"#include <iostream>\n#include <string>\n\nclass person {\npublic:\n    person(std::string const& name, int const age);\n    auto get_name() -> std::string const&;\n    auto get_age() -> int const&;\n\nprivate:\n    std::string name_;\n    int age_;\n};\n\nperson::person(std::string const& name, int const age)\n{\n    name_ = name;\n    age_ = age;\n}\n\nauto person::get_name() -> std::string const&\n{\n    return name_;\n}\n\nauto person::get_age() -> int const&\n{\n    return age_;\n}\n\nauto main() -> int\n{\n    auto p = person { \"Hayden\", 99 };\n    std::cout << p.get_name() << \"\\n\";\n}","path":"code/6771/24T2/3.1/scope-3.cpp","fileext":"cpp"},"scope.cpp":{"name":"scope.cpp","content":"#include <iostream>\r\n\r\nint i = 1;\r\nint main()\r\n{\r\n    std::cout << i << \"\\n\";\r\n    if (i > 0) {\r\n        int i = 2;\r\n        std::cout << i << \"\\n\";\r\n        {\r\n            int i = 3;\r\n            std::cout << i << \"\\n\";\r\n        }\r\n        std::cout << i << \"\\n\";\r\n    }\r\n    std::cout << i << \"\\n\";\r\n}","path":"code/6771/24T2/3.1/scope.cpp","fileext":"cpp"},"static-1.cpp":{"name":"static-1.cpp","content":"#include <string>\n\nclass user {\npublic:\n    user(std::string const& name)\n        : name_ { name }\n    {\n    }\n    auto valid_name(std::string const& name) -> bool\n    {\n        return name.length() < 20;\n    }\n\nprivate:\n    std::string name_;\n};\n\nauto main() -> int\n{\n    auto n = std::string { \"Santa Clause\" };\n    auto u = user { n };\n    if (u.valid_name(n)) {\n        user user1 { n };\n    }\n}","path":"code/6771/24T2/3.1/static-1.cpp","fileext":"cpp"},"static-2.cpp":{"name":"static-2.cpp","content":"#include <string>\n\nclass user {\npublic:\n    user(std::string const& name)\n        : name_ { name }\n    {\n    }\n    static auto valid_name(std::string const& name) -> bool\n    {\n        return name.length() < 20;\n    }\n\nprivate:\n    std::string name_;\n};\n\nauto main() -> int\n{\n    auto n = std::string { \"Santa Clause\" };\n    if (user::valid_name(n)) {\n        user user1 { n };\n    }\n}","path":"code/6771/24T2/3.1/static-2.cpp","fileext":"cpp"},"this-1.cpp":{"name":"this-1.cpp","content":"#include <iostream>\n\nclass myclass {\npublic:\n    myclass(int i)\n    {\n        i_ = i;\n    }\n    int getval()\n    {\n        return i_;\n    }\n\nprivate:\n    int i_;\n};\n\nint main()\n{\n    auto mc = myclass { 1 };\n    std::cout << mc.getval() << \"\\n\";\n}","path":"code/6771/24T2/3.1/this-1.cpp","fileext":"cpp"},"this-2.cpp":{"name":"this-2.cpp","content":"#include <iostream>\n\nclass myclass {\npublic:\n    myclass(int i)\n    {\n        this->i_ = i;\n    }\n    int getval()\n    {\n        return this->i_;\n    }\n\nprivate:\n    int i_;\n};\n\nint main()\n{\n    auto mc = myclass { 1 };\n    std::cout << mc.getval() << \"\\n\";\n}","path":"code/6771/24T2/3.1/this-2.cpp","fileext":"cpp"}}},"__N_SSG":true}