COMP6771 -comp6771代写
时间:2023-04-25
COMP6771
Advanced C++ Programming
10.2 Conclusion
COMP6771 in 60 Minutes or Less
a.k.a.: "Revision"
~COMP6771() {
Week 01: C -> C++
• C++ is a general-purpose programming language.
• CPU-native types: int, double, void*, etc.
• Class-like types: struct, class, union
• Functions: void foo(int, double*)
• Opt-in immutability: const int i = 5;
• auto: auto it = std::vector{}.begin();
• Value-semantics and reference semantics: T/T&/T*
• A rich standard library: std::vector, std::tuple, etc.
• Modular code-sharing: #include
• Separate compilation and linking.
Week 02: STL
• Standard Template Library (STL)
• Containers, e.g.:
• std::vector.
• std::list.
• ​Algorithms, e.g.:
• std::copy
• std::transform
• Iterators
• ​Input, Output, Forward, Bidirectional, RandomAccess, Contiguous
• Glue between containers and algorithms
Week 03: Scope & Classes
• Scope​.
• Functions, for, if, while, {}, namespace introduce scopes.
• Variables are accessible according to their scope.
• ​Object Lifetime.
• Lifetime starts when brought into scope.
• Lifetime ends when the scope ends.
• ​Classes are user-defined types that mirror primitives like int.
• Initialisation customisable through constructors.
• Clean-up customisable through destructor.
• ​​Internal entities of a class are members.
• Member functions.
• Data Members.
• Static member functions and static data members​.
• API extension through the power of friendship.
Week 04: Advanced Classes
• Operator Overloading:
• Provide user-defined meanings for operators in C++.
• Chained-operations very easy to read.
• Make classes "feel" like primitives.
• e.g. v1 + v2 is more natural than add(v1, v2).
• Custom Iterators:
• Use operator overloading to make a class-type look like a pointer.
• Implement operations that align with the various iterator categories.
• Transformed custom containers into iterable ranges.
Week 05: Exceptionally Resourceful
• Exceptions:
• ​Classes that represent unexpected runtime errors.
• Dedicated syntax: throw/try/catch.
• Compiler-enforced stack unwinding.
• Throw by value, catch by const&!!.
• C++ manages resources through RAII:
• Acquire resources (memory, locks, etc.) in the constructor, release them through the
destructor.
• Prevents resource leaks (by exceptions, forgetfulness, etc.).
• Able to prevent deep copies by deleting copy-constructor and copy-assign.
• Efficient transfer of ownership through move semantics.
• RAII-conforming Smart Pointers ™ replace "owning" raw pointers:
• std::unique_ptr/T* for unique ownership/observeration.
• std::shared_ptr/std::weak_ptr for shared ownership.
Week 07: Dynamic Polymorphism
(Inheritance)
• Classic OOP through Dynamic Polymorphism.
• Inheritance and derived classes.
• virtual methods.
• override, final, pure virtual (abstract) methods.
• Static (at compile-time) binding vs. dynamic (at runtime) binding.
• Implemented through vtables:
• Table of function pointers to virtual methods.
• Compiler-generated.
• Can cast up and down type hierarchies with dynamic_cast.
• Important considerations:
• Polymorphic classes must have virtual destructors!
• Dynamic polymorphism only happens for T* and T&!
• Copying/moving a derived object into a base object causes object slicing.
Week 08: Static Polymorphism (Templates)
• Generic Programming through compile-time type paramerisation.
• Function, Class, Alias, Variable, and Variadic templates.
• Compiler synthesises function/class/alias/variable definition from
the template when required.
• Can be forced by explicit instantiation.
• Primary template customisable through specialisation, either:
• Fully (explicit specialisation); or
• Partially (partial specialisation, not for function templates).
• Parameterisable by:
• Types (e.g. template ).
• Non-type template parameters (e.g. template ).
• Template template parameters (e.g. template