├── .gitignore ├── .vscode └── settings.json ├── README.md ├── README_EN.md ├── WORKSPACE ├── basic_content ├── abstract │ ├── BUILD │ ├── README.md │ ├── abstract.cpp │ ├── abstract_base.h │ ├── derived_full.cpp │ ├── interesting_facts1.cpp │ ├── interesting_facts2.cpp │ ├── interesting_facts3.cpp │ ├── interesting_facts4.cpp │ ├── interesting_facts5.cpp │ └── pure_virtual.cpp ├── assert │ ├── BUILD │ ├── README.md │ ├── assert.c │ └── ignore_assert.c ├── bit │ ├── BUILD │ ├── README.md │ └── bit.cpp ├── c_poly │ ├── BUILD │ ├── README.md │ ├── c++_examp.cpp │ └── c_examp.c ├── const │ ├── BUILD │ ├── README.md │ ├── class_const │ │ ├── c++11_example │ │ │ ├── BUILD │ │ │ ├── apple.cpp │ │ │ ├── apple.h │ │ │ └── main.cpp │ │ ├── first_example │ │ │ ├── BUILD │ │ │ ├── apple.cpp │ │ │ ├── apple.h │ │ │ └── main.cpp │ │ ├── overload_example │ │ │ ├── BUILD │ │ │ ├── apple.cpp │ │ │ ├── apple.h │ │ │ └── main.cpp │ │ └── static_example │ │ │ ├── BUILD │ │ │ ├── apple.cpp │ │ │ ├── apple.h │ │ │ └── main.cpp │ ├── const_function.cpp │ ├── const_num.cpp │ ├── extern_const │ │ ├── BUILD │ │ ├── const_file1.cpp │ │ ├── const_file2.cpp │ │ ├── file1.cpp │ │ └── file2.cpp │ └── funciton_const │ │ ├── condition1 │ │ ├── BUILD │ │ ├── condition1.cpp │ │ ├── condition2.cpp │ │ └── condition3.cpp │ │ ├── condition2 │ │ ├── BUILD │ │ ├── condition1.cpp │ │ ├── condition2.cpp │ │ └── condition3.cpp │ │ └── condition3 │ │ ├── BUILD │ │ └── condition1.cpp ├── decltype │ ├── BUILD │ ├── README.md │ └── decltype.cpp ├── enum │ ├── BUILD │ ├── README.md │ ├── classic_practice.cpp │ └── tradition_color.cpp ├── explicit │ ├── BUILD │ ├── README.md │ └── explicit.cpp ├── extern │ ├── README.md │ ├── extern_c++ │ │ ├── BUILD │ │ ├── add.c │ │ ├── add.cpp │ │ └── add.h │ └── extern_c │ │ ├── BUILD │ │ ├── add.c │ │ ├── add.cpp │ │ └── add.h ├── friend │ ├── BUILD │ ├── README.md │ ├── friend_class.cpp │ └── friend_func.cpp ├── func_pointer │ ├── BUILD │ └── func_pointer.cpp ├── img │ └── wechat.jpg ├── inline │ ├── BUILD │ ├── README.md │ ├── inline.cpp │ ├── inline.h │ └── inline_virtual.cpp ├── macro │ ├── BUILD │ ├── README.md │ ├── do_while.cpp │ └── sig_examp.cpp ├── maohao │ ├── BUILD │ ├── README.md │ └── maohao.cpp ├── pointer_refer │ ├── BUILD │ ├── README.md │ ├── copy_construct.cpp │ └── effec.cpp ├── sizeof │ ├── BUILD │ ├── README.md │ ├── blackclass.cpp │ ├── genA.cpp │ ├── geninhe.cpp │ ├── moreinhe.cpp │ ├── morevir.cpp │ ├── static.cpp │ ├── virinhe.cpp │ └── virmoreinhe.cpp ├── static │ ├── BUILD │ ├── README.md │ ├── nostatic_class.cpp │ ├── static_class.cpp │ ├── static_demo.cpp │ ├── static_error_variable.cpp │ ├── static_funciton.cpp │ └── static_variable.cpp ├── struct │ ├── BUILD │ ├── README.md │ ├── ext_struct_func.cpp │ ├── struct_func.c │ ├── struct_func.cpp │ ├── struct_func_func.cpp │ └── stu ├── struct_class │ └── README.md ├── this │ ├── BUILD │ ├── README.md │ ├── img │ │ ├── constthis.png │ │ ├── genthis.png │ │ └── thiscontrust.png │ └── person.cpp ├── union │ ├── BUILD │ ├── README.md │ └── union.cpp ├── using │ ├── BUILD │ ├── README.md │ ├── derived_base.cpp │ ├── using_derived.cpp │ ├── using_global.cpp │ └── using_typedef.cpp ├── virtual │ ├── README.md │ ├── set1 │ │ ├── BUILD │ │ └── emp.cpp │ ├── set2 │ │ ├── BUILD │ │ └── default_arg.cpp │ ├── set3 │ │ ├── BUILD │ │ ├── copy_consrtuct.cpp │ │ ├── full_virde.cpp │ │ ├── inline_virtual.cpp │ │ ├── static_error.cpp │ │ ├── vir_con.cpp │ │ ├── vir_de.cpp │ │ ├── virtual_function.cpp │ │ ├── virtual_function1.cpp │ │ └── virtual_inline.cpp │ └── set4 │ │ ├── BUILD │ │ ├── rtti.cpp │ │ └── warn_rtti.cpp ├── volatile │ ├── BUILD │ ├── README.md │ ├── noopt_vola.cpp │ └── volatile.cpp └── vptr_vtable │ ├── BUILD │ ├── README.md │ ├── img │ └── base.jpg │ └── vptr1.cpp ├── codingStyleIdioms ├── 1_classInitializers │ ├── 1.1_nest.cpp │ ├── 1.2_nodefault_ctor.cpp │ ├── 1.3_const.cpp │ ├── BUILD │ ├── README.md │ └── initializer.cpp ├── 2_enumclass │ ├── BUILD │ ├── README.md │ └── namespace.cpp ├── 3_RAII │ ├── BUILD │ ├── RAII.cpp │ ├── RAII.md │ ├── RAII_fstram.cpp │ ├── c++_example.cpp │ ├── c++_example1.cpp │ ├── c++_example2.cpp │ └── c_example.cpp ├── 4_copy-swap │ ├── BUILD │ ├── README.md │ └── copy-swapAndADL.cpp ├── 5_pImpl │ ├── BUILD │ ├── README.md │ ├── noPimpl.cpp │ ├── pimpl.cpp │ ├── pimplTime.cpp │ └── pimplTime.h └── README.md ├── concurrency ├── Threading_In_CPlusPlus │ ├── 1.thread │ │ ├── BUILD │ │ ├── intro.cpp │ │ └── thread.cpp │ ├── 2.create_type │ │ ├── 1.function_pointer.cpp │ │ ├── 2.lambda_function.cpp │ │ ├── 3.functor.cpp │ │ ├── 4.no_static_member_function.cpp │ │ ├── 5.static_member_function.cpp │ │ └── BUILD │ ├── 3.join_detach │ │ ├── BUILD │ │ ├── detach.cpp │ │ └── join.cpp │ └── 4.mutex │ │ ├── BUILD │ │ └── critical_section.cpp └── concurrency_v1 │ ├── chapter1 │ ├── 1_helloworld.cpp │ └── BUILD │ └── chapter2 │ ├── 2.1_basic.cpp │ ├── 2.2_transfer.cpp │ ├── 2.3_ownership.cpp │ ├── 2.4_runtime.cpp │ ├── 2_5_id.cpp │ └── BUILD ├── cpp2.0 └── cpp11 │ ├── BUILD │ ├── README.md │ ├── alias.cpp │ ├── auto.cpp │ ├── constexpr.cpp │ ├── decltype.cpp │ ├── default_delete.cpp │ ├── explicit.cpp │ ├── final.cpp │ ├── hash.cpp │ ├── initializer.cpp │ ├── lambda.cpp │ ├── move.cpp │ ├── noexcept.cpp │ ├── nullptr.cpp │ ├── override.cpp │ ├── rvalue.cpp │ ├── template_template.cpp │ ├── tuple.cpp │ ├── type_alias.cpp │ ├── uniform_initialization.cpp │ └── variadic │ ├── BUILD │ ├── variadic.cpp │ ├── variadic1.cpp │ ├── variadic2.cpp │ ├── variadic3_4.cpp │ ├── variadic5.cpp │ ├── variadic6.cpp │ └── variadic7.cpp ├── design_pattern ├── producer_consumer │ ├── BUILD │ └── producer_consumer.cpp └── singleton │ ├── README.md │ ├── barrier_singleton.cpp │ ├── cpulpuls11_singleton.cpp │ ├── dcl_singleton.cpp │ ├── hungrysingleton.cpp │ ├── iazysingleton.cpp │ ├── lock_singleton.cpp │ ├── pthreadoncesingleton.cpp │ └── static_local_singleton.cpp ├── effective_cpp ├── 2.cpp └── BUILD ├── english └── basic_content │ ├── abstract │ ├── README.md │ ├── abstract.cpp │ ├── derived_full.cpp │ ├── interesting_facts1.cpp │ ├── interesting_facts2.cpp │ ├── interesting_facts3.cpp │ ├── interesting_facts4.cpp │ ├── interesting_facts5.cpp │ ├── pure_virtual.cpp │ └── test.cpp │ ├── assert │ ├── README.md │ ├── assert.c │ └── ignore_assert.c │ ├── bit │ ├── README.md │ └── learn.cpp │ ├── c_poly │ ├── README.md │ ├── c++_examp │ ├── c++_examp.cpp │ ├── c_examp │ └── c_examp.c │ ├── const │ ├── README.md │ ├── class_const │ │ ├── c++11_example │ │ │ ├── apple.cpp │ │ │ ├── main │ │ │ └── main.cpp │ │ ├── first_example │ │ │ ├── apple.cpp │ │ │ └── main.cpp │ │ ├── overload_example │ │ │ ├── apple.cpp │ │ │ ├── main │ │ │ └── main.cpp │ │ └── static_example │ │ │ ├── apple.cpp │ │ │ ├── main │ │ │ └── main.cpp │ ├── const_function.cpp │ ├── const_num.cpp │ ├── extern_const │ │ ├── const_file1.cpp │ │ ├── const_file2.cpp │ │ ├── file1.cpp │ │ └── file2.cpp │ └── funciton_const │ │ ├── condition1 │ │ ├── condition1.cpp │ │ ├── condition2.cpp │ │ └── condition3.cpp │ │ ├── condition2 │ │ ├── condition1 │ │ ├── condition1.cpp │ │ ├── condition2.cpp │ │ ├── condition3 │ │ └── condition3.cpp │ │ └── condition3 │ │ ├── condition1 │ │ └── condition1.cpp │ ├── decltype │ ├── README.md │ ├── decltype │ └── decltype.cpp │ ├── enum │ ├── README.md │ ├── classic_practice │ ├── classic_practice.cpp │ └── tradition_color.cpp │ ├── explicit │ ├── README.md │ └── explicit.cpp │ ├── extern │ ├── README.md │ ├── extern_c++ │ │ ├── add.c │ │ ├── add.cpp │ │ ├── add.h │ │ ├── add.o │ │ └── main │ └── extern_c │ │ ├── add.c │ │ ├── add.cpp │ │ ├── add.h │ │ ├── add.o │ │ └── main │ ├── friend │ ├── README.md │ ├── friend_class │ ├── friend_class.cpp │ ├── friend_func │ └── friend_func.cpp │ ├── func_pointer │ ├── func1 │ └── func_pointer.cpp │ ├── img │ └── wechat.jpg │ ├── inline │ ├── README.md │ ├── inline │ ├── inline.cpp │ ├── inline.h │ ├── inline_virtual.cpp │ └── iv │ ├── macro │ ├── README.md │ ├── do_while │ ├── do_while.cpp │ ├── sig_examp │ └── sig_examp.cpp │ ├── maohao │ ├── .README.md.un~ │ ├── README.md │ ├── README.md~ │ └── maohao.cpp │ ├── pointer_refer │ ├── README.md │ ├── copy_construct │ ├── copy_construct.cpp │ ├── effec │ └── effec.cpp │ ├── sizeof │ ├── README.md │ ├── blackclass.cpp │ ├── genA.cpp │ ├── geninhe.cpp │ ├── moreinhe.cpp │ ├── morevir.cpp │ ├── static.cpp │ ├── virinhe.cpp │ └── virmoreinhe.cpp │ ├── static │ ├── README.md │ ├── demo │ ├── nostatic_class.cpp │ ├── static_class.cpp │ ├── static_demo.cpp │ ├── static_error_variable.cpp │ ├── static_funciton.cpp │ └── static_variable.cpp │ ├── struct │ ├── README.md │ ├── ext │ ├── ext_struct_func.cpp │ ├── sf │ ├── stff │ ├── struct_func │ ├── struct_func.c │ ├── struct_func.cpp │ ├── struct_func_func.cpp │ └── stu │ ├── struct_class │ └── README.md │ ├── this │ ├── README.md │ ├── img │ │ ├── constthis.png │ │ ├── genthis.png │ │ └── thiscontrust.png │ ├── person │ └── person.cpp │ ├── union │ ├── README.md │ ├── union │ └── union.cpp │ ├── using │ ├── README.md │ ├── derived_base │ ├── derived_base.cpp │ ├── using_derived │ ├── using_derived.cpp │ ├── using_global │ ├── using_global.cpp │ ├── using_typedef │ └── using_typedef.cpp │ ├── virtual │ ├── README.md │ ├── set1 │ │ ├── emp │ │ └── emp.cpp │ ├── set2 │ │ └── default_arg.cpp │ ├── set3 │ │ ├── copy_consrtuct.cpp │ │ ├── full_virde.cpp │ │ ├── inline_virtual.cpp │ │ ├── static_error.cpp │ │ ├── vir_con.cpp │ │ ├── vir_de.cpp │ │ ├── virtual_function.cpp │ │ ├── virtual_function1.cpp │ │ └── virtual_inline.cpp │ └── set4 │ │ ├── rtti │ │ ├── rtti.cpp │ │ └── warn_rtti.cpp │ ├── volatile │ ├── README.md │ ├── noopt_vola.cpp │ ├── nv │ └── volatile.cpp │ └── vptr_vtable │ ├── README.md │ ├── img │ └── base.jpg │ └── vptr1.cpp ├── extension └── some_problem │ └── string_int.md ├── img ├── cpp.jpg └── wechat.jpg ├── learn_class └── modern_cpp_30 │ ├── RAII │ ├── BUILD │ ├── RAII.cpp │ ├── heap.cpp │ └── stack.cpp │ ├── SFINAE │ ├── README.md │ ├── SFINAE.cpp │ ├── SFINAE.pdf │ └── sfinae paper │ │ ├── auto.cpp │ │ ├── blending1.cpp │ │ ├── blending2.cpp │ │ ├── combiningAndGenius.cpp │ │ ├── constexpr.cpp │ │ ├── decltype.cpp │ │ ├── fis_valid.cpp │ │ ├── hana.cpp │ │ ├── is_valid.cpp │ │ ├── lambda.cpp │ │ ├── overload1.cpp │ │ ├── overload2.cpp │ │ ├── p1SFINAE.cpp │ │ ├── p2SFINAE.cpp │ │ ├── packis_valid.cpp │ │ ├── serialize.cpp │ │ ├── sizeof1.cpp │ │ ├── sizeof2.cpp │ │ ├── structData.h │ │ └── timeGenius.cpp │ ├── compilercompute │ ├── BUILD │ ├── IF.cpp │ ├── WhileLoop.cpp │ ├── factorial.cpp │ └── fmap.cpp │ ├── compilerpoly │ ├── BUILD │ ├── README.md │ └── template.cpp │ ├── constexpr │ ├── BUILD │ ├── container.cpp │ ├── newconstexpr.cpp │ ├── output_container.h │ ├── sqrt.cpp │ └── test3.cpp │ ├── container1 │ ├── BUILD │ ├── container.cpp │ ├── output_container.h │ └── vector_l.cpp │ ├── container2 │ ├── BUILD │ ├── array.cpp │ ├── hash.cpp │ ├── priority_queue.cpp │ ├── relacontainer.cpp │ └── unorder.cpp │ ├── exception │ ├── BUILD │ └── exception.cpp │ ├── functionLambda │ ├── BUILD │ ├── adder.cpp │ ├── autoLambda.cpp │ └── function.cpp │ ├── literalAssert │ ├── BUILD │ ├── assert.cpp │ ├── default_delete.cpp │ ├── literal.cpp │ └── overridefinal.cpp │ ├── memorymodel_atomic │ ├── barrier_singleton.cpp │ ├── cpulpuls11_singleton.cpp │ ├── dcl_singleton.cpp │ ├── hungrysingleton.cpp │ ├── iazysingleton.cpp │ ├── lock_singleton.cpp │ ├── pthreadoncesingleton.cpp │ └── static_local_singleton.cpp │ ├── obj │ ├── BUILD │ ├── all.cpp │ ├── obj1.cpp │ ├── obj2.cpp │ ├── obj3.cpp │ ├── obj4.cpp │ └── obj5.cpp │ ├── reference │ ├── BUILD │ ├── collapses.cpp │ ├── forward.cpp │ ├── lifetime.cpp │ ├── ref.cpp │ ├── reference.cpp │ └── shape.h │ └── smart_ptr │ ├── README.md │ ├── auto_scope.cpp │ ├── shared_ptr.cpp │ ├── unique_ptr.cpp │ └── unique_ptr_U.cpp ├── practical_exercises ├── 10_day_practice │ ├── day1 │ │ ├── BUILD │ │ ├── annotate.cpp │ │ ├── print.cpp │ │ ├── runnian.cpp │ │ └── union.cpp │ ├── day10 │ │ ├── file │ │ │ ├── 10-4.cpp │ │ │ ├── 12-1.cpp │ │ │ ├── 12-2.cpp │ │ │ ├── 12-3.cpp │ │ │ ├── 12-5.cpp │ │ │ ├── 12-6.cpp │ │ │ ├── 12-7.cpp │ │ │ ├── 12-9.cpp │ │ │ ├── BUILD │ │ │ ├── input │ │ │ │ ├── BUILD │ │ │ │ ├── get.cpp │ │ │ │ ├── get2.cpp │ │ │ │ └── getline.cpp │ │ │ └── practice.cpp │ │ └── readme.md │ ├── day2 │ │ ├── BUILD │ │ ├── compute.cpp │ │ ├── enum.cpp │ │ ├── hanoi.cpp │ │ ├── pow.cpp │ │ ├── rec1.cpp │ │ ├── rec2.cpp │ │ ├── shaizi.cpp │ │ ├── st.cpp │ │ └── static.cpp │ ├── day3 │ │ ├── BUILD │ │ ├── inline.cpp │ │ ├── pratice.cpp │ │ ├── predeclare.cpp │ │ ├── static_data.cpp │ │ ├── static_member1.cpp │ │ ├── static_member2.cpp │ │ └── swap.cpp │ ├── day4 │ │ ├── clock │ │ │ ├── BUILD │ │ │ ├── operator.cpp │ │ │ └── operator_plus.cpp │ │ ├── const │ │ │ ├── BUILD │ │ │ ├── obj_func.cpp │ │ │ ├── obj_ref.cpp │ │ │ └── readme.md │ │ ├── copy_ctor │ │ │ ├── BUILD │ │ │ ├── clock.cpp │ │ │ └── clock.h │ │ └── friend │ │ │ ├── BUILD │ │ │ ├── class.cpp │ │ │ ├── func.cpp │ │ │ └── readme.md │ ├── day5 │ │ ├── ctor_dtor │ │ │ ├── BUILD │ │ │ ├── cseq.cpp │ │ │ ├── ctor.cpp │ │ │ ├── ctor_d.cpp │ │ │ ├── noctor.cpp │ │ │ ├── param.cpp │ │ │ ├── readme.md │ │ │ └── seq.cpp │ │ ├── inherit_access │ │ │ ├── BUILD │ │ │ ├── private.cpp │ │ │ ├── protected.cpp │ │ │ ├── protected_inherit.cpp │ │ │ ├── public.cpp │ │ │ └── readme.md │ │ ├── rela │ │ │ ├── BUILD │ │ │ ├── readme.md │ │ │ └── rela.cpp │ │ ├── rule │ │ │ ├── BUILD │ │ │ ├── direct.cpp │ │ │ └── readme.md │ │ └── virtual │ │ │ ├── BUILD │ │ │ ├── example1.cpp │ │ │ ├── example2.cpp │ │ │ ├── init.cpp │ │ │ ├── readme.md │ │ │ └── seq.cpp │ ├── day6 │ │ ├── abstract_class │ │ │ ├── BUILD │ │ │ └── main.cpp │ │ ├── readme.md │ │ └── virtual_func │ │ │ ├── BUILD │ │ │ ├── example.cpp │ │ │ ├── virtual.cpp │ │ │ ├── virtual_dtor.cpp │ │ │ ├── virtual_feature.cpp │ │ │ └── vis.cpp │ ├── day7 │ │ ├── binary_operator │ │ │ ├── BUILD │ │ │ ├── friend_operator.cpp │ │ │ └── operator.cpp │ │ ├── brackets │ │ │ ├── BUILD │ │ │ └── brac.cpp │ │ ├── equal_operator │ │ │ ├── BUILD │ │ │ └── equal_operator.cpp │ │ ├── example │ │ │ ├── BUILD │ │ │ └── example.cpp │ │ ├── index_parentheses │ │ │ ├── BUILD │ │ │ └── example.cpp │ │ ├── readme.md │ │ ├── subscript_operator │ │ │ ├── BUILD │ │ │ └── subscript_operator.cpp │ │ └── unary_operator │ │ │ ├── BUILD │ │ │ ├── time_counter.cpp │ │ │ └── time_increase.cpp │ ├── day8 │ │ ├── class_template │ │ │ ├── BUILD │ │ │ ├── spec.cpp │ │ │ └── stack.cpp │ │ ├── func │ │ │ ├── BUILD │ │ │ ├── main.cpp │ │ │ ├── max.cpp │ │ │ ├── max_spec.cpp │ │ │ └── sort.cpp │ │ ├── readme.md │ │ └── stl │ │ │ ├── BUILD │ │ │ └── map.cpp │ ├── day9 │ │ ├── exception │ │ │ ├── 1.cpp │ │ │ ├── 10.cpp │ │ │ ├── 2.cpp │ │ │ ├── 3.cpp │ │ │ ├── 4.cpp │ │ │ ├── 5.cpp │ │ │ ├── 6.cpp │ │ │ ├── 7-1.cpp │ │ │ ├── 7.cpp │ │ │ ├── 8.cpp │ │ │ ├── 9-2.cpp │ │ │ ├── 9.cpp │ │ │ └── BUILD │ │ └── readme.md │ └── readme.md └── key_exercises │ ├── BUILD │ ├── README.md │ ├── array.cpp │ ├── array_template.cpp │ ├── bracket_overloading.cpp │ ├── clock.cpp │ ├── func_temp.cpp │ ├── io_operator.cpp │ ├── io_operator_overload.cpp │ ├── map_insert_look.cpp │ ├── operator_cast.cpp │ ├── operator_circle.cpp │ ├── output.cpp │ ├── override.cpp │ ├── read_file.cpp │ ├── stack.cpp │ └── try.cpp ├── proj └── README.md ├── src_analysis └── stl │ ├── array.md │ ├── deque.md │ ├── hashtable.md │ ├── iterator.md │ ├── list.md │ ├── map_multimap.md │ ├── myhashtable.md │ ├── queue_stack.md │ ├── rb_tree.md │ ├── set_multiset.md │ ├── traits.md │ ├── typename.md │ ├── unordered_map.md │ ├── vector.md │ └── 谈谈STL设计之EBO优化.md └── tool ├── C++的Debug工具dbg-macro.md ├── output ├── BUILD ├── container.cpp └── output_container.h ├── 像Python一样玩CC++.md └── 用rr来进行debug.md /.gitignore: -------------------------------------------------------------------------------- 1 | com.sh 2 | bazel-CPlusPlusThings 3 | bazel-bin 4 | bazel-out 5 | bazel-testlogs 6 | bazel-cplusplus_bazel 7 | .vscode -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/WORKSPACE -------------------------------------------------------------------------------- /basic_content/abstract/abstract.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file abstract.cpp 3 | * @brief 4 | * 抽象类中:在成员函数内可以调用纯虚函数,在构造函数/析构函数内部不能使用纯虚函数 5 | * 如果一个类从抽象类派生而来,它必须实现了基类中的所有纯虚函数,才能成为非抽象类 6 | * @author 光城 7 | * @version v1 8 | * @date 2019-07-20 9 | */ 10 | 11 | #include 12 | using namespace std; 13 | 14 | class A { 15 | public: 16 | virtual void f() = 0; // 纯虚函数 17 | void g() { this->f(); } 18 | A() {} 19 | }; 20 | class B : public A { 21 | public: 22 | void f() { cout << "B:f()" << endl; } 23 | }; 24 | int main() { 25 | B b; 26 | b.g(); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /basic_content/abstract/abstract_base.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file abstreact_base.cpp 3 | * @brief 4 | * C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数!我们只需声明它!通过声明中赋值0来声明纯虚函数! 5 | * 纯虚函数:没有函数体的虚函数 6 | * @author 光城 7 | * @version v1 8 | * @date 2019-07-20 9 | */ 10 | 11 | /** 12 | * @brief 抽象类 13 | */ 14 | class AbstractBase { 15 | // Data members of class 16 | public: 17 | // Pure Virtual Function 18 | virtual void show() = 0; 19 | 20 | /* Other members */ 21 | }; 22 | -------------------------------------------------------------------------------- /basic_content/abstract/derived_full.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file derived_full.cpp 3 | * @brief 完整示例!抽象类由派生类继承实现! 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-20 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | class Base { 13 | int x; 14 | 15 | public: 16 | virtual void fun() = 0; 17 | int getX() { return x; } 18 | }; 19 | 20 | class Derived : public Base { 21 | public: 22 | void fun() { cout << "fun() called"; } // 实现了fun()函数 23 | }; 24 | 25 | int main(void) { 26 | Derived d; 27 | d.fun(); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /basic_content/abstract/interesting_facts1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file interesting_facts1.cpp 3 | * @brief 纯虚函数使一个类变成抽象类 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-20 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | /** 13 | * @brief 抽象类至少包含一个纯虚函数 14 | */ 15 | class Test { 16 | int x; 17 | 18 | public: 19 | virtual void show() = 0; 20 | int getX() { return x; } 21 | }; 22 | 23 | int main(void) { 24 | Test t; // error! 不能创建抽象类的对象 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /basic_content/abstract/interesting_facts2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file interesting_facts2.cpp 3 | * @brief 抽象类类型的指针和引用 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-20 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | /** 13 | * @brief 抽象类至少包含一个纯虚函数 14 | */ 15 | class Base { 16 | int x; 17 | 18 | public: 19 | virtual void show() = 0; 20 | int getX() { return x; } 21 | }; 22 | class Derived : public Base { 23 | public: 24 | void show() { cout << "In Derived \n"; } 25 | Derived() {} 26 | }; 27 | int main(void) { 28 | // Base b; //error! 不能创建抽象类的对象 29 | // Base *b = new Base(); error! 30 | Base *bp = new Derived(); // 抽象类的指针和引用 -> 由抽象类派生出来的类的对象 31 | bp->show(); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /basic_content/abstract/interesting_facts3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file interesting_facts3.cpp 3 | * @brief 如果我们不在派生类中覆盖纯虚函数,那么派生类也会变成抽象类。 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-20 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | class Base { 13 | int x; 14 | 15 | public: 16 | virtual void show() = 0; 17 | int getX() { return x; } 18 | }; 19 | class Derived : public Base { 20 | public: 21 | // void show() { } 22 | }; 23 | int main(void) { 24 | Derived 25 | d; // error! 26 | // 派生类没有实现纯虚函数,那么派生类也会变为抽象类,不能创建抽象类的对象 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /basic_content/abstract/interesting_facts4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file interesting_facts4.cpp 3 | * @brief 抽象类可以有构造函数 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-20 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | // An abstract class with constructor 13 | class Base { 14 | protected: 15 | int x; 16 | 17 | public: 18 | virtual void fun() = 0; 19 | Base(int i) { x = i; } 20 | }; 21 | 22 | class Derived : public Base { 23 | int y; 24 | 25 | public: 26 | Derived(int i, int j) : Base(i) { y = j; } 27 | void fun() { cout << "x = " << x << ", y = " << y; } 28 | }; 29 | 30 | int main(void) { 31 | Derived d(4, 5); 32 | d.fun(); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /basic_content/abstract/interesting_facts5.cpp: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @file interesting_facts5.cpp 4 | * @brief 构造函数不能是虚函数,而析构函数可以是虚析构函数。 5 | * 例如:当基类指针指向派生类对象并删除对象时,我们可能希望调用适当的析构函数。如果析构函数不是虚拟的,则只能调用基类析构函数。 6 | * @author 光城 7 | * @version v1 8 | * @date 2019-07-20 9 | */ 10 | #include 11 | using namespace std; 12 | 13 | class Base { 14 | public: 15 | Base() { cout << "Constructor: Base" << endl; } 16 | virtual ~Base() { cout << "Destructor : Base" << endl; } 17 | }; 18 | 19 | class Derived : public Base { 20 | public: 21 | Derived() { cout << "Constructor: Derived" << endl; } 22 | ~Derived() { cout << "Destructor : Derived" << endl; } 23 | }; 24 | 25 | int main() { 26 | Base *Var = new Derived(); 27 | delete Var; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /basic_content/abstract/pure_virtual.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file pure_virtual.cpp 3 | * @brief 纯虚函数:没有函数体的虚函数 4 | * 抽象类:包含纯虚函数的类 5 | * 6 | * @author 光城 7 | * @version v1 8 | * @date 2019-07-20 9 | */ 10 | 11 | #include 12 | 13 | using namespace std; 14 | class A { 15 | private: 16 | int a; 17 | 18 | public: 19 | virtual void show() = 0; // 纯虚函数 20 | }; 21 | 22 | int main() { 23 | /* 24 | * 1. 抽象类只能作为基类来派生新类使用 25 | * 2. 抽象类的指针和引用->由抽象类派生出来的类的对象! 26 | */ 27 | A a; // error 抽象类,不能创建对象 28 | 29 | A *a1; // ok 可以定义抽象类的指针 30 | 31 | A *a2 = new A(); // error, A是抽象类,不能创建对象 32 | } 33 | -------------------------------------------------------------------------------- /basic_content/assert/BUILD: -------------------------------------------------------------------------------- 1 | # you can run some main program, just replace binary name 2 | # such as: `bazel run basic_content/assert:ignore_assert` 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "ignore_assert", 7 | srcs = ["ignore_assert.c"], 8 | ) 9 | 10 | cc_binary( 11 | name = "assert", 12 | srcs = ["assert.c"], 13 | ) 14 | -------------------------------------------------------------------------------- /basic_content/assert/assert.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int x = 7; 7 | 8 | /* Some big code in between and let's say x 9 | * is accidentally changed to 9 10 | */ 11 | x = 9; 12 | 13 | // Programmer assumes x to be 7 in rest of the code 14 | assert(x==7); 15 | 16 | /* Rest of the code */ 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /basic_content/assert/ignore_assert.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file ignore_assert.c 3 | * @brief 忽略断言 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-25 7 | */ 8 | 9 | # define NDEBUG // 忽略断言 10 | 11 | #include 12 | 13 | int main(){ 14 | assert(x==5); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /basic_content/bit/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/bit:bit` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | cc_binary( 5 | name = "bit", 6 | srcs = ["bit.cpp"], 7 | ) -------------------------------------------------------------------------------- /basic_content/bit/bit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | struct stuff { 5 | unsigned int field1 : 30; 6 | unsigned int : 2; 7 | unsigned int field2 : 4; 8 | unsigned int : 0; 9 | unsigned int field3 : 3; 10 | }; 11 | int main() { 12 | struct stuff s = {1, 3, 5}; 13 | cout << s.field1 << endl; 14 | cout << s.field2 << endl; 15 | cout << s.field3 << endl; 16 | cout << sizeof(s) << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /basic_content/c_poly/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/c_poly:c_examp` 2 | # please run `bazel run basic_content/c_poly:c++_examp` 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "c_examp", 7 | srcs = ["c_examp.c"], 8 | ) 9 | 10 | cc_binary( 11 | name = "c++_examp", 12 | srcs = ["c++_examp.cpp"], 13 | ) -------------------------------------------------------------------------------- /basic_content/c_poly/c++_examp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file c++_examp.cpp 3 | * @brief c++中的多态 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-08-06 7 | */ 8 | 9 | #include 10 | 11 | using namespace std; 12 | class A { 13 | public: 14 | virtual void f() //虚函数实现 15 | { 16 | cout << "Base A::f() " << endl; 17 | } 18 | }; 19 | 20 | class B : public A // 必须为共有继承,否则后面调用不到,class默认为私有继承! 21 | { 22 | public: 23 | virtual void f() //虚函数实现,子类中virtual关键字可以没有 24 | { 25 | cout << "Derived B::f() " << endl; 26 | } 27 | }; 28 | 29 | int main() { 30 | A a; //基类对象 31 | B b; //派生类对象 32 | 33 | A *pa = &a; //父类指针指向父类对象 34 | pa->f(); //调用父类的函数 35 | 36 | pa = &b; //父类指针指向子类对象,多态实现 37 | pa->f(); //调用派生类同名函数 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /basic_content/const/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/const:const_function` 2 | # please run `bazel run basic_content/const:const_num` 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | # Don't panic if you get compilation errors, this is what this code demonstrates, as expected. 6 | cc_binary( 7 | name = "const_function", 8 | srcs = ["const_function.cpp"], 9 | copts = ["-std=c++11"] 10 | ) 11 | 12 | # Don't panic if you get compilation errors, this is what this code demonstrates, as expected. 13 | cc_binary( 14 | name = "const_num", 15 | srcs = ["const_num.cpp"], 16 | copts = ["-std=c++11"] 17 | ) -------------------------------------------------------------------------------- /basic_content/const/class_const/c++11_example/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/const/class_const/c++11_example:main` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") 3 | 4 | cc_library( 5 | name = "apple", 6 | srcs = ["apple.cpp"], 7 | hdrs = ["apple.h"], 8 | ) 9 | 10 | cc_binary( 11 | name = "main", 12 | srcs = ["main.cpp"], 13 | deps = [ 14 | ":apple", 15 | ], 16 | ) -------------------------------------------------------------------------------- /basic_content/const/class_const/c++11_example/apple.cpp: -------------------------------------------------------------------------------- 1 | #include "apple.h" 2 | #include 3 | 4 | using namespace std; 5 | Apple::Apple(int i) {} 6 | int Apple::add(int num) { 7 | take(num); 8 | return 0; 9 | } 10 | int Apple::add(int num) const { 11 | take(num); 12 | return 0; 13 | } 14 | void Apple::take(int num) const { cout << "take func " << num << endl; } 15 | int Apple::getCount() const { 16 | take(1); 17 | // add(); //error 18 | return apple_number; 19 | } -------------------------------------------------------------------------------- /basic_content/const/class_const/c++11_example/apple.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Apple { 4 | public: 5 | Apple(int i); 6 | // 使用c++11标准编译 7 | static const int apple_number = 10; 8 | // const int apple_number=10; 9 | void take(int num) const; 10 | int add(int num); 11 | int add(int num) const; 12 | int getCount() const; 13 | }; 14 | -------------------------------------------------------------------------------- /basic_content/const/class_const/c++11_example/main.cpp: -------------------------------------------------------------------------------- 1 | #include "apple.h" 2 | #include 3 | using namespace std; 4 | int main() { 5 | Apple a(2); 6 | cout << a.getCount() << endl; 7 | a.add(10); 8 | const Apple b(3); 9 | b.add(100); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /basic_content/const/class_const/first_example/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/const/class_const/first_example:main` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") 3 | 4 | cc_library( 5 | name = "apple", 6 | srcs = ["apple.cpp"], 7 | hdrs = ["apple.h"], 8 | ) 9 | 10 | cc_binary( 11 | name = "main", 12 | srcs = ["main.cpp"], 13 | deps = [ 14 | ":apple", 15 | ], 16 | ) -------------------------------------------------------------------------------- /basic_content/const/class_const/first_example/apple.cpp: -------------------------------------------------------------------------------- 1 | #include "apple.h" 2 | #include 3 | 4 | Apple::Apple(int i) : apple_number(i) {} 5 | int Apple::add(int num) { 6 | take(num); 7 | return 0; 8 | } 9 | int Apple::add(int num) const { 10 | take(num); 11 | return 0; 12 | } 13 | void Apple::take(int num) const { 14 | std::cout << "take func " << num << std::endl; 15 | } 16 | int Apple::getCount() const { 17 | take(1); 18 | // add(); //error 19 | return apple_number; 20 | } -------------------------------------------------------------------------------- /basic_content/const/class_const/first_example/apple.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Apple { 4 | public: 5 | Apple(int i); 6 | const int apple_number; 7 | void take(int num) const; 8 | int add(int num); 9 | int add(int num) const; 10 | int getCount() const; 11 | }; 12 | -------------------------------------------------------------------------------- /basic_content/const/class_const/first_example/main.cpp: -------------------------------------------------------------------------------- 1 | #include "apple.h" 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | Apple a(2); 7 | cout << a.getCount() << endl; 8 | a.add(10); 9 | const Apple b(3); 10 | b.add(100); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /basic_content/const/class_const/overload_example/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/const/class_const/overload_example:main` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") 3 | 4 | cc_library( 5 | name = "apple", 6 | srcs = ["apple.cpp"], 7 | hdrs = ["apple.h"], 8 | ) 9 | 10 | # Don't panic if you get compilation errors, this is what this code demonstrates, as expected. 11 | cc_binary( 12 | name = "main", 13 | srcs = ["main.cpp"], 14 | deps = [ 15 | ":apple", 16 | ], 17 | copts = ["-std=c++11"] 18 | ) -------------------------------------------------------------------------------- /basic_content/const/class_const/overload_example/apple.cpp: -------------------------------------------------------------------------------- 1 | #include "apple.h" 2 | #include 3 | 4 | int Apple::apple_number = 10; 5 | 6 | Apple::Apple(int i) { apple_number = i; } 7 | int Apple::add() { 8 | take(1); 9 | return 0; 10 | } 11 | int Apple::add(int num) const { 12 | take(num); 13 | return num; 14 | } 15 | void Apple::take(int num) const { 16 | std::cout << "take func " << num << std::endl; 17 | } 18 | int Apple::getCount() const { 19 | take(1); 20 | add(); // error 21 | return apple_number; 22 | } -------------------------------------------------------------------------------- /basic_content/const/class_const/overload_example/apple.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Apple { 4 | public: 5 | Apple(int i); 6 | static int apple_number; 7 | void take(int num) const; 8 | int add(); 9 | int add(int num) const; 10 | int getCount() const; 11 | }; 12 | -------------------------------------------------------------------------------- /basic_content/const/class_const/overload_example/main.cpp: -------------------------------------------------------------------------------- 1 | #include "apple.h" 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | Apple a(2); 7 | cout << a.getCount() << endl; 8 | a.add(10); 9 | // const Apple b(3); 10 | // b.add(); // error 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /basic_content/const/class_const/static_example/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/const/class_const/static_example:main` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") 3 | 4 | cc_library( 5 | name = "apple", 6 | srcs = ["apple.cpp"], 7 | hdrs = ["apple.h"], 8 | ) 9 | 10 | cc_binary( 11 | name = "main", 12 | srcs = ["main.cpp"], 13 | deps = [ 14 | ":apple", 15 | ], 16 | copts = ["-std=c++11"] 17 | ) -------------------------------------------------------------------------------- /basic_content/const/class_const/static_example/apple.cpp: -------------------------------------------------------------------------------- 1 | #include "apple.h" 2 | #include 3 | 4 | const int Apple::apple_number = 10; 5 | int Apple::ap = 666; 6 | Apple::Apple(int i) {} 7 | int Apple::add(int num) { 8 | take(num); 9 | return 0; 10 | } 11 | int Apple::add(int num) const { 12 | take(num); 13 | return 0; 14 | } 15 | void Apple::take(int num) const { 16 | std::cout << "take func " << num << std::endl; 17 | } 18 | int Apple::getCount() const { 19 | take(1); 20 | // add(); //error 21 | return apple_number; 22 | } -------------------------------------------------------------------------------- /basic_content/const/class_const/static_example/apple.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | class Apple { 3 | public: 4 | Apple(int i); 5 | static int ap; // 在类实现文件中定义并初始化 6 | static const int apple_number; 7 | void take(int num) const; 8 | int add(int num); 9 | int add(int num) const; 10 | int getCount() const; 11 | }; 12 | -------------------------------------------------------------------------------- /basic_content/const/class_const/static_example/main.cpp: -------------------------------------------------------------------------------- 1 | #include "apple.h" 2 | #include 3 | int main() { 4 | Apple a(2); 5 | std::cout << a.getCount() << std::endl; 6 | std::cout << a.ap << std::endl; 7 | a.add(10); 8 | const Apple b(3); 9 | b.add(100); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /basic_content/const/const_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void f(const int i) { 5 | i = 10; // error: assignment of read-only parameter ‘i’ 6 | cout << i << endl; 7 | } 8 | 9 | int main() { f(1); } 10 | -------------------------------------------------------------------------------- /basic_content/const/const_num.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | const int b = 10; 5 | b = 0; // error 6 | const string s = "helloworld"; 7 | const int i, j = 0; 8 | } 9 | -------------------------------------------------------------------------------- /basic_content/const/extern_const/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/const/extern_const:const_file1` and `# please run `bazel run basic_content/const/extern_const:file` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | cc_binary( 5 | name = "const_file1", 6 | srcs = ["const_file1.cpp", "const_file2.cpp"], 7 | copts = ["-std=c++11"] 8 | ) 9 | 10 | cc_binary( 11 | name = "file", 12 | srcs = ["file1.cpp", "file2.cpp"], 13 | copts = ["-std=c++11"] 14 | ) -------------------------------------------------------------------------------- /basic_content/const/extern_const/const_file1.cpp: -------------------------------------------------------------------------------- 1 | extern const int ext = 12; -------------------------------------------------------------------------------- /basic_content/const/extern_const/const_file2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * by 光城 4 | * compile: g++ -o file const_file2.cpp const_file1.cpp 5 | * execute: ./file 6 | */ 7 | extern const int ext; 8 | int main() { std::cout << ext << std::endl; } -------------------------------------------------------------------------------- /basic_content/const/extern_const/file1.cpp: -------------------------------------------------------------------------------- 1 | int ext; -------------------------------------------------------------------------------- /basic_content/const/extern_const/file2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * by 光城 4 | * compile: g++ -o file file2.cpp file1.cpp 5 | * execute: ./file 6 | */ 7 | extern int ext; 8 | int main() { std::cout << (ext + 10) << std::endl; } -------------------------------------------------------------------------------- /basic_content/const/funciton_const/condition1/condition1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | const int *ptr; 6 | *ptr = 10; // error 7 | } 8 | -------------------------------------------------------------------------------- /basic_content/const/funciton_const/condition1/condition2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | const int p = 10; 6 | const void *vp = &p; 7 | void *vp = &p; // error 8 | } 9 | -------------------------------------------------------------------------------- /basic_content/const/funciton_const/condition1/condition3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | const int *ptr; 6 | int val = 3; 7 | ptr = &val; // ok 8 | int *ptr1 = &val; 9 | *ptr1 = 4; 10 | cout << *ptr << endl; 11 | } 12 | -------------------------------------------------------------------------------- /basic_content/const/funciton_const/condition2/condition1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int num = 0; 5 | int *const ptr = # // const指针必须初始化!且const指针的指向不能修改 6 | int *t = # 7 | *t = 1; 8 | cout << *ptr << endl; 9 | } 10 | -------------------------------------------------------------------------------- /basic_content/const/funciton_const/condition2/condition2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | const int num = 0; 5 | int *const ptr = # // error! const int* -> int* 6 | cout << *ptr << endl; 7 | } 8 | -------------------------------------------------------------------------------- /basic_content/const/funciton_const/condition2/condition3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | const int num = 10; 5 | const int *const ptr = # 6 | cout << *ptr << endl; 7 | } 8 | -------------------------------------------------------------------------------- /basic_content/const/funciton_const/condition3/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/const/funciton_const/condition3:condition1` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | # Don't panic if you get compilation errors, this is what this code demonstrates, as expected. 5 | cc_binary( 6 | name = "condition1", 7 | srcs = ["condition1.cpp"], 8 | copts = ["-std=c++11"] 9 | ) -------------------------------------------------------------------------------- /basic_content/const/funciton_const/condition3/condition1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | const int p = 3; 6 | const int *const ptr = &p; 7 | cout << *ptr << endl; 8 | } 9 | -------------------------------------------------------------------------------- /basic_content/decltype/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/decltype:decltype` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | cc_binary( 5 | name = "decltype", 6 | srcs = ["decltype.cpp"], 7 | copts = ["-std=c++11"] 8 | ) 9 | -------------------------------------------------------------------------------- /basic_content/enum/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/enum:classic_practice` 2 | # please run `bazel run basic_content/enum:tradition_color` 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "classic_practice", 7 | srcs = ["classic_practice.cpp"], 8 | copts = ["-std=c++11"] 9 | ) 10 | 11 | # Don't panic if you get compilation errors, this is what this code demonstrates, as expected. 12 | cc_binary( 13 | name = "tradition_color", 14 | srcs = ["tradition_color.cpp"], 15 | copts = ["-std=c++11"] 16 | ) -------------------------------------------------------------------------------- /basic_content/enum/tradition_color.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | enum Color { RED, BLUE }; 5 | enum Feeling { EXCITED, BLUE }; 6 | 7 | int main() { 8 | Color a = BLUE; // error 9 | Feeling b = EXCITED; 10 | std::cout << a << ":" << b << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /basic_content/explicit/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/explicit:explicit` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | # Don't panic if you get compilation errors, this is what this code demonstrates, as expected. 5 | cc_binary( 6 | name = "explicit", 7 | srcs = ["explicit.cpp"], 8 | copts = ["-std=c++11"] 9 | ) -------------------------------------------------------------------------------- /basic_content/explicit/README.md: -------------------------------------------------------------------------------- 1 | # explicit(显式)关键字那些事 2 | 3 | ## 关于作者: 4 | 5 | 个人公众号: 6 | 7 | ![](../img/wechat.jpg) 8 | 9 | - explicit 修饰构造函数时,可以防止隐式转换和复制初始化 10 | - explicit 修饰转换函数时,可以防止隐式转换,但按语境转换除外 11 | 12 | 13 | 代码参见:[.explicit.cpp](./explicit.cpp) 14 | 15 | 参考链接: 16 | > https://stackoverflow.com/questions/4600295/what-is-the-meaning-of-operator-bool-const 17 | -------------------------------------------------------------------------------- /basic_content/extern/extern_c++/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/extern/extern_c++:main` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") 3 | 4 | cc_library( 5 | name = "add", 6 | srcs = ["add.c"], 7 | hdrs = ["add.h"], 8 | ) 9 | 10 | cc_binary( 11 | name = "main", 12 | srcs = ["add.cpp"], 13 | deps = [":add"], 14 | copts = ["-std=c++11"] 15 | ) -------------------------------------------------------------------------------- /basic_content/extern/extern_c++/add.c: -------------------------------------------------------------------------------- 1 | #include "add.h" 2 | 3 | int add(int x,int y) { 4 | return x+y; 5 | } 6 | -------------------------------------------------------------------------------- /basic_content/extern/extern_c++/add.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | extern "C" { 4 | #include "add.h" 5 | } 6 | int main() { 7 | std::cout << add(2, 3) << std::endl; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /basic_content/extern/extern_c++/add.h: -------------------------------------------------------------------------------- 1 | #ifndef ADD_H 2 | #define ADD_H 3 | extern int add(int x, int y); 4 | #endif 5 | -------------------------------------------------------------------------------- /basic_content/extern/extern_c/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/extern/extern_c:main` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") 3 | 4 | cc_library( 5 | name = "add", 6 | srcs = ["add.cpp"], 7 | hdrs = ["add.h"], 8 | ) 9 | 10 | cc_binary( 11 | name = "main", 12 | srcs = ["add.c"], 13 | deps = [":add"], 14 | ) -------------------------------------------------------------------------------- /basic_content/extern/extern_c/add.c: -------------------------------------------------------------------------------- 1 | extern int add(int x,int y); 2 | int main() { 3 | add(2,3); 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /basic_content/extern/extern_c/add.cpp: -------------------------------------------------------------------------------- 1 | #include "add.h" 2 | 3 | int add(int x, int y) { return x + y; } 4 | -------------------------------------------------------------------------------- /basic_content/extern/extern_c/add.h: -------------------------------------------------------------------------------- 1 | #ifndef ADD_H 2 | #define ADD_H 3 | extern "C" { 4 | int add(int x, int y); 5 | } 6 | #endif 7 | -------------------------------------------------------------------------------- /basic_content/friend/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/friend:friend_class` 2 | # please run `bazel run basic_content/friend:friend_func` 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "friend_class", 7 | srcs = ["friend_class.cpp"], 8 | copts = ["-std=c++11"] 9 | ) 10 | 11 | cc_binary( 12 | name = "friend_func", 13 | srcs = ["friend_func.cpp"], 14 | copts = ["-std=c++11"] 15 | ) -------------------------------------------------------------------------------- /basic_content/friend/friend_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class A { 6 | public: 7 | A(int _a) : a(_a){}; 8 | friend class B; 9 | 10 | private: 11 | int a; 12 | }; 13 | 14 | class B { 15 | public: 16 | int getb(A ca) { return ca.a; }; 17 | }; 18 | 19 | int main() { 20 | A a(3); 21 | B b; 22 | cout << b.getb(a) << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /basic_content/friend/friend_func.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class A { 6 | public: 7 | A(int _a) : a(_a){}; 8 | friend int geta(A &ca); ///< 友元函数 9 | private: 10 | int a; 11 | }; 12 | 13 | int geta(A &ca) { return ca.a; } 14 | 15 | int main() { 16 | A a(3); 17 | cout << geta(a) << endl; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /basic_content/func_pointer/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/func_pointer:func_pointer` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | cc_binary( 5 | name = "func_pointer", 6 | srcs = ["func_pointer.cpp"], 7 | copts = ["-std=c++11"] 8 | ) -------------------------------------------------------------------------------- /basic_content/func_pointer/func_pointer.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file func_pointer.cpp 3 | * @brief 函数指针的使用! 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-20 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | /** 13 | * @brief 14 | * 定义了一个变量pFun,这个变量是个指针,指向返回值为空和参数为int的函数的指针! 15 | */ 16 | void (*pFun)(int); 17 | 18 | /** 19 | * @brief 代表一种新类型,不是变量!所以与上述的pFun不一样! 20 | */ 21 | typedef void (*func)(void); 22 | 23 | void myfunc(void) { cout << "asda" << endl; } 24 | 25 | void glFun(int a) { cout << a << endl; } 26 | int main() { 27 | func pfun = myfunc; /*赋值*/ 28 | pfun(); /*调用*/ 29 | pFun = glFun; 30 | (*pFun)(2); 31 | } 32 | -------------------------------------------------------------------------------- /basic_content/img/wechat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/basic_content/img/wechat.jpg -------------------------------------------------------------------------------- /basic_content/inline/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/inline:inline_virtual` 2 | # please run `bazel run basic_content/inline:main` 3 | load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") 4 | 5 | cc_library( 6 | name = "inline", 7 | hdrs = ["inline.h"], 8 | ) 9 | 10 | cc_binary( 11 | name = "inline_virtual", 12 | srcs = ["inline_virtual.cpp"], 13 | copts = ["-std=c++11"] 14 | ) 15 | 16 | cc_binary( 17 | name = "main", 18 | srcs = ["inline.cpp"], 19 | deps = [ 20 | ":inline", 21 | ], 22 | copts = ["-std=c++11"] 23 | ) -------------------------------------------------------------------------------- /basic_content/inline/inline.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | class A { 3 | public: 4 | void f1(int x); 5 | 6 | /** 7 | * @brief 8 | * 类中定义了的函数是隐式内联函数,声明要想成为内联函数,必须在实现处(定义处)加inline关键字。 9 | * 10 | * @param x 11 | * @param y 12 | */ 13 | void Foo(int x, int y) ///< 定义即隐式内联函数! 14 | { 15 | 16 | }; 17 | void f2(int x); ///< 声明后,要想成为内联函数,必须在定义处加inline关键字。 18 | }; 19 | -------------------------------------------------------------------------------- /basic_content/inline/inline_virtual.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Base { 4 | public: 5 | inline virtual void who() { cout << "I am Base\n"; } 6 | virtual ~Base() {} 7 | }; 8 | class Derived : public Base { 9 | public: 10 | inline void who() // 不写inline时隐式内联 11 | { 12 | cout << "I am Derived\n"; 13 | } 14 | }; 15 | 16 | int main() { 17 | // 此处的虚函数 18 | // who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。 19 | Base b; 20 | b.who(); 21 | 22 | // 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。 23 | Base *ptr = new Derived(); 24 | ptr->who(); 25 | 26 | // 因为Base有虚析构函数(virtual ~Base() {}),所以 delete 27 | // 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。 28 | delete ptr; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /basic_content/macro/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/macro:do_while` 2 | # please run `bazel run basic_content/sig_examp:sig_examp` 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "do_while", 7 | srcs = ["do_while.cpp"], 8 | copts = ["-std=c++11"], 9 | ) 10 | 11 | cc_binary( 12 | name = "sig_examp", 13 | srcs = ["sig_examp.cpp"], 14 | copts = ["-std=c++11"], 15 | ) 16 | -------------------------------------------------------------------------------- /basic_content/maohao/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/maohao:maohao` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | cc_binary( 5 | name = "maohao", 6 | srcs = ["maohao.cpp"], 7 | copts = ["-std=c++11"] 8 | ) -------------------------------------------------------------------------------- /basic_content/maohao/README.md: -------------------------------------------------------------------------------- 1 | # :: 范围解析运算符那些事 2 | 3 | ## 关于作者: 4 | 5 | 个人公众号: 6 | 7 | ![](../img/wechat.jpg) 8 | 9 | - 全局作用域符(::name):用于类型名称(类、类成员、成员函数、变量等)前,表示作用域为全局命名空间 10 | - 类作用域符(class::name):用于表示指定类型的作用域范围是具体某个类的 11 | - 命名空间作用域符(namespace::name):用于表示指定类型的作用域范围是具体某个命名空间的 12 | 13 | 具体代码见:[maohao.cpp](maohao.cpp) 14 | 15 | -------------------------------------------------------------------------------- /basic_content/maohao/maohao.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int count = 0; // 全局(::)的count 5 | 6 | class A { 7 | public: 8 | static int count; // 类A的count (A::count) 9 | }; 10 | // 静态变量必须在此处定义 11 | int A::count; 12 | int main() { 13 | ::count = 1; // 设置全局的count为1 14 | A::count = 5; // 设置类A的count为2 15 | cout << A::count << endl; 16 | // int count=3; // 局部count 17 | // count=4; // 设置局部的count为4 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /basic_content/pointer_refer/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/pointer_refer:copy_construct` 2 | # please run `bazel run basic_content/pointer_refer:effec` 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "copy_construct", 7 | srcs = ["copy_construct.cpp"], 8 | copts = ["-std=c++11", "-fno-elide-constructors"] 9 | ) 10 | 11 | cc_binary( 12 | name = "effec", 13 | srcs = ["effec.cpp"], 14 | copts = ["-std=c++11"] 15 | ) -------------------------------------------------------------------------------- /basic_content/pointer_refer/effec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void test1(int *p) { 4 | *p = 3; //此处应该首先判断p是否为空,为了测试的需要,此处我们没加。 5 | return; 6 | } 7 | 8 | void test2(int &p) { 9 | p = 3; //此处应该首先判断p是否为空,为了测试的需要,此处我们没加。 10 | return; 11 | } 12 | 13 | int main() { 14 | int a = 10; 15 | int *p = &a; 16 | test1(p); 17 | test2(a); 18 | cout << a << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /basic_content/sizeof/blackclass.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file blackclass.cpp 3 | * @brief 空类的大小为1字节 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-21 7 | */ 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | class A {}; 13 | int main() { 14 | cout << sizeof(A) << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /basic_content/sizeof/genA.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file genA.cpp 3 | * @brief 4 | * 普通成员函数,大小为1,一个类中,虚函数本身、成员函数(包括静态与非静态)和静态数据成员都是不占用类对象的存储空间。 5 | * @author 光城 6 | * @version v1 7 | * @date 2019-07-21 8 | */ 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | class A { 15 | public: 16 | A(); 17 | ~A(); 18 | static int a; 19 | static void fun3(); 20 | void fun(); 21 | void fun1(); 22 | }; 23 | 24 | int main() { 25 | cout << sizeof(A) << endl; // 1 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /basic_content/sizeof/moreinhe.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file moreinhe.cpp 3 | * @brief 普通多继承与虚函数多继承 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-21 7 | */ 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | class A { 14 | public: 15 | char a; 16 | int b; 17 | }; 18 | 19 | class B { 20 | public: 21 | short a; 22 | long b; 23 | }; 24 | 25 | /** 26 | * @brief 8+16+8=32 27 | */ 28 | class C : A, B { 29 | char c; 30 | }; 31 | 32 | int main() { 33 | cout << sizeof(A) << endl; // 8 34 | cout << sizeof(B) << endl; // 16 35 | cout << sizeof(C) << endl; // 32 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /basic_content/sizeof/morevir.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file morevir.cpp 3 | * @brief 对于包含虚函数的类,不管有多少个虚函数,只有一个虚指针,vptr的大小。 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-21 7 | */ 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | class A { 14 | 15 | virtual void fun(); 16 | virtual void fun1(); 17 | virtual void fun2(); 18 | virtual void fun3(); 19 | }; 20 | int main() { 21 | cout << sizeof(A) << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /basic_content/sizeof/static.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file static.cpp 3 | * @brief 静态数据成员 4 | * 静态数据成员被编译器放在程序的一个global data 5 | * members中,它是类的一个数据成员,但不影响类的大小。不管这个类产生了多少个实例,还是派生了多少新的类,静态数据成员只有一个实例。静态数据成员,一旦被声明,就已经存在。 6 | * @author 光城 7 | * @version v1 8 | * @date 2019-07-21 9 | */ 10 | 11 | #include 12 | using namespace std; 13 | class A { 14 | public: 15 | char b; 16 | virtual void fun(){}; 17 | static int c; 18 | static int d; 19 | static int f; 20 | }; 21 | 22 | int main() { 23 | 24 | /** 25 | * @brief 16 字节对齐、静态变量不影响类的大小、vptr指针=8 26 | */ 27 | cout << sizeof(A) << endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /basic_content/sizeof/virinhe.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file virnhe.cpp 3 | * @brief 虚继承 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-21 7 | */ 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | class A { 14 | virtual void fun() {} 15 | }; 16 | 17 | class B { 18 | virtual void fun2() {} 19 | }; 20 | class C : virtual public A, virtual public B { 21 | public: 22 | virtual void fun3() {} 23 | }; 24 | 25 | int main() { 26 | 27 | /** 28 | * @brief 8 8 16 派生类虚继承多个虚函数,会继承所有虚函数的vptr 29 | */ 30 | cout << sizeof(A) << " " << sizeof(B) << " " << sizeof(C); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /basic_content/sizeof/virmoreinhe.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file virmoreinhe.cpp 3 | * @brief 虚函数多继承 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-21 7 | */ 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | class A { 14 | virtual void fun() {} 15 | }; 16 | 17 | class B { 18 | virtual void fun2() {} 19 | }; 20 | class C : public A, public B { 21 | public: 22 | virtual void fun3() {} 23 | }; 24 | 25 | int main() { 26 | 27 | /** 28 | * @brief 8 8 16 派生类继承多个虚函数,会继承所有虚函数的vptr 29 | */ 30 | cout << sizeof(A) << " " << sizeof(B) << " " << sizeof(C); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /basic_content/static/nostatic_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Apple { 5 | int i; 6 | 7 | public: 8 | Apple() { 9 | i = 0; 10 | cout << "Inside Constructor\n"; 11 | } 12 | ~Apple() { cout << "Inside Destructor\n"; } 13 | }; 14 | 15 | int main() { 16 | int x = 0; 17 | if (x == 0) { 18 | Apple obj; 19 | } 20 | cout << "End of main\n"; 21 | } 22 | -------------------------------------------------------------------------------- /basic_content/static/static_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Apple { 5 | int i; 6 | 7 | public: 8 | Apple() { 9 | i = 0; 10 | cout << "Inside Constructor\n"; 11 | } 12 | ~Apple() { cout << "Inside Destructor\n"; } 13 | }; 14 | 15 | int main() { 16 | int x = 0; 17 | if (x == 0) { 18 | static Apple obj; 19 | } 20 | cout << "End of main\n"; 21 | } 22 | -------------------------------------------------------------------------------- /basic_content/static/static_demo.cpp: -------------------------------------------------------------------------------- 1 | // the use of static Static 2 | // variables in a Function 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void demo() { 8 | // static variable 9 | static int count = 0; 10 | cout << count << " "; 11 | 12 | // value is updated and 13 | // will be carried to next 14 | // function calls 15 | count++; 16 | } 17 | 18 | int main() { 19 | for (int i = 0; i < 5; i++) 20 | demo(); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /basic_content/static/static_error_variable.cpp: -------------------------------------------------------------------------------- 1 | // variables inside a class 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Apple { 7 | public: 8 | static int i; 9 | 10 | Apple(){ 11 | // Do nothing 12 | }; 13 | }; 14 | 15 | int main() { 16 | Apple obj1; 17 | Apple obj2; 18 | obj1.i = 2; 19 | obj2.i = 3; 20 | 21 | // prints value of i 22 | cout << obj1.i << " " << obj2.i; 23 | } 24 | -------------------------------------------------------------------------------- /basic_content/static/static_funciton.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Apple { 5 | public: 6 | // static member function 7 | static void printMsg() { cout << "Welcome to Apple!"; } 8 | }; 9 | 10 | // main function 11 | int main() { 12 | // invoking a static member function 13 | Apple::printMsg(); 14 | } 15 | -------------------------------------------------------------------------------- /basic_content/static/static_variable.cpp: -------------------------------------------------------------------------------- 1 | // variables inside a class 2 | 3 | #include 4 | using namespace std; 5 | 6 | class GfG { 7 | public: 8 | static int i; 9 | 10 | GfG(){ 11 | // Do nothing 12 | }; 13 | }; 14 | 15 | int GfG::i = 1; 16 | 17 | int main() { 18 | GfG obj; 19 | // prints value of i 20 | cout << obj.i; 21 | } 22 | -------------------------------------------------------------------------------- /basic_content/struct/ext_struct_func.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | struct Base { 5 | int v1; 6 | // private: //error! 7 | int v3; 8 | 9 | public: // 显示声明public 10 | int v2; 11 | virtual void print() { printf("%s\n", "Base"); }; 12 | Base() { cout << "Base construct" << endl; }; 13 | virtual ~Base() { cout << "Base deconstruct" << endl; }; 14 | }; 15 | 16 | struct Derived : Base { 17 | 18 | Derived() { cout << "Derived construct" << endl; }; 19 | virtual ~Derived() { cout << "Derived deconstruct" << endl; }; 20 | 21 | public: 22 | int v2; 23 | void print() { printf("%s\n", "Derived"); }; 24 | }; 25 | 26 | int main() { 27 | Base *b = new Derived(); 28 | b->print(); 29 | delete b; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /basic_content/struct/struct_func.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Base 4 | { // public 5 | int v1; 6 | // public: //error 7 | int v2; 8 | // private: 9 | int v3; 10 | // void print(){ // c中不能在结构体中嵌入函数 11 | // printf("%s\n","hello world"); 12 | // }; //error! 13 | }; 14 | 15 | void Base() 16 | { 17 | printf("%s\n", "I am Base func"); 18 | } 19 | // struct Base base1; //ok 20 | // Base base2; //error 21 | 22 | int main() 23 | { 24 | struct Base base; 25 | base.v1 = 1; 26 | // base.print(); 27 | printf("%d\n", base.v1); 28 | Base(); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /basic_content/struct/struct_func.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Base { 5 | int v1; 6 | // private: //error! 7 | int v3; 8 | 9 | public: // 显示声明public 10 | int v2; 11 | void print() { printf("%s\n", "hello world"); }; 12 | }; 13 | 14 | int main() { 15 | struct Base base1; // ok 16 | Base base2; // ok 17 | Base base; 18 | base.v1 = 1; 19 | base.v3 = 2; 20 | base.print(); 21 | printf("%d\n", base.v1); 22 | printf("%d\n", base.v3); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /basic_content/struct/stu: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/basic_content/struct/stu -------------------------------------------------------------------------------- /basic_content/struct_class/README.md: -------------------------------------------------------------------------------- 1 | # struct与class区别 2 | 3 | ## 关于作者: 4 | 5 | 个人公众号: 6 | 7 | ![](../img/wechat.jpg) 8 | 9 | 关于C与C++中struct内容:见[struct那些事](../struct) 10 | 11 | 总的来说,struct 更适合看成是一个数据结构的实现体,class 更适合看成是一个对象的实现体。 12 | 13 | 区别: 14 | 15 | 最本质的一个区别就是默认的访问控制 16 | 17 | 默认的继承访问权限。struct 是 public 的,class 是 private 的。 18 | 19 | struct 作为数据结构的实现体,它默认的数据访问控制是 public 的,而 class 作为对象的实现体,它默认的成员变量访问控制是 private 的。 20 | -------------------------------------------------------------------------------- /basic_content/this/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/this:person` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | cc_binary( 5 | name = "person", 6 | srcs = ["person.cpp"], 7 | copts = ["-std=c++11"] 8 | ) -------------------------------------------------------------------------------- /basic_content/this/img/constthis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/basic_content/this/img/constthis.png -------------------------------------------------------------------------------- /basic_content/this/img/genthis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/basic_content/this/img/genthis.png -------------------------------------------------------------------------------- /basic_content/this/img/thiscontrust.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/basic_content/this/img/thiscontrust.png -------------------------------------------------------------------------------- /basic_content/this/person.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | class Person { 6 | public: 7 | typedef enum { BOY = 0, GIRL } SexType; 8 | Person(char *n, int a, SexType s) { 9 | name = new char[strlen(n) + 1]; 10 | strcpy(name, n); 11 | age = a; 12 | sex = s; 13 | } 14 | int get_age() const { return this->age; } 15 | Person &add_age(int a) { 16 | age += a; 17 | return *this; 18 | } 19 | ~Person() { delete[] name; } 20 | 21 | private: 22 | char *name; 23 | int age; 24 | SexType sex; 25 | }; 26 | 27 | int main() { 28 | Person p("zhangsan", 20, Person::BOY); 29 | cout << p.get_age() << endl; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /basic_content/union/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/union:union` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | cc_binary( 5 | name = "union", 6 | srcs = ["union.cpp"], 7 | copts = ["-std=c++11"] 8 | ) -------------------------------------------------------------------------------- /basic_content/union/README.md: -------------------------------------------------------------------------------- 1 | # UNION那些事 2 | 3 | ## 关于作者: 4 | 5 | 个人公众号: 6 | 7 | ![](../img/wechat.jpg) 8 | 9 | 联合(union)是一种节省空间的特殊的类,一个 union 可以有多个数据成员,但是在任意时刻只有一个数据成员可以有值。当某个成员被赋值后其他成员变为未定义状态。联合有如下特点: 10 | 11 | - 默认访问控制符为 public 12 | - 可以含有构造函数、析构函数 13 | - 不能含有引用类型的成员 14 | - 不能继承自其他类,不能作为基类 15 | - 不能含有虚函数 16 | - 匿名 union 在定义所在作用域可直接访问 union 成员 17 | - 匿名 union 不能包含 protected 成员或 private 成员 18 | - 全局匿名联合必须是静态(static)的 19 | -------------------------------------------------------------------------------- /basic_content/using/using_derived.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file using_derived.cpp 3 | * @brief 函数重装 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-08-07 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | class Base { 13 | public: 14 | void f() { cout << "f()" << endl; } 15 | void f(int n) { cout << "Base::f(int)" << endl; } 16 | }; 17 | 18 | class Derived : private Base { 19 | public: 20 | using Base::f; 21 | void f(int n) { cout << "Derived::f(int)" << endl; } 22 | }; 23 | 24 | int main() { 25 | Derived d; 26 | d.f(); 27 | d.f(1); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /basic_content/using/using_global.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file using_global.cpp 3 | * @brief using各种使用 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-08-07 7 | */ 8 | 9 | #include 10 | #define isNs1 1 11 | // #define isGlobal 2 12 | using namespace std; 13 | void func() { cout << "::func" << endl; } 14 | 15 | namespace ns1 { 16 | void func() { cout << "ns1::func" << endl; } 17 | } // namespace ns1 18 | 19 | namespace ns2 { 20 | #ifdef isNs1 21 | using ns1::func; /// ns1中的函数 22 | #elif isGlobal 23 | using ::func; /// 全局中的函数 24 | #else 25 | void func() { cout << "other::func" << endl; } 26 | #endif 27 | } // namespace ns2 28 | 29 | int main() { 30 | /** 31 | * 这就是为什么在c++中使用了cmath而不是math.h头文件 32 | */ 33 | ns2::func(); // 会根据当前环境定义宏的不同来调用不同命名空间下的func()函数 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /basic_content/using/using_typedef.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file using_typedef.cpp 3 | * @brief bazel run basic_content/using:using_typedef 4 | * 取代typedef,使用using来定义别名 5 | * @author 光城 6 | * @version v1 7 | * @date 2019-08-07 8 | */ 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | typedef vector V1; 15 | using V2 = vector; 16 | 17 | int main() { 18 | int nums1[] = {1, 2, 3, 4, 5, 6}; 19 | V1 vec1(nums1, nums1 + sizeof(nums1) / sizeof(int)); 20 | int nums2[] = {5, 7, 6}; 21 | V2 vec2(nums2, nums2 + sizeof(nums2) / sizeof(int)); 22 | 23 | for (auto i : vec1) 24 | cout << i << " "; 25 | cout << endl; 26 | 27 | for (auto i : vec2) 28 | cout << i << " "; 29 | cout << endl; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /basic_content/virtual/set1/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/virtual/set1:emp` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | cc_binary( 5 | name = "emp", 6 | srcs = ["emp.cpp"], 7 | copts = ["-std=c++11"] 8 | ) -------------------------------------------------------------------------------- /basic_content/virtual/set2/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/virtual/set2:default_arg` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | cc_binary( 5 | name = "default_arg", 6 | srcs = ["default_arg.cpp"], 7 | copts = ["-std=c++11"] 8 | ) -------------------------------------------------------------------------------- /basic_content/virtual/set2/default_arg.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file first_example.cpp 3 | * @brief 虚函数中默认参数 4 | * 规则:虚函数是动态绑定的,默认参数是静态绑定的。默认参数的使用需要看指针或者应用本身的类型,而不是对象的类型! 5 | * @author 光城 6 | * @version v1 7 | * @date 2019-07-24 8 | */ 9 | 10 | #include 11 | using namespace std; 12 | 13 | class Base { 14 | public: 15 | virtual void fun(int x = 10) { cout << "Base::fun(), x = " << x << endl; } 16 | }; 17 | 18 | class Derived : public Base { 19 | public: 20 | virtual void fun(int x = 20) { cout << "Derived::fun(), x = " << x << endl; } 21 | }; 22 | 23 | int main() { 24 | Derived d1; 25 | Base *bp = &d1; 26 | bp->fun(); // 10 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /basic_content/virtual/set3/copy_consrtuct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Base { 5 | public: 6 | }; 7 | 8 | class Derived : public Base { 9 | public: 10 | Derived() { cout << "Derived created" << endl; } 11 | 12 | Derived(const Derived &rhs) { 13 | cout << "Derived created by deep copy" << endl; 14 | } 15 | 16 | ~Derived() { cout << "Derived destroyed" << endl; } 17 | }; 18 | 19 | int main() { 20 | Derived s1; 21 | 22 | Derived s2 = s1; // Compiler invokes "copy constructor" 23 | // Type of s1 and s2 are concrete to compiler 24 | 25 | // How can we create Derived1 or Derived2 object 26 | // from pointer (reference) to Base class pointing Derived object? 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /basic_content/virtual/set3/inline_virtual.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Base { 4 | public: 5 | inline virtual void who() { cout << "I am Base\n"; } 6 | virtual ~Base() {} 7 | }; 8 | class Derived : public Base { 9 | public: 10 | inline void who() // 不写inline时隐式内联 11 | { 12 | cout << "I am Derived\n"; 13 | } 14 | }; 15 | 16 | int main() { 17 | // 此处的虚函数 18 | // who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。 19 | Base b; 20 | b.who(); 21 | 22 | // 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。 23 | Base *ptr = new Derived(); 24 | ptr->who(); 25 | 26 | // 因为Base有虚析构函数(virtual ~Base() {}),所以 delete 27 | // 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。 28 | delete ptr; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /basic_content/virtual/set3/static_error.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file static_error.cpp 3 | * @brief 静态函数不可以声明为虚函数,同时也不能被const和volatile关键字修饰! 4 | * 原因如下: 5 | * static成员函数不属于任何类对象或类实例,所以即使给此函数加上virutal也是没有任何意义 6 | * 虚函数依靠vptr和vtable来处理。vptr是一个指针,在类的构造函数中创建生成,并且只能用this指针来访问它,静态成员函数没有this指针,所以无法访问vptr。 7 | * @author 光城 8 | * @version v1 9 | * @date 2019-07-24 10 | */ 11 | 12 | virtual static void fun() {} 13 | static void fun() const {} 14 | -------------------------------------------------------------------------------- /basic_content/virtual/set3/virtual_function.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file virtual_function.cpp 3 | * @brief 虚函数可以被私有化,但有一些细节需要注意。 4 | * 基类指针指向继承类对象,则调用继承类对象的函数; 5 | * int main()必须声明为Base类的友元,否则编译失败。 编译器报错: 6 | * ptr无法访问私有函数。 当然,把基类声明为public, 7 | * 继承类为private,该问题就不存在了。----> 见另外一个例子! 8 | * @author 光城 9 | * @version v1 10 | * @date 2019-07-24 11 | */ 12 | 13 | #include 14 | using namespace std; 15 | 16 | class Derived; 17 | 18 | class Base { 19 | private: 20 | virtual void fun() { cout << "Base Fun"; } 21 | friend int main(); 22 | }; 23 | 24 | class Derived : public Base { 25 | public: 26 | void fun() { cout << "Derived Fun"; } 27 | }; 28 | 29 | int main() { 30 | Base *ptr = new Derived; 31 | ptr->fun(); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /basic_content/virtual/set3/virtual_function1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Derived; 5 | 6 | class Base { 7 | public: 8 | virtual void fun() { cout << "Base Fun"; } 9 | // friend int main(); 10 | }; 11 | 12 | class Derived : public Base { 13 | private: 14 | void fun() { cout << "Derived Fun"; } 15 | }; 16 | 17 | int main() { 18 | Base *ptr = new Derived; 19 | ptr->fun(); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /basic_content/virtual/set4/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/virtual/set4:rtti` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | cc_binary( 5 | name = "rtti", 6 | srcs = ["rtti.cpp"], 7 | copts = ["-std=c++11"] 8 | ) -------------------------------------------------------------------------------- /basic_content/virtual/set4/warn_rtti.cpp: -------------------------------------------------------------------------------- 1 | // 在使用时需要注意:被转换对象obj的类型T1必须是多态类型,即T1必须公有继承自其它类,或者T1拥有虚函数(继承或自定义)。若T1为非多态类型,使用dynamic_cast会报编译错误。 2 | 3 | // A为非多态类型 4 | 5 | class A {}; 6 | 7 | // B为多态类型 8 | 9 | class B { 10 | 11 | public: 12 | virtual ~B() {} 13 | }; 14 | 15 | // D为非多态类型 16 | 17 | class D : public A {}; 18 | 19 | // E为非多态类型 20 | 21 | class E : private A {}; 22 | 23 | // F为多态类型 24 | 25 | class F : private B {}; 26 | -------------------------------------------------------------------------------- /basic_content/volatile/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/volatile:noopt_vola` 2 | # please run `bazel run basic_content/volatile:volatile` 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "noopt_vola", 7 | srcs = ["noopt_vola.cpp"], 8 | copts = ["-std=c++11"] 9 | ) 10 | 11 | cc_binary( 12 | name = "volatile", 13 | srcs = ["volatile.cpp"], 14 | copts = ["-std=c++11"] 15 | ) -------------------------------------------------------------------------------- /basic_content/volatile/noopt_vola.cpp: -------------------------------------------------------------------------------- 1 | /* Compile code without optimization option */ 2 | #include 3 | int main(void) { 4 | const int local = 10; 5 | int *ptr = (int *)&local; 6 | 7 | printf("Initial value of local : %d \n", local); 8 | 9 | *ptr = 100; 10 | 11 | printf("Modified value of local: %d \n", local); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /basic_content/volatile/volatile.cpp: -------------------------------------------------------------------------------- 1 | /* Compile code with optimization option */ 2 | #include 3 | 4 | int main(void) { 5 | const volatile int local = 10; 6 | int *ptr = (int *)&local; 7 | 8 | printf("Initial value of local : %d \n", local); 9 | 10 | *ptr = 100; 11 | 12 | printf("Modified value of local: %d \n", local); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /basic_content/vptr_vtable/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run basic_content/vptr_vtable:vptr1` 2 | load("@rules_cc//cc:defs.bzl", "cc_binary") 3 | 4 | cc_binary( 5 | name = "vptr1", 6 | srcs = ["vptr1.cpp"], 7 | copts = ["-std=c++11"] 8 | ) -------------------------------------------------------------------------------- /basic_content/vptr_vtable/img/base.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/basic_content/vptr_vtable/img/base.jpg -------------------------------------------------------------------------------- /codingStyleIdioms/1_classInitializers/1.3_const.cpp: -------------------------------------------------------------------------------- 1 | /* 1.3_类中const数据成员、引用数据成员.cpp */ 2 | // 3 | // Created by light on 19-12-9. 4 | // 5 | 6 | #include 7 | 8 | class Animal { 9 | public: 10 | Animal(int age,std::string name):age_(age),name_(name) { 11 | std::cout << "Animal(int age) is called" << std::endl; 12 | } 13 | private: 14 | int &age_; 15 | const std::string name_; 16 | }; 17 | 18 | int main() { 19 | Animal animal(10,"hh"); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /codingStyleIdioms/1_classInitializers/initializer.cpp: -------------------------------------------------------------------------------- 1 | /* initializer.cpp */ 2 | // 3 | // Created by light on 19-12-9. 4 | // 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | class A { 11 | public: 12 | A(int a) : _a(a), _p(nullptr) { // 初始化列表 13 | 14 | } 15 | 16 | private: 17 | int _a; 18 | int *_p; 19 | }; 20 | 21 | int main() { 22 | A aa(10); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /codingStyleIdioms/2_enumclass/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //codingStyleIdioms/2_enumclass:namespace` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "namespace", 7 | srcs = ["namespace.cpp"], 8 | copts = ["-std=c++11"], 9 | ) 10 | -------------------------------------------------------------------------------- /codingStyleIdioms/3_RAII/c++_example.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-12. 3 | // 4 | #include 5 | int main() { 6 | std::string str = std::string ("toptal"); 7 | std::cout << "string object: " << str << " @ " << &str << "\n"; 8 | 9 | str += ".com"; 10 | std::cout << "string object: " << str << " @ " << &str << "\n"; 11 | 12 | return(0); 13 | } -------------------------------------------------------------------------------- /codingStyleIdioms/3_RAII/c_example.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-12. 3 | // 4 | #include 5 | #include 6 | #include 7 | int main() { 8 | char *str = (char *) malloc(7); 9 | strcpy(str, "toptal"); 10 | printf("char array = \"%s\" @ %u\n", str, str); 11 | 12 | str = (char *) realloc(str, 11); 13 | strcat(str, ".com"); 14 | printf("char array = \"%s\" @ %u\n", str, str); 15 | 16 | free(str); 17 | 18 | return(0); 19 | } -------------------------------------------------------------------------------- /codingStyleIdioms/4_copy-swap/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //codingStyleIdioms/4_copy-swap:copy-swapAndADL` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "copy-swapAndADL", 7 | srcs = ["copy-swapAndADL.cpp"], 8 | copts = ["-std=c++11"], 9 | ) 10 | -------------------------------------------------------------------------------- /codingStyleIdioms/5_pImpl/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //codingStyleIdioms/5_pImpl:pimpl` 2 | # please run `bazel run //codingStyleIdioms/5_pImpl:pimplTime` 3 | # please run `bazel run //codingStyleIdioms/5_pImpl:noPimpl` 4 | 5 | load("@rules_cc//cc:defs.bzl", "cc_binary") 6 | 7 | cc_binary( 8 | name = "pimpl", 9 | srcs = ["pimpl.cpp"], 10 | copts = ["-std=c++11"], 11 | ) 12 | cc_binary( 13 | name = "pimplTime", 14 | srcs = ["pimplTime.cpp"], 15 | copts = ["-std=c++11"], 16 | ) 17 | cc_binary( 18 | name = "noPimpl", 19 | srcs = ["noPimpl.cpp"], 20 | copts = ["-std=c++11"], 21 | ) 22 | -------------------------------------------------------------------------------- /codingStyleIdioms/5_pImpl/noPimpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class C { 6 | vector v; 7 | string s; 8 | }; 9 | class D { 10 | string s; 11 | }; 12 | 13 | class X { 14 | private: 15 | C c; 16 | D d; 17 | }; 18 | 19 | 20 | int main() { 21 | 22 | X x; 23 | } 24 | -------------------------------------------------------------------------------- /codingStyleIdioms/5_pImpl/pimpl.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-9. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | // pImpl: Pointer-to-Implementation 9 | 10 | class private_foo; 11 | class foo { 12 | public: 13 | foo(); 14 | 15 | ~foo(); 16 | 17 | void bar(); 18 | 19 | private: 20 | private_foo *pImpl; 21 | }; 22 | 23 | class private_foo { 24 | public: 25 | void bar() { 26 | cout<<"private_foo invoke bar funciton."<bar(); 42 | } 43 | 44 | 45 | int main() { 46 | foo f; 47 | f.bar(); 48 | } -------------------------------------------------------------------------------- /codingStyleIdioms/5_pImpl/pimplTime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class C { 6 | vector v; 7 | string s; 8 | }; 9 | class D { 10 | string s; 11 | }; 12 | 13 | class X { 14 | private: 15 | struct XImpl; 16 | XImpl* pImpl; 17 | }; 18 | 19 | struct X::XImpl { 20 | C c; 21 | D d; 22 | }; 23 | 24 | int main() { 25 | X x; 26 | } 27 | -------------------------------------------------------------------------------- /codingStyleIdioms/5_pImpl/pimplTime.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class C { 6 | vector v; 7 | string s; 8 | }; 9 | class D { 10 | string s; 11 | }; 12 | 13 | class X { 14 | private: 15 | struct XImpl; 16 | XImpl* pImpl; 17 | }; 18 | 19 | 20 | -------------------------------------------------------------------------------- /codingStyleIdioms/README.md: -------------------------------------------------------------------------------- 1 | # 你最喜欢的c++编程风格惯用法是什么? 2 | 3 | 在stackoverflow上找到了一篇文章,写的蛮好的,地址如下: 4 | 5 | > https://stackoverflow.com/questions/276173/what-are-your-favorite-c-coding-style-idioms#comment60171463_2034439 6 | 7 | 由于是英文的,且比较重要,于是总结成下面几条! 8 | 9 | - [1.类初始化列表](./1_classInitializers) 10 | - [2.枚举类替换命名空间](./2_enumclass_namespace) 11 | - [3.RAII(资源获取即初始化)](./3_RAII) 12 | - [4.copy and swap](./4_copy-swap) 13 | - [5.pImpl(指针指向具体实现)](./5_pImpl) 14 | 15 | -------------------------------------------------------------------------------- /concurrency/Threading_In_CPlusPlus/1.thread/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //concurrency/Threading_In_CPlusPlus/1.thread:intro` 2 | # please run `bazel run //concurrency/Threading_In_CPlusPlus/1.thread:thread` 3 | 4 | load("@rules_cc//cc:defs.bzl", "cc_binary") 5 | 6 | cc_binary( 7 | name = "intro", 8 | srcs = ["intro.cpp"], 9 | copts = ["-std=c++11"], 10 | ) 11 | cc_binary( 12 | name = "thread", 13 | srcs = ["thread.cpp"], 14 | copts = ["-std=c++11"], 15 | ) 16 | -------------------------------------------------------------------------------- /concurrency/Threading_In_CPlusPlus/2.create_type/1.function_pointer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-31. 3 | // 4 | 5 | // 1.函数指针 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | void fun(int x) { 12 | while (x-- > 0) { 13 | cout << x << endl; 14 | } 15 | } 16 | // 注意:如果我们创建多线程 并不会保证哪一个先开始 17 | int main() { 18 | std::thread t1(fun, 10); 19 | // std::1.thread t2(fun, 10); 20 | t1.join(); 21 | // t2.join(); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /concurrency/Threading_In_CPlusPlus/2.create_type/2.lambda_function.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-31. 3 | // 4 | 5 | // 1.函数指针 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | // 注意:如果我们创建多线程 并不会保证哪一个先开始 12 | int main() { 13 | // 2.Lambda函数 14 | auto fun = [](int x) { 15 | while (x-- > 0) { 16 | cout << x << endl; 17 | } 18 | }; 19 | // std::1.thread t1(fun, 10); 20 | // 也可以写成下面: 21 | std::thread t1_1([](int x) { 22 | while (x-- > 0) { 23 | cout << x << endl; 24 | } 25 | }, 11); 26 | // std::1.thread t2(fun, 10); 27 | // t1.join(); 28 | t1_1.join(); 29 | // t2.join(); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /concurrency/Threading_In_CPlusPlus/2.create_type/3.functor.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-31. 3 | // 4 | 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | // 3.functor (Funciton Object) 12 | class Base { 13 | public: 14 | void operator()(int x) { 15 | while (x-- > 0) { 16 | cout << x << endl; 17 | } 18 | } 19 | }; 20 | 21 | int main() { 22 | thread t(Base(), 10); 23 | t.join(); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /concurrency/Threading_In_CPlusPlus/2.create_type/4.no_static_member_function.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-31. 3 | // 4 | 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | // 4.Non-static member function 12 | class Base { 13 | public: 14 | void fun(int x) { 15 | while (x-- > 0) { 16 | cout << x << endl; 17 | } 18 | } 19 | }; 20 | 21 | int main() { 22 | Base b; 23 | thread t(&Base::fun,&b, 10); 24 | t.join(); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /concurrency/Threading_In_CPlusPlus/2.create_type/5.static_member_function.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-31. 3 | // 4 | 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | // 4.Non-static member function 12 | class Base { 13 | public: 14 | static void fun(int x) { 15 | while (x-- > 0) { 16 | cout << x << endl; 17 | } 18 | } 19 | }; 20 | 21 | int main() { 22 | thread t(&Base::fun, 10); 23 | t.join(); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /concurrency/Threading_In_CPlusPlus/3.join_detach/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //concurrency/Threading_In_CPlusPlus/3.join_detach:join` 2 | # please run `bazel run //concurrency/Threading_In_CPlusPlus/3.join_detach:detach` 3 | 4 | load("@rules_cc//cc:defs.bzl", "cc_binary") 5 | 6 | cc_binary( 7 | name = "join", 8 | srcs = ["join.cpp"], 9 | copts = ["-std=c++11"], 10 | ) 11 | cc_binary( 12 | name = "detach", 13 | srcs = ["detach.cpp"], 14 | copts = ["-std=c++11"], 15 | ) 16 | -------------------------------------------------------------------------------- /concurrency/Threading_In_CPlusPlus/3.join_detach/detach.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-31. 3 | // 4 | 5 | // join 注意点 6 | 7 | /** 8 | * 这用于从父线程分离新创建的线程 9 | * 在分离线程之前,请务必检查它是否可以joinable, 10 | * 否则可能会导致两次分离,并且双重detach()将导致程序终止 11 | * 如果我们有分离的线程并且main函数正在返回,那么分离的线程执行将被挂起 12 | */ 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | 19 | void run(int count) { 20 | while (count-- > 0) { 21 | cout << count << endl; 22 | } 23 | std::this_thread::sleep_for(chrono::seconds(3)); 24 | } 25 | 26 | int main() { 27 | thread t1(run, 10); 28 | cout << "main()" << endl; 29 | t1.detach(); 30 | if(t1.joinable()) 31 | t1.detach(); 32 | cout << "main() after" << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /concurrency/Threading_In_CPlusPlus/3.join_detach/join.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-31. 3 | // 4 | 5 | // join 注意点 6 | 7 | /** 8 | * 一旦线程开始,我们要想等待线程完成,需要在该对象上调用join() 9 | * 双重join将导致程序终止 10 | * 在join之前我们应该检查显示是否可以被join,通过使用joinable() 11 | */ 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | 19 | void run(int count) { 20 | while (count-- > 0) { 21 | cout << count << endl; 22 | } 23 | std::this_thread::sleep_for(chrono::seconds(3)); 24 | } 25 | 26 | int main() { 27 | thread t1(run, 10); 28 | cout << "main()" << endl; 29 | t1.join(); 30 | if (t1.joinable()) { 31 | t1.join(); 32 | } 33 | cout << "main() after" << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /concurrency/Threading_In_CPlusPlus/4.mutex/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //concurrency/Threading_In_CPlusPlus/4.mutex:critical_section` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "critical_section", 7 | srcs = ["critical_section.cpp"], 8 | copts = ["-std=c++11"], 9 | ) 10 | -------------------------------------------------------------------------------- /concurrency/Threading_In_CPlusPlus/4.mutex/critical_section.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-2-1. 3 | // 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int sum = 0; //shared 11 | 12 | mutex m; 13 | 14 | void *countgold() { 15 | int i; //local to each thread 16 | for (i = 0; i < 10000000; i++) { 17 | m.lock(); 18 | sum += 1; 19 | m.unlock(); 20 | } 21 | return NULL; 22 | } 23 | 24 | int main() { 25 | thread t1(countgold); 26 | thread t2(countgold); 27 | 28 | //Wait for both threads to finish 29 | t1.join(); 30 | t2.join(); 31 | 32 | cout << "sum = " << sum << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /concurrency/concurrency_v1/chapter1/1_helloworld.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-11-5. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | void hello() { 12 | cout << "hello world" << endl; 13 | } 14 | 15 | int main() { 16 | thread t(hello); 17 | t.join(); // must add this line otherwise will failed! 18 | // 需要注意的是线程对象执行了join后就不再joinable了,所以只能调用join一次。 19 | return 0; 20 | } -------------------------------------------------------------------------------- /concurrency/concurrency_v1/chapter1/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //concurrency/concurrency_v1/chapter1:1_helloworld` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "1_helloworld", 7 | srcs = ["1_helloworld.cpp"], 8 | ) 9 | -------------------------------------------------------------------------------- /concurrency/concurrency_v1/chapter2/2.2_transfer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-11-5. 3 | // 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class X { 10 | public: 11 | void do_length_work() {}; 12 | }; 13 | 14 | void process_big_object(std::unique_ptr) 15 | { 16 | // TODO 17 | } 18 | 19 | int main() { 20 | X my_x; 21 | thread t(&X::do_length_work, &my_x); // 1 22 | 23 | 24 | std::unique_ptr p(new X); 25 | p->do_length_work(); 26 | std::thread tt(process_big_object,std::move(p)); 27 | //std::thread实例的可移动且不可复制性。不可复制保性证了在同一时间点, 28 | // 一个std::thread实例只能关联一个执行线程;可移动性使得程序员可以自己决定,哪个实例拥有实际执行线程的所有权。 29 | return 0; 30 | } -------------------------------------------------------------------------------- /cpp2.0/cpp11/alias.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-11-3. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | // 不可以对alias template做偏特化或全特化 10 | template 11 | using Vec=vector>; // alias template using新用法 12 | //# define Vec template vector> // 使用宏不行 13 | 14 | int main() { 15 | Vec col; 16 | // 如果使用宏上述会被替换为template vector> error 不是我们想要的 17 | // typedef vector> Vec; // typedef也无法做到,没法指定模板参数 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /cpp2.0/cpp11/final.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-11-3. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | // final关键字用于两个地方 10 | // 第一个用在类,用于说明该类是继承体系下最后的一个类,不要其他类继承我,当继承时就会报错。 11 | class Base final { 12 | public: 13 | Base() {} 14 | 15 | virtual void func() {} 16 | }; 17 | 18 | class Derivered : public Base { // error: cannot derive from ‘final’ base ‘Base’ in derived type ‘Derivered’ 19 | }; 20 | // 第二个用在虚函数,表示这个虚函数不能再被override了,再override就会报错。 21 | class Base1 { 22 | public: 23 | Base1() {} 24 | 25 | virtual void func() final {} 26 | }; 27 | 28 | class Derivered1 : public Base1 { 29 | virtual void func() {} // error: overriding final function ‘virtual void Base1::func()’ 30 | }; 31 | int main() { 32 | 33 | } -------------------------------------------------------------------------------- /cpp2.0/cpp11/initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-11-3. 3 | // 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | int i; 11 | int j(); // 0 12 | int *p; 13 | int *q(); // nullptr 14 | 15 | // int x1{5.0}; // error:narrowing conversion 16 | // 编译器调用initializer_list私有构造之前,编译器准备好array(array头)与len,传递给该构造,并没有内含这个array, 17 | // 指针指向array,copy只是浅copy. 18 | // 如今所有容器都接受任意数量的值 insert()或assign() max() min() 19 | 20 | cout< 6 | #include 7 | 8 | using namespace std; 9 | 10 | // noexcepte ()中可以加条件 11 | // 异常可传递 a调用b b抛出异常,a没处理,就会抛出异常 12 | // 1.异常的回传机制:调用foo如果抛出异常,foo会接着往上层抛出异常, 13 | // 如果最上层没有处理,则会调用terminate函数,terminate函数内部调用abort,使程序退出 14 | 15 | 16 | // 2.在使用vector deque等容器的移动构造和移动赋值的时候,如果移动构造和移动赋值没有加上noexcept, 17 | // 则容器增长的时候不会调用move constructor,效率就会偏低,所以后面需要加上noexcept,安心使用。 18 | 19 | void foo() noexcept(true) { 20 | } 21 | 22 | int main() { 23 | foo(); 24 | vector vec; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /cpp2.0/cpp11/override.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-11-3. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Base 10 | { 11 | public: 12 | Base() {} 13 | virtual void func() {} 14 | }; 15 | class Derivered : public Base 16 | { 17 | virtual void func(int) override {} // error: ‘virtual void Derivered::func(int)’ marked ‘override’, but does not override 18 | }; 19 | // override用于虚函数,上面的virtual void func(int)实际上不是重写父类的虚函数,而是定义一个新的虚函数, 20 | // 我们的本意是重写虚函数,当不加overrride的时候,这样写编译器不会报错, 21 | // 那如果像下面加上override的话,则会报错,表示告诉了编译器,我确实要重写,但写错了,没有重写,于是就报错了, 22 | // 这样就能给我们对虚函数的重写做检查! 23 | 24 | int main() 25 | { 26 | } -------------------------------------------------------------------------------- /cpp2.0/cpp11/template_template.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-11-3. 3 | // 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | // template template parameter 模板模板参数 9 | template class Container> 11 | class XCls { 12 | private: 13 | Container c; 14 | }; 15 | 16 | // alias template 17 | template 18 | using Lst = list; 19 | 20 | int main() { 21 | // XCls mylist1; // error 22 | XCls mylist2; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /cpp2.0/cpp11/uniform_initialization.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-11-2. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() { 11 | int value[]{1, 2, 3}; // initializer_list 会关联一个array 里面元素被编译器逐一分解传给函数 12 | vector v{2, 3, 5}; 13 | return 0; 14 | } -------------------------------------------------------------------------------- /cpp2.0/cpp11/variadic/variadic1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-11-4. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // example 1 11 | void printX() { 12 | 13 | } 14 | 15 | // 特化 16 | template 17 | void printX(const T &firstArg, const Type &...args) { 18 | cout << firstArg << endl; // 先减少一个做操作 19 | printX(args...); // 其余的继续分为一个+一包进行递归知道没有了 调用printX() 20 | } 21 | 22 | // 如果写了下面接收任意参数,下面这个跟上面可以共存 泛化版 永远不会被调用,前面特化把它取代了 23 | template 24 | void printX(const Type &...args) { 25 | printX(args...); 26 | } 27 | 28 | 29 | int main() { 30 | printX(1, "hello", bitset<16>(377), 42); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /cpp2.0/cpp11/variadic/variadic3_4.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-11-4. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | // 参数type相同的max 10 | int max(initializer_list initializerList) { 11 | int res = *initializerList.begin(); 12 | for (auto elem:initializerList) 13 | res = max(res, elem); 14 | return res; 15 | } 16 | 17 | 18 | // 参数type相同的maximum 19 | int maximum(int n) { 20 | return n; 21 | } 22 | 23 | template 24 | int maximum(int n, Args...args) { 25 | return std::max(n, maximum(args...)); 26 | } 27 | 28 | int main() { 29 | cout << max({10, 8, 100, 1}) << endl; 30 | cout << maximum(57, 48, 60, 100, 20, 18) << endl; 31 | } -------------------------------------------------------------------------------- /design_pattern/producer_consumer/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //design_pattern/producer_consumer:producer_consumer` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "producer_consumer", 7 | srcs = ["producer_consumer.cpp"], 8 | ) 9 | -------------------------------------------------------------------------------- /design_pattern/singleton/hungrysingleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-2-6. 3 | // 4 | 5 | class singleton { 6 | private: 7 | singleton() {} 8 | static singleton *p; 9 | 10 | public: 11 | static singleton *instance(); 12 | }; 13 | 14 | singleton *singleton::p = new singleton(); 15 | singleton *singleton::instance() { return p; } 16 | -------------------------------------------------------------------------------- /design_pattern/singleton/iazysingleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-2-6. 3 | // 4 | 5 | class singleton { 6 | private: 7 | singleton() {} 8 | static singleton *p; 9 | 10 | public: 11 | static singleton *instance(); 12 | }; 13 | 14 | singleton *singleton::p = nullptr; 15 | 16 | singleton *singleton::instance() { 17 | if (p == nullptr) 18 | p = new singleton(); 19 | return p; 20 | } 21 | -------------------------------------------------------------------------------- /design_pattern/singleton/lock_singleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-2-7. 3 | // 4 | #include 5 | using namespace std; 6 | 7 | #include 8 | 9 | class singleton { 10 | private: 11 | singleton() {} 12 | static singleton *p; 13 | static mutex lock_; 14 | 15 | public: 16 | static singleton *instance(); 17 | }; 18 | 19 | singleton *singleton::p = nullptr; 20 | 21 | singleton *singleton::instance() { 22 | lock_guard guard(lock_); 23 | if (p == nullptr) 24 | p = new singleton(); 25 | return p; 26 | } 27 | -------------------------------------------------------------------------------- /design_pattern/singleton/pthreadoncesingleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-2-6. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | class singleton { 9 | private: 10 | singleton(); //私有构造函数,不允许使用者自己生成对象 11 | singleton(const singleton &other); 12 | 13 | //要写成静态方法的原因:类成员函数隐含传递this指针(第一个参数) 14 | static void init() { p = new singleton(); } 15 | 16 | static pthread_once_t ponce_; 17 | static singleton *p; //静态成员变量 18 | public: 19 | singleton *instance() { 20 | // init函数只会执行一次 21 | pthread_once(&ponce_, &singleton::init); 22 | return p; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /design_pattern/singleton/static_local_singleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-2-7. 3 | // 在C++11标准下,《Effective C++》提出了一种更优雅的单例模式实现,使用函数内的 4 | // local static 对象。 这种方法也被称为Meyers' Singleton。 5 | // 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | class singleton { 12 | private: 13 | singleton() {} 14 | 15 | public: 16 | static singleton &instance(); 17 | }; 18 | 19 | singleton &singleton::instance() { 20 | static singleton p; 21 | return p; 22 | } 23 | -------------------------------------------------------------------------------- /effective_cpp/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //effective_cpp:2` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "2", 7 | srcs = ["2.cpp"], 8 | copts = ["-std=c++17"], 9 | ) 10 | -------------------------------------------------------------------------------- /english/basic_content/abstract/abstract.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file abstract.cpp 3 | * @brief 抽象类中:在成员函数内可以调用纯虚函数,在构造函数/析构函数内部不能使用纯虚函数 4 | * 如果一个类从抽象类派生而来,它必须实现了基类中的所有纯虚函数,才能成为非抽象类 5 | * @author 光城 6 | * @version v1 7 | * @date 2019-07-20 8 | */ 9 | 10 | #include 11 | using namespace std; 12 | 13 | class A { 14 | public: 15 | virtual void f() = 0; // 纯虚函数 16 | void g(){ this->f(); } 17 | A(){} 18 | }; 19 | class B:public A{ 20 | public: 21 | void f(){ cout<<"B:f()"< 10 | using namespace std; 11 | 12 | class Base 13 | { 14 | int x; 15 | public: 16 | virtual void fun() = 0; 17 | int getX() { return x; } 18 | }; 19 | 20 | class Derived: public Base 21 | { 22 | int y; 23 | public: 24 | void fun() { cout << "fun() called"; } // 实现了fun()函数 25 | }; 26 | 27 | int main(void) 28 | { 29 | Derived d; 30 | d.fun(); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /english/basic_content/abstract/interesting_facts1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file interesting_facts1.cpp 3 | * @brief 纯虚函数使一个类变成抽象类 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-20 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | /** 13 | * @brief 抽象类至少包含一个纯虚函数 14 | */ 15 | class Test 16 | { 17 | int x; 18 | public: 19 | virtual void show() = 0; 20 | int getX() { return x; } 21 | }; 22 | 23 | int main(void) 24 | { 25 | Test t; //error! 不能创建抽象类的对象 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /english/basic_content/abstract/interesting_facts3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file interesting_facts3.cpp 3 | * @brief 如果我们不在派生类中覆盖纯虚函数,那么派生类也会变成抽象类。 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-20 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | class Base 13 | { 14 | int x; 15 | public: 16 | virtual void show() = 0; 17 | int getX() { return x; } 18 | }; 19 | class Derived: public Base 20 | { 21 | public: 22 | // void show() { } 23 | }; 24 | int main(void) 25 | { 26 | Derived d; //error! 派生类没有实现纯虚函数,那么派生类也会变为抽象类,不能创建抽象类的对象 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /english/basic_content/abstract/interesting_facts4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file interesting_facts4.cpp 3 | * @brief 抽象类可以有构造函数 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-07-20 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | // An abstract class with constructor 13 | class Base 14 | { 15 | protected: 16 | int x; 17 | public: 18 | virtual void fun() = 0; 19 | Base(int i) { x = i; } 20 | }; 21 | 22 | class Derived: public Base 23 | { 24 | int y; 25 | public: 26 | Derived(int i, int j):Base(i) { y = j; } 27 | void fun() { cout << "x = " << x << ", y = " << y; } 28 | }; 29 | 30 | int main(void) 31 | { 32 | Derived d(4, 5); 33 | d.fun(); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /english/basic_content/abstract/pure_virtual.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file pure_virtual.cpp 3 | * @brief 纯虚函数:没有函数体的虚函数 4 | * 抽象类:包含纯虚函数的类 5 | * 6 | * @author 光城 7 | * @version v1 8 | * @date 2019-07-20 9 | */ 10 | 11 | #include 12 | 13 | using namespace std; 14 | class A 15 | { 16 | private: 17 | int a; 18 | public: 19 | virtual void show()=0; // 纯虚函数 20 | }; 21 | 22 | 23 | int main() 24 | { 25 | /* 26 | * 1. 抽象类只能作为基类来派生新类使用 27 | * 2. 抽象类的指针和引用->由抽象类派生出来的类的对象! 28 | */ 29 | A a; // error 抽象类,不能创建对象 30 | 31 | A *a1; // ok 可以定义抽象类的指针 32 | 33 | A *a2 = new A(); // error, A是抽象类,不能创建对象 34 | } 35 | -------------------------------------------------------------------------------- /english/basic_content/abstract/test.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file test.cpp 3 | * @brief C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数!我们只需声明它!通过声明中赋值0来声明纯虚函数! 4 | * 纯虚函数:没有函数体的虚函数 5 | * @author 光城 6 | * @version v1 7 | * @date 2019-07-20 8 | */ 9 | 10 | 11 | 12 | /** 13 | * @brief 抽象类 14 | */ 15 | class Test 16 | { 17 | // Data members of class 18 | public: 19 | // Pure Virtual Function 20 | virtual void show() = 0; 21 | 22 | /* Other members */ 23 | }; 24 | -------------------------------------------------------------------------------- /english/basic_content/assert/assert.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int x = 7; 7 | 8 | /* Some big code in between and let's say x 9 | * is accidentally changed to 9 */ 10 | x = 9; 11 | 12 | // Programmer assumes x to be 7 in rest of the code 13 | assert(x==7); 14 | 15 | /* Rest of the code */ 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /english/basic_content/assert/ignore_assert.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file ignore_assert.c 3 | * @brief Ignore assertion 4 | * @author Light-City 5 | * @version v1 6 | * @date 2019-07-25 7 | */ 8 | 9 | #define NDEBUG // ignore assertion 10 | 11 | #include 12 | 13 | int main(){ 14 | int x=7; 15 | assert(x==5); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /english/basic_content/bit/learn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | struct stuff 5 | { 6 | unsigned int field1: 30; 7 | unsigned int : 2; 8 | unsigned int field2: 4; 9 | unsigned int : 0; 10 | unsigned int field3: 3; 11 | }; 12 | int main(){ 13 | struct stuff s={1,3,5}; 14 | cout< 2 | #include"apple.cpp" 3 | using namespace std; 4 | Apple::Apple(int i) 5 | { 6 | 7 | } 8 | int Apple::add(int num){ 9 | take(num); 10 | } 11 | int Apple::add(int num) const{ 12 | take(num); 13 | } 14 | void Apple::take(int num) const 15 | { 16 | cout<<"take func "< 2 | #include"apple.cpp" 3 | using namespace std; 4 | 5 | Apple::Apple(int i):apple_number(i) 6 | { 7 | 8 | } 9 | int Apple::add(int num){ 10 | take(num); 11 | } 12 | int Apple::add(int num) const{ 13 | take(num); 14 | } 15 | void Apple::take(int num) const 16 | { 17 | cout<<"take func "< 2 | #include"apple.cpp" 3 | using namespace std; 4 | const int Apple::apple_number=10; 5 | Apple::Apple(int i) 6 | { 7 | 8 | } 9 | int Apple::add(int num){ 10 | take(num); 11 | } 12 | int Apple::add(int num) const{ 13 | take(num); 14 | } 15 | void Apple::take(int num) const 16 | { 17 | cout<<"take func "< 2 | #include"apple.cpp" 3 | using namespace std; 4 | const int Apple::apple_number=10; 5 | int Apple::ap=666; 6 | Apple::Apple(int i) 7 | { 8 | 9 | } 10 | int Apple::add(int num){ 11 | take(num); 12 | } 13 | int Apple::add(int num) const{ 14 | take(num); 15 | } 16 | void Apple::take(int num) const 17 | { 18 | cout<<"take func "< 2 | using namespace std; 3 | 4 | void f(const int i){ 5 | i=10; // error: assignment of read-only parameter ‘i’ 6 | cout< 2 | using namespace std; 3 | int main(){ 4 | const int b = 10; 5 | b = 0; //error 6 | const string s = "helloworld"; 7 | const int i,j=0; 8 | } 9 | -------------------------------------------------------------------------------- /english/basic_content/const/extern_const/const_file1.cpp: -------------------------------------------------------------------------------- 1 | extern const int ext=12; 2 | -------------------------------------------------------------------------------- /english/basic_content/const/extern_const/const_file2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * by 光城 4 | * compile: g++ -o file const_file2.cpp const_file1.cpp 5 | * execute: ./file 6 | */ 7 | extern const int ext; 8 | int main(){ 9 | 10 | std::cout< 2 | /** 3 | * by 光城 4 | * compile: g++ -o file file2.cpp file1.cpp 5 | * execute: ./file 6 | */ 7 | extern int ext; 8 | int main(){ 9 | 10 | std::cout<<(ext+10)< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | const int *ptr; 7 | *ptr=10; //error 8 | } 9 | -------------------------------------------------------------------------------- /english/basic_content/const/funciton_const/condition1/condition2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | const int p = 10; 7 | const void *vp = &p; 8 | void *vp = &p; //error 9 | } 10 | -------------------------------------------------------------------------------- /english/basic_content/const/funciton_const/condition1/condition3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | const int *ptr; 6 | int val = 3; 7 | ptr = &val; //ok 8 | int *ptr1 = &val; 9 | *ptr1=4; 10 | cout<<*ptr< 2 | using namespace std; 3 | int main(){ 4 | 5 | int num=0; 6 | int * const ptr=# //const指针必须初始化!且const指针的值不能修改 7 | int * t = # 8 | *t = 1; 9 | cout<<*ptr< 2 | using namespace std; 3 | int main(){ 4 | 5 | const int num=0; 6 | int * const ptr=# //error! const int* -> int* 7 | cout<<*ptr< 2 | using namespace std; 3 | int main(){ 4 | 5 | const int num=10; 6 | const int * const ptr=# //error! const int* -> int* 7 | cout<<*ptr< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | const int p = 3; 7 | const int * const ptr = &p; 8 | cout<<*ptr< 2 | using namespace std; 3 | 4 | enum Color {RED,BLUE}; 5 | enum Feeling {EXCITED,BLUE}; 6 | 7 | int main() 8 | { 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /english/basic_content/explicit/README.md: -------------------------------------------------------------------------------- 1 | # Things about explicit 2 | 3 | ## About Author: 4 | 5 | 6 | ![](../img/wechat.jpg) 7 | 8 | - explicit When decorating a constructor, you can prevent implicit conversion and copy initialization 9 | - explicit When modifying a conversion function, you can prevent implicit conversion, except for context conversion 10 | 11 | 12 | Code :[.explicit.cpp](./explicit.cpp) 13 | 14 | Reference Link: 15 | > https://stackoverflow.com/questions/4600295/what-is-the-meaning-of-operator-bool-const 16 | -------------------------------------------------------------------------------- /english/basic_content/extern/extern_c++/add.c: -------------------------------------------------------------------------------- 1 | #include "add.h" 2 | 3 | int add(int x,int y) { 4 | return x+y; 5 | } 6 | -------------------------------------------------------------------------------- /english/basic_content/extern/extern_c++/add.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | extern "C" { 4 | #include "add.h" 5 | } 6 | int main() { 7 | add(2,3); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /english/basic_content/extern/extern_c++/add.h: -------------------------------------------------------------------------------- 1 | #ifndef ADD_H 2 | #define ADD_H 3 | extern int add(int x,int y); 4 | #endif 5 | -------------------------------------------------------------------------------- /english/basic_content/extern/extern_c++/add.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/extern/extern_c++/add.o -------------------------------------------------------------------------------- /english/basic_content/extern/extern_c++/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/extern/extern_c++/main -------------------------------------------------------------------------------- /english/basic_content/extern/extern_c/add.c: -------------------------------------------------------------------------------- 1 | extern int add(int x,int y); 2 | int main() { 3 | add(2,3); 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /english/basic_content/extern/extern_c/add.cpp: -------------------------------------------------------------------------------- 1 | #include "add.h" 2 | 3 | int add(int x,int y) { 4 | return x+y; 5 | } 6 | -------------------------------------------------------------------------------- /english/basic_content/extern/extern_c/add.h: -------------------------------------------------------------------------------- 1 | #ifndef ADD_H 2 | #define ADD_H 3 | extern "C" { 4 | int add(int x,int y); 5 | } 6 | #endif 7 | -------------------------------------------------------------------------------- /english/basic_content/extern/extern_c/add.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/extern/extern_c/add.o -------------------------------------------------------------------------------- /english/basic_content/extern/extern_c/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/extern/extern_c/main -------------------------------------------------------------------------------- /english/basic_content/friend/friend_class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/friend/friend_class -------------------------------------------------------------------------------- /english/basic_content/friend/friend_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class A 6 | { 7 | public: 8 | A(int _a):a(_a){}; 9 | friend class B; 10 | private: 11 | int a; 12 | }; 13 | 14 | class B 15 | { 16 | public: 17 | int getb(A ca) { 18 | return ca.a; 19 | }; 20 | }; 21 | 22 | int main() 23 | { 24 | A a(3); 25 | B b; 26 | cout< 10 | 11 | using namespace std; 12 | 13 | class A 14 | { 15 | public: 16 | A(int _a):a(_a){}; 17 | friend int geta(A &ca); ///< 友元函数 18 | private: 19 | int a; 20 | }; 21 | 22 | int geta(A &ca) 23 | { 24 | return ca.a; 25 | } 26 | 27 | int main() 28 | { 29 | A a(3); 30 | cout< 10 | using namespace std; 11 | 12 | /** 13 | * @brief 定义了一个变量pFun,这个变量是个指针,指向返回值和参数都是空的函数的指针! 14 | */ 15 | void (*pFun)(int); 16 | 17 | /** 18 | * @brief 代表一种新类型,不是变量!所以与上述的pFun不一样! 19 | */ 20 | typedef void (*func)(void); 21 | 22 | void myfunc(void) 23 | { 24 | cout<<"asda"< 2 | using namespace std; 3 | 4 | int count=0; // 全局(::)的count 5 | 6 | class A { 7 | public: 8 | static int count; // 类A的count (A::count) 9 | 10 | }; 11 | // 静态变量必须在此处定义 12 | int A::count; 13 | int main() { 14 | ::count=1; // 设置全局的count为1 15 | A::count=5; // 设置类A的count为2 16 | cout< 2 | using namespace std; 3 | void test1(int* p) 4 | { 5 | *p = 3; //此处应该首先判断p是否为空,为了测试的需要,此处我们没加。 6 | return; 7 | } 8 | 9 | void test2(int& p) 10 | { 11 | p = 3; //此处应该首先判断p是否为空,为了测试的需要,此处我们没加。 12 | return; 13 | } 14 | 15 | int main() { 16 | int a=10; 17 | int *p=&a; 18 | test1(p); 19 | test2(a); 20 | cout< 10 | 11 | using namespace std; 12 | 13 | class A{}; 14 | int main() 15 | { 16 | cout< 10 | 11 | 12 | using namespace std; 13 | 14 | class A 15 | { 16 | public: 17 | A(); 18 | ~A(); 19 | static int a; 20 | static void fun3(); 21 | void fun(); 22 | void fun1(); 23 | }; 24 | 25 | int main() 26 | { 27 | cout< 10 | 11 | using namespace std; 12 | 13 | class A 14 | { 15 | public: 16 | char a; 17 | int b; 18 | }; 19 | 20 | class B 21 | { 22 | public: 23 | short a; 24 | long b; 25 | }; 26 | 27 | /** 28 | * @brief 8+16+8=32 29 | */ 30 | class C:A,B 31 | { 32 | char c; 33 | }; 34 | 35 | 36 | int main() 37 | { 38 | cout< 10 | 11 | using namespace std; 12 | 13 | class A{ 14 | 15 | virtual void fun(); 16 | virtual void fun1(); 17 | virtual void fun2(); 18 | virtual void fun3(); 19 | }; 20 | int main() 21 | { 22 | cout< 11 | using namespace std; 12 | class A 13 | { 14 | public: 15 | char b; 16 | virtual void fun() {}; 17 | static int c; 18 | static int d; 19 | static int f; 20 | }; 21 | 22 | 23 | 24 | int main() 25 | { 26 | 27 | /** 28 | * @brief 16 字节对齐、静态变量不影响类的大小、vptr指针=8 29 | */ 30 | cout< 10 | 11 | using namespace std; 12 | 13 | class A 14 | { 15 | virtual void fun() {} 16 | }; 17 | 18 | class B 19 | { 20 | virtual void fun2() {} 21 | }; 22 | class C : virtual public A, virtual public B 23 | { 24 | public: 25 | virtual void fun3() {} 26 | }; 27 | 28 | 29 | int main() 30 | { 31 | 32 | /** 33 | * @brief 8 8 16 派生类虚继承多个虚函数,会继承所有虚函数的vptr 34 | */ 35 | cout< 10 | 11 | using namespace std; 12 | 13 | class A 14 | { 15 | virtual void fun() {} 16 | }; 17 | 18 | class B 19 | { 20 | virtual void fun2() {} 21 | }; 22 | class C : public A, public B 23 | { 24 | public: 25 | virtual void fun3() {} 26 | }; 27 | 28 | 29 | int main() 30 | { 31 | 32 | /** 33 | * @brief 8 8 16 派生类继承多个虚函数,会继承所有虚函数的vptr 34 | */ 35 | cout< 2 | using namespace std; 3 | 4 | class Apple 5 | { 6 | int i; 7 | public: 8 | Apple() 9 | { 10 | i = 0; 11 | cout << "Inside Constructor\n"; 12 | } 13 | ~Apple() 14 | { 15 | cout << "Inside Destructor\n"; 16 | } 17 | }; 18 | 19 | int main() 20 | { 21 | int x = 0; 22 | if (x==0) 23 | { 24 | Apple obj; 25 | } 26 | cout << "End of main\n"; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /english/basic_content/static/static_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Apple 5 | { 6 | int i; 7 | public: 8 | Apple() 9 | { 10 | i = 0; 11 | cout << "Inside Constructor\n"; 12 | } 13 | ~Apple() 14 | { 15 | cout << "Inside Destructor\n"; 16 | } 17 | }; 18 | 19 | int main() 20 | { 21 | int x = 0; 22 | if (x==0) 23 | { 24 | static Apple obj; 25 | } 26 | cout << "End of main\n"; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /english/basic_content/static/static_demo.cpp: -------------------------------------------------------------------------------- 1 | // the use of static Static 2 | // variables in a Function 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void demo() 8 | { 9 | // static variable 10 | static int count = 0; 11 | cout << count << " "; 12 | 13 | // value is updated and 14 | // will be carried to next 15 | // function calls 16 | count++; 17 | } 18 | 19 | int main() 20 | { 21 | for (int i=0; i<5; i++) 22 | demo(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /english/basic_content/static/static_error_variable.cpp: -------------------------------------------------------------------------------- 1 | // variables inside a class 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Apple 7 | { 8 | public: 9 | static int i; 10 | 11 | Apple() 12 | { 13 | // Do nothing 14 | }; 15 | }; 16 | 17 | int main() 18 | { 19 | Apple obj1; 20 | Apple obj2; 21 | obj1.i =2; 22 | obj2.i = 3; 23 | 24 | // prints value of i 25 | cout << obj1.i<<" "< 2 | using namespace std; 3 | 4 | class Apple 5 | { 6 | public: 7 | // static member function 8 | static void printMsg() 9 | { 10 | cout<<"Welcome to Apple!"; 11 | } 12 | }; 13 | 14 | // main function 15 | int main() 16 | { 17 | // invoking a static member function 18 | Apple::printMsg(); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /english/basic_content/static/static_variable.cpp: -------------------------------------------------------------------------------- 1 | // variables inside a class 2 | 3 | #include 4 | using namespace std; 5 | 6 | class GfG 7 | { 8 | public: 9 | static int i; 10 | 11 | GfG() 12 | { 13 | // Do nothing 14 | }; 15 | }; 16 | 17 | int GfG::i = 1; 18 | 19 | int main() 20 | { 21 | GfG obj; 22 | // prints value of i 23 | cout << obj.i; 24 | } 25 | -------------------------------------------------------------------------------- /english/basic_content/struct/ext: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/struct/ext -------------------------------------------------------------------------------- /english/basic_content/struct/sf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/struct/sf -------------------------------------------------------------------------------- /english/basic_content/struct/stff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/struct/stff -------------------------------------------------------------------------------- /english/basic_content/struct/struct_func: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/struct/struct_func -------------------------------------------------------------------------------- /english/basic_content/struct/struct_func.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Base { // public 4 | int v1; 5 | // public: //error 6 | int v2; 7 | //private: 8 | int v3; 9 | //void print(){ // c中不能在结构体中嵌入函数 10 | // printf("%s\n","hello world"); 11 | //}; //error! 12 | }; 13 | 14 | void Base(){ 15 | printf("%s\n","I am Base func"); 16 | } 17 | //struct Base base1; //ok 18 | //Base base2; //error 19 | 20 | int main() { 21 | struct Base base; 22 | base.v1=1; 23 | //base.print(); 24 | printf("%d\n",base.v1); 25 | Base(); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /english/basic_content/struct/struct_func.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Base { 5 | int v1; 6 | // private: //error! 7 | int v3; 8 | public: //显示声明public 9 | int v2; 10 | void print(){ 11 | printf("%s\n","hello world"); 12 | }; 13 | }; 14 | 15 | int main() { 16 | struct Base base1; //ok 17 | Base base2; //ok 18 | Base base; 19 | base.v1=1; 20 | base.v3=2; 21 | base.print(); 22 | printf("%d\n",base.v1); 23 | printf("%d\n",base.v3); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /english/basic_content/struct/stu: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/struct/stu -------------------------------------------------------------------------------- /english/basic_content/struct_class/README.md: -------------------------------------------------------------------------------- 1 | # The difference struct and class 2 | 3 | 4 | 5 | ![](../img/wechat.jpg) 6 | 7 | More detail about struct [Story about struct](../struct) 8 | 9 | Generally speaking, struct is more suitable to be regarded as an implementation body of data structure, and class is more suitable to be regarded as the implementation body of an object. 10 | 11 | Dif: 12 | 13 | One of the essential differences is the default access control 14 | 15 | 16 | 17 | Default inherited access.Struct is public, while class is private. 18 | 19 | Structure as the implementation body of data structure.Its default data access control is public, while class is the implementation body of the object, and its default member variable access control is private. 20 | -------------------------------------------------------------------------------- /english/basic_content/this/img/constthis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/this/img/constthis.png -------------------------------------------------------------------------------- /english/basic_content/this/img/genthis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/this/img/genthis.png -------------------------------------------------------------------------------- /english/basic_content/this/img/thiscontrust.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/this/img/thiscontrust.png -------------------------------------------------------------------------------- /english/basic_content/this/person: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/this/person -------------------------------------------------------------------------------- /english/basic_content/union/README.md: -------------------------------------------------------------------------------- 1 | # Story About Union 2 | 3 | 4 | 5 | 6 | ![](../img/wechat.jpg) 7 | 8 | (Union)is a space saving special class.A union can have multiple data members.However, only one data member can have a value at any time. When a member is assigned a value, other members become undefined. 9 | 10 | - The default access control character is public 11 | - It can contain constructors and destructors 12 | - Cannot contain a member of a reference type 13 | - Cannot inherit from other classes, cannot be a base class 14 | - Cannot contain virtual functions 15 | - Anonymous union can directly access union members in the scope of definition 16 | - Anonymous union cannot contain protected or private members 17 | - Global anonymous union must be static 18 | 19 | 20 | -------------------------------------------------------------------------------- /english/basic_content/union/union: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/union/union -------------------------------------------------------------------------------- /english/basic_content/using/derived_base: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/using/derived_base -------------------------------------------------------------------------------- /english/basic_content/using/using_derived: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/using/using_derived -------------------------------------------------------------------------------- /english/basic_content/using/using_derived.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file using_derived.cpp 3 | * @brief 函数重装 4 | * @author 光城 5 | * @version v1 6 | * @date 2019-08-07 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | class Base{ 13 | public: 14 | void f(){ cout<<"f()"< 11 | #include 12 | using namespace std; 13 | 14 | typedef vector V1; 15 | using V2 = vector; 16 | 17 | 18 | int main() 19 | { 20 | int nums1[] = {1,2,3,4,5,6}; 21 | V1 vec1(nums1,nums1+sizeof(nums1)/sizeof(int)); 22 | int nums2[] = {5,7,6}; 23 | V2 vec2(nums2,nums2+sizeof(nums2)/sizeof(int)); 24 | 25 | for(auto i:vec1) 26 | cout< 2 | using namespace std; 3 | 4 | class Derived; 5 | 6 | class Base { 7 | public: 8 | virtual void fun() { cout << "Base Fun"; } 9 | // friend int main(); 10 | }; 11 | 12 | class Derived: public Base { 13 | private: 14 | void fun() { cout << "Derived Fun"; } 15 | }; 16 | 17 | int main() 18 | { 19 | Base *ptr = new Derived; 20 | ptr->fun(); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /english/basic_content/virtual/set4/rtti: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/virtual/set4/rtti -------------------------------------------------------------------------------- /english/basic_content/virtual/set4/warn_rtti.cpp: -------------------------------------------------------------------------------- 1 | // 在使用时需要注意:被转换对象obj的类型T1必须是多态类型,即T1必须公有继承自其它类,或者T1拥有虚函数(继承或自定义)。若T1为非多态类型,使用dynamic_cast会报编译错误。 2 | 3 | // A为非多态类型 4 | 5 | class A{ 6 | 7 | }; 8 | 9 | //B为多态类型 10 | 11 | class B{ 12 | 13 | public: virtual ~B(){} 14 | 15 | }; 16 | 17 | //D为多态类型 18 | 19 | class D: public A{ 20 | 21 | }; 22 | 23 | //E为非多态类型 24 | 25 | class E : private A{ 26 | 27 | }; 28 | 29 | //F为多态类型 30 | 31 | class F : private B{ 32 | 33 | } 34 | 35 | 36 | -------------------------------------------------------------------------------- /english/basic_content/volatile/noopt_vola.cpp: -------------------------------------------------------------------------------- 1 | /* Compile code without optimization option */ 2 | #include 3 | int main(void) 4 | { 5 | const int local = 10; 6 | int *ptr = (int*) &local; 7 | 8 | printf("Initial value of local : %d \n", local); 9 | 10 | *ptr = 100; 11 | 12 | printf("Modified value of local: %d \n", local); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /english/basic_content/volatile/nv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/volatile/nv -------------------------------------------------------------------------------- /english/basic_content/volatile/volatile.cpp: -------------------------------------------------------------------------------- 1 | /* Compile code with optimization option */ 2 | #include 3 | 4 | int main(void) 5 | { 6 | const volatile int local = 10; 7 | int *ptr = (int*) &local; 8 | 9 | printf("Initial value of local : %d \n", local); 10 | 11 | *ptr = 100; 12 | 13 | printf("Modified value of local: %d \n", local); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /english/basic_content/vptr_vtable/img/base.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/english/basic_content/vptr_vtable/img/base.jpg -------------------------------------------------------------------------------- /img/cpp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/img/cpp.jpg -------------------------------------------------------------------------------- /img/wechat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/img/wechat.jpg -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/RAII/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //learn_class/modern_cpp_30/RAII:stack` 2 | # please run `bazel run //learn_class/modern_cpp_30/RAII:heap` 3 | # please run `bazel run //learn_class/modern_cpp_30/RAII:RAII` 4 | 5 | load("@rules_cc//cc:defs.bzl", "cc_binary") 6 | 7 | cc_binary( 8 | name = "stack", 9 | srcs = ["stack.cpp"], 10 | copts = ["-std=c++11"], 11 | ) 12 | cc_binary( 13 | name = "heap", 14 | srcs = ["heap.cpp"], 15 | copts = ["-std=c++11"], 16 | ) 17 | cc_binary( 18 | name = "RAII", 19 | srcs = ["RAII.cpp"], 20 | copts = ["-std=c++11"], 21 | ) 22 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/RAII/heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class bar {}; 6 | 7 | // java 程序员风格 8 | void foo() { 9 | cout << "method 1" << endl; 10 | bar *ptr = new bar(); 11 | delete ptr; 12 | } 13 | 14 | bar *make_bar() { 15 | bar *ptr = nullptr; 16 | try { 17 | ptr = new bar(); 18 | } catch (...) { 19 | delete ptr; 20 | throw; 21 | } 22 | return ptr; 23 | } 24 | // 独立出函数 分配和释放不在一个函数里 25 | void foo1() { 26 | cout << "method 2" << endl; 27 | bar *ptr = make_bar(); 28 | delete ptr; 29 | } 30 | 31 | int main() { 32 | foo(); 33 | foo1(); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/RAII/stack.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-9. 3 | // 4 | #include 5 | class Obj { 6 | public: 7 | Obj() { puts("Obj()"); } 8 | ~Obj() { puts("~Obj()"); } 9 | }; 10 | void foo(int n) { 11 | Obj obj; 12 | if (n == 42) 13 | throw "life, the universe and everything"; 14 | } 15 | // 不管是否发生了异常,obj 的析构函数都会得到执行。 16 | int main() { 17 | try { 18 | foo(41); 19 | foo(42); 20 | } catch (const char *s) { 21 | puts(s); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/SFINAE/README.md: -------------------------------------------------------------------------------- 1 | 学习自:https://jguegant.github.io/blogs/tech/sfinae-introduction.html 2 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/SFINAE/SFINAE.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-5. 3 | // 4 | #include 5 | 6 | using namespace std; 7 | 8 | template class IsClassT { 9 | private: 10 | typedef char One; 11 | typedef struct { 12 | char a[2]; 13 | } Two; 14 | 15 | template static One test(int C::*); 16 | 17 | // Will be chosen if T is anything except a class. 18 | template static Two test(...); 19 | 20 | public: 21 | enum { Yes = sizeof(IsClassT::test(0)) == 1 }; 22 | enum { No = !Yes }; 23 | }; 24 | 25 | struct A {}; 26 | 27 | int main() { 28 | // 0不能转换为int int::*因为int不是类,所以它不能有成员指针。 29 | cout << IsClassT::Yes << endl; 30 | cout << IsClassT::Yes << endl; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/SFINAE/SFINAE.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/learn_class/modern_cpp_30/SFINAE/SFINAE.pdf -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/SFINAE/sfinae paper/decltype.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-6. 3 | // 4 | #include 5 | struct Default { 6 | int foo() const {return 1;} 7 | }; 8 | 9 | struct NonDefault { 10 | NonDefault(const NonDefault&) {} 11 | int foo() const {return 1;} 12 | }; 13 | 14 | int main() 15 | { 16 | decltype(Default().foo()) n1 = 1; // int n1 17 | // decltype(NonDefault().foo()) n2 = n1; // error: no default constructor 18 | decltype(std::declval().foo()) n2 = n1; // int n2 19 | std::cout << "n2 = " << n2 << '\n'; 20 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/SFINAE/sfinae paper/overload1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-6. 3 | // 4 | #include 5 | 6 | void f(std::string s); // int can't be convert into a string. 7 | void f(double d); // int can be implicitly convert into a double, so this version could be selected, but... 8 | void f(int i); // ... this version using the type int directly is even more close! 9 | 10 | 11 | 12 | int main() { 13 | f(1); // Call f(int i); 14 | } 15 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/SFINAE/sfinae paper/overload2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-6. 3 | // 4 | #include 5 | 6 | std::string f(...) // Variadic functions are so "untyped" that... 7 | { 8 | return "..."; 9 | } 10 | 11 | template 12 | std::string f(const T &t)// ...this templated function got the precedence! 13 | { 14 | 15 | return "T"; 16 | } 17 | 18 | int main() { 19 | std::cout << f(1) << std::endl; 20 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/SFINAE/sfinae paper/p1SFINAE.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-6. 3 | // 4 | 5 | /* 6 | The compiler will try this overload since it's less generic than the variadic. 7 | T will be replace by int which gives us void f(const int& t, int::iterator* b = nullptr); 8 | int doesn't have an iterator sub-type, but the compiler doesn't throw a bunch of errors. 9 | It simply tries the next overload. 10 | 11 | 编译器尝试此重载,因为模板化函数比可变参数函数更精确(通用)。T将被int取代,这将使我们得到 12 | void f(const int& t, int::iterator* b = nullptr); int 没有迭代器子类型,但是编译器不会抛出一堆错误。 13 | 它只是尝试下一个重载。 14 | */ 15 | template 16 | void f(const T &t, typename T::iterator *it = nullptr) {} 17 | 18 | // The sink-hole. 19 | void f(...) {} 20 | 21 | int main() { 22 | f(1); // Calls void f(...) { } 23 | } 24 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/SFINAE/sfinae paper/p2SFINAE.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-6. 3 | // 4 | 5 | 6 | // The compiler will be really unhappy when it will later discover the call to hahahaICrash. 7 | // 当以后发现对hahahaICrash的调用时,编译器将非常不满意。 8 | template void f(T t) { t.hahahaICrash(); } 9 | void f(...) { } // The sink-hole wasn't even considered. 10 | 11 | int main() { 12 | f(1); 13 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/SFINAE/sfinae paper/sizeof1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-6. 3 | // 4 | 5 | #include 6 | 7 | typedef char type_test[42]; 8 | 9 | type_test &f() {} 10 | 11 | int main() { 12 | 13 | // In the following lines f won't even be truly called but we can still access to the size of its return type. 14 | // Thanks to the "fake evaluation" of the sizeof operator. 15 | char arrayTest[sizeof(f())]; 16 | std::cout << sizeof(f()) << std::endl; // Output 42. 17 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/SFINAE/sfinae paper/sizeof2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // 4 | // Created by light on 20-1-6. 5 | // 6 | 7 | typedef char yes; // Size: 1 byte. 8 | typedef yes no[2]; // Size: 2 bytes. 9 | 10 | // Two functions using our type with different size. 11 | yes &f1() {} 12 | no &f2() {} 13 | 14 | int main() { 15 | std::cout << sizeof(f1()) << std::endl; 16 | std::cout << sizeof(f2()) << std::endl; 17 | std::cout << (sizeof(f1()) == sizeof(f2())) << std::endl; // Output 0. 18 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/SFINAE/sfinae paper/structData.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-6. 3 | // 4 | 5 | #ifndef MORDEN_C_STRUCTDATA_H 6 | #define MORDEN_C_STRUCTDATA_H 7 | // 类型A只有to_string 方法 8 | struct A { 9 | }; 10 | 11 | std::string to_string(const A &) { 12 | return "I am A"; 13 | } 14 | 15 | // 类型B有serialize方法 16 | struct B { 17 | std::string serialize() const { 18 | return "I am B"; 19 | } 20 | }; 21 | 22 | // 类型C有个serialize数据成员与to_string方法 23 | struct C { 24 | std::string serialize; 25 | }; 26 | 27 | std::string to_string(const C &) { 28 | return "I am C"; 29 | } 30 | #endif //MORDEN_C_STRUCTDATA_H 31 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/compilercompute/factorial.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-28. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | template struct factorial { 10 | static_assert(n >= 0, "Arg must be non-negative"); 11 | static const int value = n * factorial::value; 12 | }; 13 | 14 | template <> struct factorial<0> { static const int value = 1; }; 15 | 16 | int main() { 17 | 18 | printf("%d\n", factorial<10>::value); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/compilerpoly/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //learn_class/modern_cpp_30/compilerpoly:template` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "template", 7 | srcs = ["template.cpp"], 8 | copts = ["-std=c++17"], 9 | ) 10 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/compilerpoly/README.md: -------------------------------------------------------------------------------- 1 | # 编译期多态:泛型编程和模板入门 2 | 3 | 运行时多态通过利用接口或者虚函数实现,本节则主要阐述编译时多态。 4 | 5 | >如果一只鸟走起来像鸭子、游起泳来像鸭子、叫起来也像鸭子,那么这只鸟就可以被当作鸭子。 6 | 7 | 鸭子类型使得开发者可以不使用继承体系来灵活地实现一些“约定”,尤其是使得混合不同来源、使用不同对象继承体系的代码成为可能。唯一的要求只是,这些不同的对象有“共通”的成员函数。这些成员函数应当有相同的名字和相同结构的参数(并不要求参数类型相同)。 8 | 9 | 在C++中实现鸭子类型可以通过模板或者说泛型编程。不管是类模板还是函数模板,编译器在看到其定义时只能做最基本的语法检查,真正的类型检查要在实例化(instantiation)的时候才能做。一般而言,这也是编译器会报错的时候。 10 | 11 | > “动态”多态和“静态”多态的对比 12 | 13 | “动态”多态解决的是运行时的行为变化—这个是无法在编译时确定的。“静态”多态或者“泛型”—解决的是很不同的问题,让适用于不同类型的“同构”算法可以用同一套代码来实现,实际上强调的是对代码的复用。 -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/constexpr/container.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-16. 3 | // 4 | 5 | #include "../container1/output_container.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int main() { 14 | map mp{{1, 1}, {2, 4}, {3, 9}}; 15 | cout << mp << endl; 16 | vector> vv{{1, 1}, {2, 4}, {3, 9}}; 17 | cout << vv << endl; 18 | 19 | pair p{1, 2}; 20 | cout << p << endl; 21 | 22 | set s{1, 2, 3}; 23 | cout << s << endl; 24 | 25 | vector v{'a', 'b'}; 26 | cout << v << endl; 27 | set vs{"a", "b"}; 28 | cout << vs << endl; 29 | 30 | map mm{{1, "23"}, {2, "234hi"}}; 31 | cout << mm << endl; 32 | } 33 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/constexpr/test3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-10. 3 | // 4 | 5 | #include 6 | 7 | class test3 { 8 | public: 9 | int value; 10 | 11 | // constexpr const method - can't chanage the values of object fields and can 12 | // be evaluated at compile time. 13 | constexpr int getvalue() const { return (value); } 14 | 15 | constexpr test3(int Value) : value(Value) {} 16 | }; 17 | 18 | int main() { 19 | 20 | // 加不加都行 21 | constexpr test3 x(100); // OK. Constructor is constexpr. 22 | 23 | int array[x.getvalue()]; // OK. x.getvalue() is constexpr and can be evaluated 24 | // at compile time. 25 | } 26 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/container1/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //learn_class/modern_cpp_30/container1:container` 2 | # please run `bazel run //learn_class/modern_cpp_30/container1:vector_l` 3 | 4 | load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") 5 | 6 | cc_library( 7 | name = "output_container", 8 | hdrs = ["output_container.h"], 9 | visibility = ["//visibility:public"], 10 | ) 11 | 12 | cc_binary( 13 | name = "container", 14 | srcs = ["container.cpp"], 15 | copts = ["-std=c++17"], 16 | deps = [ 17 | ":output_container", 18 | ], 19 | ) 20 | cc_binary( 21 | name = "vector_l", 22 | srcs = ["vector_l.cpp"], 23 | copts = ["-std=c++17"], 24 | deps = [ 25 | ":output_container", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/container1/container.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-16. 3 | // 4 | 5 | #include "output_container.h" 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() { 13 | map mp{{1, 1}, {2, 4}, {3, 9}}; 14 | cout << mp << endl; 15 | vector> vv{{1, 1}, {2, 4}, {3, 9}}; 16 | cout << vv << endl; 17 | } 18 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/container2/priority_queue.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-16. 3 | // 4 | 5 | #include "../container1/output_container.h" 6 | #include // std::greater 7 | #include // std::cout/endl 8 | #include // std::pair 9 | #include // std::priority_queue 10 | #include // std::vector 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | priority_queue, vector>, 16 | greater>> 17 | q; 18 | q.push({1, 1}); 19 | q.push({2, 2}); 20 | q.push({0, 3}); 21 | q.push({9, 4}); 22 | while (!q.empty()) { 23 | cout << q.top() << endl; 24 | q.pop(); 25 | } 26 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/exception/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //learn_class/modern_cpp_30/exception:exception` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "exception", 7 | srcs = ["exception.cpp"], 8 | copts = ["-std=c++11"], 9 | ) 10 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/exception/exception.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-17. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | // “首先是内存分配。如果 new 出错,按照 C++ 的规则,一般会得到异常 bad_alloc,对象的构造也就失败了。 9 | // 这种情况下,在 catch 捕捉到这个异常之前,所有的栈上对象会全部被析构,资源全部被自动清理。” 10 | // 之所以是栈上对象会全被被析构原因是堆上的东西都是由栈上的变量所引用的,栈上对象析构的过程, 11 | // 堆上相应的资源自然就被释放了。而且被释放的对象的范围还被栈帧限定了。 12 | 13 | 14 | // -fexceptions(缺省开启) 15 | // g++ test.cpp -std=c++17 -fno-exceptions 16 | // 关闭异常,可看到可执行文件的大小的变化。 17 | int main() 18 | { 19 | std::vector v{1, 2, 3, 4, 5}; 20 | v.push_back(20); 21 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/functionLambda/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //learn_class/modern_cpp_30/functionLambda:adder` 2 | # please run `bazel run //learn_class/modern_cpp_30/functionLambda:function` 3 | # please run `bazel run //learn_class/modern_cpp_30/functionLambda:autoLambda` 4 | 5 | load("@rules_cc//cc:defs.bzl", "cc_binary") 6 | 7 | cc_binary( 8 | name = "adder", 9 | srcs = ["adder.cpp"], 10 | copts = ["-std=c++17"], 11 | ) 12 | cc_binary( 13 | name = "function", 14 | srcs = ["function.cpp"], 15 | copts = ["-std=c++17"], 16 | ) 17 | cc_binary( 18 | name = "autoLambda", 19 | srcs = ["autoLambda.cpp"], 20 | copts = ["-std=c++17"], 21 | ) 22 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/functionLambda/adder.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-11. 3 | // 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct adder { 9 | adder(int n) : n_(n) {} 10 | 11 | int operator()(int x) const { return x + n_; } 12 | 13 | private: 14 | int n_; 15 | }; 16 | 17 | int main() { 18 | auto add_2 = adder(2); 19 | 20 | // x+2 21 | cout << add_2(3) << endl; 22 | 23 | auto t = bind1st(plus(), 2); 24 | cout << t(1) << endl; 25 | // 上述的C++98 26 | binder2nd> a2(plus(), 2); 27 | cout << a2(3) << endl; 28 | 29 | cout << [](int x) { return x * x; }(3) << endl; 30 | return 0; 31 | // lambda表达式默认就是constexpr函数 32 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/functionLambda/function.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-1-11. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main() { 12 | 13 | map> op_dict{ 14 | {"+", [](int x, int y) { return x + y; }}, 15 | {"-", [](int x, int y) { return x - y; }}, 16 | {"*", [](int x, int y) { return x * y; }}, 17 | {"/", [](int x, int y) { return x / y; }}, 18 | }; 19 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/literalAssert/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //learn_class/modern_cpp_30/literalAssert:literal` 2 | # please run `bazel run //learn_class/modern_cpp_30/literalAssert:assert` 3 | 4 | load("@rules_cc//cc:defs.bzl", "cc_binary") 5 | 6 | cc_binary( 7 | name = "literal", 8 | srcs = ["literal.cpp"], 9 | copts = ["-std=c++17"], 10 | ) 11 | cc_binary( 12 | name = "assert", 13 | srcs = ["assert.cpp"], 14 | copts = ["-std=c++11"], 15 | ) -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/literalAssert/assert.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-25. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | int main() { 9 | const int alignment = 5; 10 | assert((alignment & (alignment - 1)) == 0); 11 | static_assert((alignment & (alignment - 1)) == 0, 12 | "Alignment must be power of two"); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/literalAssert/default_delete.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-25. 3 | // 4 | 5 | class myFun { 6 | public: 7 | myFun() = default; 8 | 9 | myFun(const myFun &) = default; 10 | 11 | myFun &operator=(const myFun &) = default; 12 | 13 | myFun(myFun &&) = delete; 14 | 15 | myFun &operator=(myFun &&) = delete; 16 | 17 | ~myFun() = default; 18 | }; -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/literalAssert/overridefinal.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-25. 3 | // 4 | 5 | class A { 6 | public: 7 | virtual void foo(); 8 | virtual void bar(); 9 | void foobar(); 10 | }; 11 | 12 | class B : public A { 13 | public: 14 | void foo() override; // OK 15 | void bar() override final; // OK 16 | // void foobar() override; 17 | // 非虚函数不能 override 18 | }; 19 | 20 | class C final : public B { 21 | public: 22 | void foo() override; // OK 23 | // void bar() override; 24 | // final 函数不可 override 25 | }; 26 | 27 | class D : public C { 28 | // 错误:final 类不可派生 29 | }; -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/memorymodel_atomic/hungrysingleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-2-6. 3 | // 4 | 5 | class singleton { 6 | private: 7 | singleton() {} 8 | static singleton *p; 9 | public: 10 | static singleton *instance(); 11 | }; 12 | 13 | singleton *singleton::p = new singleton(); 14 | singleton* singleton::instance() { 15 | return p; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/memorymodel_atomic/iazysingleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-2-6. 3 | // 4 | 5 | class singleton { 6 | private: 7 | singleton() {} 8 | static singleton *p; 9 | public: 10 | static singleton *instance(); 11 | }; 12 | 13 | singleton *singleton::p = nullptr; 14 | 15 | singleton* singleton::instance() { 16 | if (p == nullptr) 17 | p = new singleton(); 18 | return p; 19 | } 20 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/memorymodel_atomic/lock_singleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-2-7. 3 | // 4 | #include 5 | using namespace std; 6 | 7 | #include 8 | 9 | class singleton { 10 | private: 11 | singleton() {} 12 | static singleton *p; 13 | static mutex lock_; 14 | public: 15 | static singleton *instance(); 16 | }; 17 | 18 | singleton *singleton::p = nullptr; 19 | 20 | singleton* singleton::instance() { 21 | lock_guard guard(lock_); 22 | if (p == nullptr) 23 | p = new singleton(); 24 | return p; 25 | } 26 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/memorymodel_atomic/pthreadoncesingleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-2-6. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | class singleton { 9 | private: 10 | singleton(); //私有构造函数,不允许使用者自己生成对象 11 | singleton(const singleton &other); 12 | 13 | //要写成静态方法的原因:类成员函数隐含传递this指针(第一个参数) 14 | static void init() { 15 | p = new singleton(); 16 | } 17 | 18 | static pthread_once_t ponce_; 19 | static singleton *p; //静态成员变量 20 | public: 21 | singleton *instance() { 22 | // init函数只会执行一次 23 | pthread_once(&ponce_, &singleton::init); 24 | return p; 25 | } 26 | }; 27 | 28 | 29 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/memorymodel_atomic/static_local_singleton.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 20-2-7. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | class singleton { 10 | private: 11 | static singleton *p; 12 | singleton() {} 13 | public: 14 | singleton *instance(); 15 | }; 16 | 17 | singleton *singleton::instance() { 18 | static singleton p; 19 | return &p; 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/obj/obj4.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-22. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | // Can copy and move 10 | class A { 11 | public: 12 | A() { cout << "Create A\n"; } 13 | 14 | ~A() { cout << "Destroy A\n"; } 15 | 16 | A(const A &) { cout << "Copy A\n"; } 17 | 18 | A(A &&) = delete; 19 | }; 20 | 21 | A getA_duang() { 22 | A a1; 23 | A a2; 24 | if (rand() > 42) { 25 | return a1; 26 | } else { 27 | return a2; 28 | } 29 | } 30 | 31 | int main() { auto a = getA_duang(); } -------------------------------------------------------------------------------- /learn_class/modern_cpp_30/obj/obj5.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-22. 3 | // 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | // Can copy and move 10 | class A { 11 | public: 12 | A() { cout << "Create A\n"; } 13 | 14 | ~A() { cout << "Destroy A\n"; } 15 | 16 | // A(const A &) { cout << "Copy A\n"; } 17 | // 18 | // A(A &&) { cout << "Move A\n"; } 19 | 20 | A(const A &&) = delete; 21 | A(A &&) = delete; 22 | }; 23 | 24 | A getA_unnamed() { return A(); } 25 | 26 | int main() { auto a = getA_unnamed(); } -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day1/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day1:union` 2 | # please run `bazel run //practical_exercises/10_day_practice/day1:print` 3 | # please run `bazel run //practical_exercises/10_day_practice/day1:annotate` 4 | # please run `bazel run //practical_exercises/10_day_practice/day1:runnian` 5 | 6 | load("@rules_cc//cc:defs.bzl", "cc_binary") 7 | 8 | cc_binary( 9 | name = "union", 10 | srcs = ["union.cpp"], 11 | ) 12 | cc_binary( 13 | name = "print", 14 | srcs = ["print.cpp"], 15 | ) 16 | cc_binary( 17 | name = "annotate", 18 | srcs = ["annotate.cpp"], 19 | ) 20 | cc_binary( 21 | name = "runnian", 22 | srcs = ["runnian.cpp"], 23 | ) 24 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day1/annotate.cpp: -------------------------------------------------------------------------------- 1 | /* 注释.cpp */ 2 | #include 3 | 4 | //另一种注释方法 5 | #if 0 6 | asd 7 | #endif 8 | 9 | //打开注释 10 | //条件编译指令 11 | #if 1 12 | #endif 13 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day1/print.cpp: -------------------------------------------------------------------------------- 1 | /* 打印练习.cpp */ 2 | #include 3 | using namespace std; 4 | int main(int argc, char const *argv[]) { 5 | int i, j, k, f; 6 | for (i = 1; i <= 4; i++) { 7 | for (j = 1; j <= 30; j++) 8 | cout << " "; 9 | for (k = 1; k <= 8 - 2 * i; k++) 10 | cout << " "; 11 | for (f = 1; f <= 2 * i; f++) 12 | cout << '*'; 13 | cout << endl; 14 | } 15 | for (i = 1; i <= 3; i++) { 16 | for (j = 1; j <= 30; j++) 17 | cout << " "; 18 | for (f = 1; f <= 7 - 2 * i; f++) 19 | cout << '*'; 20 | cout << endl; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day1/runnian.cpp: -------------------------------------------------------------------------------- 1 | /* 是否闰年.cpp */ 2 | #include 3 | using namespace std; 4 | int main(int argc, char const *argv[]) { 5 | int year; 6 | bool isLeapYear; 7 | cout << "Enter the year: "; 8 | cin >> year; 9 | isLeapYear = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); 10 | if (isLeapYear) { 11 | cout << year << " is a leap year" << endl; 12 | } else { 13 | cout << year << " is not a leap year" << endl; 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day1/union.cpp: -------------------------------------------------------------------------------- 1 | /* 联合体学习.cpp */ 2 | #include 3 | using namespace std; 4 | //相同的内存地址 5 | union myun { 6 | struct { 7 | int x; 8 | int y; 9 | int z; 10 | } u; 11 | int k; 12 | } a; 13 | int main() { 14 | a.u.x = 4; 15 | a.u.y = 5; 16 | a.u.z = 6; 17 | a.k = 0; //覆盖掉第一个int空间值 18 | printf("%d %d %d %d\n", a.u.x, a.u.y, a.u.z, a.k); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day10/file/12-1.cpp: -------------------------------------------------------------------------------- 1 | //用cin输入字符串数据时,如果字符串中含有空白就不能完整输入。因为遇到空白字符时,cin就认为字符串结束了。 2 | #include 3 | using namespace std; 4 | int main(int argc, char const *argv[]) { 5 | char a[50]; 6 | cout << "please input a string:"; 7 | cin >> a; 8 | cout << a << endl; 9 | 10 | return 0; 11 | } 12 | /* 13 | 若a的内容是: 14 | this is a string! 15 | 就难以输入啦! 16 | 这样的数据应用输入流类的成员函数输入 17 | */ -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day10/file/12-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(int argc, char const *argv[]) { 4 | char stu[5][10]; 5 | int i; 6 | for (i = 0; i < 5; i++) 7 | cin.getline(stu[i], 10, ','); 8 | for (i = 0; i < 5; i++) 9 | cout << stu[i] << endl; 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day10/file/12-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //函数原型 4 | // put(char c) 5 | // write(const char*c, int n) 6 | int main() { 7 | char c; 8 | char a[50] = "this is a string..."; 9 | cout << "use get() input char:"; 10 | while ((c = cin.get()) != '\n') { 11 | cout.put(c); 12 | cout.put('\n'); 13 | cout.put('t').put('h').put('i').put('s').put('\n'); 14 | cout.write(a, 12).put('\n'); 15 | cout << "look" 16 | << "\t here!" << endl; 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day10/file/12-5.cpp: -------------------------------------------------------------------------------- 1 | // Eg12-5.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | char c[30] = "this is string"; 7 | double d = -1234.8976; 8 | cout << setw(30) << left << setfill('*') << c << "----L1" << endl; 9 | cout << setw(30) << right << setfill('*') << c << "----L2" << endl; 10 | // showbase显示数值的基数前缀 11 | cout << dec << showbase << showpoint << setw(30) << d << "----L3" 12 | << "\n"; 13 | // showpoint显示小数点 14 | cout << setw(30) << showpoint << setprecision(10) << d << "----L4" 15 | << "\n"; 16 | // setbase(8)设置八进制 17 | cout << setw(30) << setbase(16) << 100 << "----L5" 18 | << "\n"; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day10/file/12-7.cpp: -------------------------------------------------------------------------------- 1 | // Eg12-7.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | char ch; 7 | ofstream out("/test.dat", ios::out | ios::binary); // L1 8 | for (int i = 0; i < 90; i++) { 9 | if (i > 0 && (i % 30) == 0) 10 | out.put('\n'); 11 | out.put(i); 12 | out.put(' '); 13 | } 14 | out.close(); 15 | ifstream in("/test.dat", ios::in | ios::binary); 16 | while (in.get(ch)) 17 | cout << ch; 18 | in.close(); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day10/file/input/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day10/file/input:getline` 2 | # please run `bazel run //practical_exercises/10_day_practice/day10/file/input:get2` 3 | # please run `bazel run //practical_exercises/10_day_practice/day10/file/input:get` 4 | 5 | load("@rules_cc//cc:defs.bzl", "cc_binary") 6 | 7 | cc_binary( 8 | name = "getline", 9 | srcs = ["getline.cpp"], 10 | ) 11 | cc_binary( 12 | name = "get2", 13 | srcs = ["get2.cpp"], 14 | ) 15 | cc_binary( 16 | name = "get", 17 | srcs = ["get.cpp"], 18 | ) 19 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day10/file/input/get.cpp: -------------------------------------------------------------------------------- 1 | //【例12-2】 用函数get和getline读取数据。 2 | #include 3 | using namespace std; 4 | int main() { 5 | char a, b, c, d; 6 | cin.get(a); 7 | cin.get(b); 8 | c = cin.get(); 9 | d = cin.get(); 10 | cout << int(a) << ',' << int(b) << ',' << int(c) << ',' << int(d) << endl; 11 | 12 | return 0; 13 | } 14 | 15 | /* 16 | 用法:a = cin.get() ?或者 ?cin.get(a) 17 | 结束条件:输入字符足够后回车 18 | 说明:这个是单字符的输入,用途是输入一个字符,把它的ASCALL码存入到a中 19 | 处理方法:与cin不同,cin.get()在缓冲区遇到[enter],[space],[tab]不会作为舍弃,而是继续留在缓冲区中 20 | */ -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day10/readme.md: -------------------------------------------------------------------------------- 1 | 文件例题 2 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day2/compute.cpp: -------------------------------------------------------------------------------- 1 | /* 求π.cpp */ 2 | #include 3 | using namespace std; 4 | double arctan(double x); 5 | int main(int argc, char const *argv[]) { 6 | double a = 16.0 * arctan(1.0 / 5.0); 7 | double b = 4.0 * arctan(1.0 / 239.0); 8 | double pi = a - b; 9 | cout << pi << endl; 10 | 11 | return 0; 12 | } 13 | double arctan(double x) { 14 | double head = x; 15 | int tail = 1; 16 | double art = 0; 17 | while (double(head / tail) > 1e-15) { 18 | art = (tail % 4 == 1) ? art + head / tail : art - head / tail; 19 | head *= x * x; 20 | tail += 2; 21 | cout << "---------------" << endl; 22 | cout << tail << endl; 23 | cout << "---------------" << endl; 24 | } 25 | return art; 26 | } 27 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day2/enum.cpp: -------------------------------------------------------------------------------- 1 | /* 枚举类型.cpp */ 2 | #include 3 | using namespace std; 4 | 5 | enum weekday { s, m, t, w, thu, f, s1 }; 6 | 7 | int main(int argc, char const *argv[]) { 8 | enum weekday wek = s; 9 | // weekday wek=s; 10 | for (int i = wek; i != f; i++) { 11 | cout << i << endl; 12 | cout << wek + s << endl; 13 | cout << "-------哈哈-------" << endl; 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day2/hanoi.cpp: -------------------------------------------------------------------------------- 1 | /* 汉诺塔.cpp */ 2 | #include 3 | using namespace std; 4 | void move(char A, char B); 5 | void hanoi(int n, char A, char B, char C); 6 | int main(int argc, char const *argv[]) { 7 | cout << "请输入盘子数量:"; 8 | int disks; 9 | cin >> disks; 10 | hanoi(disks, 'A', 'B', 'C'); 11 | 12 | return 0; 13 | } 14 | 15 | void move(char A, char B) { cout << A << "->" << B << endl; } 16 | 17 | void hanoi(int n, char A, char B, char C) { 18 | if (n == 1) { 19 | move(A, C); 20 | } else { 21 | hanoi(n - 1, A, C, B); 22 | move(A, C); 23 | hanoi(n - 1, B, A, C); 24 | } 25 | } -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day2/pow.cpp: -------------------------------------------------------------------------------- 1 | /* x的n次方.cpp */ 2 | #include 3 | using namespace std; 4 | double power(double x, int n); 5 | 6 | int main(int argc, char const *argv[]) { 7 | int x; 8 | cin >> x; 9 | int wei = 0; 10 | int sum = 0; 11 | int each, chu; 12 | for (int i = 0; i < 8; i++) { 13 | each = x % 10; 14 | chu = x / 10; 15 | sum += each * power(2, wei); 16 | x = chu; 17 | wei++; 18 | } 19 | cout << sum << endl; 20 | 21 | return 0; 22 | } 23 | double power(double x, int n) { 24 | double val = 1.0; 25 | while (n--) { 26 | val *= x; 27 | } 28 | return val; 29 | } 30 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day2/rec1.cpp: -------------------------------------------------------------------------------- 1 | /* 递归1.cpp */ 2 | #include 3 | using namespace std; 4 | 5 | int f(int n); 6 | int main(int argc, char const *argv[]) { 7 | cout << "input x:"; 8 | int x; 9 | cin >> x; 10 | cout << f(x) << endl; 11 | 12 | return 0; 13 | } 14 | 15 | int f(int n) { 16 | if (n == 0) { 17 | return 1; 18 | } else { 19 | return n * f(n - 1); 20 | } 21 | } -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day2/rec2.cpp: -------------------------------------------------------------------------------- 1 | /* 递归2.cpp */ 2 | #include 3 | using namespace std; 4 | int f(int n, int k); 5 | int main(int argc, char const *argv[]) { 6 | cout << "请输入n与k" << endl; 7 | int n, k; 8 | cin >> n; 9 | cin >> k; 10 | cout << f(n, k) << endl; 11 | 12 | return 0; 13 | } 14 | 15 | int f(int n, int k) { 16 | if ((n == k) || (k == 0)) { 17 | return 1; 18 | } else { 19 | return f(n - 1, k - 1) + f(n - 1, k); 20 | } 21 | } -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day2/st.cpp: -------------------------------------------------------------------------------- 1 | /* 结构体.cpp */ 2 | #include 3 | using namespace std; 4 | struct student { 5 | int num; 6 | char name[20]; 7 | char gender; 8 | }; 9 | 10 | int main(int argc, char const *argv[]) { 11 | student s = {10, "asd", 'M'}; 12 | cout << s.num << endl; 13 | cout << sizeof(s.num) << endl; 14 | cout << sizeof(s.name) << endl; 15 | cout << sizeof(s.gender) << endl; 16 | cout << sizeof(s) << endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day3/inline.cpp: -------------------------------------------------------------------------------- 1 | /* 内联函数.cpp */ 2 | #include 3 | using namespace std; 4 | //函数声明 5 | inline double CalArea(double radius); 6 | int main(int argc, char const *argv[]) { 7 | double r(3.0); 8 | double area; 9 | area = CalArea(r); 10 | cout << area << endl; 11 | 12 | return 0; 13 | } 14 | //加关键字inline 15 | inline double CalArea(double radius) { return 3.14 * radius * radius; } -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day3/predeclare.cpp: -------------------------------------------------------------------------------- 1 | /* 类前向声明.cpp */ 2 | /* 3 | 使用前向引用声明虽然可以解决一些问题,但它并不是万能的。需要注意的是, 4 | 尽管使用了前向引用声明,但是在提供一个完整的类声明之前,不能声明该类的对象, 5 | 也不能在内联成员函数中使用该类的对象。请看下面的程序段: 6 | */ 7 | 8 | //第一种 9 | #include 10 | class Fred; //前向引用声明 11 | class Barney { 12 | Fred x; //错误:类Fred的声明尚不完善 13 | }; 14 | class Fred { 15 | Barney y; 16 | }; 17 | 18 | //第二种 19 | class Fred; //前向引用声明 20 | 21 | class Barney { 22 | public: 23 | void method() { 24 | x->yabbaDabbaDo(); //错误:Fred类的对象在定义之前被使用 25 | } 26 | 27 | private: 28 | Fred *x; //正确,经过前向引用声明,可以声明Fred类的对象指针 29 | }; 30 | 31 | class Fred { 32 | public: 33 | void yabbaDabbaDo(); 34 | 35 | private: 36 | Barney *y; 37 | }; 38 | 39 | /* 40 | 总结:当使用前向引用声明时,只能使用被声明的符号,而不能涉及类的任何细节。 41 | */ -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day3/static_member1.cpp: -------------------------------------------------------------------------------- 1 | /* 静态成员函数1.cpp */ 2 | /* 3 | 知识点: 4 | 静态成员函数 5 | 类外代码可以使用类名和作用域操作符来调用静态成员函数。 6 | 静态成员函数只能引用属于该类的静态数据成员或静态成员函数。 7 | */ 8 | #include 9 | using namespace std; 10 | class Application { 11 | public: 12 | static void f(); 13 | static void g(); 14 | 15 | private: 16 | static int global; 17 | }; 18 | int Application::global = 0; 19 | void Application::f() { global = 5; } 20 | void Application::g() { cout << global << endl; } 21 | 22 | int main() { 23 | Application::f(); 24 | Application::g(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day3/static_member2.cpp: -------------------------------------------------------------------------------- 1 | /* 静态成员函数2.cpp */ 2 | #include 3 | using namespace std; 4 | class A { 5 | public: 6 | static void f(A a); 7 | 8 | private: 9 | int x; 10 | }; 11 | void A::f(A a) { 12 | 13 | //静态成员函数只能引用属于该类的静态数据成员或静态成员函数。 14 | // cout< 3 | using namespace std; 4 | void swap(int &a, int &b); 5 | int main(int argc, char const *argv[]) { 6 | int x1(5); 7 | int x2(7); 8 | swap(x1, x2); 9 | cout << x1 << " " << x2 << endl; 10 | 11 | return 0; 12 | } 13 | 14 | void swap(int &a, int &b) { 15 | int t; 16 | t = a; 17 | a = b; 18 | b = t; 19 | } 20 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day4/clock/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day4/clock:operator` 2 | # please run `bazel run //practical_exercises/10_day_practice/day4/clock:operator_plus` 3 | 4 | load("@rules_cc//cc:defs.bzl", "cc_binary") 5 | 6 | cc_binary( 7 | name = "operator", 8 | srcs = ["operator.cpp"], 9 | ) 10 | cc_binary( 11 | name = "operator_plus", 12 | srcs = ["operator_plus.cpp"], 13 | ) 14 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day4/clock/operator.cpp: -------------------------------------------------------------------------------- 1 | /* 重载()的时钟.cpp */ 2 | #include 3 | using namespace std; 4 | class Time { 5 | private: 6 | int hh, mm, ss; 7 | 8 | public: 9 | Time(int h = 0, int m = 0, int s = 0) : hh(h), mm(m), ss(s) {} 10 | void operator()(int h, int m, int s) { 11 | hh = h; 12 | mm = m; 13 | ss = s; 14 | } 15 | void ShowTime() { cout << hh << ":" << mm << ":" << ss << endl; } 16 | }; 17 | int main() { 18 | Time t1(12, 10, 11); 19 | t1.ShowTime(); 20 | t1.operator()(23, 20, 34); 21 | t1.ShowTime(); 22 | t1(10, 10, 10); 23 | t1.ShowTime(); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day4/const/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day4/const:obj_func` 2 | # please run `bazel run //practical_exercises/10_day_practice/day4/const:obj_ref` 3 | 4 | load("@rules_cc//cc:defs.bzl", "cc_binary") 5 | 6 | cc_binary( 7 | name = "obj_func", 8 | srcs = ["obj_func.cpp"], 9 | ) 10 | cc_binary( 11 | name = "obj_ref", 12 | srcs = ["obj_ref.cpp"], 13 | ) 14 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day4/const/obj_ref.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | void display(const double &r); 5 | 6 | class A { 7 | public: 8 | A(int i, int j) { 9 | x = i; 10 | y = j; 11 | } 12 | 13 | private: 14 | int x, y; 15 | }; 16 | int main() { 17 | double d(9.5); 18 | display(d); 19 | A const a(3, 4); // a是常对象,不能被更新 20 | 21 | return 0; 22 | } 23 | void display(const double &r) 24 | //常引用做形参,在函数中不能更新 r所引用的对象。 25 | { 26 | cout << r << endl; 27 | } 28 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day4/const/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/practical_exercises/10_day_practice/day4/const/readme.md -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day4/copy_ctor/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day4/copy_ctor:clock` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") 4 | 5 | cc_library( 6 | name = "clock", 7 | hdrs = ["clock.h"], 8 | ) 9 | 10 | cc_binary( 11 | name = "main", 12 | srcs = ["clock.cpp"], 13 | deps = [ 14 | ":clock", 15 | ], 16 | ) 17 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day4/copy_ctor/clock.h: -------------------------------------------------------------------------------- 1 | #ifndef CLOCK 2 | #define CLOCK 3 | class Clock { 4 | public: 5 | Clock(int NewH, int NewM, int NewS); 6 | Clock( 7 | Clock & 8 | c); //拷贝构造函数,如果不加,编译器会自动生成一个拷贝构造函数,因此加不加都可以直接使用。 9 | void SetTime(int NewH, int NewM, int NewS); 10 | void ShowTime(); 11 | ~Clock(); //析构函数,编译器会自动产生一个默认的析构函数。 12 | private: 13 | int Hour, Minute, Second; 14 | }; 15 | 16 | #endif 17 | 18 | /* 19 | #ifndef 标识符 20 | 程序段1 21 | #else 22 | 程序段2 23 | #endif 24 | 如果“标识符”未被定义过,则编译程序段1,否则编译程序段2。 25 | 26 | */ -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day4/friend/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day4/friend:class` 2 | # please run `bazel run //practical_exercises/10_day_practice/day4/friend:func` 3 | 4 | load("@rules_cc//cc:defs.bzl", "cc_binary") 5 | 6 | cc_binary( 7 | name = "class", 8 | srcs = ["class.cpp"], 9 | ) 10 | cc_binary( 11 | name = "func", 12 | srcs = ["func.cpp"], 13 | ) 14 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day4/friend/func.cpp: -------------------------------------------------------------------------------- 1 | //使用友元函数计算两点间距离 2 | #include 3 | #include 4 | using namespace std; 5 | class Point { 6 | public: 7 | Point(int x = 0, int y = 0) : X(x), Y(y) {} 8 | int GetX() { return X; } 9 | int GetY() { return Y; } 10 | friend float Distance(Point &a, Point &b); 11 | 12 | private: 13 | int X, Y; //私有数据成员 14 | }; 15 | //通过将一个模块声明为另一个模块的友元,一个模块能够引用到另一个模块中本是被隐藏的信息。 16 | float Distance(Point &a, Point &b) { 17 | double dx = a.X - b.X; 18 | double dy = a.Y - b.Y; 19 | return sqrt(dx * dx + dy * dy); 20 | } 21 | 22 | int main() { 23 | Point p1(3.0, 5.0), p2(4.0, 6.0); 24 | cout << "两点距离为:" << Distance(p1, p2) << endl; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day4/friend/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/practical_exercises/10_day_practice/day4/friend/readme.md -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/ctor_dtor/cseq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | 先构造成员 6 | 再构造自身(调用构造函数) 7 | */ 8 | 9 | class A { 10 | public: 11 | A() { cout << "Constructing A" << endl; } 12 | ~A() { cout << "Destructing A" << endl; } 13 | }; 14 | class B { 15 | public: 16 | B() { cout << "Constructing B" << endl; } 17 | ~B() { cout << "Destructing B" << endl; } 18 | }; 19 | 20 | class C { 21 | public: 22 | C() { cout << "Constructing C" << endl; } 23 | ~C() { cout << "Destructing C" << endl; } 24 | B b; 25 | A a; 26 | }; 27 | 28 | int main() { 29 | C c; 30 | 31 | } 32 | 33 | /* 34 | 执行结果: 35 | Constructing B 36 | Constructing A 37 | Constructing C 38 | Destructing C 39 | Destructing A 40 | Destructing B 41 | */ -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/ctor_dtor/ctor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Base { 4 | private: 5 | int x; 6 | 7 | public: 8 | Base(int a) { 9 | x = a; 10 | cout << "Base constructor x=" << x << endl; 11 | } 12 | ~Base() { cout << "Base destructor..." << endl; } 13 | }; 14 | class Derived : public Base { 15 | private: 16 | int y; 17 | 18 | public: 19 | Derived(int a, int b) : Base(a) { //派生类构造函数的初始化列表 20 | y = b; 21 | cout << "Derived constructor y=" << y << endl; 22 | } 23 | ~Derived() { cout << "Derived destructor..." << endl; } 24 | }; 25 | int main() { 26 | Derived d(1, 2); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/ctor_dtor/noctor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | public: 5 | A() { cout << "Constructing A" << endl; } 6 | ~A() { cout << "Destructing A" << endl; } 7 | }; 8 | class B : public A { 9 | public: 10 | ~B() { cout << "Destructing B" << endl; } 11 | }; 12 | int main() { 13 | B b; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/ctor_dtor/param.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Point { 4 | protected: 5 | int x, y; 6 | 7 | public: 8 | Point(int a, int b = 0) { 9 | x = a; 10 | y = b; 11 | cout << "constructing point(" << x << "," << y << ")" << endl; 12 | } 13 | }; 14 | class Line : public Point { 15 | protected: 16 | int len; 17 | 18 | public: 19 | Line(int a, int b, int l) : Point(a, b) { //构造函数初始化列表 20 | len = l; 21 | cout << "Constructing Line,len ..." << len << endl; 22 | } 23 | }; 24 | int main() { 25 | Line L1(1, 2, 3); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/ctor_dtor/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/practical_exercises/10_day_practice/day5/ctor_dtor/readme.md -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/ctor_dtor/seq.cpp: -------------------------------------------------------------------------------- 1 | // Eg6-12.cpp 2 | #include 3 | using namespace std; 4 | class A { 5 | int x; 6 | 7 | public: 8 | A(int i = 0) { 9 | x = i; 10 | cout << "A-----" << x << endl; 11 | } 12 | }; 13 | class B { 14 | int y; 15 | 16 | public: 17 | B(int i) { 18 | y = i; 19 | cout << "B-----" << y << endl; 20 | } 21 | }; 22 | class C { 23 | int z; 24 | 25 | public: 26 | C(int i) { 27 | z = i; 28 | cout << "C-----" << z << endl; 29 | } 30 | }; 31 | class D : public B { 32 | public: 33 | C c1, c2; 34 | A *a1 = new A(10); 35 | A a0, a4; 36 | D() : a4(4), c2(2), c1(1), B(1) { cout << "D-----5" << endl; } 37 | }; 38 | int main() { 39 | D d; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/inherit_access/private.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class base { 4 | int x; 5 | 6 | public: 7 | void setx(int n) { x = n; } 8 | int getx() { return x; } 9 | void showx() { cout << x << endl; } 10 | }; 11 | //派生类 12 | class derived : public base { 13 | int y; 14 | 15 | public: 16 | void sety(int n) { y = n; } 17 | void sety() { y = getx(); } 18 | void showy() { cout << y << endl; } 19 | }; 20 | //派生类不可直接访问基类的private成员,可通过基类的共有成员函数访问 21 | int main() { 22 | derived obj; 23 | obj.setx(10); 24 | obj.sety(20); 25 | obj.showx(); 26 | obj.showy(); 27 | obj.sety(); 28 | obj.showx(); 29 | obj.showy(); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/inherit_access/protected.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 基类中protected的成员 3 | 类内部:可以访问 4 | 类的使用者:不能访问 5 | 类的派生类成员:可以访问 6 | */ 7 | #include 8 | class B { 9 | private: 10 | int i; 11 | 12 | protected: 13 | int j; 14 | 15 | public: 16 | int k; 17 | }; 18 | class D : public B { 19 | public: 20 | void f() { 21 | i = 1; // cannot access 派生类不可访问基类私有成员 22 | j = 2; //派生类可以访问基类保护成员 23 | k = 3; 24 | } 25 | }; 26 | int main() { 27 | B b; 28 | b.i = 1; // cannot access 私有成员,类的使用者不能访问 29 | b.j = 2; // cannot access 保护成员,类的使用者不能访问 30 | b.k = 3; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/inherit_access/public.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Base { 4 | int x; 5 | 6 | public: 7 | void setx(int n) { x = n; } 8 | int getx() { return x; } 9 | void showx() { cout << x << endl; } 10 | }; 11 | //私有继承 12 | //基类的中的public成员在派生类中是private, private成员在派生类中不可访问。 13 | class derived : private Base { 14 | int y; 15 | 16 | public: 17 | void sety(int n) { y = n; } 18 | void sety() { y = getx(); } 19 | void showy() { cout << y << endl; } 20 | }; 21 | int main() { 22 | derived obj; 23 | obj.setx(10); // cannot access 24 | obj.sety(20); 25 | obj.showx(); // cannot access 26 | obj.showy(); 27 | } 28 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/inherit_access/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/practical_exercises/10_day_practice/day5/inherit_access/readme.md -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/rela/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day5/rela:rela` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "rela", 7 | srcs = ["rela.cpp"], 8 | ) 9 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/rela/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/practical_exercises/10_day_practice/day5/rela/readme.md -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/rule/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day5/rule:direct` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "direct", 7 | srcs = ["direct.cpp"], 8 | ) 9 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/rule/direct.cpp: -------------------------------------------------------------------------------- 1 | /* 直接基类与间接基类.cpp */ 2 | //当同时存在直接基类和间接基类时,每个派生类只负责其直接基类的构造。 3 | #include 4 | using namespace std; 5 | class A { 6 | int x; 7 | 8 | public: 9 | A(int aa) { 10 | x = aa; 11 | cout << "Constructing A" << endl; 12 | } 13 | ~A() { cout << "Destructing A" << endl; } 14 | }; 15 | class B : public A { 16 | public: 17 | B(int x) : A(x) { cout << "Constructing B" << endl; } 18 | }; 19 | class C : public B { 20 | public: 21 | C(int y) : B(y) { cout << "Constructing C" << endl; } 22 | }; 23 | int main() { 24 | C c(1); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/rule/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/practical_exercises/10_day_practice/day5/rule/readme.md -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/virtual/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day5/virtual:example2` 2 | # please run `bazel run //practical_exercises/10_day_practice/day5/virtual:init` 3 | # please run `bazel run //practical_exercises/10_day_practice/day5/virtual:seq` 4 | # please run `bazel run //practical_exercises/10_day_practice/day5/virtual:example1` 5 | 6 | load("@rules_cc//cc:defs.bzl", "cc_binary") 7 | 8 | cc_binary( 9 | name = "example2", 10 | srcs = ["example2.cpp"], 11 | ) 12 | cc_binary( 13 | name = "init", 14 | srcs = ["init.cpp"], 15 | ) 16 | cc_binary( 17 | name = "seq", 18 | srcs = ["seq.cpp"], 19 | ) 20 | cc_binary( 21 | name = "example1", 22 | srcs = ["example1.cpp"], 23 | ) 24 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/virtual/example1.cpp: -------------------------------------------------------------------------------- 1 | /* 例1.cpp */ 2 | #include 3 | using namespace std; 4 | class A { 5 | public: 6 | void vf() { cout << "I come from class A" << endl; } 7 | }; 8 | class B : public A {}; 9 | class C : public A {}; 10 | class D : public B, public C {}; 11 | 12 | int main() { 13 | D d; 14 | d.vf(); // error 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/virtual/example2.cpp: -------------------------------------------------------------------------------- 1 | /* 例2.cpp */ 2 | #include 3 | using namespace std; 4 | class A { 5 | public: 6 | void vf() { cout << "I come from class A" << endl; } 7 | }; 8 | class B : virtual public A {}; 9 | class C : virtual public A {}; 10 | class D : public B, public C {}; 11 | 12 | int main() { 13 | D d; 14 | d.vf(); // okay 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/virtual/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/practical_exercises/10_day_practice/day5/virtual/readme.md -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day5/virtual/seq.cpp: -------------------------------------------------------------------------------- 1 | /* 虚基类调用次序(重要).cpp */ 2 | //重要!!! 3 | #include 4 | using namespace std; 5 | class A { 6 | int a; 7 | 8 | public: 9 | A() { cout << "Constructing A" << endl; } 10 | }; 11 | class B { 12 | public: 13 | B() { cout << "Constructing B" << endl; } 14 | }; 15 | class B1 : virtual public B, virtual public A { 16 | public: 17 | B1(int i) { cout << "Constructing B1" << endl; } 18 | }; 19 | class B2 : public A, virtual public B { 20 | public: 21 | B2(int j) { cout << "Constructing B2" << endl; } 22 | }; 23 | class D : public B1, public B2 { 24 | public: 25 | D(int m, int n) : B1(m), B2(n) { cout << "Constructing D" << endl; } 26 | A a; 27 | }; 28 | 29 | int main() { 30 | D d(1, 2); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day6/abstract_class/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day6/abstract_class:main` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "main", 7 | srcs = ["main.cpp"], 8 | ) 9 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day6/virtual_func/virtual_dtor.cpp: -------------------------------------------------------------------------------- 1 | /* 虚析构函数.cpp */ 2 | 3 | #include 4 | using namespace std; 5 | class A { 6 | public: 7 | virtual ~A() { cout << "call A::~A()" << endl; } 8 | }; 9 | class B : public A { 10 | char *buf; 11 | 12 | public: 13 | B(int i) { buf = new char[i]; } 14 | ~B() { 15 | delete[] buf; 16 | cout << "call B::~()" << endl; 17 | } 18 | }; 19 | int main() { 20 | A *a = new B(10); 21 | delete a; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day6/virtual_func/vis.cpp: -------------------------------------------------------------------------------- 1 | /* 从基类继承的成员将访问到派生类版本.cpp */ 2 | #include 3 | using namespace std; 4 | class B { 5 | public: 6 | void f() { g(); } 7 | virtual void g() { cout << "B::g"; } 8 | }; 9 | class D : public B { 10 | public: 11 | void g() { cout << "D::g\n"; } 12 | }; 13 | int main() { 14 | D d; 15 | d.f(); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day7/binary_operator/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day7/binary_operator:operator` 2 | # please run `bazel run //practical_exercises/10_day_practice/day7/binary_operator:friend_operator` 3 | 4 | load("@rules_cc//cc:defs.bzl", "cc_binary") 5 | 6 | cc_binary( 7 | name = "operator", 8 | srcs = ["operator.cpp"], 9 | ) 10 | cc_binary( 11 | name = "friend_operator", 12 | srcs = ["friend_operator.cpp"], 13 | ) 14 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day7/brackets/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day7/brackets:brac` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "brac", 7 | srcs = ["brac.cpp"], 8 | ) 9 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day7/brackets/brac.cpp: -------------------------------------------------------------------------------- 1 | /* 重载()的时钟.cpp */ 2 | #include 3 | using namespace std; 4 | class Time { 5 | private: 6 | int hh, mm, ss; 7 | 8 | public: 9 | Time(int h = 0, int m = 0, int s = 0) : hh(h), mm(m), ss(s) {} 10 | void operator()(int h, int m, int s) { 11 | hh = h; 12 | mm = m; 13 | ss = s; 14 | } 15 | void ShowTime() { cout << hh << ":" << mm << ":" << ss << endl; } 16 | }; 17 | int main() { 18 | Time t1(12, 10, 11); 19 | t1.ShowTime(); 20 | t1.operator()(23, 20, 34); 21 | t1.ShowTime(); 22 | t1(10, 10, 10); 23 | t1.ShowTime(); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day7/equal_operator/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day7/equal_operator:equal_operator` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "equal_operator", 7 | srcs = ["equal_operator.cpp"], 8 | ) 9 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day7/equal_operator/equal_operator.cpp: -------------------------------------------------------------------------------- 1 | /* =运算符重载.cpp */ 2 | //例题ch.cppi 3 | #include 4 | using namespace std; 5 | class X { 6 | public: 7 | X &operator=(const X &x) { 8 | cout << "a:"; 9 | return *this; 10 | }; 11 | }; 12 | int main() { 13 | X obj1, obj2, obj3; 14 | obj1 = obj2; //调用重载“=” 15 | obj1.operator=(obj2); //调用重载“=” 16 | obj1 = obj2 = obj3; //调用重载“=” 17 | 18 | } 19 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day7/example/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day7/example:example` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "example", 7 | srcs = ["example.cpp"], 8 | ) 9 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day7/index_parentheses/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day7/subscript_operator:example` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "exexampleample1", 7 | srcs = ["example.cpp"], 8 | ) 9 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day7/subscript_operator/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day7/subscript_operator:subscript_operator` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "subscript_operator", 7 | srcs = ["subscript_operator.cpp"], 8 | ) 9 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day7/unary_operator/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day7/unary_operator:time_increase` 2 | # please run `bazel run //practical_exercises/10_day_practice/day7/unary_operator:time_counter` 3 | 4 | load("@rules_cc//cc:defs.bzl", "cc_binary") 5 | 6 | cc_binary( 7 | name = "time_increase", 8 | srcs = ["time_increase.cpp"], 9 | ) 10 | cc_binary( 11 | name = "time_counter", 12 | srcs = ["time_counter.cpp"], 13 | ) 14 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day8/class_template/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day8/class_template:stack` 2 | # please run `bazel run //practical_exercises/10_day_practice/day8/class_template:spec` 3 | 4 | load("@rules_cc//cc:defs.bzl", "cc_binary") 5 | 6 | cc_binary( 7 | name = "stack", 8 | srcs = ["stack.cpp"], 9 | ) 10 | cc_binary( 11 | name = "spec", 12 | srcs = ["spec.cpp"], 13 | ) 14 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day8/func/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day8/func:main` 2 | # please run `bazel run //practical_exercises/10_day_practice/day8/func:max_spec` 3 | # please run `bazel run //practical_exercises/10_day_practice/day8/func:max` 4 | # please run `bazel run //practical_exercises/10_day_practice/day8/func:sort` 5 | 6 | load("@rules_cc//cc:defs.bzl", "cc_binary") 7 | 8 | cc_binary( 9 | name = "main", 10 | srcs = ["main.cpp"], 11 | ) 12 | cc_binary( 13 | name = "max_spec", 14 | srcs = ["max_spec.cpp"], 15 | ) 16 | cc_binary( 17 | name = "max", 18 | srcs = ["max.cpp"], 19 | ) 20 | cc_binary( 21 | name = "sort", 22 | srcs = ["sort.cpp"], 23 | ) 24 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day8/func/max.cpp: -------------------------------------------------------------------------------- 1 | /* 求最大值.cpp */ 2 | // Eg9-2.cpp 3 | #include 4 | using namespace std; 5 | template T Max(T a, T b) { return (a > b) ? a : b; } 6 | /* 7 | C++在实例化函数模板的过程中,只是简单地将模板参数替换成调用实参的类型,并以此生成模板函数,不会进行参数类型的任何转换。 8 | */ 9 | int main() { 10 | double a = 2, b = 3.4; 11 | float c = 5.1, d = 3.2; 12 | //在模板调用时进行参数类型的强制转换 13 | cout << "2, 3.2 的最大值是:" << Max(double(2), 3.2) << endl; 14 | cout << "a, c 的最大值是:" << Max(float(a), c) << endl; 15 | //显示指定函数模板实例化的参数类型 16 | cout << "'a', 3 的最大值是:" << Max('a', 3) << endl; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day8/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Light-City/CPlusPlusThings/c9d0f14d92976a5aa65819e9543e2f812d4079ba/practical_exercises/10_day_practice/day8/readme.md -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day8/stl/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //practical_exercises/10_day_practice/day8/stl:map` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary") 4 | 5 | cc_binary( 6 | name = "map", 7 | srcs = ["map.cpp"], 8 | ) 9 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day9/exception/1.cpp: -------------------------------------------------------------------------------- 1 | // Eg10-1.cpp 2 | #include 3 | using namespace std; 4 | int main() { 5 | cout << "1--befroe try block..." << endl; 6 | try { 7 | cout << "2--Inside try block..." << endl; 8 | throw 10; 9 | cout << "3--After throw ...." << endl; 10 | } catch (int i) { 11 | cout << "4--In catch block1 ... exception..errcode is.." << i << endl; 12 | } catch (char *s) { 13 | cout << "5--In catch block2 ... exception..errcode is.." << s << endl; 14 | } 15 | cout << "6--After Catch..."; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day9/exception/2.cpp: -------------------------------------------------------------------------------- 1 | // Eg10-2.cpp 2 | #include 3 | using namespace std; 4 | int main() { 5 | cout << "1--befroe try block..." << endl; 6 | try { 7 | cout << "2--Inside try block..." << endl; 8 | throw 10; 9 | cout << "3--After throw ...." << endl; 10 | } catch (double i) { //仅此与例10.1不同 11 | cout << "4--In catch block1 .. an int type is.." << i << endl; 12 | } 13 | cout << "5--After Catch..."; 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day9/exception/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void temperature(int t) { 4 | try { 5 | if (t == 100) 6 | throw "It's at the boiling point."; 7 | else if (t == 0) 8 | throw "It reached the freezing point"; 9 | else 10 | cout << "the temperature is OK..." << endl; 11 | } catch (int x) { 12 | cout << "temperature=" << x << endl; 13 | } catch (char const *s) { 14 | cout << s << endl; 15 | } 16 | } 17 | int main() { 18 | temperature(0); // L1 19 | temperature(10); // L2 20 | temperature(100); // L3 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day9/exception/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void temperature(int t) { 4 | 5 | if (t == 100) 6 | throw "沸点!"; 7 | else if (t == 0) 8 | throw "冰点!"; 9 | else { 10 | cout << "temperatore=" << t << endl; 11 | } 12 | } 13 | int main() { 14 | try { 15 | temperature(0); // L1 16 | temperature(10); // L2 17 | temperature(100); // L3 18 | } catch (char const *s) { 19 | cout << s << endl; 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day9/exception/5.cpp: -------------------------------------------------------------------------------- 1 | // Eg10-5.cpp 2 | #include 3 | using namespace std; 4 | 5 | void handler(int n) throw(int, char, double) { 6 | if (n == 1) 7 | throw n; 8 | if (n == 2) 9 | throw 'x'; 10 | if (n == 3) 11 | throw 1.1; 12 | } 13 | int main() { 14 | cout << "Before handler..." << endl; 15 | try { 16 | handler(1); 17 | } catch (int i) { 18 | cout << "catch an integer..." << endl; 19 | } catch (char c) { 20 | cout << "catch an char..." << endl; 21 | } catch (double d) { 22 | cout << "catch an double..." << endl; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day9/exception/6.cpp: -------------------------------------------------------------------------------- 1 | // Eg10-6.cpp 2 | #include 3 | using namespace std; 4 | void Errhandler(int n) throw() { 5 | try { 6 | if (n == 1) 7 | throw n; 8 | if (n == 2) 9 | throw "dx"; 10 | if (n == 3) 11 | throw 1.1; 12 | } catch (...) { 13 | cout << "catch an exception..." << endl; 14 | } 15 | } 16 | int main() { 17 | Errhandler(1); 18 | Errhandler(2); 19 | Errhandler(3); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/day9/exception/7.cpp: -------------------------------------------------------------------------------- 1 | // Eg10-7.cpp 2 | #include 3 | using namespace std; 4 | //内部再次throw异常的时候,函数不要带throw() 5 | void Errhandler(int n) { 6 | try { 7 | if (n == 1) 8 | throw n; 9 | cout << "all is ok..." << endl; 10 | } catch (int n) { 11 | cout << "catch an int exception inside..." << n << endl; 12 | throw n; //再次抛出本catch捕获的异常 13 | } 14 | } 15 | int main() { 16 | try { 17 | Errhandler(1); 18 | } catch (int x) { 19 | cout << "catch int an exception in main..." << x << endl; 20 | } 21 | cout << "....End..." << endl; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /practical_exercises/10_day_practice/readme.md: -------------------------------------------------------------------------------- 1 | # 10日c++实战狂练 2 | 3 | - [x] [day1](day1) 4 | 5 | 基本c++语法 6 | 7 | - [x] [day2](day2) 8 | 9 | 递归、结构体、枚举、静态变量等 10 | 11 | - [x] [day3](day3) 12 | 13 | 函数 14 | 15 | - [x] [day4](day4) 16 | 17 | 函数深入 18 | 19 | - [x] [day5](day5) 20 | 21 | 继承多态 22 | 23 | - [x] [day6](day6) 24 | 25 | 虚函数、抽象类 26 | 27 | - [x] [day7](day7) 28 | 29 | 运算符重载 30 | 31 | - [x] [day8](day8) 32 | 33 | 模板与STL 34 | 35 | - [x] [day9](day9) 36 | 37 | 异常 38 | 39 | - [x] [day10](day10) 40 | 41 | 文件与流 -------------------------------------------------------------------------------- /practical_exercises/key_exercises/README.md: -------------------------------------------------------------------------------- 1 | ## 重点实战练习 2 | 3 | ├── [中括号重载.cpp](bracket_overloading.cpp) 4 | ├── [时钟++运算符重载.cpp](clock.cpp) 5 | ├── [运算符重载之强制转换.cpp](operator_cast.cpp) 6 | └── [重载圆括号的时钟.cpp](operator_circle.cpp) 7 | 8 | ├── [函数模板.cpp](func_temp.cpp) 9 | 10 | ├── [动态数组.cpp](array.cpp) 11 | 12 | ├── [字典插入与查找.cpp](map_insert_look.cpp) 13 | 14 | ├── [异常捕获.cpp](try.cpp) 15 | 16 | ├── [类模板之栈.cpp](stack.cpp) 17 | 18 | ├── [类模板特化之数组.cpp](array_template.cpp) 19 | 20 | ├── [继承与封装.cpp](override.cpp) 21 | 22 | ├── [读写文件综合题.cpp](read_file.cpp) 23 | ├── [输入输出运算符重载.cpp](io_operator_overload.cpp) 24 | ├── [输入输出重载.cpp](io_operator.cpp) 25 | ├── [输出格式.cpp](output.cpp) 26 | 27 | -------------------------------------------------------------------------------- /practical_exercises/key_exercises/array.cpp: -------------------------------------------------------------------------------- 1 | /* 数组.cpp */ 2 | #include 3 | #include 4 | 5 | int main() { 6 | 7 | char *sPtr; 8 | const char *s = "hello"; 9 | sPtr = new char[strlen(s) + 1]; 10 | strncpy(sPtr, s, strlen(s)); 11 | std::cout << sPtr << std::endl; 12 | delete sPtr; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /practical_exercises/key_exercises/func_temp.cpp: -------------------------------------------------------------------------------- 1 | /* 函数模板.cpp */ 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | template T compareMax(T t1, T t2) { return t1 > t2 ? t1 : t2; } 7 | 8 | template <> 9 | const char *compareMax(const char *s1, const char *s2) { 10 | cout << "[for debug]" 11 | << " call compareMax template" << endl; 12 | return strcmp(s1, s2) >= 0 ? s1 : s2; 13 | } 14 | int main(int argc, char const *argv[]) { 15 | cout << compareMax(1, 2) << endl; 16 | cout << compareMax("asda", "qweq") << endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /practical_exercises/key_exercises/operator_circle.cpp: -------------------------------------------------------------------------------- 1 | /* 重载圆括号的时钟.cpp */ 2 | #include 3 | using namespace std; 4 | class Time { 5 | private: 6 | int hh, mm, ss; 7 | 8 | public: 9 | Time(int h = 0, int m = 0, int s = 0) : hh(h), mm(m), ss(s) {} 10 | void operator()(int h, int m, int s) { 11 | hh = h; 12 | mm = m; 13 | ss = s; 14 | } 15 | void ShowTime() { cout << hh << ":" << mm << ":" << ss << endl; } 16 | }; 17 | int main() { 18 | Time t1(12, 10, 11); 19 | t1.ShowTime(); 20 | t1.operator()(23, 20, 34); 21 | t1.ShowTime(); 22 | t1(10, 10, 10); 23 | t1.ShowTime(); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /practical_exercises/key_exercises/output.cpp: -------------------------------------------------------------------------------- 1 | /* 输出格式.cpp */ 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(int argc, char const *argv[]) { 7 | char s[20] = "this is a string"; 8 | double digit = -36.96656; 9 | cout << setw(30) << left << setfill('*') << s << endl; 10 | cout << dec << setprecision(4) << digit << endl; 11 | cout << dec << 15 << endl; 12 | // setbase(int x)设置进制后,后面所有操作都是按照这个进制来计算! 13 | cout << setbase(10) << 15 << endl; 14 | //四舍五入,并保留2位有效数组 15 | float x = 6.6937; 16 | cout << float(int(x * 1000 + 0.5) / 1000.0) << endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /practical_exercises/key_exercises/try.cpp: -------------------------------------------------------------------------------- 1 | /* 异常捕获.cpp */ 2 | #include 3 | using namespace std; 4 | //函数异常可以抛出去由主函数来处理 5 | void fun(int x) { 6 | try { 7 | if (x == 0) 8 | throw "异常"; 9 | } catch (...) { 10 | cout << "in fun" << endl; 11 | throw 1; 12 | } 13 | } 14 | 15 | int main(int argc, char const *argv[]) { 16 | 17 | try { 18 | fun(0); 19 | } catch (int n) { 20 | cout << "in main" << endl; 21 | } 22 | 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /tool/output/BUILD: -------------------------------------------------------------------------------- 1 | # please run `bazel run //tool/output:container` 2 | 3 | load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") 4 | 5 | cc_library( 6 | name = "output_container", 7 | hdrs = ["output_container.h"], 8 | copts = ["-std=c++17"], 9 | ) 10 | 11 | cc_binary( 12 | name = "container", 13 | srcs = ["container.cpp"], 14 | deps = [ 15 | ":output_container", 16 | ], 17 | copts = ["-std=c++17"], 18 | ) -------------------------------------------------------------------------------- /tool/output/container.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by light on 19-12-16. 3 | // 4 | 5 | #include "output_container.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int main() { 14 | map mp{{1, 1}, {2, 4}, {3, 9}}; 15 | cout << mp << endl; 16 | vector> vv{{1, 1}, {2, 4}, {3, 9}}; 17 | cout << vv << endl; 18 | 19 | pair p{1, 2}; 20 | cout << p << endl; 21 | 22 | set s{1, 2, 3}; 23 | cout << s << endl; 24 | 25 | vector v{'a', 'b'}; 26 | cout << v << endl; 27 | set vs{"a", "b"}; 28 | cout << vs << endl; 29 | 30 | map mm{{1, "23"}, {2, "234hi"}}; 31 | cout << mm << endl; 32 | } 33 | --------------------------------------------------------------------------------