├── .gitignore ├── 02.EnvironmentSetup ├── 1.Windows │ ├── 4.V_s_CodeMsvcConfiguration │ │ ├── c_cpp_properties.json │ │ ├── main.cpp │ │ └── tasks.json │ ├── 6.V_s_CodeGccConfiguration │ │ ├── c_cpp_properties.json │ │ ├── main.cpp │ │ └── tasks.json │ ├── 7.V_s_CodeClangConfiguration │ │ ├── c_cpp_properties.json │ │ ├── main.cpp │ │ └── tasks.json │ └── 8.C++20 Template Project_all_compilers │ │ ├── c_cpp_properties.json │ │ ├── main.cpp │ │ └── tasks.json └── 2.Linux │ ├── 4.V_sCodeGccConfiguration │ ├── main.cpp │ └── tasks.json │ ├── 6.V_sCodeClangConfiguration │ ├── main.cpp │ └── tasks.json │ └── 7.Cpp20TemplateProject_all_compilers │ ├── main.cpp │ └── tasks.json ├── 03.FirstSteps ├── 3.2FirstCppProgram │ └── main.cpp ├── 3.3Comments │ └── main.cpp ├── 3.4ErrorsWarnings │ └── main.cpp ├── 3.5StatementsAndFunctions │ └── main.cpp ├── 3.6DataInputAndOutput │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 04.VariablesAndDatatypes ├── 4.2NumberSystems │ └── main.cpp ├── 4.3IntegerTypes │ └── main.cpp ├── 4.4IntegerModifiers │ └── main.cpp ├── 4.5FractionalNumbers │ └── main.cpp ├── 4.6Booleans │ └── main.cpp ├── 4.7CharactersAndText │ └── main.cpp ├── 4.8Auto │ └── main.cpp ├── 4.9Assignments │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 05.OperationsOnData ├── 5.10.MathFunctions │ └── main.cpp ├── 5.11.WeirdIntegralTypes │ └── main.cpp ├── 5.2.BasicOperations │ └── main.cpp ├── 5.3.PrecedenceAndAssociativity │ └── main.cpp ├── 5.4.PrefixPostfixIncrementDecrement │ └── main.cpp ├── 5.5.CompoundAssignmentOperators │ └── main.cpp ├── 5.6.RelationalOperators │ └── main.cpp ├── 5.7.LogicalOperators │ └── main.cpp ├── 5.8.OutputFormatting │ └── main.cpp ├── 5.9.NumericLimits │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 06.LiteralsAndConstants ├── 6.2Literals │ └── main.cpp ├── 6.3Constants │ └── main.cpp ├── 6.4ConstantExpressions │ └── main.cpp ├── 6.5constinit │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 07.ConversionsOverflowAndUnderflow ├── 7.2ImplicitDataConversions │ └── main.cpp ├── 7.3ExplicitDataConversions │ └── main.cpp ├── 7.4OverflowAndUnderflow │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 08.BitwiseOperators ├── 8.2PrintingIntegersInBinary │ └── main.cpp ├── 8.3ShiftOperators │ └── main.cpp ├── 8.4LogicalBitwiseOperators │ └── main.cpp ├── 8.5CompoundBitwiseOperators │ └── main.cpp ├── 8.6Masks │ └── main.cpp ├── 8.7MasksExample │ └── main.cpp ├── 8.8PackingColorInformation │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 09.VariableLifetimeAndScope ├── 9.2VariableScope │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 10.FlowControl ├── 10.10SwitchWithInitializer │ └── main.cpp ├── 10.11VariableScopeRevisited │ └── main.cpp ├── 10.12SwitchScope │ └── main.cpp ├── 10.2IfStatements │ └── main.cpp ├── 10.3ElseIf │ └── main.cpp ├── 10.4Switch │ └── main.cpp ├── 10.5ShortCircuitEvaluation │ └── main.cpp ├── 10.6IntegralLogicConditions │ └── main.cpp ├── 10.7TernaryOperators │ └── main.cpp ├── 10.8IfConstexpr │ └── main.cpp ├── 10.9IfWithInitializer │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 11.Loops ├── 11.10InfiniteLoopPractice │ └── main.cpp ├── 11.11DecrementingLoops │ └── main.cpp ├── 11.12NestedLoops │ └── main.cpp ├── 11.13BreakAndContinue │ └── main.cpp ├── 11.14FixCalculator │ └── main.cpp ├── 11.15ForLoopWithInitCondition │ └── main.cpp ├── 11.2ForLoop │ └── main.cpp ├── 11.3ForLoopMultipleDeclarations │ └── main.cpp ├── 11.4CommaOperator │ └── main.cpp ├── 11.5RangeBasedForLoop │ └── main.cpp ├── 11.6WhileLoop │ └── main.cpp ├── 11.7HugeLoopsWithOutput │ └── main.cpp ├── 11.8DoWhileLoop │ └── main.cpp ├── 11.9InfiniteLoops │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 12.Arrays ├── 12.10Practice_FortuneTellerV2 │ └── main.cpp ├── 12.2DeclaringAndUsingArrays │ └── main.cpp ├── 12.3SizeOfAnArray │ └── main.cpp ├── 12.4ArraysOfCharacters │ └── main.cpp ├── 12.5ArrayBounds │ └── main.cpp ├── 12.6GeneratingRandomNumbers │ └── main.cpp ├── 12.7Practice_FortuneTellerV1 │ └── main.cpp ├── 12.8MultiDimensionalArrays │ └── main.cpp ├── 12.9MultiDimensionalArraysOfCharacters │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 13.Pointers ├── 13.10PointerArithmetic_Navigation │ └── main.cpp ├── 13.11PointerArithmetic_DistanceBetweenElements │ └── main.cpp ├── 13.12PointerArithmetic_ComparingPointers │ └── main.cpp ├── 13.13SwappingArrayData_PointerArithmetic │ └── main.cpp ├── 13.15DynamicMemoryAllocation │ └── main.cpp ├── 13.16DanglingPointers │ └── main.cpp ├── 13.17WhenNewFails │ └── main.cpp ├── 13.18NullPointerSafety │ └── main.cpp ├── 13.19MemoryLeaks │ └── main.cpp ├── 13.20DynamicallyAllocatedArrays │ └── main.cpp ├── 13.2DeclaringAndUsingPointers │ └── main.cpp ├── 13.3PointerToChar │ └── main.cpp ├── 13.4ArrayOfPointerToChar │ └── main.cpp ├── 13.5ConstPointerAndPointerToConst │ └── main.cpp ├── 13.6ArrayOfContPointerToConstChar │ └── main.cpp ├── 13.7PointersAndArrays │ └── main.cpp ├── 13.8SwappingArrayData_Demo │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 14.References ├── 14.2.DeclaringAndUsingReferences │ └── main.cpp ├── 14.3.ComparingPointersAndReferences │ └── main.cpp ├── 14.4.ReferencesAndConst │ └── main.cpp ├── 14.5.ReferencesWithRangeBasedForLoops │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 15.CharacterManipulationAndStrings ├── 15.10ModifyingStdStrings │ └── main.cpp ├── 15.11ComparingStdStrings │ └── main.cpp ├── 15.12StdStringCompare │ └── main.cpp ├── 15.13StdStringReplacingCopyingResizingSwapping │ └── main.cpp ├── 15.14SearchingStdString │ └── main.cpp ├── 15.15TransformingStdStringFromToNumbers │ └── main.cpp ├── 15.16EscapeSequences │ └── main.cpp ├── 15.17RawStringLiterals │ └── main.cpp ├── 15.18CopiedStrings │ └── main.cpp ├── 15.19StringView │ └── main.cpp ├── 15.2CharacterManipulation │ └── main.cpp ├── 15.3CStringManipulation │ └── main.cpp ├── 15.4CStringConcatenationAndCopy │ └── main.cpp ├── 15.6DeclaringAndUsingStdString │ └── main.cpp ├── 15.7ConcatenatingStdStrings │ └── main.cpp ├── 15.8AccessingCharactersInStdString │ └── main.cpp ├── 15.9StdStringSizeAndCapacity │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 16.Functions ├── 16.10PassByReference │ └── main.cpp ├── 16.11PassByConstReference │ └── main.cpp ├── 16.13ArrayFunctionParameters │ └── main.cpp ├── 16.14SizedArrayFunctionParameters │ └── main.cpp ├── 16.15SizedArraysByReference │ └── main.cpp ├── 16.16MultiDimensionalArrayFunctionParameters │ └── main.cpp ├── 16.17DefaultFunctionParameters │ └── main.cpp ├── 16.18ImplicitConversions │ └── main.cpp ├── 16.19ImplicitConversionsWithReferences │ └── main.cpp ├── 16.20ImplicitConversionsWithPointers │ └── main.cpp ├── 16.21String_viewParameters │ └── main.cpp ├── 16.22ImplicitConversionsFromStringViewToString │ └── main.cpp ├── 16.23constexprFunctions │ └── main.cpp ├── 16.24constevalFunctions │ └── main.cpp ├── 16.2FirstHandOnCppFunctions │ └── main.cpp ├── 16.3FunctionDeclarationsAndDefinitions │ └── main.cpp ├── 16.4MultipleFiles_CompilationModelRevisited │ ├── compare.cpp │ ├── compare.h │ ├── main.cpp │ ├── operations.cpp │ ├── operations.h │ └── some_other_file.cpp ├── 16.5PassByValue │ └── main.cpp ├── 16.6PassByConstValue │ └── main.cpp ├── 16.7PassByPointer │ └── main.cpp ├── 16.8PassByPointerToConst │ └── main.cpp ├── 16.9PassByConstPointerToConst │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 17.EnumsAndTypeAliases ├── 17.2EnumClasses │ └── main.cpp ├── 17.3UsingEnum │ └── main.cpp ├── 17.4OldEnums │ └── main.cpp ├── 17.5TypeAliases │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 18.ArgumentsToTheMainFunction ├── 18.2GrabAndUseTheArguments │ └── main.cpp ├── 18.3CalculatorV1 │ └── main.cpp ├── 18.4CalculatorV2 │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 19.GettingThingsOutOfFuntions ├── 19.10FunctionDefinitionsWithReturnTypeDeduction │ └── main.cpp ├── 19.11OptionalOutputFromFunctions │ └── main.cpp ├── 19.12IntroducingStdOptional │ └── main.cpp ├── 19.13OptionalOutputWithStdOptional │ └── main.cpp ├── 19.2InputAndOutputParameters │ └── main.cpp ├── 19.3ReturningFromFunctionsByValue │ └── main.cpp ├── 19.4ReturningByReference │ └── main.cpp ├── 19.5ReturningByPointer │ └── main.cpp ├── 19.6ReturningArrayElementIndexByPointer │ └── main.cpp ├── 19.7BareAutoTypeDeduction │ └── main.cpp ├── 19.8FunctionReturnTypeDeduction │ └── main.cpp ├── 19.9ReturnTypeDeductionWithReferences │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 20.FunctionOverloading ├── 20.2OverloadingWithDifferentParameters │ └── main.cpp ├── 20.3OverloadingWithPointerParameters │ └── main.cpp ├── 20.4OverloadingWithReferenceParameters │ └── main.cpp ├── 20.5OverloadingWithConstParametersByValue │ └── main.cpp ├── 20.6OverloadingWithConstPointerAndPointerToConstParameters │ └── main.cpp ├── 20.7OverloadingWithConstReferences │ └── main.cpp ├── 20.8OverloadsWithDefaultParameters │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 21.LambdaFunctions ├── 21.2DeclaringAndUsingLambdas │ └── main.cpp ├── 21.3CaptureLists │ └── main.cpp ├── 21.4CaptureAllLists │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 22.FunctionsTheMisfits ├── 22.2StaticVariables │ └── main.cpp ├── 22.3InlineFunctions │ └── main.cpp ├── 22.4RecursiveFunctions │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 23.FunctionCallStackD_ebugging ├── 23.4D_ebuggingInV_S_C0de │ └── main.cpp ├── 23.7D_ebuggingArraysLoopsAndPointers │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 24.FunctionTemplates ├── 24.10DecltypeAuto │ └── main.cpp ├── 24.11DefaultArguments │ └── main.cpp ├── 24.12NonTypeTemplateParameters │ └── main.cpp ├── 24.13AutoFunctionTemplates │ └── main.cpp ├── 24.14NamedTemplateParametersForLambdas │ └── main.cpp ├── 24.15TypeTraits │ └── main.cpp ├── 24.16ConstexprIf │ └── main.cpp ├── 24.2TryingOutFunctionTemplates │ └── main.cpp ├── 24.3TemplateTypeDeductionAndExplicitArguments │ └── main.cpp ├── 24.4TemplateTypeParametersByReference │ └── main.cpp ├── 24.5TemplateSpecialization │ └── main.cpp ├── 24.6FunctionTemplatesWithOverloading │ └── main.cpp ├── 24.7FunctionTemplatesWithMultipleParameters │ └── main.cpp ├── 24.8TemplateReturnTypeDeductionWithAuto │ └── main.cpp ├── 24.9DecltypeAndTrailingReturnTypes │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 25.Concepts ├── 25.02UsingConcepts │ └── main.cpp ├── 25.03BuildingYourOwnConcepts │ └── main.cpp ├── 25.04ZoomingInOnRequiresClause │ └── main.cpp ├── 25.05CombiningConcepts │ └── main.cpp ├── 25.06ConceptsAndAuto │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 26.Classes ├── 26.10OrderOfConstructorDestructorCalls │ └── main.cpp ├── 26.11ThisPointer │ └── main.cpp ├── 26.12Struct │ └── main.cpp ├── 26.13.SizeOfClassObjects │ └── main.cpp ├── 26.2YourFirstClass │ └── main.cpp ├── 26.3Constructors │ └── main.cpp ├── 26.4DefaultedConstructors │ └── main.cpp ├── 26.5SettersAndGetters │ └── main.cpp ├── 26.6ClassAcrossMultipleFiles │ ├── constants.h │ ├── cylinder.cpp │ ├── cylinder.h │ └── main.cpp ├── 26.8ManagingClassObjectsThroughPointers │ ├── constants.h │ ├── cylinder.cpp │ ├── cylinder.h │ └── main.cpp ├── 26.9Destructors │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 27.ZoomingInOnClassObjects ├── 27.2ConstObjects │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 27.3ConstObjectsAsFunctionParameters │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 27.4ConstMemberFunctions │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 27.5GettersThatDoubleAsSetters │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 27.6DanglingPointersAndReferences │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 27.8MutableObjects │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 27.9StructuredBindings │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 28.DivingDeepIntoConstructorsAndInitialization ├── 28.10MoveConstructors │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 28.11DeletedConstructors │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 28.12InitializerListConstructors │ └── main.cpp ├── 28.13AggregateInitialization │ └── main.cpp ├── 28.14DesignatedInitializers │ └── main.cpp ├── 28.15UniformInitializationForAggregates │ └── main.cpp ├── 28.2DefaultParametersForConstructors │ ├── cylinder.cpp │ ├── cylinder.h │ └── main.cpp ├── 28.3InitializerListsForConstructors │ ├── cylinder.cpp │ ├── cylinder.h │ └── main.cpp ├── 28.5ExplicitConstructors │ ├── main.cpp │ ├── square.cpp │ └── square.h ├── 28.6ConstructorDelegation │ ├── main.cpp │ ├── square.cpp │ └── square.h ├── 28.7CopyConstructors │ ├── main.cpp │ ├── person.cpp │ └── person.h ├── 28.8ObjectsStoredInArraysAreCopies │ ├── main.cpp │ ├── person.cpp │ └── person.h └── C++ 20 Template Project │ └── main.cpp ├── 29.Friends ├── 29.2FriendFunctions │ └── main.cpp ├── 29.3FriendClasses │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 30.ConstAndStaticMembers ├── 30.10NestedClasses │ ├── main.cpp │ ├── outer.cpp │ └── outer.h ├── 30.11InClassMemberVariableInitialization │ ├── integer.cpp │ ├── integer.h │ └── main.cpp ├── 30.3StaticMemberVariables │ ├── main.cpp │ ├── point.cpp │ ├── point.h │ └── some_other_file.cpp ├── 30.4InlineStaticMemberVariables │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 30.5StaticConstants │ ├── cylinder.cpp │ ├── cylinder.h │ └── main.cpp ├── 30.6StaticConstantsPreCpp17 │ ├── bird.cpp │ ├── bird.h │ ├── cylinder.cpp │ ├── cylinder.h │ └── main.cpp ├── 30.7MemberVariablesOfTypeSelf │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 30.8MemberVariablesOfOtherTypes │ ├── integer.cpp │ ├── integer.h │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 30.9StaticMemberFunctions │ ├── main.cpp │ ├── point.cpp │ └── point.h └── C++ 20 Template Project │ └── main.cpp ├── 31.Namespaces ├── 31.2CreatingNamespaces │ └── main.cpp ├── 31.3NamespacesAcrossMultipleFiles │ ├── cylinder.cpp │ ├── cylinder.h │ ├── line.cpp │ ├── line.h │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 31.4DefaultGlobalNamespace │ └── main.cpp ├── 31.5BuiltInNamespaces │ └── main.cpp ├── 31.6UsingDeclarations │ ├── cylinder.cpp │ ├── cylinder.h │ ├── line.cpp │ ├── line.h │ ├── main.cpp │ ├── operations.cpp │ ├── operations.h │ ├── point.cpp │ └── point.h ├── 31.7AnonymousNamespaces │ ├── main.cpp │ └── some_other_file.cpp ├── 31.8NestedNamespaces │ └── main.cpp ├── 31.9NamespaceAliases │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 32.ProgramsWithMultipleFiles ├── 32.10InlineVersusStatic │ ├── main.cpp │ ├── utility1.cpp │ └── utility2.cpp ├── 32.11ForwardDeclarations │ ├── dog.cpp │ ├── dog.h │ ├── farm.cpp │ ├── farm.h │ └── main.cpp ├── 32.2CompilingAndLinking_Model │ ├── main.cpp │ ├── main.o │ ├── utilities.cpp │ ├── utilities.h │ └── utilities.o ├── 32.4DeclarationsAndDefinitions │ ├── main.cpp │ ├── person.h │ └── some_other_file.cpp ├── 32.5OneDefinitionRule │ ├── main.cpp │ ├── person.h │ └── some_other_file.cpp ├── 32.6Linkage │ ├── main.cpp │ └── some_other_file.cpp ├── 32.8FlippingLinkage │ ├── main.cpp │ └── some_other_file.cpp ├── 32.9InlineVariablesAndFunctions │ ├── main.cpp │ ├── utility.h │ ├── utility1.cpp │ └── utility2.cpp └── C++ 20 Template Project │ └── main.cpp ├── 33.SmartPointers ├── 33.2UniquePointers │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 33.3UniquePointersAsFunctionParametersAndReturnTypes │ ├── dog.cpp │ ├── dog.h │ ├── main.cpp │ ├── person.cpp │ └── person.h ├── 33.4UniquePointersAndArrays │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 33.5SharedPointers │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 33.6SharedPointersFromUniquePointers │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 33.7SharedPointersWithArrays │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 33.8SharedPointersAsFunctionParametersAndReturnTypes │ ├── dog.cpp │ ├── dog.h │ └── main.cpp ├── 33.9WeakPointers │ ├── dog.cpp │ ├── dog.h │ ├── main.cpp │ ├── person.cpp │ └── person.h └── C++ 20 Template Project │ └── main.cpp ├── 34.OperatorOverloading ├── 34.10CompoundOperators_ReusingOperators │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.11CustomTypeConversions │ ├── main.cpp │ ├── number.cpp │ ├── number.h │ ├── point.cpp │ └── point.h ├── 34.12ImplicitConversionsWithOverloadedBinaryOperators │ ├── main.cpp │ ├── number.cpp │ └── number.h ├── 34.13UnaryPrefixIncrementOperatorAsMember │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.14UnaryPrefixIncrementOperatorAsNonMember │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.15UnaryPostfixIncrementOperator │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.16UnaryPrefixPostfixDecrementOperator │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.17CopyAssignmentOperator │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.18CopyAssignmentOperatorForOtherTypes │ ├── car.cpp │ ├── car.h │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.19TypeConversionsRecap │ ├── main.cpp │ ├── number.cpp │ ├── number.h │ ├── point.cpp │ └── point.h ├── 34.20Functors │ └── main.cpp ├── 34.2AdditionOperatorAsMember │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.3AdditionOperatorAsNonMember │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.4SubscriptOperatorReading │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.5SubscriptOperatorReadingWritting │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.6SubscriptOperatorForCollectionTypes │ ├── main.cpp │ ├── scores.cpp │ └── scores.h ├── 34.7StreamInsertionOperator │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.8StreamExtractionOperator │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 34.9OtherArithmeticOperators │ ├── main.cpp │ ├── point.cpp │ └── point.h └── C++ 20 Template Project │ └── main.cpp ├── 35.LogicalOperatorsAndThreeWayComparison ├── 35.10CustomSpaceshipOperatorForOrdering │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 35.11LogicalOperatorsSimplified │ ├── main.cpp │ ├── number.cpp │ └── number.h ├── 35.12SpaceshipAsNonMember │ ├── main.cpp │ ├── number.cpp │ └── number.h ├── 35.13ZoomingInOnWeakOrderingExample1 │ └── main.cpp ├── 35.14ZoomingInOnWeakOrderingExample2 │ └── main.cpp ├── 35.15ZoomingInOnPartialOrderingExample │ └── main.cpp ├── 35.2AllLogicalOperators │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 35.3Rel_OpsNamespace │ ├── main.cpp │ ├── my_utility.h │ ├── point.cpp │ └── point.h ├── 35.4LogicalOperatorsWithImplicitConversions │ ├── main.cpp │ ├── number.cpp │ └── number.h ├── 35.5ThreeWayComparisonOperator │ └── main.cpp ├── 35.6DefaultedEqualityOperator │ └── main.cpp ├── 35.7CustomEqualityOperator │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 35.8DefaultOrderingWithSpaceship │ └── main.cpp ├── 35.9MembersWithoutSpaceshipOperator │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 36.Inheritance ├── 36.10CopyConstructorsWithInheritance │ ├── civilengineer.cpp │ ├── civilengineer.h │ ├── engineer.cpp │ ├── engineer.h │ ├── main.cpp │ ├── person.cpp │ └── person.h ├── 36.11InheritingBaseConstructors │ ├── engineer.cpp │ ├── engineer.h │ ├── main.cpp │ ├── person.cpp │ └── person.h ├── 36.12InheritanceAndDestructors │ ├── civilengineer.cpp │ ├── civilengineer.h │ ├── engineer.cpp │ ├── engineer.h │ ├── main.cpp │ ├── person.cpp │ └── person.h ├── 36.13ReusedSymbolsInInheritance │ ├── child.cpp │ ├── child.h │ ├── main.cpp │ ├── parent.cpp │ └── parent.h ├── 36.2FirstTryOnInheritance │ ├── main.cpp │ ├── person.cpp │ ├── person.h │ ├── player.cpp │ └── player.h ├── 36.3ProtectedMembers │ ├── main.cpp │ ├── person.cpp │ ├── person.h │ ├── player.cpp │ └── player.h ├── 36.5BaseClassAccessSpecifiersADemo │ ├── engineer.cpp │ ├── engineer.h │ ├── main.cpp │ ├── nurse.cpp │ ├── nurse.h │ ├── person.cpp │ ├── person.h │ ├── player.cpp │ └── player.h ├── 36.6ClosingInOnPrivateInheritance │ ├── civilengineer.cpp │ ├── civilengineer.h │ ├── engineer.cpp │ ├── engineer.h │ ├── main.cpp │ ├── person.cpp │ └── person.h ├── 36.7ResurectingMembersBackInContext │ ├── civilengineer.cpp │ ├── civilengineer.h │ ├── engineer.cpp │ ├── engineer.h │ ├── main.cpp │ ├── person.cpp │ └── person.h ├── 36.8DefaultArgConstructorsWithInheritance │ ├── civilengineer.cpp │ ├── civilengineer.h │ ├── engineer.cpp │ ├── engineer.h │ ├── main.cpp │ ├── person.cpp │ └── person.h ├── 36.9ConstructorsWithInheritance │ ├── civilengineer.cpp │ ├── civilengineer.h │ ├── engineer.cpp │ ├── engineer.h │ ├── main.cpp │ ├── person.cpp │ └── person.h └── C++ 20 Template Project │ └── main.cpp ├── 37.Polymorphism ├── 37.10Final │ ├── animal.cpp │ ├── animal.h │ ├── bird.cpp │ ├── bird.h │ ├── bulldog.cpp │ ├── bulldog.h │ ├── cat.cpp │ ├── cat.h │ ├── crow.cpp │ ├── crow.h │ ├── dog.cpp │ ├── dog.h │ ├── feline.cpp │ ├── feline.h │ ├── main.cpp │ ├── pigeon.cpp │ ├── pigeon.h │ ├── wildcat.cpp │ └── wildcat.h ├── 37.11FinalAndOverrideAreNotKeywords │ └── main.cpp ├── 37.12PolymorphicFunctionsAndAccessSpecifiers │ ├── ellipse.cpp │ ├── ellipse.h │ ├── main.cpp │ ├── shape.cpp │ └── shape.h ├── 37.13NonPolymorphicFunctionsAndAccessSpecifiers │ ├── ellipse.cpp │ ├── ellipse.h │ ├── main.cpp │ ├── shape.cpp │ └── shape.h ├── 37.14VirtualFunctionsWithDefaultArguments │ ├── base.cpp │ ├── base.h │ ├── derived.cpp │ ├── derived.h │ └── main.cpp ├── 37.15VirtualDestructors │ ├── animal.cpp │ ├── animal.h │ ├── dog.cpp │ ├── dog.h │ ├── feline.cpp │ ├── feline.h │ └── main.cpp ├── 37.16DynamicCasts │ ├── animal.cpp │ ├── animal.h │ ├── dog.cpp │ ├── dog.h │ ├── feline.cpp │ ├── feline.h │ └── main.cpp ├── 37.17PolymorphicFunctionsAndDestructors │ └── main.cpp ├── 37.18TypeIdOperator │ ├── main.cpp │ ├── nonpolymorphic.h │ └── polymorphic.h ├── 37.19PureVirtualFunctionsAndAbstractClasses │ ├── circle.cpp │ ├── circle.h │ ├── main.cpp │ ├── rectangle.cpp │ ├── rectangle.h │ ├── shape.cpp │ └── shape.h ├── 37.20AbstractClassesAsInterfaces │ ├── animal.cpp │ ├── animal.h │ ├── bird.cpp │ ├── bird.h │ ├── cat.cpp │ ├── cat.h │ ├── crow.cpp │ ├── crow.h │ ├── dog.cpp │ ├── dog.h │ ├── feline.cpp │ ├── feline.h │ ├── main.cpp │ ├── pigeon.cpp │ ├── pigeon.h │ ├── stream_insertable.cpp │ └── stream_insertable.h ├── 37.2StaticBindingWithInheritance │ ├── circle.cpp │ ├── circle.h │ ├── main.cpp │ ├── oval.cpp │ ├── oval.h │ ├── shape.cpp │ └── shape.h ├── 37.3PolymorphismWithVirtualFunctions │ ├── circle.cpp │ ├── circle.h │ ├── main.cpp │ ├── oval.cpp │ ├── oval.h │ ├── shape.cpp │ └── shape.h ├── 37.4SizeOfPolymorphicObjectsAndSlicing │ ├── circle.cpp │ ├── circle.h │ ├── main.cpp │ ├── oval.cpp │ ├── oval.h │ ├── shape.cpp │ └── shape.h ├── 37.5PolymorphicObjectsStoredInCollections │ ├── circle.cpp │ ├── circle.h │ ├── main.cpp │ ├── oval.cpp │ ├── oval.h │ ├── shape.cpp │ └── shape.h ├── 37.6Override │ ├── circle.cpp │ ├── circle.h │ ├── main.cpp │ ├── oval.cpp │ ├── oval.h │ ├── shape.cpp │ └── shape.h ├── 37.7OverloadingOverridingAndHiding │ ├── circle.cpp │ ├── circle.h │ ├── main.cpp │ ├── oval.cpp │ ├── oval.h │ ├── shape.cpp │ └── shape.h ├── 37.8PolymorphismAtDifferentLevels │ ├── animal.cpp │ ├── animal.h │ ├── bird.cpp │ ├── bird.h │ ├── cat.cpp │ ├── cat.h │ ├── crow.cpp │ ├── crow.h │ ├── dog.cpp │ ├── dog.h │ ├── feline.cpp │ ├── feline.h │ ├── main.cpp │ ├── pigeon.cpp │ └── pigeon.h ├── 37.9InheritanceAndPolymorphismWithStaticMembers │ ├── ellipse.cpp │ ├── ellipse.h │ ├── main.cpp │ ├── shape.cpp │ └── shape.h └── C++ 20 Template Project │ └── main.cpp ├── 38.Exceptions ├── 38.10RethrownExceptions │ ├── exceptions.h │ └── main.cpp ├── 38.11ProgramCustomTermimation │ └── main.cpp ├── 38.12EllipsisCatchAllBlock │ ├── exceptions.h │ └── main.cpp ├── 38.13NoexceptSpecifier │ └── main.cpp ├── 38.14ExceptionsInDestructors │ └── main.cpp ├── 38.16CatchingStandardExceptions │ └── main.cpp ├── 38.17ThrowingStdExceptions │ └── main.cpp ├── 38.18DerivingFromStandardExceptions │ └── main.cpp ├── 38.2TryCatchBlocks │ └── main.cpp ├── 38.3TheNeedForExceptions │ └── main.cpp ├── 38.4HandlingExceptionsAtDifferentLevels │ └── main.cpp ├── 38.5MultipleHandlersForExceptions │ └── main.cpp ├── 38.6NestedTryBlocks │ └── main.cpp ├── 38.7ThrowingClassObjects │ └── main.cpp ├── 38.8ExceptionsAsClassObjectsWithInheritanceHierarchies │ ├── exceptions.h │ └── main.cpp ├── 38.9PolymorphicExceptions │ ├── exceptions.h │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 39.Practice-BoxContainerType ├── 39.2ConstructingDestroying │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ ├── main.cpp │ ├── stream_insertable.cpp │ └── stream_insertable.h ├── 39.3AddingItems │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ ├── main.cpp │ ├── stream_insertable.cpp │ └── stream_insertable.h ├── 39.4RemovingItems │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ ├── main.cpp │ ├── stream_insertable.cpp │ └── stream_insertable.h ├── 39.5OtherOperators │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ ├── main.cpp │ ├── stream_insertable.cpp │ └── stream_insertable.h ├── 39.7StoringInDifferentTypes │ ├── charcontainer.cpp │ ├── charcontainer.h │ ├── doublecontainer.cpp │ ├── doublecontainer.h │ ├── intcontainer.cpp │ ├── intcontainer.h │ ├── main.cpp │ ├── stream_insertable.cpp │ └── stream_insertable.h └── C++ 20 Template Project │ └── main.cpp ├── 40.ClassTemplates ├── 40.10FriendFunctionsForClassTemplates │ └── main.cpp ├── 40.11StreamInsertionOperatorForClassTemplates │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.12ClassTemplatesWithTypeTraitsAndStaticAsserts │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.13ClassTemplatesWithConcepts │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.14BuiltInConcepts │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.15ConceptsExample1 │ └── main.cpp ├── 40.16ConceptsExample2 │ └── main.cpp ├── 40.2YourFirstClassTemplate │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.4NonTypeTemplateParameters │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.5DefaultValuesForTemplateParameters │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.7TemplateSpecializations │ ├── adder.cpp │ ├── adder.h │ ├── main.cpp │ └── some_other_file.cpp ├── 40.8TemplateSpecializationWithSelectMemberFunctions │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 41.MoveSemantics ├── 41.08MoveOnlyTypes │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 41.09PassingByRvalueReference │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 41.2LvaluesAndRvalues │ └── main.cpp ├── 41.3RvalueReferences │ └── main.cpp ├── 41.4MovingTemporariesAround │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 41.5MoveConstructorsMoveAssignmentOperators │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 41.6MovingLvaluesWithStdMove │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 41.7InvalidatePointersAfterStdMove │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 42.FunctionLikeEntities ├── 42.10LambdaFunctionsAsCallbacks │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 42.11CapturingByValueUnderTheHood │ └── main.cpp ├── 42.12CapturingByReferenceUnderTheHood │ └── main.cpp ├── 42.13MixinCapturing │ └── main.cpp ├── 42.14CapturingTheThisPointer │ └── main.cpp ├── 42.15StdFunction │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 42.2FunctionPointers │ └── main.cpp ├── 42.3CallbackFunctions │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 42.4FunctionPointerTypeAliases │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 42.5FunctionPointerTypeAliasesWithTemplates │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 42.6Functors │ ├── decrypt.cpp │ ├── decrypt.h │ ├── encrypt.cpp │ ├── encrypt.h │ └── main.cpp ├── 42.7StandardFunctors │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 42.8FunctorsWithParameters │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 42.9FunctorsAndLambdaFunctions │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 43.StlContainersAndIterators ├── 43.2StdVector │ └── main.cpp ├── 43.3StdArray │ └── main.cpp ├── 43.4Iterators │ └── main.cpp ├── 43.5TraversingContainerSubsetsWithIterators │ └── main.cpp ├── 43.6ReverseIterators │ └── main.cpp ├── 43.7ContantIterators │ └── main.cpp ├── 43.9StdBeginStdEnd │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 44.ZoomingOnSTLContainers ├── 44.10Map │ └── main.cpp ├── 44.11MultisetMultimap │ └── main.cpp ├── 44.12UnorderedSetUnorderedMap │ └── main.cpp ├── 44.14Stack │ └── main.cpp ├── 44.15Queue │ └── main.cpp ├── 44.16PriorityQueue │ └── main.cpp ├── 44.2Deque │ └── main.cpp ├── 44.3ForwardList │ └── main.cpp ├── 44.4List │ └── main.cpp ├── 44.5Vector │ └── main.cpp ├── 44.6Array │ └── main.cpp ├── 44.8Pair │ └── main.cpp ├── 44.9Set │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 45.StlAlgorithms ├── 45.2AllOf │ └── main.cpp ├── 45.3ForEach │ └── main.cpp ├── 45.4.MaxEltMinElt │ └── main.cpp ├── 45.5FindAndFindIf │ └── main.cpp ├── 45.6Copy │ └── main.cpp ├── 45.7Sort │ └── main.cpp ├── 45.8Transform │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 46.RangesLibraryInCpp20 ├── 46.2RangeAlgorithms │ └── main.cpp ├── 46.3RangeLibraryIteratorPairAlgorithms │ └── main.cpp ├── 46.4Projections │ ├── main.cpp │ ├── point.cpp │ └── point.h ├── 46.5ViewsAndRangeAdaptors │ └── main.cpp ├── 46.6ViewCompositionAndPipeOperator │ └── main.cpp ├── 46.7RangeFactories │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 47.BuildingIteratorsForCustomContainers ├── 40.02.IteratorPowers │ └── main.cpp ├── 40.04CustomInputIterator │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.05CustomOutputIterator │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.06CustomForwardIterator │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.07CustomBidirectionalIteator │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.08CustomRandomAccessIterator │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.09CustomIteratorsWithViews │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.10ConstantIterators │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.11RawPointersAsIterators │ ├── boxcontainer.cpp │ ├── boxcontainer.h │ └── main.cpp ├── 40.12WrappingIteratorsFromOtherContainers │ └── main.cpp └── C++ 20 Template Project │ └── main.cpp ├── 48.Coroutines ├── 47.5CoAwait │ └── main.cpp ├── 47.6CoYield │ └── main.cpp ├── 47.7CoReturn │ └── main.cpp ├── 47.8CustomGenerator │ └── main.cpp ├── 47.9ThirdPartyCorotineTypes │ ├── main.cpp │ └── unique_generator.h └── C++ 20 Template Project │ └── main.cpp ├── 49.Modules ├── 48.10ModuleInterfacePartitions │ ├── README │ ├── add_partition.ixx │ ├── main.cpp │ ├── math.ixx │ └── mult_partition.ixx ├── 48.11BoxContainerModule │ ├── README │ ├── boxcontainer.ixx │ └── main.cpp ├── 48.12ModulesWithNamespaces │ ├── README │ ├── main.cpp │ └── math.ixx ├── 48.13VisibilityReachability │ ├── README │ ├── main.cpp │ └── math.ixx ├── 48.14PrivateModuleFragment │ ├── README │ ├── main.cpp │ └── math.ixx ├── 48.15DosAndDonts │ ├── README │ ├── main.cpp │ └── math.ixx ├── 48.1Introduction │ ├── README │ ├── main.cpp │ └── math.ixx ├── 48.2YourFirstModule │ ├── README │ ├── main.cpp │ └── math.ixx ├── 48.3BlockExport │ ├── README │ ├── main.cpp │ └── math.ixx ├── 48.4SeparateInterfaceFromImplementation-SameFile │ ├── README │ ├── main.cpp │ └── math.ixx ├── 48.5SeparateInterfaceFromImplementation-DifferentFiles │ ├── README │ ├── main.cpp │ ├── math.ixx │ └── math_impl.cpp ├── 48.6MultipleImplementationFiles │ ├── README │ ├── main.cpp │ ├── math.ixx │ ├── math_impl.cpp │ └── print_impl.cpp ├── 48.7MultipleInterfaceFiles │ ├── README │ ├── main.cpp │ ├── math.ixx │ ├── math_impl.cpp │ ├── print.ixx │ └── print_impl.cpp ├── 48.8ExportImport │ ├── README │ ├── line.ixx │ ├── main.cpp │ └── point.ixx └── 48.9SubModules │ ├── README │ ├── add_sub.ixx │ ├── main.cpp │ ├── math.ixx │ └── mult_div.ixx └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/* 2 | *vs* 3 | *Debug* 4 | *.exe 5 | *.pdb 6 | *.obj 7 | *.ilk 8 | -------------------------------------------------------------------------------- /02.EnvironmentSetup/1.Windows/4.V_s_CodeMsvcConfiguration/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //. 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /02.EnvironmentSetup/1.Windows/4.V_s_CodeMsvcConfiguration/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "type": "cppbuild", 6 | "label": "Build with MSVC", 7 | "command": "cl.exe", 8 | "args": [ 9 | "/Zi", 10 | "/std:c++latest", 11 | "/EHsc", 12 | "/Fe:", 13 | "${fileDirname}\\rooster.exe", 14 | "${workspaceFolder}\\*.cpp" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$msCompile" 21 | ], 22 | "group": "build", 23 | "detail": "compiler: cl.exe" 24 | } 25 | ] 26 | } -------------------------------------------------------------------------------- /02.EnvironmentSetup/1.Windows/6.V_s_CodeGccConfiguration/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /02.EnvironmentSetup/1.Windows/6.V_s_CodeGccConfiguration/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "type": "cppbuild", 6 | "label": "Build with GCC", 7 | "command": "C:\\mingw64\\bin\\g++.exe", 8 | "args": [ 9 | "-g", 10 | "-std=c++20", 11 | "${workspaceFolder}\\*.cpp", 12 | "-o", 13 | "${fileDirname}\\rooster.exe" 14 | ], 15 | "options": { 16 | "cwd": "${fileDirname}" 17 | }, 18 | "problemMatcher": [ 19 | "$gcc" 20 | ], 21 | "group": "build", 22 | "detail": "compiler: C:\\mingw64\\bin\\g++.exe" 23 | } 24 | ] 25 | } -------------------------------------------------------------------------------- /02.EnvironmentSetup/1.Windows/7.V_s_CodeClangConfiguration/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /02.EnvironmentSetup/1.Windows/7.V_s_CodeClangConfiguration/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "type": "cppbuild", 6 | "label": "Build with Clang", 7 | "command": "C:\\mingw64\\bin\\clang++.exe", 8 | "args": [ 9 | "-g", 10 | "-std=c++20", 11 | "${workspaceFolder}\\*.cpp", 12 | "-o", 13 | "${fileDirname}\\rooster.exe" 14 | ], 15 | "options": { 16 | "cwd": "${fileDirname}" 17 | }, 18 | "problemMatcher": [ 19 | "$gcc" 20 | ], 21 | "group": "build", 22 | "detail": "compiler: C:\\mingw64\\bin\\clang++.exe" 23 | } 24 | ] 25 | } -------------------------------------------------------------------------------- /02.EnvironmentSetup/1.Windows/8.C++20 Template Project_all_compilers/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /02.EnvironmentSetup/2.Linux/4.V_sCodeGccConfiguration/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /02.EnvironmentSetup/2.Linux/4.V_sCodeGccConfiguration/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "type": "cppbuild", 6 | "label": "Build with GCC", 7 | "command": "/usr/bin/g++-10", 8 | "args": [ 9 | "-g", 10 | "-std=c++20", 11 | "${workspaceFolder}/*.cpp", 12 | "-o", 13 | "${fileDirname}/rooster" 14 | ], 15 | "options": { 16 | "cwd": "${fileDirname}" 17 | }, 18 | "problemMatcher": [ 19 | "$gcc" 20 | ], 21 | "group": "build", 22 | "detail": "compiler: /usr/bin/g++-10" 23 | } 24 | ] 25 | } -------------------------------------------------------------------------------- /02.EnvironmentSetup/2.Linux/6.V_sCodeClangConfiguration/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /02.EnvironmentSetup/2.Linux/6.V_sCodeClangConfiguration/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "type": "cppbuild", 6 | "label": "Build with Clang", 7 | "command": "/usr/bin/clang++-12", 8 | "args": [ 9 | "-g", 10 | "-std=c++20", 11 | "${workspaceFolder}/*.cpp", 12 | "-o", 13 | "${fileDirname}/rooster" 14 | ], 15 | "options": { 16 | "cwd": "${fileDirname}" 17 | }, 18 | "problemMatcher": [ 19 | "$gcc" 20 | ], 21 | "group": "build", 22 | "detail": "compiler: /usr/bin/clang++-12" 23 | } 24 | ] 25 | } -------------------------------------------------------------------------------- /02.EnvironmentSetup/2.Linux/7.Cpp20TemplateProject_all_compilers/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /03.FirstSteps/3.2FirstCppProgram/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | std::cout << "Number1" << std::endl; 6 | std::cout << "Number2" << std::endl; 7 | std::cout << "Number3" << std::endl; 8 | 9 | return 0; 10 | } -------------------------------------------------------------------------------- /03.FirstSteps/3.3Comments/main.cpp: -------------------------------------------------------------------------------- 1 | //One line comment 2 | 3 | //This brings in the iostream library 4 | #include 5 | 6 | /* 7 | This is a block comment that englobes multiple 8 | lines of text 9 | */ 10 | 11 | 12 | 13 | 14 | int main(){ 15 | //This is going to print "Hello World" to the console 16 | std::cout << "Hello World!" << std::endl; 17 | return 0; 18 | //Program ends here 19 | } -------------------------------------------------------------------------------- /03.FirstSteps/3.4ErrorsWarnings/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | //Compile time error 6 | std::cout << "Hello Word!" << std::endl; 7 | 8 | //Run time error 9 | int value = 7/0; 10 | std::cout << "value : " << value << std::endl; 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /03.FirstSteps/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /04.VariablesAndDatatypes/4.2NumberSystems/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(){ 5 | 6 | int number1 = 15; // Decimal 7 | int number2 = 017; // Octal 8 | int number3 = 0x0F; // Hexadecimal 9 | int number4 = 0b00001111; // Binary 10 | 11 | std::cout << "number1 : " << number1 << std::endl; 12 | std::cout << "number2 : " << number2 << std::endl; 13 | std::cout << "number3 : " << number3 << std::endl; 14 | std::cout << "number4 : " << number4 << std::endl; 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /04.VariablesAndDatatypes/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /05.OperationsOnData/5.3.PrecedenceAndAssociativity/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(){ 5 | 6 | int a {6}; 7 | int b {3}; 8 | int c {8}; 9 | int d {9}; 10 | int e {3}; 11 | int f {2}; 12 | int g {5}; 13 | 14 | int result = a + b * c -d/e -f + g; // 6 + 24 - 3 - 2 + 5 15 | 16 | std::cout << "result : " << result << std::endl; 17 | 18 | result = a/b*c +d - e + f; // 16 + 9 - 3 + 2 19 | std::cout << "result : " << result << std::endl; 20 | 21 | result = (a + b) * c -d/e -f + g; // 72-3-2+5 22 | std::cout << "result () : " << result << std::endl; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /05.OperationsOnData/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /06.LiteralsAndConstants/6.3Constants/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | int main(){ 6 | 7 | const int age {34}; 8 | const float height {1.67f}; 9 | 10 | //age = 55; // Can't modify 11 | //height = 1.8f; 12 | 13 | 14 | int years { 3 * age}; 15 | 16 | std::cout << "age : " << age << std::endl; 17 | std::cout << "height : " << height << std::endl; 18 | std::cout << "years : " << years << std::endl; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /06.LiteralsAndConstants/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /07.ConversionsOverflowAndUnderflow/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /08.BitwiseOperators/8.2PrintingIntegersInBinary/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(){ 6 | 7 | unsigned short int data {15}; 8 | 9 | std::cout << "data (dec) : " <(data) << std::endl; 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /08.BitwiseOperators/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /09.VariableLifetimeAndScope/9.2VariableScope/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int global_var1{23}; // global variable 4 | 5 | 6 | void some_function(){ 7 | int local_var{10}; 8 | std::cout << "Inside function global_var : " << global_var1 << std::endl; 9 | std::cout << "Inside functoin local_var : " << local_var << std::endl; 10 | } 11 | 12 | void some_other_function(){ 13 | //local_var = 5; 14 | } 15 | 16 | 17 | 18 | int main(){ 19 | std::cout << "Inside main function global_var : " << global_var1 << std::endl; 20 | //local_var = 5; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /09.VariableLifetimeAndScope/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /10.FlowControl/10.6IntegralLogicConditions/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(){ 5 | 6 | int condition {-5}; 7 | 8 | bool bool_condition = condition; 9 | std::cout << std::boolalpha; 10 | 11 | 12 | if(bool_condition){ 13 | std::cout << "We have a " << bool_condition << " in our variable " << std::endl; // different from 0 14 | }else 15 | { 16 | std::cout << "We have " << bool_condition << " in our variable" << std::endl; // zero 17 | } 18 | 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /10.FlowControl/10.7TernaryOperators/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(){ 5 | 6 | int max{}; 7 | 8 | int a{35}; 9 | int b{200}; 10 | 11 | std::cout << std::endl; 12 | std::cout << "using regular if " << std::endl; 13 | 14 | /* 15 | if(a > b){ 16 | max = a; 17 | }else{ 18 | max = b; 19 | } 20 | */ 21 | 22 | max = (a > b)? a : b; // Ternary operator 23 | 24 | std::cout << "max : " << max << std::endl; 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /10.FlowControl/10.8IfConstexpr/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(){ 5 | 6 | constexpr bool condition {false}; 7 | 8 | if constexpr (condition){ 9 | std::cout << "Condition is true" << std::endl; 10 | }else{ 11 | std::cout << "Condition is false" << std::endl; 12 | } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /10.FlowControl/10.9IfWithInitializer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | int main(){ 6 | 7 | 8 | bool go {true}; 9 | 10 | if(int speed {10};go){ 11 | std::cout << "speed : " << speed << std::endl; 12 | 13 | if(speed > 5){ 14 | std::cout << "Slow down!" << std::endl; 15 | }else{ 16 | std::cout << "All good!" << std::endl; 17 | } 18 | }else{ 19 | std::cout << "speed : " << speed << std::endl; 20 | std::cout << "Stop" << std::endl; 21 | } 22 | 23 | //std::cout << "Out of the if block , speed : " << speed << std::endl; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /10.FlowControl/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /11.Loops/11.15ForLoopWithInitCondition/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | 6 | for(double multiplier{4}; auto i : {1,2,3,4,5,6,7,8,9,10}){ 7 | std::cout << "result : " << ( i * multiplier) << std::endl; 8 | } 9 | 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /11.Loops/11.3ForLoopMultipleDeclarations/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | for (size_t i{0} , x {5}, y{22} ; y > 15 ; ++i , x+=5 , y-=1){ 6 | std::cout << "i: " << i << ", x : " << x << ", y : " << y << std::endl; 7 | 8 | } 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /11.Loops/11.7HugeLoopsWithOutput/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | /* 6 | for(size_t i{0} ; i < 100000 ; ++i){ 7 | //std::cout << "i : " << i << std::endl; 8 | } 9 | std::cout << "Done!" << std::endl; 10 | */ 11 | 12 | size_t i{0}; // Iterator 13 | 14 | while(i < 100000){ 15 | //std::cout << "i : " << i << std::endl; 16 | ++i; 17 | } 18 | std::cout << "Done!" << std::endl; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /11.Loops/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /12.Arrays/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /13.Pointers/13.3PointerToChar/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(){ 5 | 6 | const char * message {"Hello World!"}; 7 | std::cout << "message : " << message << std::endl; 8 | 9 | //*message = "B"; // Compiler error 10 | std::cout << "*message : " << *message << std::endl; 11 | 12 | //Allow users to modify the string 13 | char message1[] {"Hello World!"}; 14 | message1[0] = 'B'; 15 | std::cout << "message1 : " << message1 << std::endl; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /13.Pointers/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /14.References/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /15.CharacterManipulationAndStrings/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /16.Functions/16.10PassByReference/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void say_age(int& age); // Declaration 4 | 5 | int main(){ 6 | 7 | int age{23}; // Local 8 | std::cout << "age (before call) : " << age << "&age : " << &age << std::endl; //23 9 | say_age(age); // Argument 10 | std::cout << "age (after call) : " << age << "&age : " << &age << std::endl; //24 11 | 12 | return 0; 13 | } 14 | 15 | 16 | void say_age(int& age){ // Parameter 17 | ++age; 18 | std::cout << "Hello , you are " << age << " years old! &age : " << &age << std::endl;//24 19 | 20 | } -------------------------------------------------------------------------------- /16.Functions/16.11PassByConstReference/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void say_age(const int& age); // Declaration 4 | 5 | int main(){ 6 | 7 | int age{23}; // Local 8 | std::cout << "age (before call) : " << age << "&age : " << &age << std::endl; //23 9 | say_age(age); // Argument 10 | std::cout << "age (after call) : " << age << "&age : " << &age << std::endl; //24 11 | 12 | return 0; 13 | } 14 | 15 | 16 | void say_age(const int& age){ // Parameter 17 | //++age; 18 | std::cout << "Hello , you are " << age << " years old! &age : " << &age << std::endl;//24 19 | 20 | } -------------------------------------------------------------------------------- /16.Functions/16.14SizedArrayFunctionParameters/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | double sum ( double scores[], size_t count){ 5 | double sum{}; 6 | 7 | for(size_t i{} ; i < count ; ++i){ 8 | sum += scores[i]; 9 | } 10 | return sum; 11 | } 12 | 13 | 14 | int main(){ 15 | 16 | double student_scores[] {10.0,20.0,30.0,4,5,6,7,8,9}; // Less than 5 parameters 17 | 18 | double result = sum(student_scores,std::size(student_scores)); 19 | std::cout << "result : " << result << std::endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /16.Functions/16.15SizedArraysByReference/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Declaration 4 | double sum( const double (&scores) [10]); 5 | 6 | 7 | int main(){ 8 | 9 | double student_scores[] {10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,1}; 10 | 11 | double sum_result = sum(student_scores); 12 | 13 | std::cout << "result is : " << sum_result << std::endl; 14 | 15 | return 0; 16 | } 17 | 18 | //Definition 19 | double sum( const double (&scores) [10]){ 20 | double sum{}; 21 | for(size_t i{}; i < std::size(scores) ; ++i){ 22 | sum += scores[i]; 23 | } 24 | return sum; 25 | } -------------------------------------------------------------------------------- /16.Functions/16.18ImplicitConversions/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | void print_sum(int a, int b){ 5 | 6 | int sum = a + b; 7 | std::cout << "sizeof(a) : " << sizeof(a) << std::endl; 8 | std::cout << "sizeof(b) : " << sizeof(b) << std::endl; 9 | std::cout << "sizeof(int) : " << sizeof(int) << std::endl; 10 | std::cout << "sizeof(double) : " << sizeof(double) << std::endl; 11 | std::cout << "sum : " << sum << std::endl; 12 | std::cout << std::endl; 13 | } 14 | 15 | 16 | int main(){ 17 | 18 | double a{4.5}; 19 | double b{7.8}; 20 | 21 | print_sum(a,b); 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /16.Functions/16.20ImplicitConversionsWithPointers/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void print_sum(int* param1, int* param2){ 4 | std::cout << "sum : " << (*param1 + *param2) << std::endl; 5 | } 6 | 7 | int main(){ 8 | 9 | int a{3}; 10 | int b{12}; 11 | 12 | print_sum(&a,&b); 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /16.Functions/16.21String_viewParameters/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void say_my_name(std::string_view name){ 5 | std::cout << "Hello your name is : " << name << std::endl; 6 | } 7 | 8 | int main(){ 9 | 10 | std::string some_name{"John"}; 11 | //say_my_name("John"); 12 | //say_my_name(some_name); 13 | say_my_name(std::string_view("Samuel")); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /16.Functions/16.24constevalFunctions/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Evaluate this function at compile time. If you can't 4 | //do that throw a compiler error 5 | consteval int get_value(int multiplier){ 6 | return 3 * multiplier; 7 | } 8 | 9 | int main(){ 10 | 11 | int result = get_value(4); 12 | 13 | int some_var{5}; 14 | result = get_value(some_var); 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /16.Functions/16.4MultipleFiles_CompilationModelRevisited/compare.cpp: -------------------------------------------------------------------------------- 1 | 2 | int min( int a, int b){ 3 | if(a 2 | #include "compare.h" // Preprocessor 3 | #include "operations.h" 4 | 5 | 6 | int main(){ 7 | 8 | int maximum = max(134,56); 9 | std::cout << "max : " << maximum << std::endl; 10 | 11 | int minimum = min(146,23); 12 | std::cout << "min : " << minimum << std::endl; 13 | 14 | int x{4}; // 5 15 | int y{5}; // 6 16 | 17 | int result = incr_mult(x,y); 18 | std::cout << "result : " << result << std::endl; 19 | 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /16.Functions/16.4MultipleFiles_CompilationModelRevisited/operations.cpp: -------------------------------------------------------------------------------- 1 | 2 | int incr_mult(int a, int b){ 3 | return ((++a) * (++b)); 4 | } -------------------------------------------------------------------------------- /16.Functions/16.4MultipleFiles_CompilationModelRevisited/operations.h: -------------------------------------------------------------------------------- 1 | 2 | int incr_mult(int a, int b); -------------------------------------------------------------------------------- /16.Functions/16.4MultipleFiles_CompilationModelRevisited/some_other_file.cpp: -------------------------------------------------------------------------------- 1 | //Definition 2 | int max( int a, int b){ 3 | if(a>b) 4 | return a; 5 | else 6 | return b; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /16.Functions/16.5PassByValue/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void say_age(int age){ // Parameter 4 | ++age; 5 | std::cout << "Hello , you are " << age << " years old! &age : " << &age << std::endl; 6 | } 7 | 8 | int main(){ 9 | 10 | int age{23}; // Local 11 | std::cout << "age (before call) : " << age << "&age : " << &age << std::endl; 12 | say_age(age); // Argument 13 | std::cout << "age (after call) : " << age << "&age : " << &age << std::endl; 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /16.Functions/16.6PassByConstValue/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void say_age(const int age); // Declaration 4 | 5 | int main(){ 6 | 7 | int age{23}; // Local 8 | std::cout << "age (before call) : " << age << "&age : " << &age << std::endl; 9 | say_age(age); // Argument 10 | std::cout << "age (after call) : " << age << "&age : " << &age << std::endl; 11 | 12 | return 0; 13 | } 14 | 15 | 16 | void say_age(const int age){ // Parameter 17 | ///++age; 18 | std::cout << "Hello , you are " << age << " years old! &age : " << &age << std::endl; 19 | } -------------------------------------------------------------------------------- /16.Functions/16.7PassByPointer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void say_age(int* age); // Declaration 4 | 5 | int main(){ 6 | 7 | int age{23}; // Local 8 | std::cout << "age (before call) : " << age << "&age : " << &age << std::endl; //23 9 | say_age(&age); // Argument 10 | std::cout << "age (after call) : " << age << "&age : " << &age << std::endl; //24 11 | 12 | return 0; 13 | } 14 | 15 | 16 | void say_age(int* age){ // Parameter 17 | ++(*age); 18 | std::cout << "Hello , you are " << *age << " years old! &age : " << &age << std::endl;//24 19 | } -------------------------------------------------------------------------------- /16.Functions/16.8PassByPointerToConst/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void say_age(const int* age); // Declaration 4 | 5 | int main(){ 6 | 7 | int age{23}; // Local 8 | std::cout << "age (before call) : " << age << "&age : " << &age << std::endl; //23 9 | say_age(&age); // Argument 10 | std::cout << "age (after call) : " << age << "&age : " << &age << std::endl; //24 11 | 12 | return 0; 13 | } 14 | 15 | 16 | void say_age(const int* age){ // Parameter 17 | //++(*age); // 18 | std::cout << "Hello , you are " << *age << " years old! &age : " << &age << std::endl;//24 19 | } -------------------------------------------------------------------------------- /16.Functions/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /17.EnumsAndTypeAliases/17.2EnumClasses/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Declare an enum type 4 | //The default type associated with enum classes with C++ is int 5 | //unsigned char : 0 ~ 255 6 | enum class Month : char { 7 | Jan = -1, January = Jan, Feb, Mar, 8 | Apr, May, Jun, 9 | Jul, Aug, Sep, 10 | Oct = 100, Nov, Dec 11 | }; 12 | 13 | 14 | int main(){ 15 | 16 | Month month {Month::Jan}; 17 | std::cout << "month : " << static_cast(month) << std::endl; 18 | std::cout << "sizeof(month) : " << sizeof(month) << std::endl; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /17.EnumsAndTypeAliases/17.4OldEnums/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | enum Direction : unsigned char { 4 | TopLeft = 64, TopRight,Center,BottomLeft,BottomRight 5 | }; 6 | 7 | enum Tool { 8 | Pen,Marker, Eraser,Rectangle,Circle,PaintBucket 9 | }; 10 | 11 | 12 | int main(){ 13 | 14 | Direction direction{Direction::TopLeft}; 15 | Tool tool {Tool::Pen}; 16 | 17 | std::cout << "direction : " << static_cast(direction) << std::endl; 18 | std::cout << "tool : " << tool << std::endl; 19 | //std::cout << "(tool > direction) : " << (tool > direction) << std::endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /17.EnumsAndTypeAliases/17.5TypeAliases/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | using HugeInt = unsigned long long int; // Recommended in modern C++ 6 | //typedef unsigned long long int HugeInt; // Older C++ syntax for type aliases 7 | HugeInt huge_number {123378997}; 8 | 9 | std::cout << "sizeof(unsigned long long int ) : " << sizeof(unsigned long long int) << std::endl; 10 | std::cout << "sizeof(HugeInt) : " << sizeof(HugeInt) << std::endl; 11 | 12 | std::cout << "huge_number : " << huge_number << std::endl; 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /17.EnumsAndTypeAliases/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /18.ArgumentsToTheMainFunction/18.2GrabAndUseTheArguments/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(int argc , char * argv[]){ 5 | 6 | std::cout << "We have " << argc << " parameters in our program" << std::endl; 7 | 8 | for(size_t i {0}; i < argc ; ++i){ 9 | std::cout << "parameter [" << i << "] :" << argv[i] << std::endl; 10 | } 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /18.ArgumentsToTheMainFunction/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /19.GettingThingsOutOfFuntions/19.10FunctionDefinitionsWithReturnTypeDeduction/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Definition 4 | auto max(int& a, int& b){ 5 | if(a>b){ 6 | return a; 7 | }else{ 8 | return b; // Will return a copy. 9 | } 10 | } 11 | 12 | int main(){ 13 | 14 | int x{5}; 15 | int y{9}; 16 | 17 | int int_val = max(x,y); 18 | 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /19.GettingThingsOutOfFuntions/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /20.FunctionOverloading/20.5OverloadingWithConstParametersByValue/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | /* 5 | int max(int a, int b){ 6 | return (a > b)? a : b; 7 | } 8 | */ 9 | 10 | 11 | int max( int a, int b); 12 | 13 | int main(){ 14 | 15 | 16 | return 0; 17 | } 18 | 19 | int max( int a, int b){ 20 | ++a; 21 | return (a > b)? a : b; 22 | } -------------------------------------------------------------------------------- /20.FunctionOverloading/20.8OverloadsWithDefaultParameters/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Declarations 4 | void print_age(int age = 33); 5 | 6 | void print_age(long int age = 44); 7 | 8 | int main(){ 9 | 10 | 11 | print_age(); 12 | 13 | 14 | return 0; 15 | } 16 | 17 | //Definitions 18 | void print_age(int age ){ 19 | std::cout << "Your age is( int version) : " << age << std::endl; 20 | } 21 | 22 | void print_age(long int age){ 23 | std::cout << "Your age is (long int version) : " << age << std::endl; 24 | } 25 | -------------------------------------------------------------------------------- /20.FunctionOverloading/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /21.LambdaFunctions/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /22.FunctionsTheMisfits/22.3InlineFunctions/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | inline int max(int a, int b){ 4 | if(a> b){ 5 | return a; 6 | }else{ 7 | return b; 8 | } 9 | } 10 | 11 | int main(){ 12 | 13 | int a{15}; 14 | int b{8}; 15 | 16 | //std::cout << "max : " << max(a,b) << std::endl; 17 | 18 | //What the compiler might do to inline your function call 19 | std::cout << "max : " ; 20 | if(a> b){ 21 | std::cout << a << std::endl; 22 | }else{ 23 | std::cout << b << std::endl; 24 | } 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /22.FunctionsTheMisfits/22.4RecursiveFunctions/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t sum_up_to_zero(size_t value){ 4 | if(value!=0) 5 | return value + sum_up_to_zero(value-1); 6 | return 0; 7 | } 8 | 9 | int main(){ 10 | 11 | std::cout << "result : " << sum_up_to_zero(10) << std::endl; 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /22.FunctionsTheMisfits/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /23.FunctionCallStackD_ebugging/23.4D_ebuggingInV_S_C0de/main.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int& adjust( int& input); 5 | int sum(int x , int y); 6 | 7 | 8 | int main(int argc, char **argv){ 9 | 10 | int a{10}; 11 | int b{12}; 12 | 13 | int summation = sum( a , b); 14 | std::cout << "sum : " 15 | << summation << std::endl; 16 | 17 | return 0; 18 | } 19 | 20 | int& adjust( int& input){ 21 | int adjustment{2}; 22 | input += adjustment; 23 | return input; 24 | } 25 | 26 | int sum(int x , int y){ 27 | 28 | int result = x + y; 29 | adjust(result); 30 | return result; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /23.FunctionCallStackD_ebugging/23.7D_ebuggingArraysLoopsAndPointers/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double sum ( double array[], size_t count){ 4 | 5 | double sum{}; 6 | for(size_t i{} ; i < count ; ++i){ 7 | sum += array[i]; 8 | } 9 | return sum; 10 | 11 | } 12 | 13 | 14 | int main(int argc, char **argv) 15 | { 16 | double numbers[] {10.0,20.0,30.0,40.0,50.0}; // Sum should be 150.0 17 | 18 | double total = sum(numbers,std::size(numbers)); 19 | 20 | std::cout << "sum : " << total << std::endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /23.FunctionCallStackD_ebugging/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /24.FunctionTemplates/24.10DecltypeAuto/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | decltype(auto) maximum(T a, P b){ 5 | return (a > b) ? a : b; 6 | } 7 | 8 | 9 | int main(){ 10 | int x{7}; 11 | double y{45.9}; 12 | 13 | auto result = maximum(x,y); 14 | std::cout << "max : " << result << std::endl; 15 | std::cout << "sizeof(result) : " << sizeof(result) << std::endl; 16 | 17 | return 0; 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /24.FunctionTemplates/24.13AutoFunctionTemplates/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | template 5 | decltype(auto) func_add( T a, P b){ 6 | return a + b; 7 | } 8 | */ 9 | 10 | 11 | auto func_add (auto a , auto b){ 12 | return a + b; 13 | } 14 | 15 | 16 | int main(){ 17 | 18 | int a{7}; 19 | double b{78.2}; 20 | 21 | auto result = func_add(a,b); 22 | std::cout << "result : " << result << std::endl; 23 | std::cout << "sizeof(result) : " << sizeof(result) << std::endl; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /24.FunctionTemplates/24.14NamedTemplateParametersForLambdas/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(){ 5 | 6 | auto func = [] (T a, P b){ 7 | return a + b; 8 | }; 9 | 10 | char a {'c'}; 11 | int b{63}; 12 | 13 | auto result = func(a,b); 14 | std::cout << "result : " << result << std::endl; 15 | std::cout << "sizeof(result) : " << sizeof(result) << std::endl; 16 | 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /24.FunctionTemplates/24.2TryingOutFunctionTemplates/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | T maximum(T a , T b){ 6 | return (a > b)? a : b; 7 | } 8 | 9 | 10 | int main(){ 11 | 12 | int x{5}; 13 | int y{7}; 14 | 15 | int* p_x {&x}; 16 | int* p_y{&y}; 17 | 18 | auto result = maximum(p_x,p_y); 19 | std::cout << "result : " << *result << std::endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /24.FunctionTemplates/24.3TemplateTypeDeductionAndExplicitArguments/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template T maximum( T a, T b){ 5 | return (a > b) ? a : b; 6 | } 7 | 8 | int main(){ 9 | 10 | int a{10}; 11 | int b{23}; 12 | double c{34.7}; 13 | double d{23.4}; 14 | std::string e{"hello"}; 15 | std::string f{"world"}; 16 | 17 | //Explicit template arguments 18 | 19 | auto max = maximum(a,f); 20 | std::cout << "max : " << max << std::endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /24.FunctionTemplates/24.8TemplateReturnTypeDeductionWithAuto/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | auto maximum ( T a, P b){ 5 | return (a > b)? a : b; 6 | } 7 | 8 | int main(){ 9 | 10 | //Largest type is going to be deduced as return type 11 | 12 | auto max1 = maximum ('e', 33); // double return type deduced 13 | std::cout << "max1 : " << max1 << std::endl; 14 | std::cout << "size of max1 : " << sizeof(max1) << std::endl; 15 | 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /24.FunctionTemplates/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /25.Concepts/25.06ConceptsAndAuto/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //This syntax constrains the auto parameters you pass in 5 | //to comply with the std::integral concept 6 | std::integral auto add (std::integral auto a,std::integral auto b){ 7 | return a + b; 8 | } 9 | 10 | 11 | int main(){ 12 | 13 | std::floating_point auto x = add(5,8); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /25.Concepts/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /26.Classes/26.6ClassAcrossMultipleFiles/constants.h: -------------------------------------------------------------------------------- 1 | #ifndef CONSTANTS_H 2 | #define CONSTANTS_H 3 | 4 | const double PI {3.1415926535897932384626433832795}; 5 | 6 | #endif -------------------------------------------------------------------------------- /26.Classes/26.6ClassAcrossMultipleFiles/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "cylinder.h" 3 | 4 | int main(){ 5 | Cylinder cylinder1(10,10); 6 | std::cout << "volume : " << cylinder1.volume() << std::endl; 7 | 8 | return 0; 9 | } -------------------------------------------------------------------------------- /26.Classes/26.8ManagingClassObjectsThroughPointers/constants.h: -------------------------------------------------------------------------------- 1 | #ifndef CONSTANTS_H 2 | #define CONSTANTS_H 3 | 4 | const double PI {3.1415926535897932384626433832795}; 5 | 6 | #endif -------------------------------------------------------------------------------- /26.Classes/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /27.ZoomingInOnClassObjects/27.5GettersThatDoubleAsSetters/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | int main(){ 5 | 6 | const Dog dog1("Fluffy","Shepherd",2); 7 | dog1.print_info(); 8 | std::cout << "dog name : " << dog1.name() << std::endl; // Getter 9 | 10 | //Modify the object 11 | //dog1.name() = "Milou"; // Setter 12 | dog1.print_info(); 13 | 14 | 15 | 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /27.ZoomingInOnClassObjects/27.6DanglingPointersAndReferences/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | int main(){ 5 | 6 | Dog dog1("Fluffy","Shepherd",2); 7 | 8 | /* 9 | const std::string& str_ref = dog1.compile_dog_info(); 10 | std::cout << " info : " << str_ref << std::endl; 11 | */ 12 | 13 | unsigned int* int_ptr = dog1.jumps_per_minute(); 14 | std::cout << "jumps_per_minute : " << *int_ptr << std::endl; 15 | 16 | std::cout << "Done!" << std::endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /27.ZoomingInOnClassObjects/27.8MutableObjects/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | int main(){ 5 | 6 | Dog dog1("Fluffy","Shepherd",2); 7 | dog1.print_info(); //1 8 | dog1.print_info();//2 9 | 10 | for(size_t i{0}; i <10;++i){ 11 | dog1.print_info(); 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /27.ZoomingInOnClassObjects/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /28.DivingDeepIntoConstructorsAndInitialization/28.10MoveConstructors/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | 5 | int main(){ 6 | //Point p1(10.4,15.6); 7 | //p1.print_info(); 8 | 9 | //Create a copy from a temporary 10 | Point p3(std::move(Point(20.5,5.8))); 11 | p3.print_info(); 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /28.DivingDeepIntoConstructorsAndInitialization/28.11DeletedConstructors/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | Point p1(std::move(Point(4.6,5.2))); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /28.DivingDeepIntoConstructorsAndInitialization/28.13AggregateInitialization/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Point 4 | { 5 | double x; 6 | double y; 7 | }; 8 | 9 | void print_point(const Point& p){ 10 | std::cout << "Point [ x : " << p.x << " , y : " << p.y << "]" << std::endl; 11 | } 12 | 13 | 14 | int main(){ 15 | 16 | Point p1{10,20}; 17 | print_point(p1); 18 | 19 | int scores[] {44,62,67,82,98,43,2,5,67}; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /28.DivingDeepIntoConstructorsAndInitialization/28.2DefaultParametersForConstructors/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "cylinder.h" 3 | 4 | 5 | int main(){ 6 | 7 | Cylinder c1(4); 8 | std::cout << "volume : " << c1.volume() << std::endl; 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /28.DivingDeepIntoConstructorsAndInitialization/28.3InitializerListsForConstructors/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "cylinder.h" 3 | 4 | 5 | int main(){ 6 | Cylinder c1(5); 7 | std::cout << "base_rad : " << c1.get_base_radius() << std::endl; 8 | std::cout << "height : " << c1.get_height() << std::endl; 9 | std::cout << "volume : " << c1.volume() << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /28.DivingDeepIntoConstructorsAndInitialization/28.5ExplicitConstructors/square.cpp: -------------------------------------------------------------------------------- 1 | #include "square.h" 2 | 3 | Square::Square(double side_param , const std::string& color_param) 4 | : m_side{side_param}, 5 | m_color{color_param} 6 | { 7 | } 8 | 9 | double Square::surface() const { 10 | return m_side*m_side; 11 | } 12 | 13 | Square::~Square() 14 | { 15 | } 16 | 17 | -------------------------------------------------------------------------------- /28.DivingDeepIntoConstructorsAndInitialization/28.5ExplicitConstructors/square.h: -------------------------------------------------------------------------------- 1 | #ifndef SQUARE_H 2 | #define SQUARE_H 3 | 4 | #include 5 | 6 | class Square 7 | { 8 | public: 9 | explicit Square(double side_param , const std::string& color_param = "black"); 10 | ~Square(); 11 | double surface() const; 12 | 13 | private : 14 | double m_side; 15 | std::string m_color; 16 | }; 17 | 18 | #endif // SQUARE_H 19 | -------------------------------------------------------------------------------- /28.DivingDeepIntoConstructorsAndInitialization/28.6ConstructorDelegation/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "square.h" 3 | 4 | int main(){ 5 | 6 | Square s1(100.0); 7 | 8 | 9 | return 0; 10 | } -------------------------------------------------------------------------------- /28.DivingDeepIntoConstructorsAndInitialization/28.6ConstructorDelegation/square.h: -------------------------------------------------------------------------------- 1 | #ifndef SQUARE_H 2 | #define SQUARE_H 3 | 4 | #include 5 | 6 | class Square 7 | { 8 | public: 9 | explicit Square(double side_param); 10 | Square( double side_param, const std::string& color_param, int shading_param); 11 | ~Square(); 12 | double surface() const; 13 | 14 | private : 15 | double m_side; 16 | std::string m_color; 17 | int m_shading; 18 | double m_position; 19 | }; 20 | 21 | #endif // SQUARE_H 22 | -------------------------------------------------------------------------------- /28.DivingDeepIntoConstructorsAndInitialization/28.7CopyConstructors/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "person.h" 3 | 4 | 5 | int main(){ 6 | 7 | Person p1("John","Snow",25); 8 | p1.print_info(); 9 | 10 | //Create a person copy 11 | Person p2(p1); 12 | p2.print_info(); 13 | 14 | std::cout << "-------" << std::endl; 15 | 16 | p1.set_age(30); 17 | p1.print_info(); 18 | p2.print_info(); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /28.DivingDeepIntoConstructorsAndInitialization/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /29.Friends/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.10NestedClasses/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "outer.h" 3 | 4 | 5 | int main(){ 6 | 7 | Outer outer1(10,20.1); 8 | outer1.do_something(); 9 | 10 | 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.10NestedClasses/outer.cpp: -------------------------------------------------------------------------------- 1 | #include "outer.h" 2 | Outer::Outer(int int_param, double double_param) 3 | : m_var1(int_param) , m_var2(double_param) 4 | { 5 | } 6 | 7 | //default constructor . Delegates to two param constructor 8 | Outer::Outer() : Outer(0,0.0){ 9 | 10 | } 11 | 12 | Outer::~Outer() 13 | { 14 | } 15 | 16 | Outer::Inner::Inner(double double_param) : inner_var(double_param){ 17 | 18 | } 19 | 20 | -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.11InClassMemberVariableInitialization/integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "integer.h" 3 | 4 | Integer::Integer(int value) : inner_int(value) 5 | { 6 | std::cout << "Constructor for Integer " << inner_int << " called" << std::endl; 7 | } 8 | 9 | Integer::~Integer() 10 | { 11 | } 12 | 13 | -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.11InClassMemberVariableInitialization/integer.h: -------------------------------------------------------------------------------- 1 | #ifndef INTEGER_H 2 | #define INTEGER_H 3 | 4 | class Integer 5 | { 6 | public: 7 | explicit Integer(int value); 8 | Integer(){ 9 | std::cout << "Default constructor for Integer called" << std::endl; 10 | } 11 | ~Integer(); 12 | 13 | int get_value () const{ 14 | return inner_int; 15 | } 16 | 17 | void set_value(int new_val){ 18 | inner_int = new_val; 19 | } 20 | 21 | private : 22 | int inner_int{}; 23 | 24 | }; 25 | 26 | #endif // INTEGER_H 27 | -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.3StaticMemberVariables/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | //Print the point count in our program 7 | //std::cout << "Point count : " << Point::m_point_count << std::endl; //0 8 | 9 | Point p1(10.0,20.1); 10 | 11 | std::cout << "Point count : " << p1.get_point_count() << std::endl; //1 12 | 13 | Point points[] {Point(1,1),Point(),Point(4)}; 14 | 15 | std::cout << "Point count : " << p1.get_point_count() << std::endl; //4 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.3StaticMemberVariables/some_other_file.cpp: -------------------------------------------------------------------------------- 1 | #include "point.h" 2 | 3 | //Initialize static member var 4 | size_t Point::m_point_count {0}; -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.4InlineStaticMemberVariables/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | //Print the point count in our program 7 | //std::cout << "Point count : " << Point::m_point_count << std::endl; //0 8 | 9 | Point p1(10.0,20.1); 10 | 11 | std::cout << "Point count : " << p1.get_point_count() << std::endl; //1 12 | 13 | Point points[] {Point(1,1),Point(),Point(4)}; 14 | 15 | std::cout << "Point count : " << p1.get_point_count() << std::endl; //4 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.5StaticConstants/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "cylinder.h" 3 | 4 | 5 | 6 | int main(){ 7 | Cylinder cylinder1(3.3,10.0); 8 | std::cout << "Volume of cylinder : " << cylinder1.volume() << std::endl; 9 | std::cout << "Cylinder default color : " << Cylinder::default_color << std::endl; 10 | std::cout << "Cylinder default color : " << cylinder1.default_color << std::endl; 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.6StaticConstantsPreCpp17/bird.cpp: -------------------------------------------------------------------------------- 1 | #include "bird.h" 2 | #include "cylinder.h" 3 | 4 | const float Bird::BIRD_WEIGHT_CONSTANT {Cylinder::FLOAT_CONSTANT}; -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.6StaticConstantsPreCpp17/bird.h: -------------------------------------------------------------------------------- 1 | #ifndef BIRD_H 2 | #define BIRD_H 3 | 4 | class Bird 5 | { 6 | public: 7 | static const float BIRD_WEIGHT_CONSTANT; 8 | }; 9 | 10 | #endif // BIRD_H -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.7MemberVariablesOfTypeSelf/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | Point p1(5,5); // The most vexing parse 7 | p1.initialize_pointer(10,10); 8 | 9 | p1.m_point4.print_info(); 10 | 11 | 12 | 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.8MemberVariablesOfOtherTypes/integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "integer.h" 3 | 4 | Integer::Integer(int value) : inner_int(value) 5 | { 6 | std::cout << "Constructor for Integer " << inner_int << " called" << std::endl; 7 | } 8 | 9 | Integer::~Integer() 10 | { 11 | } 12 | 13 | -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.8MemberVariablesOfOtherTypes/integer.h: -------------------------------------------------------------------------------- 1 | #ifndef INTEGER_H 2 | #define INTEGER_H 3 | 4 | class Integer 5 | { 6 | public: 7 | explicit Integer(int value); 8 | Integer() = default; 9 | ~Integer(); 10 | 11 | int get_value () const{ 12 | return inner_int; 13 | } 14 | 15 | void set_value(int new_val){ 16 | inner_int = new_val; 17 | } 18 | 19 | private : 20 | int inner_int{0}; 21 | }; 22 | 23 | #endif // INTEGER_H 24 | -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/30.9StaticMemberFunctions/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | 5 | int main(){ 6 | 7 | // std::cout << "Point count : " << Point::get_point_count() << std::endl; 8 | 9 | Point p1{6,7}; 10 | 11 | // std::cout << "Point count : " << p1.get_point_count() << std::endl; 12 | p1.print_info(p1) ; 13 | 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /30.ConstAndStaticMembers/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /31.Namespaces/31.3NamespacesAcrossMultipleFiles/cylinder.cpp: -------------------------------------------------------------------------------- 1 | #include "cylinder.h" 2 | 3 | namespace Geom{ 4 | 5 | Cylinder::Cylinder(double base_rad, double height) 6 | : m_base_rad{base_rad} , m_height{height} 7 | { 8 | } 9 | 10 | } 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /31.Namespaces/31.3NamespacesAcrossMultipleFiles/cylinder.h: -------------------------------------------------------------------------------- 1 | #ifndef CYLINDER_H 2 | #define CYLINDER_H 3 | 4 | namespace Geom{ 5 | 6 | class Cylinder 7 | { 8 | public: 9 | Cylinder(double base_rad, double height); 10 | 11 | double volume() const{ 12 | return PI * m_base_rad * m_base_rad * m_height; 13 | } 14 | 15 | private : 16 | inline static const double PI {3.1415926535897932384626433832795}; 17 | double m_base_rad{1}; 18 | double m_height{1}; 19 | }; 20 | 21 | } 22 | 23 | #endif // CYLINDER_H 24 | 25 | 26 | -------------------------------------------------------------------------------- /31.Namespaces/31.3NamespacesAcrossMultipleFiles/line.cpp: -------------------------------------------------------------------------------- 1 | #include "line.h" 2 | 3 | namespace Geom{ 4 | 5 | Line::Line(const Point& start, const Point& end) 6 | : m_start{start}, m_end{end} 7 | { 8 | } 9 | } 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /31.Namespaces/31.3NamespacesAcrossMultipleFiles/line.h: -------------------------------------------------------------------------------- 1 | #ifndef LINE_H 2 | #define LINE_H 3 | 4 | #include "point.h" 5 | 6 | namespace Geom{ 7 | 8 | class Line 9 | { 10 | public: 11 | Line(const Point& start, const Point& end); 12 | 13 | void print_info()const{ 14 | std::cout << "Line from " << std::endl; 15 | m_start.print_info(); 16 | std::cout << "to : " << std::endl; 17 | m_end.print_info(); 18 | } 19 | private : 20 | Point m_start; 21 | Point m_end; 22 | }; 23 | 24 | } 25 | 26 | 27 | 28 | #endif // LINE_H 29 | -------------------------------------------------------------------------------- /31.Namespaces/31.3NamespacesAcrossMultipleFiles/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | #include "line.h" 4 | #include "cylinder.h" 5 | 6 | 7 | int main(){ 8 | 9 | Geom::Point p1(10,20); 10 | Geom::Point p2(3.4,6.1); 11 | p1.print_info(); 12 | p2.print_info(); 13 | 14 | std::cout << "---" << std::endl; 15 | 16 | Geom::Line l1(p1,p2); 17 | l1.print_info(); 18 | 19 | std::cout << "---" << std::endl; 20 | 21 | Geom::Cylinder c1(1.4,10); 22 | std::cout << "c1.volume : " << c1.volume() << std::endl; 23 | 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /31.Namespaces/31.3NamespacesAcrossMultipleFiles/point.cpp: -------------------------------------------------------------------------------- 1 | #include "point.h" 2 | 3 | namespace Geom{ 4 | 5 | Point::Point(double x, double y) : m_x{x}, m_y{y}{ 6 | 7 | } 8 | 9 | Point::Point() : Point(0.0,0.0) 10 | { 11 | } 12 | 13 | Point::~Point() 14 | { 15 | } 16 | 17 | } 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /31.Namespaces/31.3NamespacesAcrossMultipleFiles/point.h: -------------------------------------------------------------------------------- 1 | #ifndef POINT_H 2 | #define POINT_H 3 | 4 | #include 5 | 6 | namespace Geom{ 7 | 8 | class Point 9 | { 10 | public: 11 | Point(); 12 | Point( double x, double y); 13 | void print_info()const{ 14 | std::cout << "Point [ x : " << m_x << ", y : " << m_y << "]" << std::endl; 15 | } 16 | ~Point(); 17 | private : 18 | double m_x; 19 | double m_y; 20 | }; 21 | 22 | } 23 | 24 | 25 | 26 | #endif // POINT_H 27 | -------------------------------------------------------------------------------- /31.Namespaces/31.4DefaultGlobalNamespace/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Global namespace 4 | double add(double a, double b){ 5 | return a + b; 6 | } 7 | 8 | namespace My_Thing{ 9 | double add(double a, double b){ 10 | return a + b - 1; 11 | } 12 | 13 | void do_something(){ 14 | double result = ::add(5,6); 15 | std::cout << "result : " << result << std::endl; 16 | } 17 | 18 | } 19 | 20 | 21 | int main(){ 22 | My_Thing::do_something(); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /31.Namespaces/31.5BuiltInNamespaces/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | 6 | std::cout << "Hello World" << std::endl; 7 | std::string message{"Hello World"}; 8 | std::cout << message << std::endl; 9 | return 0; 10 | } -------------------------------------------------------------------------------- /31.Namespaces/31.6UsingDeclarations/cylinder.cpp: -------------------------------------------------------------------------------- 1 | #include "cylinder.h" 2 | 3 | namespace Geom{ 4 | 5 | Cylinder::Cylinder(double base_rad, double height) 6 | : m_base_rad{base_rad} , m_height{height} 7 | { 8 | } 9 | 10 | } 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /31.Namespaces/31.6UsingDeclarations/cylinder.h: -------------------------------------------------------------------------------- 1 | #ifndef CYLINDER_H 2 | #define CYLINDER_H 3 | 4 | namespace Geom{ 5 | 6 | class Cylinder 7 | { 8 | public: 9 | Cylinder(double base_rad, double height); 10 | 11 | double volume() const{ 12 | return PI * m_base_rad * m_base_rad * m_height; 13 | } 14 | 15 | private : 16 | inline static const double PI {3.1415926535897932384626433832795}; 17 | double m_base_rad{1}; 18 | double m_height{1}; 19 | }; 20 | 21 | } 22 | 23 | #endif // CYLINDER_H 24 | 25 | 26 | -------------------------------------------------------------------------------- /31.Namespaces/31.6UsingDeclarations/line.cpp: -------------------------------------------------------------------------------- 1 | #include "line.h" 2 | 3 | namespace Geom{ 4 | 5 | Line::Line(const Point& start, const Point& end) 6 | : m_start{start}, m_end{end} 7 | { 8 | } 9 | } 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /31.Namespaces/31.6UsingDeclarations/line.h: -------------------------------------------------------------------------------- 1 | #ifndef LINE_H 2 | #define LINE_H 3 | 4 | #include "point.h" 5 | 6 | namespace Geom{ 7 | 8 | class Line 9 | { 10 | public: 11 | Line(const Point& start, const Point& end); 12 | 13 | void print_info()const{ 14 | std::cout << "Line from " << std::endl; 15 | m_start.print_info(); 16 | std::cout << "to : " << std::endl; 17 | m_end.print_info(); 18 | } 19 | private : 20 | Point m_start; 21 | Point m_end; 22 | }; 23 | 24 | } 25 | 26 | 27 | 28 | #endif // LINE_H 29 | -------------------------------------------------------------------------------- /31.Namespaces/31.6UsingDeclarations/operations.h: -------------------------------------------------------------------------------- 1 | #ifndef OPERATIONS_H 2 | #define OPERATIONS_H 3 | 4 | namespace Math{ 5 | double add(double a, double b); 6 | double sub(double a, double b); 7 | double mult(double a, double b); 8 | double div(double a, double b); 9 | 10 | } 11 | 12 | namespace Math_Weighted{ 13 | double add(double a, double b); 14 | double sub(double a, double b); 15 | double mult(double a, double b); 16 | double div(double a, double b); 17 | } 18 | 19 | #endif // OPERATIONS_H -------------------------------------------------------------------------------- /31.Namespaces/31.6UsingDeclarations/point.cpp: -------------------------------------------------------------------------------- 1 | #include "point.h" 2 | 3 | namespace Geom{ 4 | 5 | Point::Point(double x, double y) : m_x{x}, m_y{y}{ 6 | 7 | } 8 | 9 | Point::Point() : Point(0.0,0.0) 10 | { 11 | } 12 | 13 | Point::~Point() 14 | { 15 | } 16 | 17 | } 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /31.Namespaces/31.6UsingDeclarations/point.h: -------------------------------------------------------------------------------- 1 | #ifndef POINT_H 2 | #define POINT_H 3 | 4 | #include 5 | 6 | namespace Geom{ 7 | 8 | class Point 9 | { 10 | public: 11 | Point(); 12 | Point( double x, double y); 13 | void print_info()const{ 14 | std::cout << "Point [ x : " << m_x << ", y : " << m_y << "]" << std::endl; 15 | } 16 | ~Point(); 17 | private : 18 | double m_x; 19 | double m_y; 20 | }; 21 | 22 | } 23 | 24 | 25 | 26 | #endif // POINT_H 27 | -------------------------------------------------------------------------------- /31.Namespaces/31.7AnonymousNamespaces/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace{ 4 | double add(double a, double b); // Declaration 5 | } 6 | 7 | 8 | int main(){ 9 | 10 | double result = add(10.2,20.2); 11 | std::cout << "result : " << result << std::endl; 12 | 13 | return 0; 14 | } 15 | 16 | 17 | namespace{ 18 | double add(double a, double b){ 19 | return a + b; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /31.Namespaces/31.7AnonymousNamespaces/some_other_file.cpp: -------------------------------------------------------------------------------- 1 | namespace{ 2 | double add(double a, double b){ //Only usable in this translation unit 3 | return a + b; 4 | } 5 | } -------------------------------------------------------------------------------- /31.Namespaces/31.9NamespaceAliases/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace Level1{ 4 | namespace Level2{ 5 | namespace Level3{ 6 | const double weight{33.33}; 7 | } 8 | } 9 | } 10 | 11 | 12 | int main(){ 13 | 14 | //Namespace aliases 15 | namespace Data = Level1::Level2::Level3; 16 | 17 | //std::cout << "weight : " << Level1::Level2::Level3::weight << std::endl; 18 | std::cout << "weight : " << Data::weight << std::endl; 19 | std::cout << "weight : " << Data::weight << std::endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /31.Namespaces/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.10InlineVersusStatic/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | void print_age_1(); 5 | void print_age_2(); 6 | 7 | void print_distance_1(); 8 | void print_distance_2(); 9 | 10 | 11 | 12 | int main(){ 13 | 14 | print_age_1(); 15 | print_age_2(); 16 | 17 | std::cout << "----" << std::endl; 18 | 19 | print_distance_1(); 20 | print_distance_2(); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.11ForwardDeclarations/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | Dog::Dog(const std::string& name) : name(name) 4 | { 5 | } 6 | 7 | Dog::~Dog() 8 | { 9 | } 10 | 11 | -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.11ForwardDeclarations/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | #include 5 | #include 6 | 7 | class Dog 8 | { 9 | public: 10 | Dog() = default; 11 | Dog(const std::string& name); 12 | ~Dog(); 13 | 14 | void print_info() const{ 15 | std::cout << "Dog [ name : " << name << "]" << std::endl; 16 | } 17 | private : 18 | std::string name{}; 19 | }; 20 | 21 | #endif // DOG_H 22 | -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.11ForwardDeclarations/farm.cpp: -------------------------------------------------------------------------------- 1 | #include "farm.h" 2 | #include "dog.h" 3 | 4 | Farm::Farm() 5 | { 6 | } 7 | 8 | Farm::~Farm() 9 | { 10 | } 11 | 12 | 13 | void Farm::use_dog(const Dog& dog_param){ 14 | dog_param.print_info(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.11ForwardDeclarations/farm.h: -------------------------------------------------------------------------------- 1 | #ifndef FARM_H 2 | #define FARM_H 3 | 4 | class Dog; // Forward declaration 5 | //#include "dog.h" 6 | 7 | class Farm 8 | { 9 | public: 10 | Farm(); 11 | ~Farm(); 12 | 13 | void use_dog(const Dog& dog_param); // Doesn't require the definition 14 | 15 | // void do_something(const Dog& dog_param){ 16 | // dog_param.print_info(); 17 | // } 18 | private : 19 | //Dog dog_member; 20 | 21 | }; 22 | 23 | #endif // FARM_H 24 | -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.11ForwardDeclarations/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "dog.h" 4 | #include "farm.h" 5 | 6 | 7 | int main(){ 8 | 9 | Dog dog1("Fluffy"); 10 | 11 | Farm farm1; 12 | farm1.use_dog(dog1); 13 | 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.2CompilingAndLinking_Model/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "utilities.h" 3 | 4 | int main(){ 5 | 6 | double result = add(10.5,20.8); 7 | std::cout << "result : " << result << std::endl; 8 | 9 | return 0; 10 | } -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.2CompilingAndLinking_Model/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rutura/The-C-20-Masterclass-Source-Code/3de559892b0849f9430192e4ad939d3d294d3849/32.ProgramsWithMultipleFiles/32.2CompilingAndLinking_Model/main.o -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.2CompilingAndLinking_Model/utilities.cpp: -------------------------------------------------------------------------------- 1 | //Implementations 2 | /* 3 | double add( double a, double b){ 4 | return a + b; 5 | } 6 | 7 | double multiply(double a, double b){ 8 | return a * b; 9 | } 10 | */ -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.2CompilingAndLinking_Model/utilities.h: -------------------------------------------------------------------------------- 1 | //Declarations 2 | double add( double a, double b); 3 | double multiply(double a, double b); -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.2CompilingAndLinking_Model/utilities.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rutura/The-C-20-Masterclass-Source-Code/3de559892b0849f9430192e4ad939d3d294d3849/32.ProgramsWithMultipleFiles/32.2CompilingAndLinking_Model/utilities.o -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.4DeclarationsAndDefinitions/person.h: -------------------------------------------------------------------------------- 1 | #ifndef PERSON_H 2 | #define PERSON_H 3 | 4 | #include 5 | 6 | //Declaration and definition 7 | class Person{ 8 | public : 9 | //Declaration for the constructor 10 | Person(const std::string& names_param, int age_param); 11 | 12 | void print_info()const{ 13 | std::cout << "name : " << full_name << " , age : " << age << std::endl; 14 | } 15 | private : 16 | std::string full_name; 17 | int age; 18 | 19 | public : 20 | //static variable declaration 21 | static int person_count; 22 | }; 23 | 24 | 25 | #endif //PERSON_H -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.4DeclarationsAndDefinitions/some_other_file.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | 3 | //Declaration 4 | int Person::person_count = 8; 5 | 6 | 7 | Person::Person(const std::string& names_param, int age_param) 8 | : full_name{names_param}, age{age_param} 9 | { 10 | ++person_count; 11 | } 12 | 13 | 14 | 15 | 16 | double add(double a, double b){ 17 | return a + b; 18 | } -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.5OneDefinitionRule/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "person.h" 3 | 4 | //Variable : Declaration and definition 5 | double weight {}; 6 | 7 | double add(double a, double b); 8 | 9 | struct Point 10 | { 11 | double m_x{}; 12 | double m_y{}; 13 | }; 14 | 15 | int main(){ 16 | 17 | Person p1("John Snow",35); 18 | p1.print_info(); 19 | 20 | 21 | return 0; 22 | } 23 | 24 | /* 25 | double add(double a, double b){ 26 | return a + b; 27 | } 28 | */ 29 | 30 | -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.5OneDefinitionRule/person.h: -------------------------------------------------------------------------------- 1 | #ifndef PERSON_H 2 | #define PERSON_H 3 | 4 | #include 5 | 6 | class Person{ 7 | public : 8 | Person(const std::string& names_param, int age_param); 9 | 10 | void print_info()const{ 11 | std::cout << "name : " << full_name << " , age : " << age << std::endl; 12 | } 13 | private : 14 | std::string full_name; 15 | int age; 16 | 17 | public : 18 | //static variable declaration 19 | static int person_count; 20 | }; 21 | 22 | #endif // PERSON_H -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.5OneDefinitionRule/some_other_file.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | 3 | //double weight {}; 4 | 5 | 6 | 7 | 8 | 9 | 10 | double add(double a, double b){ 11 | return a + b; 12 | } 13 | 14 | int Person::person_count = 8; 15 | 16 | Person::Person(const std::string& names_param, int age_param) 17 | : full_name{names_param}, age{age_param}{ 18 | ++person_count; 19 | } 20 | -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.6Linkage/some_other_file.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const double distance{}; // Declaration and definition 4 | 5 | extern int item_count; // Declaration of some variable defined in some 6 | // other translation unit 7 | 8 | 9 | //Functions have external linkage by default 10 | void print_distance(){ 11 | std::cout << "distance (other_file) : " << distance << " &distance :" 12 | << &distance << std::endl; 13 | } 14 | 15 | void print_item_count(){ 16 | std::cout << "item_count(other_file) : " << item_count << " &item_count : " 17 | << &item_count << std::endl; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.8FlippingLinkage/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //internal linkage -> external linkage 4 | extern const double distance {45.7}; 5 | 6 | void print_distance(); // Declaration 7 | void some_other_function(); // Declaration 8 | 9 | 10 | 11 | int main(){ 12 | 13 | std::cout << "distance(main) : " << distance << std::endl; 14 | std::cout << "&distance(main) : " << &distance << std::endl; 15 | 16 | std::cout << std::endl; 17 | print_distance(); 18 | 19 | std::cout << "----" << std::endl; 20 | some_other_function(); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.9InlineVariablesAndFunctions/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "utility.h" 3 | 4 | void print_age_utility1(); 5 | void print_age_utility2(); 6 | 7 | int main(){ 8 | 9 | print_age_utility1(); 10 | print_age_utility2(); 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.9InlineVariablesAndFunctions/utility.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | inline double threashold {11.1}; // Definition 4 | 5 | 6 | //Defintion 7 | inline double add(double a, double b){ 8 | if( (a > 11.1) && (b > 11.1)){ 9 | return a + b; 10 | }else{ 11 | return threashold; 12 | } 13 | } -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.9InlineVariablesAndFunctions/utility1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "utility.h" 3 | 4 | inline int age{12}; 5 | 6 | inline void some_function(){ 7 | std::cout << "age : " << age << std::endl; 8 | std::cout << "&age : " << &age << std::endl; 9 | } 10 | 11 | void print_age_utility1(){ 12 | std::cout << "Printing from utility1 : " << std::endl; 13 | some_function(); 14 | std::cout << std::endl; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/32.9InlineVariablesAndFunctions/utility2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | inline int age{12}; 4 | 5 | inline void some_function(){ 6 | std::cout << "age : " << age << std::endl; 7 | std::cout << "&age : " << &age << std::endl; 8 | } 9 | 10 | void print_age_utility2(){ 11 | std::cout << "Printing from utility2 : " << std::endl; 12 | some_function(); 13 | std::cout << std::endl; 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /32.ProgramsWithMultipleFiles/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /33.SmartPointers/33.2UniquePointers/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | Dog::Dog(std::string name_param) : dog_name(name_param) 4 | { 5 | std::cout << "Constructor for dog " << dog_name << " called." << std::endl; 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | std::cout << "Destructor for dog " << dog_name << " called" << std::endl;; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /33.SmartPointers/33.3UniquePointersAsFunctionParametersAndReturnTypes/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | Dog::Dog(std::string name_param) : dog_name(name_param) 4 | { 5 | std::cout << "Constructor for dog " << dog_name << " called." << std::endl; 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | std::cout << "Destructor for dog " << dog_name << " called" << std::endl;; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /33.SmartPointers/33.3UniquePointersAsFunctionParametersAndReturnTypes/person.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include "dog.h" 3 | Person::Person(std::string name) : m_name{name} 4 | { 5 | } 6 | 7 | void Person::adopt_dog( std::unique_ptr dog){ 8 | std::cout << m_name << " just adopted the dog " << dog->get_name() << std::endl; 9 | } 10 | 11 | 12 | Person::~Person() 13 | { 14 | 15 | } 16 | 17 | -------------------------------------------------------------------------------- /33.SmartPointers/33.3UniquePointersAsFunctionParametersAndReturnTypes/person.h: -------------------------------------------------------------------------------- 1 | #ifndef PERSON_H 2 | #define PERSON_H 3 | 4 | #include 5 | #include 6 | 7 | class Dog; 8 | class Person 9 | { 10 | public: 11 | Person(std::string name); 12 | ~Person(); 13 | 14 | void adopt_dog( std::unique_ptr dog); 15 | private : 16 | int m_age; 17 | std::string m_name; 18 | }; 19 | 20 | #endif // PERSON_H 21 | -------------------------------------------------------------------------------- /33.SmartPointers/33.4UniquePointersAndArrays/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | Dog::Dog(std::string name_param) : dog_name(name_param) 4 | { 5 | std::cout << "Constructor for dog " << dog_name << " called." << std::endl; 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | std::cout << "Destructor for dog " << dog_name << " called" << std::endl;; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /33.SmartPointers/33.5SharedPointers/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | Dog::Dog(std::string name_param) : dog_name(name_param) 4 | { 5 | std::cout << "Constructor for dog " << dog_name << " called." << std::endl; 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | std::cout << "Destructor for dog " << dog_name << " called" << std::endl;; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /33.SmartPointers/33.5SharedPointers/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | #include 5 | #include 6 | class Dog 7 | { 8 | public: 9 | explicit Dog(std::string name_param); 10 | Dog() = default; 11 | ~Dog(); 12 | 13 | std::string get_name() const{ 14 | return dog_name; 15 | } 16 | 17 | void set_dog_name(const std::string & name){ 18 | dog_name = name; 19 | } 20 | 21 | void print_info() const{ 22 | std::cout << "Dog [ name : " << dog_name << " ]" << std::endl; 23 | } 24 | 25 | private: 26 | std::string dog_name {"Puffy"}; 27 | 28 | }; 29 | 30 | 31 | #endif // DOG_H 32 | -------------------------------------------------------------------------------- /33.SmartPointers/33.6SharedPointersFromUniquePointers/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | Dog::Dog(std::string name_param) : dog_name(name_param) 4 | { 5 | std::cout << "Constructor for dog " << dog_name << " called." << std::endl; 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | std::cout << "Destructor for dog " << dog_name << " called" << std::endl;; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /33.SmartPointers/33.7SharedPointersWithArrays/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | Dog::Dog(std::string name_param) : dog_name(name_param) 4 | { 5 | std::cout << "Constructor for dog " << dog_name << " called." << std::endl; 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | std::cout << "Destructor for dog " << dog_name << " called" << std::endl;; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /33.SmartPointers/33.8SharedPointersAsFunctionParametersAndReturnTypes/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | Dog::Dog(std::string name_param) : dog_name(name_param) 4 | { 5 | std::cout << "Constructor for dog " << dog_name << " called." << std::endl; 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | std::cout << "Destructor for dog " << dog_name << " called" << std::endl;; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /33.SmartPointers/33.9WeakPointers/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | Dog::Dog(std::string name_param) : dog_name(name_param) 4 | { 5 | std::cout << "Constructor for dog " << dog_name << " called." << std::endl; 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | std::cout << "Destructor for dog " << dog_name << " called" << std::endl;; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /33.SmartPointers/33.9WeakPointers/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | #include 5 | #include 6 | class Dog 7 | { 8 | public: 9 | explicit Dog(std::string name_param); 10 | Dog() = default; 11 | ~Dog(); 12 | 13 | std::string get_name() const{ 14 | return dog_name; 15 | } 16 | 17 | void set_dog_name(const std::string & name){ 18 | dog_name = name; 19 | } 20 | 21 | void print_info() const{ 22 | std::cout << "Dog [ name : " << dog_name << " ]" << std::endl; 23 | } 24 | 25 | private: 26 | std::string dog_name {"Puffy"}; 27 | }; 28 | 29 | 30 | #endif // DOG_H 31 | -------------------------------------------------------------------------------- /33.SmartPointers/33.9WeakPointers/person.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include 3 | 4 | Person::Person(std::string name) : m_name{name} 5 | { 6 | std::cout << "Constructor for person " << m_name << " called." << std::endl; 7 | } 8 | 9 | Person::~Person() 10 | { 11 | std::cout << "Destructor for person " << m_name << " called." << std::endl; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /33.SmartPointers/33.9WeakPointers/person.h: -------------------------------------------------------------------------------- 1 | #ifndef PERSON_H 2 | #define PERSON_H 3 | 4 | 5 | #include 6 | #include 7 | 8 | class Person 9 | { 10 | public: 11 | Person() = default; 12 | Person(std::string name); 13 | ~Person(); 14 | 15 | //Member functions 16 | void set_friend(std::shared_ptr p){ 17 | //The assignment creates a weak_ptr out of p 18 | m_friend = p; 19 | } 20 | 21 | private : 22 | std::weak_ptr m_friend; // Initialized to nullptr 23 | std::string m_name {"Unnamed"}; 24 | }; 25 | 26 | 27 | #endif // PERSON_H 28 | -------------------------------------------------------------------------------- /33.SmartPointers/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.10CompoundOperators_ReusingOperators/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | 7 | Point p1(10,10); 8 | Point p2(20,20); 9 | Point p3(5,5); 10 | 11 | std::cout << "p1 + p2 : " << (p1 + p2) << std::endl; // (30,30) 12 | std::cout << "p2 - p3 : " << (p2 - p3) << std::endl; // (15,15) 13 | 14 | std::cout << "------" << std::endl; 15 | 16 | p1+=p2; 17 | p2-=p3; 18 | 19 | std::cout << "point1 : " << p1 << std::endl; // (30,30) 20 | std::cout << "point2 : " << p2 << std::endl; // (15,15) 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.10CompoundOperators_ReusingOperators/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | double Point::length() const{ 5 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 6 | } 7 | 8 | 9 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.11CustomTypeConversions/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | #include "number.h" 4 | 5 | 6 | double sum(double a, double b){ 7 | return a + b; 8 | } 9 | void use_point(const Point& p){ 10 | std::cout << "Printing the point from use_point func : " << p << std::endl; 11 | } 12 | 13 | int main(){ 14 | 15 | Number n1(22); 16 | Number n2(10); 17 | 18 | //double result = sum(static_cast(n1),static_cast(n2)); 19 | //std::cout << "result : " << result << std::endl; 20 | use_point(static_cast(n1)); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.11CustomTypeConversions/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | #include "number.h" 4 | 5 | 6 | /* 7 | Point::Point(const Number& n) 8 | : Point(static_cast(n.get_wrapped_int()) , 9 | static_cast(n.get_wrapped_int())) 10 | { 11 | std::cout << "Using the Point constructor" << std::endl; 12 | } 13 | */ 14 | 15 | double Point::length() const{ 16 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 17 | } 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.12ImplicitConversionsWithOverloadedBinaryOperators/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "number.h" 3 | 4 | 5 | 6 | int main(){ 7 | 8 | Number n1(22); 9 | std::cout << "n1 : " << n1 << std::endl; 10 | std::cout << "n1 + 2 : " << (n1 + 2) << std::endl; 11 | std::cout << "2 + n1 : " << (2 + n1) << std::endl; 12 | std::cout << "42 - n1 : " << (42 - n1) << std::endl; 13 | 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.13UnaryPrefixIncrementOperatorAsMember/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | Point p1(10,10); 7 | std::cout << "p1 : " << p1 << std::endl; 8 | 9 | // ++p1; 10 | p1.operator++(); 11 | 12 | std::cout << "p1 : " << p1 << std::endl; // 11 13 | 14 | for(size_t i{}; i < 20 ; ++i){ 15 | ++p1; 16 | std::cout << "p1 : " << p1 << std::endl; 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.13UnaryPrefixIncrementOperatorAsMember/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | 5 | double Point::length() const{ 6 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 7 | } 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.14UnaryPrefixIncrementOperatorAsNonMember/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | Point p1(10,10); 7 | std::cout << "p1 : " << p1 << std::endl; 8 | 9 | //++p1; 10 | 11 | operator++(p1); 12 | 13 | 14 | std::cout << "p1 : " << p1 << std::endl; // 11 15 | 16 | for(size_t i{}; i < 20 ; ++i){ 17 | ++p1; 18 | std::cout << "p1 : " << p1 << std::endl; 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.14UnaryPrefixIncrementOperatorAsNonMember/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | 5 | double Point::length() const{ 6 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 7 | } 8 | 9 | void operator++(Point& operand){ 10 | ++(operand.m_x); 11 | ++(operand.m_y); 12 | } 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.15UnaryPostfixIncrementOperator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | Point p1(10,10); 7 | std::cout << "p1 : " << p1++ << std::endl; // (10,10); 8 | std::cout << "p1 : " << p1 << std::endl; // (11,11) 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.15UnaryPostfixIncrementOperator/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | 5 | double Point::length() const{ 6 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 7 | } 8 | 9 | 10 | void operator++(Point& operand){ 11 | ++(operand.m_x); 12 | ++(operand.m_y); 13 | } 14 | 15 | Point operator++(Point& operand,int){ 16 | Point local_point(operand); 17 | ++operand; 18 | return local_point; 19 | } 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.16UnaryPrefixPostfixDecrementOperator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | Point p1(10,10); 7 | std::cout << "p1 : " << p1--<< std::endl; // (10,10); 8 | std::cout << "p1 : " << p1 << std::endl; // (11,11) 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.17CopyAssignmentOperator/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | 5 | double Point::length() const{ 6 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 7 | } 8 | 9 | Point::Point(const Point &p) 10 | { 11 | std::cout << "Copy constructor called to copy point " << p << std::endl; 12 | if(this != &p){ 13 | delete p_data; 14 | p_data = new int(*(p.p_data)); 15 | m_x = p.m_x; 16 | m_y = p.m_y; 17 | } 18 | } 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.18CopyAssignmentOperatorForOtherTypes/car.cpp: -------------------------------------------------------------------------------- 1 | #include "car.h" 2 | 3 | Car::Car(const std::string& color, int speed) : 4 | m_color(color),m_speed(speed) 5 | { 6 | } 7 | 8 | Car::~Car() 9 | { 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.18CopyAssignmentOperatorForOtherTypes/car.h: -------------------------------------------------------------------------------- 1 | #ifndef CAR_H 2 | #define CAR_H 3 | 4 | #include 5 | class Car 6 | { 7 | public: 8 | Car() = default; 9 | Car(const std::string& color, int speed); 10 | ~Car(); 11 | 12 | double get_speed()const{ 13 | return m_speed; 14 | } 15 | std::string get_color() const{ 16 | return m_color; 17 | } 18 | private: 19 | std::string m_color; 20 | double m_speed; 21 | }; 22 | 23 | #endif // CAR_H 24 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.18CopyAssignmentOperatorForOtherTypes/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | #include "car.h" 4 | 5 | int main(){ 6 | 7 | 8 | Point p1(10,10,10); 9 | Car c1("red",200.0); 10 | 11 | p1 = c1; 12 | 13 | std::cout << "p1 : " << p1 << std::endl; 14 | 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.2AdditionOperatorAsMember/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | 5 | int main(){ 6 | 7 | Point p1(10,10); 8 | Point p2(20,20); 9 | Point p3{p1 + p2}; // p1.operator+(p2) 10 | 11 | Point p4{p2 + Point(5,5)}; 12 | 13 | p3.print_info(); 14 | p4.print_info(); 15 | 16 | (Point(20,20) + Point(10,10)).print_info(); 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.2AdditionOperatorAsMember/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | double Point::length() const{ 5 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 6 | } 7 | 8 | Point Point::operator+(const Point& right_operand){ 9 | return Point(this->m_x + right_operand.m_x , 10 | this->m_y + right_operand.m_y ); 11 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.3AdditionOperatorAsNonMember/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | 5 | int main(){ 6 | 7 | Point p1(10,10); 8 | Point p2(20,20); 9 | //Point p3{p1 + p2}; // opearator+(p1,p2); 10 | Point p3{operator+ (p1,p2)}; 11 | 12 | Point p4{p2 + Point(5,5)}; 13 | 14 | p3.print_info(); 15 | p4.print_info(); 16 | 17 | (Point(20,20) + Point(10,10)).print_info(); 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.3AdditionOperatorAsNonMember/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | double Point::length() const{ 5 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 6 | } 7 | 8 | /* 9 | Point operator+(const Point& left, const Point& right){ 10 | return Point(left.m_x + right.m_x , left.m_y + right.m_y ); 11 | } 12 | */ 13 | 14 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.4SubscriptOperatorReading/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | 5 | int main(){ 6 | 7 | Point p1(10,20); 8 | 9 | //std::cout << "p1.x : " << p1[0] << std::endl; // x coordinate : 10 10 | std::cout << "p1.x : " << p1.operator[](0) << std::endl; // x coordinate : 10 11 | std::cout << "p1.y : " << p1[1] << std::endl; // y coordinate : 20 12 | return 0; 13 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.4SubscriptOperatorReading/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | double Point::length() const{ 5 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 6 | } 7 | 8 | 9 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.5SubscriptOperatorReadingWritting/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | 5 | int main(){ 6 | 7 | Point p1(10,20); 8 | 9 | p1.print_info(); 10 | 11 | //Changing the data 12 | p1[0] = 35.6; 13 | p1[1] = 23.9; 14 | 15 | p1.print_info(); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.5SubscriptOperatorReadingWritting/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | double Point::length() const{ 5 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 6 | } 7 | 8 | 9 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.6SubscriptOperatorForCollectionTypes/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "scores.h" 3 | 4 | 5 | int main(){ 6 | 7 | 8 | Scores math("Math"); 9 | math.print_info(); 10 | 11 | for(size_t i{} ; i <20 ; ++i){ 12 | if(i==0){ 13 | math[0] = 73.7; 14 | }else{ 15 | math[i] = math[i-1] + 0.56; 16 | } 17 | } 18 | 19 | math.print_info(); 20 | 21 | std::cout << "------" << std::endl; 22 | 23 | 24 | //Const objecst 25 | const Scores geo("Geography"); 26 | std::cout << "geo [5] : " << geo[5] << std::endl; 27 | 28 | 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.6SubscriptOperatorForCollectionTypes/scores.cpp: -------------------------------------------------------------------------------- 1 | #include "scores.h" 2 | #include 3 | 4 | double& Scores::operator[](size_t index){ 5 | assert((index >= 0) &&(index < 20)); 6 | return m_scores[index]; 7 | } 8 | 9 | double Scores::operator[](size_t index) const{ 10 | assert((index >= 0) &&(index < 20)); 11 | return m_scores[index]; 12 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.7StreamInsertionOperator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | Point p1(10,20); 7 | Point p2(3,4); 8 | //p1.print_info(); 9 | 10 | std::cout << p1 << p2 << std::endl; 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.7StreamInsertionOperator/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | double Point::length() const{ 5 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 6 | } 7 | 8 | 9 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.8StreamExtractionOperator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | 7 | Point p2; 8 | 9 | std::cin >> p2;// 10 | 11 | std::cout << "p2 : " << p2 << std::endl; 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.8StreamExtractionOperator/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | double Point::length() const{ 5 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 6 | } 7 | 8 | 9 | -------------------------------------------------------------------------------- /34.OperatorOverloading/34.9OtherArithmeticOperators/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | int main(){ 5 | 6 | std::cout << (Point(20,20) - Point(10,10)) << std::endl; 7 | std::cout << (Point(20,20) + Point(10,10)) << std::endl; 8 | 9 | Point p1(10,10); 10 | Point p2(20,20); 11 | Point p3{p1 + p2}; 12 | Point p4{p2 - Point(5,5)}; 13 | 14 | std::cout << "point1 : " << p1 << std::endl; // (10,10) 15 | std::cout << "point3 : " << p3 << std::endl; // (30,30) 16 | std::cout << "point4 : " << p4 << std::endl; // (15,15) 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /34.OperatorOverloading/34.9OtherArithmeticOperators/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | double Point::length() const{ 5 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 6 | } 7 | 8 | 9 | -------------------------------------------------------------------------------- /34.OperatorOverloading/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /35.LogicalOperatorsAndThreeWayComparison/35.12SpaceshipAsNonMember/number.cpp: -------------------------------------------------------------------------------- 1 | #include "number.h" 2 | 3 | Number::Number(int value) : m_wrapped_int(value) 4 | { 5 | } 6 | 7 | std::ostream& operator<<(std::ostream& out , const Number& number){ 8 | out << "Number : [" << number.m_wrapped_int << "]"; 9 | return out; 10 | } 11 | 12 | Number::~Number() 13 | { 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /35.LogicalOperatorsAndThreeWayComparison/35.3Rel_OpsNamespace/my_utility.h: -------------------------------------------------------------------------------- 1 | #ifndef MY_UTILITY_H 2 | #define MY_UTILITY_H 3 | 4 | template< class T > 5 | bool operator!=( const T& lhs, const T& rhs ) 6 | { 7 | return !(lhs == rhs); 8 | } 9 | 10 | template< class T > 11 | bool operator>( const T& lhs, const T& rhs ) 12 | { 13 | return rhs < lhs; 14 | } 15 | 16 | template< class T > 17 | bool operator<=( const T& lhs, const T& rhs ) 18 | { 19 | return !(rhs < lhs); 20 | } 21 | 22 | template< class T > 23 | bool operator>=( const T& lhs, const T& rhs ) 24 | { 25 | return !(lhs < rhs); 26 | } 27 | 28 | #endif // MY_UTILITY_H -------------------------------------------------------------------------------- /35.LogicalOperatorsAndThreeWayComparison/35.3Rel_OpsNamespace/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | double Point::length() const{ 5 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 6 | } 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /35.LogicalOperatorsAndThreeWayComparison/35.4LogicalOperatorsWithImplicitConversions/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "number.h" 3 | 4 | 5 | int main(){ 6 | 7 | Number n1(10); 8 | Number n2(20); 9 | 10 | std::cout << std::boolalpha; 11 | std::cout << "n1 < n2 : " << (n1 < n2) << std::endl; 12 | std::cout << "15 < n2 : " << (15 < n2) << std::endl; 13 | std::cout << "n1 < 25 : " << (n1 < 25) << std::endl; 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /35.LogicalOperatorsAndThreeWayComparison/35.7CustomEqualityOperator/point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "point.h" 3 | 4 | double Point::length() const{ 5 | return sqrt(pow(m_x - 0, 2) + pow(m_y - 0, 2) * 1.0); 6 | } 7 | 8 | bool Point::operator==(const Point& other) const{ 9 | return (this->length() == other.length()); 10 | } 11 | 12 | /* 13 | bool Point::operator!=(const Point& other) const{ 14 | return !(*this == other); 15 | } 16 | */ 17 | 18 | 19 | -------------------------------------------------------------------------------- /35.LogicalOperatorsAndThreeWayComparison/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /36.Inheritance/36.11InheritingBaseConstructors/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "person.h" 3 | #include "engineer.h" 4 | 5 | int main(){ 6 | 7 | Engineer eng1("Daniel Gray",23,"asdl;fkjdas;fkdas;fk",4); 8 | std::cout << "eng1 : " << eng1 << std::endl; 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /36.Inheritance/36.12InheritanceAndDestructors/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "person.h" 3 | #include "engineer.h" 4 | #include "civilengineer.h" 5 | 6 | int main(){ 7 | 8 | CivilEngineer eng1("Daniel Gray",41,"Green Sky Oh Blue 33St#75",12,"Road Strength"); 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /36.Inheritance/36.13ReusedSymbolsInInheritance/child.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rutura/The-C-20-Masterclass-Source-Code/3de559892b0849f9430192e4ad939d3d294d3849/36.Inheritance/36.13ReusedSymbolsInInheritance/child.cpp -------------------------------------------------------------------------------- /36.Inheritance/36.13ReusedSymbolsInInheritance/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "child.h" 3 | 4 | 5 | 6 | int main(){ 7 | Child child(33); 8 | child.print_var();// Calls the method in Child 9 | child.Parent::print_var(); // Calls the method in Parent, 10 | // value in parent just contains junk or whatever 11 | // in class initialization we did. 12 | 13 | std::cout << "--------" << std::endl; 14 | child.show_values(); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /36.Inheritance/36.13ReusedSymbolsInInheritance/parent.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rutura/The-C-20-Masterclass-Source-Code/3de559892b0849f9430192e4ad939d3d294d3849/36.Inheritance/36.13ReusedSymbolsInInheritance/parent.cpp -------------------------------------------------------------------------------- /36.Inheritance/36.13ReusedSymbolsInInheritance/parent.h: -------------------------------------------------------------------------------- 1 | #ifndef PARENT_H 2 | #define PARENT_H 3 | 4 | #include 5 | class Parent 6 | { 7 | public: 8 | Parent() = default; 9 | Parent(int member_var) : m_member_var(member_var){ 10 | } 11 | ~Parent() = default; 12 | 13 | void print_var()const{ 14 | std::cout << "The value in parent is : " << m_member_var << std::endl; 15 | } 16 | protected: 17 | int m_member_var{100}; 18 | }; 19 | 20 | 21 | #endif // PARENT_H 22 | -------------------------------------------------------------------------------- /36.Inheritance/36.2FirstTryOnInheritance/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "player.h" 3 | 4 | 5 | int main(){ 6 | 7 | Player p1("Basketball"); 8 | p1.set_first_name("John"); 9 | p1.set_last_name("Snow"); 10 | std::cout << "player : " << p1 << std::endl; 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /36.Inheritance/36.2FirstTryOnInheritance/person.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | 3 | Person::Person(){ 4 | } 5 | 6 | Person::Person(std::string& first_name_param, std::string& last_name_param) 7 | : first_name(first_name_param), last_name(last_name_param) 8 | { 9 | } 10 | 11 | std::ostream& operator<<(std::ostream& out , const Person& person){ 12 | out << "Person [" << person.first_name << " " << person.last_name << "]"; 13 | return out; 14 | } 15 | 16 | 17 | Person::~Person() 18 | { 19 | } 20 | 21 | -------------------------------------------------------------------------------- /36.Inheritance/36.2FirstTryOnInheritance/player.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include "player.h" 3 | 4 | Player::Player(std::string_view game_param) 5 | : m_game(game_param) 6 | { 7 | //first_name = "John"; Compiler errors 8 | //last_name = "Snow"; 9 | } 10 | 11 | std::ostream& operator<<(std::ostream& out, const Player& player){ 12 | 13 | out << "Player : [ game : " << player.m_game 14 | << " names : " << player.get_first_name() 15 | << " " << player.get_last_name() << "]"; 16 | return out; 17 | } -------------------------------------------------------------------------------- /36.Inheritance/36.2FirstTryOnInheritance/player.h: -------------------------------------------------------------------------------- 1 | #ifndef PLAYER_H 2 | #define PLAYER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "person.h" 8 | 9 | class Player : public Person 10 | { 11 | friend std::ostream& operator<<(std::ostream& out, const Player& player); 12 | public: 13 | Player() = default; 14 | Player(std::string_view game_param); 15 | 16 | private : 17 | std::string m_game{"None"}; 18 | }; 19 | 20 | #endif // PLAYER_H -------------------------------------------------------------------------------- /36.Inheritance/36.3ProtectedMembers/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "player.h" 3 | 4 | 5 | int main(){ 6 | 7 | Player p1("Basketball","John","Snow"); 8 | std::cout << "player : " << p1 << std::endl; 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /36.Inheritance/36.3ProtectedMembers/person.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | 3 | Person::Person(){ 4 | } 5 | 6 | Person::Person(std::string& first_name_param, std::string& last_name_param) 7 | : first_name(first_name_param), last_name(last_name_param) 8 | { 9 | } 10 | 11 | std::ostream& operator<<(std::ostream& out , const Person& person){ 12 | out << "Person [" << person.first_name << " " << person.last_name << "]"; 13 | return out; 14 | } 15 | 16 | 17 | Person::~Person() 18 | { 19 | } 20 | 21 | -------------------------------------------------------------------------------- /36.Inheritance/36.3ProtectedMembers/player.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include "player.h" 3 | 4 | Player::Player(std::string_view game_param, std::string_view first_name_param, 5 | std::string_view last_name_param) 6 | { 7 | m_game = game_param; 8 | first_name = first_name_param; 9 | last_name = last_name_param; 10 | } 11 | 12 | std::ostream& operator<<(std::ostream& out, const Player& player){ 13 | 14 | out << "Player : [ game : " << player.m_game 15 | << " names : " << player.get_first_name() 16 | << " " << player.get_last_name() << "]"; 17 | return out; 18 | } -------------------------------------------------------------------------------- /36.Inheritance/36.3ProtectedMembers/player.h: -------------------------------------------------------------------------------- 1 | #ifndef PLAYER_H 2 | #define PLAYER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "person.h" 8 | 9 | class Player : public Person 10 | { 11 | friend std::ostream& operator<<(std::ostream& out, const Player& player); 12 | public: 13 | Player() = default; 14 | Player(std::string_view game_param, std::string_view first_name_param, 15 | std::string_view last_name_param); 16 | 17 | private : 18 | std::string m_game{"None"}; 19 | }; 20 | 21 | #endif // PLAYER_H -------------------------------------------------------------------------------- /36.Inheritance/36.5BaseClassAccessSpecifiersADemo/engineer.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include "engineer.h" 3 | #include 4 | 5 | Engineer::Engineer() 6 | { 7 | } 8 | 9 | std::ostream& operator<<(std::ostream& out , const Engineer& operand){ 10 | out << "Engineer [Full name : " << operand.get_full_name() << 11 | ",age : " << operand.get_age() << 12 | ",address : " << operand.get_address() << 13 | ",contract_count : " << operand.contract_count << "]"; 14 | return out; 15 | } 16 | 17 | 18 | Engineer::~Engineer() 19 | { 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /36.Inheritance/36.5BaseClassAccessSpecifiersADemo/engineer.h: -------------------------------------------------------------------------------- 1 | #ifndef ENGINEER_H 2 | #define ENGINEER_H 3 | #include "person.h" 4 | 5 | //Engineer is doing private inheritance 6 | 7 | class Engineer : private Person 8 | { 9 | friend std::ostream& operator<<(std::ostream& out , const Engineer& operand); 10 | public: 11 | Engineer(); 12 | ~Engineer(); 13 | 14 | void build_something(){ 15 | m_full_name = "John Snow"; // OK 16 | m_age = 23; // OK 17 | //m_address = "897-78-723"; Compiler error 18 | } 19 | 20 | private : 21 | int contract_count{0}; 22 | }; 23 | 24 | #endif // ENGINEER_H 25 | -------------------------------------------------------------------------------- /36.Inheritance/36.5BaseClassAccessSpecifiersADemo/nurse.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include "nurse.h" 3 | #include 4 | Nurse::Nurse() 5 | { 6 | } 7 | 8 | std::ostream& operator<<(std::ostream& out , const Nurse& operand){ 9 | out << "Nurse [Full name : " << operand.get_full_name() << 10 | ",age : " << operand.get_age() << 11 | ",address : " << operand.get_address() << 12 | ",practice certificate id : " << operand.practice_certificate_id << "]"; 13 | return out; 14 | } 15 | 16 | 17 | Nurse::~Nurse() 18 | { 19 | } -------------------------------------------------------------------------------- /36.Inheritance/36.5BaseClassAccessSpecifiersADemo/nurse.h: -------------------------------------------------------------------------------- 1 | #ifndef NURSE_H 2 | #define NURSE_H 3 | 4 | #include "person.h" 5 | 6 | //Nurse will do protected inheritance 7 | class Nurse : protected Person 8 | { 9 | friend std::ostream& operator<<(std::ostream& , const Nurse& operand); 10 | public: 11 | Nurse(); 12 | ~Nurse(); 13 | 14 | void treat_unwell_person(){ 15 | m_full_name = "John Snow"; // OK 16 | m_age = 23; // OK 17 | //m_address = "897-78-723"; Compiler error 18 | } 19 | 20 | private : 21 | int practice_certificate_id{0}; 22 | }; 23 | 24 | #endif // NURSE_H 25 | -------------------------------------------------------------------------------- /36.Inheritance/36.5BaseClassAccessSpecifiersADemo/person.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include 3 | 4 | Person::Person(std::string_view fullname, int age, 5 | const std::string address) 6 | : m_full_name{fullname}, m_age{age}, 7 | m_address{address} 8 | { 9 | } 10 | 11 | std::ostream& operator<<(std::ostream& out , const Person& person){ 12 | out << "Person [Full name :" << person.get_full_name() << 13 | ", Age:" << person.get_age() << 14 | ", Address:" << person.get_address() << "]"; 15 | return out; 16 | } 17 | 18 | 19 | Person::~Person() 20 | { 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /36.Inheritance/36.5BaseClassAccessSpecifiersADemo/player.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include "player.h" 3 | #include 4 | 5 | Player::Player() 6 | { 7 | } 8 | 9 | std::ostream& operator<<(std::ostream& out, const Player& player){ 10 | out << "Player[Full name : " << player.get_full_name() << 11 | ",age : " << player.get_age() << 12 | ",address : " << player.get_address() << "]"; 13 | return out; 14 | } 15 | 16 | 17 | Player::~Player() 18 | { 19 | } -------------------------------------------------------------------------------- /36.Inheritance/36.6ClosingInOnPrivateInheritance/engineer.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include "engineer.h" 3 | #include 4 | 5 | Engineer::Engineer() 6 | { 7 | } 8 | 9 | std::ostream& operator<<(std::ostream& out , const Engineer& operand){ 10 | out << "Engineer [Full name : " << operand.get_full_name() << 11 | ",age : " << operand.get_age() << 12 | ",address : " << operand.get_address() << 13 | ",contract_count : " << operand.contract_count << "]"; 14 | return out; 15 | } 16 | 17 | 18 | Engineer::~Engineer() 19 | { 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /36.Inheritance/36.6ClosingInOnPrivateInheritance/engineer.h: -------------------------------------------------------------------------------- 1 | #ifndef ENGINEER_H 2 | #define ENGINEER_H 3 | #include "person.h" 4 | 5 | //Engineer is doing private inheritance 6 | 7 | class Engineer : private Person 8 | { 9 | friend std::ostream& operator<<(std::ostream& out , const Engineer& operand); 10 | public: 11 | Engineer(); 12 | ~Engineer(); 13 | 14 | void build_something(){ 15 | m_full_name = "John Snow"; // OK 16 | m_age = 23; // OK 17 | //m_address = "897-78-723"; Compiler error 18 | } 19 | 20 | private : 21 | int contract_count{0}; 22 | }; 23 | 24 | #endif // ENGINEER_H 25 | -------------------------------------------------------------------------------- /36.Inheritance/36.6ClosingInOnPrivateInheritance/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "person.h" 3 | #include "engineer.h" 4 | #include "civilengineer.h" 5 | 6 | int main(){ 7 | 8 | Engineer eng1; 9 | 10 | CivilEngineer ce1; 11 | 12 | std::cout << "Done" << std::endl; 13 | 14 | 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /36.Inheritance/36.6ClosingInOnPrivateInheritance/person.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include 3 | 4 | Person::Person(std::string_view fullname, int age, 5 | const std::string address) 6 | : m_full_name{fullname}, m_age{age}, 7 | m_address{address} 8 | { 9 | } 10 | 11 | std::ostream& operator<<(std::ostream& out , const Person& person){ 12 | out << "Person [Full name :" << person.get_full_name() << 13 | ", Age:" << person.get_age() << 14 | ", Address:" << person.get_address() << "]"; 15 | return out; 16 | } 17 | 18 | 19 | Person::~Person() 20 | { 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /36.Inheritance/36.7ResurectingMembersBackInContext/engineer.cpp: -------------------------------------------------------------------------------- 1 | #include "person.h" 2 | #include "engineer.h" 3 | #include 4 | 5 | Engineer::Engineer() 6 | { 7 | } 8 | 9 | std::ostream& operator<<(std::ostream& out , const Engineer& operand){ 10 | out << "Engineer [Full name : " << operand.get_full_name() << 11 | ",age : " << operand.get_age() << 12 | ",address : " << operand.get_address() << 13 | ",contract_count : " << operand.contract_count << "]"; 14 | return out; 15 | } 16 | 17 | 18 | Engineer::~Engineer() 19 | { 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /36.Inheritance/36.7ResurectingMembersBackInContext/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "person.h" 3 | #include "engineer.h" 4 | #include "civilengineer.h" 5 | 6 | int main(){ 7 | 8 | 9 | 10 | 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /36.Inheritance/36.8DefaultArgConstructorsWithInheritance/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "person.h" 3 | #include "engineer.h" 4 | #include "civilengineer.h" 5 | 6 | int main(){ 7 | 8 | CivilEngineer ce1; 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /36.Inheritance/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/animal.cpp: -------------------------------------------------------------------------------- 1 | #include "animal.h" 2 | 3 | Animal::Animal(std::string_view description) 4 | : m_description(description) 5 | { 6 | } 7 | 8 | Animal::~Animal() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/animal.h: -------------------------------------------------------------------------------- 1 | #ifndef ANIMAL_H 2 | #define ANIMAL_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Animal 9 | { 10 | public: 11 | Animal() = default; 12 | Animal(std::string_view description); 13 | virtual ~Animal(); 14 | 15 | virtual void breathe()const{ 16 | std::cout << "Animal::breathe called for : " << m_description << std::endl; 17 | } 18 | 19 | protected: 20 | std::string m_description; 21 | }; 22 | 23 | #endif // ANIMAL_H 24 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/bird.cpp: -------------------------------------------------------------------------------- 1 | #include "bird.h" 2 | 3 | Bird::Bird(std::string_view wing_color, std::string_view description) 4 | : Animal(description) ,m_wing_color(wing_color) 5 | { 6 | } 7 | 8 | Bird::~Bird() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/bird.h: -------------------------------------------------------------------------------- 1 | #ifndef BIRD_H 2 | #define BIRD_H 3 | #include "animal.h" 4 | class Bird : public Animal 5 | { 6 | public: 7 | Bird() = default; 8 | Bird(std::string_view wing_color, std::string_view description); 9 | 10 | virtual ~Bird(); 11 | 12 | //This is contradictory : virtual and final have counter acting effects. 13 | //Final wins here 14 | virtual void fly() const final{ 15 | std::cout << "Bird::fly() called for bird : " << m_description << std::endl; 16 | } 17 | 18 | private : 19 | std::string m_wing_color; 20 | }; 21 | 22 | #endif // BIRD_H 23 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/bulldog.cpp: -------------------------------------------------------------------------------- 1 | #include "bulldog.h" 2 | 3 | BullDog::BullDog() 4 | { 5 | } 6 | 7 | BullDog::~BullDog() 8 | { 9 | } 10 | 11 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/bulldog.h: -------------------------------------------------------------------------------- 1 | #ifndef BULL_DOG_H 2 | #define BULL_DOG_H 3 | #include "dog.h" 4 | class BullDog : public Dog 5 | { 6 | public: 7 | BullDog(); 8 | virtual ~BullDog(); 9 | 10 | 11 | //Will throw a compiler error 12 | /* 13 | virtual void run() const override{ 14 | std::cout << "Bulldog::run() called" << std::endl; 15 | } 16 | */ 17 | 18 | }; 19 | 20 | #endif // BULL_DOG_H 21 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/cat.cpp: -------------------------------------------------------------------------------- 1 | #include "cat.h" 2 | 3 | Cat::Cat(std::string_view fur_style, std::string_view description) 4 | : Feline(fur_style, description) 5 | { 6 | } 7 | 8 | Cat::~Cat() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/crow.cpp: -------------------------------------------------------------------------------- 1 | #include "crow.h" 2 | 3 | Crow::Crow(std::string_view wing_color, std::string_view description) 4 | : Bird(wing_color,description) 5 | { 6 | } 7 | 8 | Crow::~Crow() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/crow.h: -------------------------------------------------------------------------------- 1 | #ifndef CROW_H 2 | #define CROW_H 3 | #include "bird.h" 4 | 5 | class Crow : public Bird 6 | { 7 | public: 8 | Crow() = default; 9 | Crow(std::string_view wing_color, std::string_view description); 10 | virtual ~Crow(); 11 | 12 | virtual void cow() const{ 13 | std::cout << "Crow::cow called fro crow : " << m_description << std::endl; 14 | } 15 | 16 | //This will give a compiler error is fly is marked as final in Bird 17 | /* 18 | virtual void fly() const override{ 19 | 20 | } 21 | */ 22 | 23 | }; 24 | 25 | #endif // CROW_H 26 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | Dog::Dog(std::string_view fur_style, std::string_view description) 4 | : Feline(fur_style,description) 5 | { 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/feline.cpp: -------------------------------------------------------------------------------- 1 | #include "feline.h" 2 | 3 | Feline::Feline(std::string_view fur_style, std::string_view description) 4 | : Animal(description) , m_fur_style(fur_style) 5 | { 6 | } 7 | 8 | Feline::~Feline() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/feline.h: -------------------------------------------------------------------------------- 1 | #ifndef FELINE_H 2 | #define FELINE_H 3 | #include "animal.h" 4 | class Feline : public Animal 5 | { 6 | public: 7 | Feline() = default; 8 | Feline(std::string_view fur_style, std::string_view description); 9 | virtual ~Feline(); 10 | 11 | virtual void run() const { 12 | std::cout << "Feline " << m_description << " is running" << std::endl; 13 | } 14 | std::string m_fur_style; 15 | }; 16 | 17 | #endif // FELINE_H 18 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Intersting fact #1 4 | /* 5 | class Plane final{ 6 | Plane()=default; 7 | }; 8 | 9 | //This will trigger a compiler error 10 | class FigherJet : public Plane{ 11 | 12 | }; 13 | */ 14 | 15 | int main(){ 16 | 17 | std::cout << "Hello" << std::endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/pigeon.cpp: -------------------------------------------------------------------------------- 1 | #include "pigeon.h" 2 | 3 | Pigeon::Pigeon(std::string_view wing_color, std::string_view description) 4 | : Bird(wing_color,description) 5 | { 6 | } 7 | 8 | Pigeon::~Pigeon() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/pigeon.h: -------------------------------------------------------------------------------- 1 | #ifndef PIGEON_H 2 | #define PIGEON_H 3 | #include "bird.h" 4 | class Pigeon : public Bird 5 | { 6 | public: 7 | Pigeon() = default; 8 | Pigeon(std::string_view wing_color, std::string_view description); 9 | virtual ~Pigeon(); 10 | 11 | virtual void coo() const{ 12 | std::cout << "Pigeon::coo called for pigeon : " << m_description << std::endl; 13 | } 14 | 15 | 16 | }; 17 | 18 | #endif // PIGEON_H 19 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/wildcat.cpp: -------------------------------------------------------------------------------- 1 | #include "wildcat.h" 2 | 3 | WildCat::WildCat() 4 | { 5 | } 6 | 7 | WildCat::~WildCat() 8 | { 9 | } 10 | 11 | -------------------------------------------------------------------------------- /37.Polymorphism/37.10Final/wildcat.h: -------------------------------------------------------------------------------- 1 | #ifndef WILD_CAT_H 2 | #define WILD_CAT_H 3 | #include "cat.h" 4 | class WildCat /*: public Cat*/ // Can not derive from final base class 5 | { 6 | public: 7 | WildCat(); 8 | ~WildCat(); 9 | 10 | }; 11 | 12 | #endif // WILD_CAT_H 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.11FinalAndOverrideAreNotKeywords/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class override{ 4 | 5 | }; 6 | 7 | int main(){ 8 | 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /37.Polymorphism/37.12PolymorphicFunctionsAndAccessSpecifiers/ellipse.cpp: -------------------------------------------------------------------------------- 1 | #include "ellipse.h" 2 | 3 | 4 | Ellipse::Ellipse(double x_radius, double y_radius, 5 | std::string_view description) 6 | : Shape(description), m_x_radius(x_radius), 7 | m_y_radius(y_radius) 8 | { 9 | } 10 | 11 | Ellipse::Ellipse() 12 | : Ellipse(0.0,0.0,"NoDescription") 13 | { 14 | 15 | } 16 | 17 | Ellipse::~Ellipse() 18 | { 19 | } 20 | 21 | -------------------------------------------------------------------------------- /37.Polymorphism/37.12PolymorphicFunctionsAndAccessSpecifiers/shape.cpp: -------------------------------------------------------------------------------- 1 | #include "shape.h" 2 | 3 | 4 | Shape::Shape(std::string_view description) 5 | : m_description(description) 6 | { 7 | } 8 | 9 | Shape::Shape() 10 | : Shape("NoDescription") 11 | { 12 | } 13 | 14 | Shape::~Shape() 15 | { 16 | } 17 | 18 | -------------------------------------------------------------------------------- /37.Polymorphism/37.13NonPolymorphicFunctionsAndAccessSpecifiers/ellipse.cpp: -------------------------------------------------------------------------------- 1 | #include "ellipse.h" 2 | 3 | 4 | Ellipse::Ellipse(double x_radius, double y_radius, 5 | std::string_view description) 6 | : Shape(description), m_x_radius(x_radius), 7 | m_y_radius(y_radius) 8 | { 9 | } 10 | 11 | Ellipse::Ellipse() 12 | : Ellipse(0.0,0.0,"NoDescription") 13 | { 14 | 15 | } 16 | 17 | Ellipse::~Ellipse() 18 | { 19 | } 20 | 21 | -------------------------------------------------------------------------------- /37.Polymorphism/37.13NonPolymorphicFunctionsAndAccessSpecifiers/shape.cpp: -------------------------------------------------------------------------------- 1 | #include "shape.h" 2 | 3 | 4 | Shape::Shape(std::string_view description) 5 | : m_description(description) 6 | { 7 | } 8 | 9 | Shape::Shape() 10 | : Shape("NoDescription") 11 | { 12 | } 13 | 14 | Shape::~Shape() 15 | { 16 | } 17 | 18 | -------------------------------------------------------------------------------- /37.Polymorphism/37.13NonPolymorphicFunctionsAndAccessSpecifiers/shape.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAPE_H 2 | #define SHAPE_H 3 | #include 4 | #include 5 | class Shape 6 | { 7 | public: 8 | Shape(); 9 | Shape(std::string_view description); 10 | ~Shape(); 11 | public: 12 | void draw( )const{ 13 | std::cout << "Shape::draw() called for : " << m_description << std::endl; 14 | } 15 | private : 16 | void func() const { 17 | std::cout << "Shape::func() called for : " << m_description << std::endl; 18 | } 19 | protected: 20 | std::string m_description; 21 | }; 22 | 23 | #endif // SHAPE_H 24 | -------------------------------------------------------------------------------- /37.Polymorphism/37.14VirtualFunctionsWithDefaultArguments/base.cpp: -------------------------------------------------------------------------------- 1 | #include "base.h" 2 | 3 | Base::Base() 4 | { 5 | } 6 | 7 | Base::~Base() 8 | { 9 | } 10 | 11 | -------------------------------------------------------------------------------- /37.Polymorphism/37.14VirtualFunctionsWithDefaultArguments/base.h: -------------------------------------------------------------------------------- 1 | #ifndef BASE_H 2 | #define BASE_H 3 | #include 4 | class Base 5 | { 6 | public: 7 | Base(); 8 | ~Base(); 9 | 10 | virtual double add(double a = 5 , double b = 5) const{ 11 | std::cout << "Base::add() called" << std::endl; 12 | return (a + b + 1); 13 | } 14 | }; 15 | 16 | #endif // BASE_H 17 | -------------------------------------------------------------------------------- /37.Polymorphism/37.14VirtualFunctionsWithDefaultArguments/derived.cpp: -------------------------------------------------------------------------------- 1 | #include "derived.h" 2 | 3 | Derived::Derived() 4 | { 5 | } 6 | 7 | Derived::~Derived() 8 | { 9 | } 10 | 11 | -------------------------------------------------------------------------------- /37.Polymorphism/37.14VirtualFunctionsWithDefaultArguments/derived.h: -------------------------------------------------------------------------------- 1 | #ifndef DERIVED_H 2 | #define DERIVED_H 3 | #include "base.h" 4 | 5 | class Derived : public Base 6 | { 7 | public: 8 | Derived(); 9 | ~Derived(); 10 | 11 | virtual double add(double a = 10 , double b = 10) const override{ 12 | std::cout << "Derived::add() called" << std::endl; 13 | return (a + b +2); 14 | } 15 | 16 | }; 17 | 18 | #endif // DERIVED_H 19 | -------------------------------------------------------------------------------- /37.Polymorphism/37.15VirtualDestructors/animal.cpp: -------------------------------------------------------------------------------- 1 | #include "animal.h" 2 | 3 | Animal::Animal(std::string_view description) 4 | : m_description(description) 5 | { 6 | } 7 | 8 | Animal::~Animal() 9 | { 10 | std::cout << "Animal destructor called" << std::endl; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.15VirtualDestructors/animal.h: -------------------------------------------------------------------------------- 1 | #ifndef ANIMAL_H 2 | #define ANIMAL_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Animal 9 | { 10 | public: 11 | Animal() = default; 12 | Animal(std::string_view description); 13 | virtual ~Animal(); 14 | 15 | virtual void breathe()const{ 16 | std::cout << "Animal::breathe called for : " << m_description << std::endl; 17 | } 18 | 19 | protected: 20 | std::string m_description; 21 | }; 22 | 23 | #endif // ANIMAL_H 24 | -------------------------------------------------------------------------------- /37.Polymorphism/37.15VirtualDestructors/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | Dog::Dog(std::string_view fur_style, std::string_view description) 4 | : Feline(fur_style,description) 5 | { 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | std::cout << "Dog destructor called" << std::endl; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.15VirtualDestructors/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | #include "feline.h" 4 | class Dog : public Feline 5 | { 6 | public: 7 | Dog() = default; 8 | Dog(std::string_view fur_style, std::string_view description); 9 | virtual ~Dog(); 10 | 11 | virtual void bark() const{ 12 | std::cout << "Dog::bark called : Woof!" << std::endl; 13 | } 14 | 15 | }; 16 | 17 | #endif // DOG_H 18 | -------------------------------------------------------------------------------- /37.Polymorphism/37.15VirtualDestructors/feline.cpp: -------------------------------------------------------------------------------- 1 | #include "feline.h" 2 | 3 | Feline::Feline(std::string_view fur_style, std::string_view description) 4 | : Animal(description) , m_fur_style(fur_style) 5 | { 6 | } 7 | 8 | Feline::~Feline() 9 | { 10 | std::cout << "Feline destructor called" << std::endl; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.15VirtualDestructors/feline.h: -------------------------------------------------------------------------------- 1 | #ifndef FELINE_H 2 | #define FELINE_H 3 | #include "animal.h" 4 | class Feline : public Animal 5 | { 6 | public: 7 | Feline() = default; 8 | Feline(std::string_view fur_style, std::string_view description); 9 | virtual ~Feline(); 10 | 11 | virtual void run() const{ 12 | std::cout << "Feline " << m_description << " is running" << std::endl; 13 | } 14 | std::string m_fur_style; 15 | }; 16 | 17 | #endif // FELINE_H 18 | -------------------------------------------------------------------------------- /37.Polymorphism/37.15VirtualDestructors/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | int main(){ 5 | 6 | Animal * p_animal = new Dog; 7 | 8 | delete p_animal; 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /37.Polymorphism/37.16DynamicCasts/animal.cpp: -------------------------------------------------------------------------------- 1 | #include "animal.h" 2 | 3 | Animal::Animal(std::string_view description) 4 | : m_description(description) 5 | { 6 | } 7 | 8 | Animal::~Animal() 9 | { 10 | std::cout << "Animal destructor called" << std::endl; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.16DynamicCasts/animal.h: -------------------------------------------------------------------------------- 1 | #ifndef ANIMAL_H 2 | #define ANIMAL_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Animal 9 | { 10 | public: 11 | Animal() = default; 12 | Animal(std::string_view description); 13 | virtual ~Animal(); 14 | 15 | virtual void breathe()const{ 16 | std::cout << "Animal::breathe called for : " << m_description << std::endl; 17 | } 18 | protected: 19 | std::string m_description; 20 | }; 21 | 22 | #endif // ANIMAL_H 23 | -------------------------------------------------------------------------------- /37.Polymorphism/37.16DynamicCasts/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | Dog::Dog(std::string_view fur_style, std::string_view description) 4 | : Feline(fur_style,description) 5 | { 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | std::cout << "Dog destructor called" << std::endl; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.16DynamicCasts/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | #include "feline.h" 4 | class Dog : public Feline 5 | { 6 | public: 7 | Dog() = default; 8 | Dog(std::string_view fur_style, std::string_view description); 9 | virtual ~Dog(); 10 | 11 | virtual void bark() const{ 12 | std::cout << "Dog::bark called : Woof!" << std::endl; 13 | } 14 | 15 | void do_some_dog_thingy(){ 16 | std::cout << "Doing some dog thingy...,speed : " << m_speed << std::endl; 17 | } 18 | 19 | private : 20 | double m_speed{}; 21 | }; 22 | 23 | #endif // DOG_H 24 | -------------------------------------------------------------------------------- /37.Polymorphism/37.16DynamicCasts/feline.cpp: -------------------------------------------------------------------------------- 1 | #include "feline.h" 2 | 3 | Feline::Feline(std::string_view fur_style, std::string_view description) 4 | : Animal(description) , m_fur_style(fur_style) 5 | { 6 | } 7 | 8 | Feline::~Feline() 9 | { 10 | std::cout << "Feline destructor called" << std::endl; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.16DynamicCasts/feline.h: -------------------------------------------------------------------------------- 1 | #ifndef FELINE_H 2 | #define FELINE_H 3 | #include "animal.h" 4 | class Feline : public Animal 5 | { 6 | public: 7 | Feline() = default; 8 | Feline(std::string_view fur_style, std::string_view description); 9 | virtual ~Feline(); 10 | 11 | virtual void run() const{ 12 | std::cout << "Feline " << m_description << " is running" << std::endl; 13 | } 14 | void do_some_feline_thingy(){ 15 | std::cout << "Doing some feline thingy..." << std::endl; 16 | } 17 | std::string m_fur_style; 18 | }; 19 | 20 | #endif // FELINE_H 21 | -------------------------------------------------------------------------------- /37.Polymorphism/37.18TypeIdOperator/nonpolymorphic.h: -------------------------------------------------------------------------------- 1 | #ifndef NON_POLYMORPHIC_H 2 | #define NON_POLYMORPHIC_H 3 | 4 | #include 5 | 6 | class StaticBase{ 7 | void do_something(){ 8 | std::cout << "StaticBase::do_something() called" << std::endl; 9 | } 10 | }; 11 | 12 | class StaticDerived : public StaticBase{ 13 | void do_something() { 14 | std::cout << "StaticDerived::do_something() called" << std::endl; 15 | } 16 | }; 17 | 18 | 19 | #endif // NON_POLYMORPHIC_H 20 | -------------------------------------------------------------------------------- /37.Polymorphism/37.18TypeIdOperator/polymorphic.h: -------------------------------------------------------------------------------- 1 | #ifndef POLYMORPHIC_H 2 | #define POLYMORPHIC_H 3 | 4 | #include 5 | 6 | class DynamicBase{ 7 | public: 8 | virtual void do_something(){ 9 | std::cout << "DynamicBase::do_something() called" << std::endl; 10 | } 11 | virtual ~DynamicBase() = default; 12 | }; 13 | 14 | class DynamicDerived : public DynamicBase{ 15 | public : 16 | virtual void do_something() override{ 17 | std::cout << "DynamicDerived::do_something() called" << std::endl; 18 | } 19 | virtual ~DynamicDerived() = default; 20 | }; 21 | 22 | 23 | #endif // POLYMORPHIC_H 24 | -------------------------------------------------------------------------------- /37.Polymorphism/37.19PureVirtualFunctionsAndAbstractClasses/circle.cpp: -------------------------------------------------------------------------------- 1 | #include "circle.h" 2 | 3 | Circle::Circle(double radius , std::string_view description) 4 | : Shape(description) , 5 | m_radius(radius) 6 | { 7 | } 8 | 9 | -------------------------------------------------------------------------------- /37.Polymorphism/37.19PureVirtualFunctionsAndAbstractClasses/rectangle.cpp: -------------------------------------------------------------------------------- 1 | #include "rectangle.h" 2 | 3 | Rectangle::Rectangle(double width , double height,std::string_view description) 4 | : Shape(description), m_width(width), m_height(height) 5 | { 6 | } 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /37.Polymorphism/37.19PureVirtualFunctionsAndAbstractClasses/shape.cpp: -------------------------------------------------------------------------------- 1 | #include "shape.h" 2 | 3 | Shape::Shape(std::string_view description) 4 | : m_description(description) 5 | { 6 | 7 | } 8 | 9 | -------------------------------------------------------------------------------- /37.Polymorphism/37.19PureVirtualFunctionsAndAbstractClasses/shape.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAPE_H 2 | #define SHAPE_H 3 | 4 | #include 5 | 6 | class Shape 7 | { 8 | protected: 9 | Shape() = default; 10 | Shape(std::string_view description); 11 | public : 12 | 13 | virtual ~Shape() = default; // If destructor is not public, you won't be 14 | // able to delete base_ptrs. SHOW THIS TO STUDENTS 15 | //Pure virtual functions 16 | virtual double perimeter() const = 0; 17 | virtual double surface() const = 0; 18 | private : 19 | std::string m_description; 20 | }; 21 | 22 | #endif // SHAPE_H 23 | -------------------------------------------------------------------------------- /37.Polymorphism/37.20AbstractClassesAsInterfaces/animal.cpp: -------------------------------------------------------------------------------- 1 | #include "animal.h" 2 | 3 | Animal::Animal(const std::string& description) 4 | : m_description(description) 5 | { 6 | } 7 | 8 | Animal::~Animal() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.20AbstractClassesAsInterfaces/bird.cpp: -------------------------------------------------------------------------------- 1 | #include "bird.h" 2 | 3 | Bird::Bird(const std::string& wing_color, const std::string& description) 4 | : Animal(description) ,m_wing_color(wing_color) 5 | { 6 | } 7 | 8 | Bird::~Bird() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.20AbstractClassesAsInterfaces/cat.cpp: -------------------------------------------------------------------------------- 1 | #include "cat.h" 2 | 3 | Cat::Cat(const std::string& fur_style, const std::string& description) 4 | : Feline(fur_style, description) 5 | { 6 | } 7 | 8 | Cat::~Cat() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.20AbstractClassesAsInterfaces/crow.cpp: -------------------------------------------------------------------------------- 1 | #include "crow.h" 2 | 3 | Crow::Crow(const std::string& wing_color, const std::string& description) 4 | : Bird(wing_color,description) 5 | { 6 | } 7 | 8 | Crow::~Crow() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.20AbstractClassesAsInterfaces/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | Dog::Dog(const std::string& fur_style, const std::string& description) 4 | : Feline(fur_style,description) 5 | { 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.20AbstractClassesAsInterfaces/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | #include "feline.h" 4 | class Dog : public Feline 5 | { 6 | public: 7 | Dog() = default; 8 | Dog(const std::string& fur_style, const std::string& description); 9 | ~Dog(); 10 | 11 | virtual void bark() const{ 12 | std::cout << "Dog::bark called : Woof!" << std::endl; 13 | } 14 | 15 | virtual void stream_insert(std::ostream& out)const override{ 16 | out << "Dog [description : " << m_description << ", fur_style : " << 17 | m_fur_style << "]"; 18 | } 19 | 20 | }; 21 | 22 | #endif // DOG_H 23 | -------------------------------------------------------------------------------- /37.Polymorphism/37.20AbstractClassesAsInterfaces/feline.cpp: -------------------------------------------------------------------------------- 1 | #include "feline.h" 2 | 3 | Feline::Feline(const std::string& fur_style, const std::string& description) 4 | : Animal(description) , m_fur_style(fur_style) 5 | { 6 | } 7 | 8 | Feline::~Feline() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.20AbstractClassesAsInterfaces/pigeon.cpp: -------------------------------------------------------------------------------- 1 | #include "pigeon.h" 2 | 3 | Pigeon::Pigeon(const std::string& wing_color, const std::string& description) 4 | : Bird(wing_color,description) 5 | { 6 | } 7 | 8 | Pigeon::~Pigeon() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.20AbstractClassesAsInterfaces/stream_insertable.cpp: -------------------------------------------------------------------------------- 1 | #include "stream_insertable.h" 2 | 3 | std::ostream& operator<< (std::ostream& out,const StreamInsertable& operand){ 4 | operand.stream_insert(out); 5 | return out; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /37.Polymorphism/37.20AbstractClassesAsInterfaces/stream_insertable.h: -------------------------------------------------------------------------------- 1 | #ifndef STREAM_INSERTABLE_H 2 | #define STREAM_INSERTABLE_H 3 | #include 4 | 5 | class StreamInsertable{ 6 | friend std::ostream& operator<< (std::ostream& out, const StreamInsertable& operand); 7 | 8 | public : 9 | virtual void stream_insert(std::ostream& out)const =0; 10 | }; 11 | 12 | #endif //STREAM_INSERTABLE_H -------------------------------------------------------------------------------- /37.Polymorphism/37.2StaticBindingWithInheritance/circle.cpp: -------------------------------------------------------------------------------- 1 | #include "circle.h" 2 | 3 | Circle::Circle(double radius , std::string_view description) 4 | : Oval(radius,radius,description) 5 | { 6 | } 7 | 8 | Circle::~Circle() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.2StaticBindingWithInheritance/circle.h: -------------------------------------------------------------------------------- 1 | #ifndef CIRCLE_H 2 | #define CIRCLE_H 3 | #include "oval.h" 4 | 5 | class Circle : public Oval 6 | { 7 | public: 8 | Circle() = default; 9 | Circle(double radius,std::string_view description); 10 | ~Circle(); 11 | 12 | void draw() const{ 13 | std::cout << "Circle::draw() called. Drawing " << m_description << 14 | " with radius : " << get_x_rad() << std::endl; 15 | } 16 | 17 | }; 18 | 19 | #endif // CIRCLE_H 20 | -------------------------------------------------------------------------------- /37.Polymorphism/37.2StaticBindingWithInheritance/oval.cpp: -------------------------------------------------------------------------------- 1 | #include "oval.h" 2 | 3 | Oval::Oval(double x_radius, double y_radius, 4 | std::string_view description) 5 | : Shape(description),m_x_radius(x_radius), m_y_radius(y_radius) 6 | { 7 | } 8 | 9 | Oval::~Oval() 10 | { 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.2StaticBindingWithInheritance/shape.cpp: -------------------------------------------------------------------------------- 1 | #include "shape.h" 2 | 3 | Shape::Shape(std::string_view description) 4 | : m_description(description) 5 | { 6 | } 7 | 8 | Shape::~Shape() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.2StaticBindingWithInheritance/shape.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAPE_H 2 | #define SHAPE_H 3 | 4 | #include 5 | #include 6 | #include 7 | class Shape 8 | { 9 | public: 10 | Shape() = default; 11 | Shape(std::string_view description); 12 | ~Shape(); 13 | 14 | void draw() const{ 15 | std::cout << "Shape::draw() called. Drawing " << m_description << std::endl; 16 | } 17 | 18 | protected : 19 | std::string m_description{""}; 20 | }; 21 | 22 | #endif // SHAPE_H 23 | -------------------------------------------------------------------------------- /37.Polymorphism/37.3PolymorphismWithVirtualFunctions/circle.cpp: -------------------------------------------------------------------------------- 1 | #include "circle.h" 2 | 3 | Circle::Circle(double radius , std::string_view description) 4 | : Oval(radius,radius,description) 5 | { 6 | } 7 | 8 | Circle::~Circle() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.3PolymorphismWithVirtualFunctions/circle.h: -------------------------------------------------------------------------------- 1 | #ifndef CIRCLE_H 2 | #define CIRCLE_H 3 | #include "oval.h" 4 | 5 | class Circle : public Oval 6 | { 7 | public: 8 | Circle() = default; 9 | Circle(double radius,std::string_view description); 10 | ~Circle(); 11 | 12 | virtual void draw() const{ 13 | std::cout << "Circle::draw() called. Drawing " << m_description << 14 | " with radius : " << get_x_rad() << std::endl; 15 | } 16 | 17 | }; 18 | 19 | #endif // CIRCLE_H 20 | -------------------------------------------------------------------------------- /37.Polymorphism/37.3PolymorphismWithVirtualFunctions/oval.cpp: -------------------------------------------------------------------------------- 1 | #include "oval.h" 2 | 3 | Oval::Oval(double x_radius, double y_radius, 4 | std::string_view description) 5 | : Shape(description),m_x_radius(x_radius), m_y_radius(y_radius) 6 | { 7 | } 8 | 9 | Oval::~Oval() 10 | { 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.3PolymorphismWithVirtualFunctions/shape.cpp: -------------------------------------------------------------------------------- 1 | #include "shape.h" 2 | 3 | Shape::Shape(std::string_view description) 4 | : m_description(description) 5 | { 6 | } 7 | 8 | Shape::~Shape() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.3PolymorphismWithVirtualFunctions/shape.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAPE_H 2 | #define SHAPE_H 3 | 4 | #include 5 | #include 6 | #include 7 | class Shape 8 | { 9 | public: 10 | Shape() = default; 11 | Shape(std::string_view description); 12 | ~Shape(); 13 | 14 | virtual void draw() const{ 15 | std::cout << "Shape::draw() called. Drawing " << m_description << std::endl; 16 | } 17 | 18 | protected : 19 | std::string m_description{""}; 20 | }; 21 | 22 | #endif // SHAPE_H 23 | -------------------------------------------------------------------------------- /37.Polymorphism/37.4SizeOfPolymorphicObjectsAndSlicing/circle.cpp: -------------------------------------------------------------------------------- 1 | #include "circle.h" 2 | 3 | Circle::Circle(double radius , std::string_view description) 4 | : Oval(radius,radius,description) 5 | { 6 | } 7 | 8 | Circle::~Circle() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.4SizeOfPolymorphicObjectsAndSlicing/circle.h: -------------------------------------------------------------------------------- 1 | #ifndef CIRCLE_H 2 | #define CIRCLE_H 3 | #include "oval.h" 4 | 5 | class Circle : public Oval 6 | { 7 | public: 8 | Circle() = default; 9 | Circle(double radius,std::string_view description); 10 | ~Circle(); 11 | 12 | virtual void draw() const{ 13 | std::cout << "Circle::draw() called. Drawing " << m_description << 14 | " with radius : " << get_x_rad() << std::endl; 15 | } 16 | 17 | }; 18 | 19 | #endif // CIRCLE_H 20 | -------------------------------------------------------------------------------- /37.Polymorphism/37.4SizeOfPolymorphicObjectsAndSlicing/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "shape.h" 3 | #include "oval.h" 4 | #include "circle.h" 5 | 6 | int main(){ 7 | 8 | //Comparing object sizes 9 | std::cout << "sizeof(Shape) : " << sizeof(Shape) << std::endl; // dynamic : 40 10 | std::cout << "sizeof(Oval) : " << sizeof(Oval) << std::endl; // dynamic : 56 11 | std::cout << "sizeof(Circle) : " << sizeof(Circle) << std::endl; // dynamic : 56 12 | 13 | 14 | //Slicing 15 | Circle circle1(3.3,"Circle1"); 16 | Shape shape = circle1; 17 | shape.draw(); //Shape::draw 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /37.Polymorphism/37.4SizeOfPolymorphicObjectsAndSlicing/oval.cpp: -------------------------------------------------------------------------------- 1 | #include "oval.h" 2 | 3 | Oval::Oval(double x_radius, double y_radius, 4 | std::string_view description) 5 | : Shape(description),m_x_radius(x_radius), m_y_radius(y_radius) 6 | { 7 | } 8 | 9 | Oval::~Oval() 10 | { 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.4SizeOfPolymorphicObjectsAndSlicing/shape.cpp: -------------------------------------------------------------------------------- 1 | #include "shape.h" 2 | 3 | Shape::Shape(std::string_view description) 4 | : m_description(description) 5 | { 6 | } 7 | 8 | Shape::~Shape() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.4SizeOfPolymorphicObjectsAndSlicing/shape.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAPE_H 2 | #define SHAPE_H 3 | 4 | #include 5 | #include 6 | #include 7 | class Shape 8 | { 9 | public: 10 | Shape() = default; 11 | Shape(std::string_view description); 12 | ~Shape(); 13 | 14 | virtual void draw() const{ 15 | std::cout << "Shape::draw() called. Drawing " << m_description << std::endl; 16 | } 17 | 18 | protected : 19 | std::string m_description{""}; 20 | }; 21 | 22 | #endif // SHAPE_H 23 | -------------------------------------------------------------------------------- /37.Polymorphism/37.5PolymorphicObjectsStoredInCollections/circle.cpp: -------------------------------------------------------------------------------- 1 | #include "circle.h" 2 | 3 | Circle::Circle(double radius , std::string_view description) 4 | : Oval(radius,radius,description) 5 | { 6 | } 7 | 8 | Circle::~Circle() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.5PolymorphicObjectsStoredInCollections/circle.h: -------------------------------------------------------------------------------- 1 | #ifndef CIRCLE_H 2 | #define CIRCLE_H 3 | #include "oval.h" 4 | 5 | class Circle : public Oval 6 | { 7 | public: 8 | Circle() = default; 9 | Circle(double radius,std::string_view description); 10 | ~Circle(); 11 | 12 | virtual void draw() const{ 13 | std::cout << "Circle::draw() called. Drawing " << m_description << 14 | " with radius : " << get_x_rad() << std::endl; 15 | } 16 | 17 | }; 18 | 19 | #endif // CIRCLE_H 20 | -------------------------------------------------------------------------------- /37.Polymorphism/37.5PolymorphicObjectsStoredInCollections/oval.cpp: -------------------------------------------------------------------------------- 1 | #include "oval.h" 2 | 3 | Oval::Oval(double x_radius, double y_radius, 4 | std::string_view description) 5 | : Shape(description),m_x_radius(x_radius), m_y_radius(y_radius) 6 | { 7 | } 8 | 9 | Oval::~Oval() 10 | { 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.5PolymorphicObjectsStoredInCollections/shape.cpp: -------------------------------------------------------------------------------- 1 | #include "shape.h" 2 | 3 | Shape::Shape(std::string_view description) 4 | : m_description(description) 5 | { 6 | } 7 | 8 | Shape::~Shape() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.5PolymorphicObjectsStoredInCollections/shape.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAPE_H 2 | #define SHAPE_H 3 | 4 | #include 5 | #include 6 | #include 7 | class Shape 8 | { 9 | public: 10 | Shape() = default; 11 | Shape(std::string_view description); 12 | ~Shape(); 13 | 14 | virtual void draw() const{ 15 | std::cout << "Shape::draw() called. Drawing " << m_description << std::endl; 16 | } 17 | 18 | protected : 19 | std::string m_description{""}; 20 | }; 21 | 22 | #endif // SHAPE_H 23 | -------------------------------------------------------------------------------- /37.Polymorphism/37.6Override/circle.cpp: -------------------------------------------------------------------------------- 1 | #include "circle.h" 2 | 3 | Circle::Circle(double radius , std::string_view description) 4 | : Oval(radius,radius,description) 5 | { 6 | } 7 | 8 | Circle::~Circle() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.6Override/circle.h: -------------------------------------------------------------------------------- 1 | #ifndef CIRCLE_H 2 | #define CIRCLE_H 3 | #include "oval.h" 4 | 5 | class Circle : public Oval 6 | { 7 | public: 8 | Circle() = default; 9 | Circle(double radius,std::string_view description); 10 | ~Circle(); 11 | 12 | virtual void draw() const override{ 13 | std::cout << "Circle::draw() called. Drawing " << m_description << 14 | " with radius : " << get_x_rad() << std::endl; 15 | } 16 | 17 | }; 18 | 19 | #endif // CIRCLE_H 20 | -------------------------------------------------------------------------------- /37.Polymorphism/37.6Override/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "shape.h" 4 | #include "oval.h" 5 | #include "circle.h" 6 | 7 | int main(){ 8 | 9 | Circle circle1(7.2,"circle1"); 10 | Oval oval1(13.3,1.2,"Oval1"); 11 | Circle circle2(11.2,"circle2"); 12 | Oval oval2(31.3,15.2,"Oval2"); 13 | Circle circle3(12.2,"circle3"); 14 | Oval oval3(53.3,9.2,"Oval3"); 15 | 16 | //Raw pointers 17 | 18 | Shape* shapes3[] {&circle1,&oval1,&circle2,&oval2,&circle3,&oval3}; 19 | 20 | for ( Shape* shape_ptr : shapes3){ 21 | shape_ptr->draw(); 22 | std::cout << std::endl; 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /37.Polymorphism/37.6Override/oval.cpp: -------------------------------------------------------------------------------- 1 | #include "oval.h" 2 | 3 | Oval::Oval(double x_radius, double y_radius, 4 | std::string_view description) 5 | : Shape(description),m_x_radius(x_radius), m_y_radius(y_radius) 6 | { 7 | } 8 | 9 | Oval::~Oval() 10 | { 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.6Override/shape.cpp: -------------------------------------------------------------------------------- 1 | #include "shape.h" 2 | 3 | Shape::Shape(std::string_view description) 4 | : m_description(description) 5 | { 6 | } 7 | 8 | Shape::~Shape() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.6Override/shape.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAPE_H 2 | #define SHAPE_H 3 | 4 | #include 5 | #include 6 | #include 7 | class Shape 8 | { 9 | public: 10 | Shape() = default; 11 | Shape(std::string_view description); 12 | ~Shape(); 13 | 14 | virtual void draw() const{ 15 | std::cout << "Shape::draw() called. Drawing " << m_description << std::endl; 16 | } 17 | 18 | protected : 19 | std::string m_description{""}; 20 | }; 21 | 22 | #endif // SHAPE_H 23 | -------------------------------------------------------------------------------- /37.Polymorphism/37.7OverloadingOverridingAndHiding/circle.cpp: -------------------------------------------------------------------------------- 1 | #include "circle.h" 2 | 3 | Circle::Circle(double radius , std::string_view description) 4 | : Oval(radius,radius,description) 5 | { 6 | } 7 | 8 | Circle::~Circle() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.7OverloadingOverridingAndHiding/circle.h: -------------------------------------------------------------------------------- 1 | #ifndef CIRCLE_H 2 | #define CIRCLE_H 3 | #include "oval.h" 4 | 5 | class Circle : public Oval 6 | { 7 | public: 8 | Circle() = default; 9 | Circle(double radius,std::string_view description); 10 | ~Circle(); 11 | 12 | 13 | virtual void draw() const override{ 14 | std::cout << "Circle::draw() called. Drawing " << m_description << 15 | " with radius : " << get_x_rad() << std::endl; 16 | } 17 | 18 | 19 | }; 20 | 21 | #endif // CIRCLE_H 22 | -------------------------------------------------------------------------------- /37.Polymorphism/37.7OverloadingOverridingAndHiding/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "shape.h" 4 | #include "oval.h" 5 | #include "circle.h" 6 | 7 | int main(){ 8 | 9 | Shape * shape_ptr = new Circle(10,"Circle1"); 10 | shape_ptr->draw(45,"Red"); 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /37.Polymorphism/37.7OverloadingOverridingAndHiding/oval.cpp: -------------------------------------------------------------------------------- 1 | #include "oval.h" 2 | 3 | Oval::Oval(double x_radius, double y_radius, 4 | std::string_view description) 5 | : Shape(description),m_x_radius(x_radius), m_y_radius(y_radius) 6 | { 7 | } 8 | 9 | Oval::~Oval() 10 | { 11 | } 12 | 13 | -------------------------------------------------------------------------------- /37.Polymorphism/37.7OverloadingOverridingAndHiding/shape.cpp: -------------------------------------------------------------------------------- 1 | #include "shape.h" 2 | 3 | Shape::Shape(std::string_view description) 4 | : m_description(description) 5 | { 6 | } 7 | 8 | Shape::~Shape() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.8PolymorphismAtDifferentLevels/animal.cpp: -------------------------------------------------------------------------------- 1 | #include "animal.h" 2 | 3 | Animal::Animal(std::string_view description) 4 | : m_description(description) 5 | { 6 | } 7 | 8 | Animal::~Animal() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.8PolymorphismAtDifferentLevels/animal.h: -------------------------------------------------------------------------------- 1 | #ifndef ANIMAL_H 2 | #define ANIMAL_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Animal 9 | { 10 | public: 11 | Animal() = default; 12 | Animal(std::string_view description); 13 | virtual ~Animal(); 14 | 15 | virtual void breathe()const{ 16 | std::cout << "Animal::breathe called for : " << m_description << std::endl; 17 | } 18 | 19 | protected: 20 | std::string m_description; 21 | }; 22 | 23 | #endif // ANIMAL_H 24 | -------------------------------------------------------------------------------- /37.Polymorphism/37.8PolymorphismAtDifferentLevels/bird.cpp: -------------------------------------------------------------------------------- 1 | #include "bird.h" 2 | 3 | Bird::Bird(std::string_view wing_color, std::string_view description) 4 | : Animal(description) ,m_wing_color(wing_color) 5 | { 6 | } 7 | 8 | Bird::~Bird() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.8PolymorphismAtDifferentLevels/bird.h: -------------------------------------------------------------------------------- 1 | #ifndef BIRD_H 2 | #define BIRD_H 3 | #include "animal.h" 4 | class Bird : public Animal 5 | { 6 | public: 7 | Bird() = default; 8 | Bird(std::string_view wing_color, std::string_view description); 9 | 10 | ~Bird(); 11 | 12 | virtual void fly() const{ 13 | std::cout << "Bird::fly() called for bird : " << m_description << std::endl; 14 | } 15 | 16 | private : 17 | std::string m_wing_color; 18 | }; 19 | 20 | #endif // BIRD_H 21 | -------------------------------------------------------------------------------- /37.Polymorphism/37.8PolymorphismAtDifferentLevels/cat.cpp: -------------------------------------------------------------------------------- 1 | #include "cat.h" 2 | 3 | Cat::Cat(std::string_view fur_style, std::string_view description) 4 | : Feline(fur_style, description) 5 | { 6 | } 7 | 8 | Cat::~Cat() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.8PolymorphismAtDifferentLevels/crow.cpp: -------------------------------------------------------------------------------- 1 | #include "crow.h" 2 | 3 | Crow::Crow(std::string_view wing_color, std::string_view description) 4 | : Bird(wing_color,description) 5 | { 6 | } 7 | 8 | Crow::~Crow() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.8PolymorphismAtDifferentLevels/dog.cpp: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | Dog::Dog(std::string_view fur_style, std::string_view description) 4 | : Feline(fur_style,description) 5 | { 6 | } 7 | 8 | Dog::~Dog() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.8PolymorphismAtDifferentLevels/feline.cpp: -------------------------------------------------------------------------------- 1 | #include "feline.h" 2 | 3 | Feline::Feline(std::string_view fur_style, std::string_view description) 4 | : Animal(description) , m_fur_style(fur_style) 5 | { 6 | } 7 | 8 | Feline::~Feline() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.8PolymorphismAtDifferentLevels/feline.h: -------------------------------------------------------------------------------- 1 | #ifndef FELINE_H 2 | #define FELINE_H 3 | #include "animal.h" 4 | class Feline : public Animal 5 | { 6 | public: 7 | Feline() = default; 8 | Feline(std::string_view fur_style, std::string_view description); 9 | virtual ~Feline(); 10 | 11 | virtual void run() const{ 12 | std::cout << "Feline " << m_description << " is running" << std::endl; 13 | } 14 | std::string m_fur_style; 15 | }; 16 | 17 | #endif // FELINE_H 18 | -------------------------------------------------------------------------------- /37.Polymorphism/37.8PolymorphismAtDifferentLevels/pigeon.cpp: -------------------------------------------------------------------------------- 1 | #include "pigeon.h" 2 | 3 | Pigeon::Pigeon(std::string_view wing_color, std::string_view description) 4 | : Bird(wing_color,description) 5 | { 6 | } 7 | 8 | Pigeon::~Pigeon() 9 | { 10 | } 11 | 12 | -------------------------------------------------------------------------------- /37.Polymorphism/37.9InheritanceAndPolymorphismWithStaticMembers/ellipse.cpp: -------------------------------------------------------------------------------- 1 | #include "ellipse.h" 2 | 3 | int Ellipse::m_count{0}; 4 | 5 | Ellipse::Ellipse(double x_radius, double y_radius, 6 | std::string_view description) 7 | : Shape(description), m_x_radius(x_radius), 8 | m_y_radius(y_radius) 9 | { 10 | ++m_count; 11 | } 12 | 13 | Ellipse::Ellipse() 14 | : Ellipse(0.0,0.0,"NoDescription") 15 | { 16 | 17 | } 18 | 19 | Ellipse::~Ellipse() 20 | { 21 | --m_count; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /37.Polymorphism/37.9InheritanceAndPolymorphismWithStaticMembers/ellipse.h: -------------------------------------------------------------------------------- 1 | #ifndef ELLIPSE_H 2 | #define ELLIPSE_H 3 | #include "shape.h" 4 | 5 | class Ellipse : public Shape 6 | { 7 | public: 8 | Ellipse(); 9 | Ellipse(double x_radius, double y_radius, 10 | std::string_view description); 11 | ~Ellipse(); 12 | 13 | virtual int get_count()const override{ 14 | return m_count; 15 | } 16 | 17 | static int m_count; 18 | 19 | private : 20 | double m_x_radius; 21 | double m_y_radius; 22 | }; 23 | 24 | #endif // ELLIPSE_H 25 | -------------------------------------------------------------------------------- /37.Polymorphism/37.9InheritanceAndPolymorphismWithStaticMembers/shape.cpp: -------------------------------------------------------------------------------- 1 | #include "shape.h" 2 | 3 | int Shape::m_count{0}; 4 | 5 | Shape::Shape(std::string_view description) 6 | : m_description(description) 7 | { 8 | ++m_count; 9 | } 10 | 11 | Shape::Shape() 12 | : Shape("NoDescription") 13 | { 14 | } 15 | 16 | Shape::~Shape() 17 | { 18 | --m_count; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /37.Polymorphism/37.9InheritanceAndPolymorphismWithStaticMembers/shape.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAPE_H 2 | #define SHAPE_H 3 | #include 4 | #include 5 | class Shape 6 | { 7 | public: 8 | Shape(); 9 | Shape(std::string_view description); 10 | virtual ~Shape(); 11 | void draw( )const{ 12 | std::cout << "Shape::draw() called for : " << m_description << std::endl; 13 | } 14 | 15 | virtual int get_count() const{ 16 | return m_count; 17 | } 18 | 19 | static int m_count; 20 | protected: 21 | std::string m_description; 22 | 23 | }; 24 | 25 | #endif // SHAPE_H 26 | -------------------------------------------------------------------------------- /37.Polymorphism/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /38.Exceptions/38.14ExceptionsInDestructors/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Item{ 4 | public : 5 | Item(){} 6 | ~Item() { 7 | try{ 8 | throw 0; 9 | }catch(int ex){ 10 | throw; 11 | } 12 | } 13 | 14 | }; 15 | 16 | 17 | int main(){ 18 | try{ 19 | Item item; 20 | }catch(int ex){ 21 | std::cout << "main() : Catching int exception" << std::endl; 22 | } 23 | 24 | std::cout << "END." << std::endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /38.Exceptions/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/39.2ConstructingDestroying/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "boxcontainer.h" 3 | 4 | 5 | int main(){ 6 | 7 | BoxContainer box1(15); 8 | box1.dummy_initialize(); 9 | 10 | std::cout << "box1 : " << box1 << std::endl; 11 | std::cout << "&box1 : " << &box1 << std::endl; 12 | 13 | BoxContainer box2(box1); 14 | std::cout << "box2 : " << box2 << std::endl; 15 | std::cout << "&box2 : " << &box2 << std::endl; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/39.2ConstructingDestroying/stream_insertable.cpp: -------------------------------------------------------------------------------- 1 | #include "stream_insertable.h" 2 | 3 | std::ostream& operator<< (std::ostream& out,const StreamInsertable& operand){ 4 | operand.stream_insert(out); 5 | return out; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/39.2ConstructingDestroying/stream_insertable.h: -------------------------------------------------------------------------------- 1 | #ifndef STREAM_INSERTABLE_H 2 | #define STREAM_INSERTABLE_H 3 | #include 4 | 5 | class StreamInsertable{ 6 | friend std::ostream& operator<< (std::ostream& out, const StreamInsertable& operand); 7 | 8 | public : 9 | virtual void stream_insert(std::ostream& out)const =0; 10 | }; 11 | 12 | #endif //STREAM_INSERTABLE_H -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/39.3AddingItems/stream_insertable.cpp: -------------------------------------------------------------------------------- 1 | #include "stream_insertable.h" 2 | 3 | std::ostream& operator<< (std::ostream& out,const StreamInsertable& operand){ 4 | operand.stream_insert(out); 5 | return out; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/39.3AddingItems/stream_insertable.h: -------------------------------------------------------------------------------- 1 | #ifndef STREAM_INSERTABLE_H 2 | #define STREAM_INSERTABLE_H 3 | #include 4 | 5 | class StreamInsertable{ 6 | friend std::ostream& operator<< (std::ostream& out, const StreamInsertable& operand); 7 | 8 | public : 9 | virtual void stream_insert(std::ostream& out)const =0; 10 | }; 11 | 12 | #endif //STREAM_INSERTABLE_H -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/39.4RemovingItems/stream_insertable.cpp: -------------------------------------------------------------------------------- 1 | #include "stream_insertable.h" 2 | 3 | std::ostream& operator<< (std::ostream& out,const StreamInsertable& operand){ 4 | operand.stream_insert(out); 5 | return out; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/39.4RemovingItems/stream_insertable.h: -------------------------------------------------------------------------------- 1 | #ifndef STREAM_INSERTABLE_H 2 | #define STREAM_INSERTABLE_H 3 | #include 4 | 5 | class StreamInsertable{ 6 | friend std::ostream& operator<< (std::ostream& out, const StreamInsertable& operand); 7 | 8 | public : 9 | virtual void stream_insert(std::ostream& out)const =0; 10 | }; 11 | 12 | #endif //STREAM_INSERTABLE_H -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/39.5OtherOperators/stream_insertable.cpp: -------------------------------------------------------------------------------- 1 | #include "stream_insertable.h" 2 | 3 | std::ostream& operator<< (std::ostream& out,const StreamInsertable& operand){ 4 | operand.stream_insert(out); 5 | return out; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/39.5OtherOperators/stream_insertable.h: -------------------------------------------------------------------------------- 1 | #ifndef STREAM_INSERTABLE_H 2 | #define STREAM_INSERTABLE_H 3 | #include 4 | 5 | class StreamInsertable{ 6 | friend std::ostream& operator<< (std::ostream& out, const StreamInsertable& operand); 7 | 8 | public : 9 | virtual void stream_insert(std::ostream& out)const =0; 10 | }; 11 | 12 | #endif //STREAM_INSERTABLE_H -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/39.7StoringInDifferentTypes/stream_insertable.cpp: -------------------------------------------------------------------------------- 1 | #include "stream_insertable.h" 2 | 3 | std::ostream& operator<< (std::ostream& out,const StreamInsertable& operand){ 4 | operand.stream_insert(out); 5 | return out; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/39.7StoringInDifferentTypes/stream_insertable.h: -------------------------------------------------------------------------------- 1 | #ifndef STREAM_INSERTABLE_H 2 | #define STREAM_INSERTABLE_H 3 | #include 4 | 5 | class StreamInsertable{ 6 | friend std::ostream& operator<< (std::ostream& out, const StreamInsertable& operand); 7 | 8 | public : 9 | virtual void stream_insert(std::ostream& out)const =0; 10 | }; 11 | 12 | #endif //STREAM_INSERTABLE_H -------------------------------------------------------------------------------- /39.Practice-BoxContainerType/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /40.ClassTemplates/40.11StreamInsertionOperatorForClassTemplates/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /40.ClassTemplates/40.11StreamInsertionOperatorForClassTemplates/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "boxcontainer.h" 3 | 4 | 5 | 6 | int main(){ 7 | 8 | BoxContainer int_box; 9 | int_box.add(1); 10 | int_box.add(2); 11 | int_box.add(10); 12 | int_box.add(15); 13 | 14 | std::cout << "int_box : " << int_box << std::endl; 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /40.ClassTemplates/40.12ClassTemplatesWithTypeTraitsAndStaticAsserts/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /40.ClassTemplates/40.13ClassTemplatesWithConcepts/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /40.ClassTemplates/40.14BuiltInConcepts/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /40.ClassTemplates/40.16ConceptsExample2/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | concept Number = (std::integral || std::floating_point) 6 | && !std::same_as 7 | && !std::same_as; 8 | 9 | 10 | template 11 | auto add ( T a, U b){ 12 | return a + b; 13 | } 14 | 15 | 16 | int main(){ 17 | 18 | static_assert(Number); 19 | //auto result = add(20.1, 20); 20 | //std::cout << "result : " << result << std::endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /40.ClassTemplates/40.2YourFirstClassTemplate/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /40.ClassTemplates/40.4NonTypeTemplateParameters/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /40.ClassTemplates/40.5DefaultValuesForTemplateParameters/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /40.ClassTemplates/40.7TemplateSpecializations/adder.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | #include "adder.h" 3 | //template <> // <= this is not needed if defined outside of class 4 | char* Adder::add(char* a, char* b) 5 | { 6 | return strcat(a,b); 7 | } 8 | */ 9 | -------------------------------------------------------------------------------- /40.ClassTemplates/40.7TemplateSpecializations/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "adder.h" 3 | 4 | 5 | int main(){ 6 | 7 | Adder adder_int; 8 | adder_int.do_something(); 9 | std::cout << adder_int.add(10,20) << std::endl; 10 | 11 | std::cout << "-------" << std::endl; 12 | 13 | //Char* 14 | char str1[20] {"Hello"}; 15 | char str2[] {" World!"}; 16 | 17 | Adder adder_c_str; 18 | //adder_c_str.do_something(); 19 | std::cout << adder_c_str.add(str1,str2) << std::endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /40.ClassTemplates/40.7TemplateSpecializations/some_other_file.cpp: -------------------------------------------------------------------------------- 1 | #include "adder.h" -------------------------------------------------------------------------------- /40.ClassTemplates/40.8TemplateSpecializationWithSelectMemberFunctions/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | template <> 4 | const char* BoxContainer::get_max() const 5 | { 6 | std::cout << "Specialized method called to get max on const char*" << std::endl; 7 | size_t max_index = 0; 8 | 9 | for(size_t i{}; i < m_size ; ++i){ 10 | 11 | if((strcmp(m_items[i],m_items[max_index])) > 0){ 12 | max_index = i; 13 | } 14 | 15 | } 16 | 17 | return m_items[max_index]; 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /40.ClassTemplates/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /41.MoveSemantics/41.08MoveOnlyTypes/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /41.MoveSemantics/41.09PassingByRvalueReference/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /41.MoveSemantics/41.3RvalueReferences/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double add(double a , double b){ 4 | return a + b; 5 | } 6 | 7 | 8 | int main(){ 9 | 10 | int x{5}; 11 | int y{10}; 12 | 13 | int&& result = x + y; 14 | 15 | double&& outcome = add(10.1,20.2); 16 | 17 | 18 | // 19 | 20 | std::cout << "result : " << result << std::endl; 21 | std::cout << "outcome : " << outcome << std::endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /41.MoveSemantics/41.4MovingTemporariesAround/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /41.MoveSemantics/41.5MoveConstructorsMoveAssignmentOperators/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /41.MoveSemantics/41.6MovingLvaluesWithStdMove/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /41.MoveSemantics/41.7InvalidatePointersAfterStdMove/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /41.MoveSemantics/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.10LambdaFunctionsAsCallbacks/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.14CapturingTheThisPointer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | class Item{ 5 | public : 6 | 7 | Item(int a, int b) 8 | : m_var1{a}, m_var2{b} 9 | {} 10 | void some_member_func(){ 11 | 12 | auto func = [this](){ 13 | std::cout << "member vars :" << m_var1 << "," << m_var2 << std::endl; 14 | }; 15 | func(); 16 | }; 17 | private : 18 | int m_var1; 19 | int m_var2; 20 | }; 21 | 22 | 23 | int main(){ 24 | 25 | Item item1(10,20); 26 | item1.some_member_func(); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.15StdFunction/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.3CallbackFunctions/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.4FunctionPointerTypeAliases/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.5FunctionPointerTypeAliasesWithTemplates/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.6Functors/decrypt.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.6Functors/decrypt.h: -------------------------------------------------------------------------------- 1 | #ifndef DECRYPT_H 2 | #define DECRYPT_H 3 | 4 | //Functor or function object 5 | 6 | class Decrypt 7 | { 8 | public: 9 | char operator()( const char& param){ 10 | return static_cast (param - 3); 11 | } 12 | }; 13 | 14 | #endif // DECRYPT_H 15 | -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.6Functors/encrypt.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.6Functors/encrypt.h: -------------------------------------------------------------------------------- 1 | #ifndef ENCRYPT_H 2 | #define ENCRYPT_H 3 | 4 | class Encrypt 5 | { 6 | public: 7 | char operator()( const char& param){ 8 | return static_cast (param + 3); 9 | } 10 | }; 11 | 12 | #endif // ENCRYPT_H 13 | -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.7StandardFunctors/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.8FunctorsWithParameters/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /42.FunctionLikeEntities/42.9FunctorsAndLambdaFunctions/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int result = [] (int x, int y) -> int { return x + y; }(7,3); 6 | std::cout << result << std::endl; 7 | 8 | 9 | //Auto type deduction can help deduce the type of the generated lambda function 10 | //We don't have access to that in our C++ code. 11 | auto func = [] (int x, int y) -> int { return x + y; }; 12 | result = func(10,20); 13 | std::cout << result << std::endl; 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /42.FunctionLikeEntities/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /43.StlContainersAndIterators/43.9StdBeginStdEnd/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(){ 6 | 7 | //std::vector vi {1,2,3,4,5,6,7,8,9}; 8 | int vi[] {1,2,3,4,5,6,7,8,9}; 9 | 10 | 11 | /* 12 | std::cout << " Collection : " ; 13 | for(auto it = vi.begin(); it!= vi.end(); ++it){ 14 | std::cout << *it << " "; 15 | } 16 | 17 | */ 18 | 19 | std::cout << "--------" << std::endl; 20 | 21 | std::cout << " Collection : " ; 22 | for(auto it = std::begin(vi); it!= std::end(vi); ++it){ 23 | std::cout << *it << " "; 24 | } 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /43.StlContainersAndIterators/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /44.ZoomingOnSTLContainers/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /45.StlAlgorithms/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /46.RangesLibraryInCpp20/46.7RangeFactories/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int main(){ 7 | 8 | //Generate an infinite sequence of numbers 9 | //auto infinite_view = std::views::iota(1) | std::views::take(20); // Stores the compuation 10 | //auto data_view = std::views::take( std::views::iota(1) , 20); 11 | 12 | //Numbers are generated lazily, on the fly, as we need them in each iteration 13 | for(auto i :std::views::iota(1) | std::views::take(20)){ 14 | std::cout << i << std::endl; 15 | } 16 | 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /46.RangesLibraryInCpp20/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /47.BuildingIteratorsForCustomContainers/40.04CustomInputIterator/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /47.BuildingIteratorsForCustomContainers/40.05CustomOutputIterator/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /47.BuildingIteratorsForCustomContainers/40.06CustomForwardIterator/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /47.BuildingIteratorsForCustomContainers/40.07CustomBidirectionalIteator/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /47.BuildingIteratorsForCustomContainers/40.08CustomRandomAccessIterator/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /47.BuildingIteratorsForCustomContainers/40.08CustomRandomAccessIterator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "boxcontainer.h" 5 | 6 | 7 | int main(){ 8 | 9 | BoxContainer box1; 10 | box1.add(5); 11 | box1.add(1); 12 | box1.add(4); 13 | box1.add(8); 14 | box1.add(5); 15 | box1.add(3); 16 | box1.add(7); 17 | box1.add(9); 18 | box1.add(6); 19 | 20 | std::cout << "box1 : " << box1 << std::endl; 21 | std::ranges::sort(box1.begin(),box1.end()); 22 | std::cout << "box1 : " << box1 << std::endl; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /47.BuildingIteratorsForCustomContainers/40.09CustomIteratorsWithViews/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /47.BuildingIteratorsForCustomContainers/40.10ConstantIterators/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /47.BuildingIteratorsForCustomContainers/40.11RawPointersAsIterators/boxcontainer.cpp: -------------------------------------------------------------------------------- 1 | #include "boxcontainer.h" 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /47.BuildingIteratorsForCustomContainers/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /48.Coroutines/C++ 20 Template Project/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | consteval int get_value(){ 4 | return 3; 5 | } 6 | 7 | int main(){ 8 | constexpr int value = get_value(); 9 | std::cout << "value : " << value << std::endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /49.Modules/48.10ModuleInterfacePartitions/add_partition.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | 3 | export module math:addition; 4 | 5 | export{ 6 | double add(double a, double b) { 7 | return a + b; 8 | } 9 | } -------------------------------------------------------------------------------- /49.Modules/48.10ModuleInterfacePartitions/main.cpp: -------------------------------------------------------------------------------- 1 | import ; 2 | import math; 3 | 4 | int main() 5 | { 6 | auto result1 = add(10, 20); 7 | std::cout << "result1 : " << result1 << std::endl; 8 | 9 | auto result2 = mult(10, 20); 10 | std::cout << "result2 : " << result2 << std::endl; 11 | } -------------------------------------------------------------------------------- /49.Modules/48.10ModuleInterfacePartitions/math.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | 3 | export module math; 4 | 5 | export import :addition; 6 | export import : multiplication; -------------------------------------------------------------------------------- /49.Modules/48.10ModuleInterfacePartitions/mult_partition.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | 3 | export module math:multiplication; 4 | 5 | export{ 6 | double mult(double a, double b) { 7 | return a * b; 8 | } 9 | } -------------------------------------------------------------------------------- /49.Modules/48.11BoxContainerModule/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | import ; 3 | import BoxContainer; 4 | 5 | int main() 6 | { 7 | BoxContainer box1; 8 | box1.add(5); 9 | box1.add(1); 10 | box1.add(4); 11 | box1.add(8); 12 | box1.add(5); 13 | box1.add(3); 14 | box1.add(7); 15 | box1.add(9); 16 | box1.add(6); 17 | 18 | std::cout << "box1 : " << box1 << std::endl; 19 | std::ranges::sort(box1.begin(), box1.end()); 20 | std::cout << "box1 : " << box1 << std::endl; 21 | } 22 | -------------------------------------------------------------------------------- /49.Modules/48.13VisibilityReachability/main.cpp: -------------------------------------------------------------------------------- 1 | import ; 2 | import math; 3 | 4 | int main() 5 | { 6 | //Point is not visible. We can't create its instance 7 | //math::Point p(1, 2); 8 | 9 | auto p = math::generateRandomPoint(); 10 | //print p 11 | std::cout << p << std::endl; 12 | std::cout << "x: " << p.getX() << std::endl; 13 | } -------------------------------------------------------------------------------- /49.Modules/48.14PrivateModuleFragment/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | . The example code compiles and links with visual C++ 3 | . It doesn't work with g++ (yet??) 4 | . The example code compiles fine with clang 18 5 | */ 6 | import ; 7 | import math; 8 | 9 | int main() 10 | { 11 | //Add 10 to 20 and print the result using the math module 12 | std::cout << "10 + 20 = " << math::add(10, 20) << std::endl; 13 | } -------------------------------------------------------------------------------- /49.Modules/48.14PrivateModuleFragment/math.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | export module math; 3 | 4 | namespace math { 5 | export double add(double a, double b); 6 | } 7 | 8 | module: private; 9 | 10 | namespace math { 11 | double add(double a, double b) { 12 | return a + b; 13 | } 14 | } -------------------------------------------------------------------------------- /49.Modules/48.15DosAndDonts/main.cpp: -------------------------------------------------------------------------------- 1 | import ; 2 | import math; 3 | 4 | //Using exported concept 5 | template 6 | T my_add(T a, T b) 7 | { 8 | return a + b; 9 | } 10 | 11 | int main() 12 | { 13 | //Add two numbers using my_add 14 | std::cout << my_add(1, 2) << std::endl; 15 | 16 | const char* str1{ "Hello" }; 17 | const char* str2{ "World" }; 18 | 19 | //Try to add two strings using my_add 20 | //std::cout << my_add(str1, str2) << std::endl; 21 | 22 | //Print the version from the math module 23 | std::cout << "Math version: " << VERSION << std::endl; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /49.Modules/48.1Introduction/main.cpp: -------------------------------------------------------------------------------- 1 | import my_math_module; 2 | 3 | int main() 4 | { 5 | say_something(); 6 | } -------------------------------------------------------------------------------- /49.Modules/48.1Introduction/math.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | //Global module fragment : #include , preprocessor directives 3 | export module my_math_module; 4 | //Module preamble: imports 5 | import ; 6 | 7 | //Module purview 8 | export void say_something() { 9 | std::cout << "Saying something..." << std::endl; 10 | } -------------------------------------------------------------------------------- /49.Modules/48.2YourFirstModule/main.cpp: -------------------------------------------------------------------------------- 1 | import ; 2 | import math_stuff; 3 | 4 | int main() 5 | { 6 | auto result = add(10, 20); 7 | std::cout << "result : " << result << std::endl; 8 | 9 | greet("John"); 10 | 11 | print_name_length("John"); 12 | } -------------------------------------------------------------------------------- /49.Modules/48.3BlockExport/main.cpp: -------------------------------------------------------------------------------- 1 | import ; 2 | import math_stuff; 3 | 4 | int main() 5 | { 6 | auto result = add(10, 20); 7 | std::cout << "result : " << result << std::endl; 8 | 9 | greet("John"); 10 | 11 | print_name_length("John"); 12 | } -------------------------------------------------------------------------------- /49.Modules/48.4SeparateInterfaceFromImplementation-SameFile/main.cpp: -------------------------------------------------------------------------------- 1 | import ; 2 | import math_stuff; 3 | 4 | int main() 5 | { 6 | auto result = add(10, 20); 7 | std::cout << "result : " << result << std::endl; 8 | 9 | greet("John"); 10 | 11 | print_name_length("John"); 12 | 13 | Point point1(22, 44); 14 | std::cout << "point1 : " << point1 << std::endl; 15 | } -------------------------------------------------------------------------------- /49.Modules/48.5SeparateInterfaceFromImplementation-DifferentFiles/main.cpp: -------------------------------------------------------------------------------- 1 | import ; 2 | import math_stuff; 3 | 4 | int main() 5 | { 6 | auto result = add(10, 20); 7 | std::cout << "result : " << result << std::endl; 8 | 9 | greet("John"); 10 | 11 | print_name_length("John"); 12 | 13 | Point point1(22, 44); 14 | std::cout << "point1 : " << point1 << std::endl; 15 | } -------------------------------------------------------------------------------- /49.Modules/48.6MultipleImplementationFiles/main.cpp: -------------------------------------------------------------------------------- 1 | import ; 2 | import math_stuff; 3 | 4 | int main() 5 | { 6 | auto result = add(10, 20); 7 | std::cout << "result : " << result << std::endl; 8 | 9 | greet("John"); 10 | 11 | print_name_length("John"); 12 | 13 | Point point1(22, 44); 14 | std::cout << "point1 : " << point1 << std::endl; 15 | } -------------------------------------------------------------------------------- /49.Modules/48.6MultipleImplementationFiles/math_impl.cpp: -------------------------------------------------------------------------------- 1 | module; 2 | 3 | module math_stuff; 4 | 5 | //Preamble 6 | 7 | //Implementations 8 | double add(double a, double b) { 9 | return a + b; 10 | } 11 | 12 | 13 | 14 | //Point constructor 15 | Point::Point(double x, double y) : m_x(x), m_y(y) {}; -------------------------------------------------------------------------------- /49.Modules/48.6MultipleImplementationFiles/print_impl.cpp: -------------------------------------------------------------------------------- 1 | module; 2 | #include // C function includes must show up here 3 | #include 4 | module math_stuff; 5 | 6 | import ; 7 | 8 | //Implementations 9 | void greet(const std::string& name) { 10 | std::string dest; 11 | dest = "Hello "; 12 | dest.append(name); 13 | std::cout << dest << std::endl; 14 | } 15 | 16 | void print_name_length(const char* c_str_name) { 17 | std::cout << "Length : " << std::strlen(c_str_name) << std::endl; 18 | } -------------------------------------------------------------------------------- /49.Modules/48.7MultipleInterfaceFiles/main.cpp: -------------------------------------------------------------------------------- 1 | import ; 2 | import math_stuff; 3 | import print; 4 | 5 | int main() 6 | { 7 | auto result = add(10, 20); 8 | std::cout << "result : " << result << std::endl; 9 | 10 | greet(); 11 | 12 | print_name_length("John"); 13 | 14 | Point point1(22, 44); 15 | std::cout << "point1 : " << point1 << std::endl; 16 | } -------------------------------------------------------------------------------- /49.Modules/48.7MultipleInterfaceFiles/math.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | // Global module fragment 3 | 4 | #include 5 | 6 | export module math_stuff; // Module declaration 7 | //Module preamble 8 | import ; 9 | 10 | //Module purview 11 | export{ 12 | 13 | double add(double a, double b); 14 | 15 | class Point { 16 | public: 17 | Point() = default; 18 | Point(double x, double y); 19 | friend std::ostream& operator << (std::ostream& out, const Point& point) { 20 | out << "Point [ x : " << point.m_x << ", y : " << point.m_y << "]"; 21 | return out; 22 | } 23 | private: 24 | double m_x; 25 | double m_y; 26 | }; 27 | } -------------------------------------------------------------------------------- /49.Modules/48.7MultipleInterfaceFiles/math_impl.cpp: -------------------------------------------------------------------------------- 1 | module; 2 | 3 | module math_stuff; 4 | 5 | //Preamble 6 | import ; 7 | import print; 8 | 9 | //Implementations 10 | double add(double a, double b) { 11 | return a + b; 12 | } 13 | 14 | 15 | 16 | //Point constructor 17 | Point::Point(double x, double y) : m_x(x), m_y(y) { 18 | std::cout << "Creating point object and greeting John..." << std::endl; 19 | greet(); 20 | }; -------------------------------------------------------------------------------- /49.Modules/48.7MultipleInterfaceFiles/print.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | export module print; 3 | 4 | export{ 5 | void greet(); 6 | 7 | void print_name_length(const char* c_str_name); 8 | } -------------------------------------------------------------------------------- /49.Modules/48.7MultipleInterfaceFiles/print_impl.cpp: -------------------------------------------------------------------------------- 1 | module; 2 | #include // C function includes must show up here 3 | #include 4 | module print; 5 | 6 | import ; 7 | 8 | 9 | //Implementations 10 | void greet() { 11 | std::cout << "Hello John" << std::endl; 12 | } 13 | 14 | void print_name_length(const char* c_str_name) { 15 | std::cout << "Length : " << std::strlen(c_str_name) << std::endl; 16 | } -------------------------------------------------------------------------------- /49.Modules/48.8ExportImport/line.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | 3 | export module Line; 4 | 5 | export import Point; 6 | 7 | export class Line { 8 | public: 9 | Line(Point start, Point end) : start_(start), end_(end) {} 10 | Point start() const { return start_; } 11 | Point end() const { return end_; } 12 | private: 13 | Point start_; 14 | Point end_; 15 | }; -------------------------------------------------------------------------------- /49.Modules/48.8ExportImport/main.cpp: -------------------------------------------------------------------------------- 1 | import ; 2 | import Line; 3 | 4 | int main() 5 | { 6 | Point p1{ 1, 2 }; 7 | Point p2{ 3, 4 }; 8 | 9 | Line line{ p1, p2 }; 10 | 11 | std::cout << "p1 [" << p1.x() << "," << p1.y() << "]\n"; 12 | std::cout << "p2 [" << p2.x() << "," << p2.y() << "]\n"; 13 | } -------------------------------------------------------------------------------- /49.Modules/48.8ExportImport/point.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | 3 | export module Point; 4 | 5 | export class Point { 6 | public: 7 | Point(int x, int y) : x_(x), y_(y) {} 8 | int x() const { return x_; } 9 | int y() const { return y_; } 10 | private: 11 | int x_; 12 | int y_; 13 | }; -------------------------------------------------------------------------------- /49.Modules/48.9SubModules/add_sub.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | 3 | export module math.add_sub; 4 | 5 | export{ 6 | double add(double a, double b) { 7 | return a + b; 8 | } 9 | 10 | double sub(double a, double b) { 11 | return a - b; 12 | } 13 | } -------------------------------------------------------------------------------- /49.Modules/48.9SubModules/main.cpp: -------------------------------------------------------------------------------- 1 | import ; 2 | import math; 3 | 4 | //import math.add_sub; 5 | //import math.mult_div; 6 | 7 | 8 | int main() 9 | { 10 | auto result = add(10, 20); 11 | std::cout << "result : " << result << std::endl; 12 | 13 | auto result1 = mult(10, 20); 14 | std::cout << "result1 : " << result1 << std::endl; 15 | } -------------------------------------------------------------------------------- /49.Modules/48.9SubModules/math.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | 3 | export module math; 4 | 5 | export import math.add_sub; 6 | export import math.mult_div; -------------------------------------------------------------------------------- /49.Modules/48.9SubModules/mult_div.ixx: -------------------------------------------------------------------------------- 1 | module; 2 | 3 | export module math.mult_div; 4 | 5 | export{ 6 | double mult(double a, double b) { 7 | return a * b; 8 | } 9 | 10 | double div(double a, double b) { 11 | return a / b; 12 | } 13 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The-C-20-Masterclass-Source-Code --------------------------------------------------------------------------------