├── Sem. 05 ├── Examples │ ├── tba.cpp │ ├── LinkerErrorIncludingCpp │ │ ├── other.cpp │ │ └── main.cpp │ ├── LinkerErrorDefiningFunctionInHeader │ │ ├── header.h │ │ ├── other.cpp │ │ └── main.cpp │ ├── LinkerErrorMissingFunctionBody │ │ └── main.cpp │ ├── Preprocessor │ │ └── PreprocessorDirectives.cpp │ ├── ExplicitConstructor │ │ └── main.cpp │ ├── InitializerListVsAssigningInConstructor │ │ └── main.cpp │ ├── Mutable │ │ └── main.cpp │ └── StructPointerConstructorsAndDestructors │ │ └── main.cpp └── Solutions │ ├── TextCensorer │ ├── SI │ │ ├── TextCensorer │ │ │ ├── TextCensorer.h │ │ │ └── TextCensorer.cpp │ │ └── Text │ │ │ ├── Text.cpp │ │ │ └── Text.h │ ├── main.cpp │ └── IS │ │ ├── TextCensorer │ │ ├── TextCensorer.h │ │ └── TextCensorer.cpp │ │ └── Text │ │ ├── Text.cpp │ │ └── Text.h │ ├── EventCollection │ ├── EventCollection.h │ └── EventCollection.cpp │ └── Event │ ├── Date │ └── Date.h │ ├── Event.h │ └── Time │ └── Time.h ├── Practicum ├── README.md ├── Pract. 01 │ └── img │ │ ├── structC.jpg │ │ ├── structD.jpg │ │ └── transpose.png ├── Pract. 08 │ ├── blitz-test-2.pdf │ ├── String │ │ └── String.h │ └── README.md └── Pract. 07 │ └── README.md ├── Sem. 13 ├── Solutions │ ├── Chess │ │ ├── Figures │ │ │ ├── FigureBase │ │ │ │ ├── Figure.cpp │ │ │ │ └── Figure.h │ │ │ ├── King │ │ │ │ ├── King.h │ │ │ │ └── King.cpp │ │ │ ├── Knight │ │ │ │ ├── Knight.h │ │ │ │ └── Knight.cpp │ │ │ ├── Rook │ │ │ │ ├── Rook.h │ │ │ │ └── Rook.cpp │ │ │ ├── Queen │ │ │ │ ├── Queen.h │ │ │ │ └── Queen.cpp │ │ │ ├── Bishop │ │ │ │ ├── Bishop.h │ │ │ │ └── Bishop.cpp │ │ │ └── Pawn │ │ │ │ ├── Pawn.h │ │ │ │ └── Pawn.cpp │ │ ├── ConfigEnums │ │ │ └── ConfigEnums.h │ │ ├── FigureFactory │ │ │ ├── FigureFactory.h │ │ │ └── FigureFactory.cpp │ │ ├── main.cpp │ │ └── ChessBoard │ │ │ └── ChessBoard.h │ ├── README.md │ └── polymorphic │ │ ├── polymorphic_container.hpp │ │ └── main.cpp ├── img │ └── Diamond-problem.png └── Examples │ ├── MemoryLeakWithVector.cpp │ ├── DiamondProblemDuplicatedBaseClass.cpp │ ├── VirtualInheritanceConstructors.cpp │ ├── VirtualInheritance.cpp │ ├── MultipleInhDestructors.cpp │ ├── VirtualFunctionsAndDestructors.cpp │ └── VTableMultipleInheritance.cpp ├── Sem. 11 ├── Solutions │ ├── ReadWriteFiles │ │ ├── TestFiles │ │ │ ├── numbers.dat │ │ │ ├── numbers.arr │ │ │ └── numbers.csv │ │ ├── Base │ │ │ ├── FileReader.cpp │ │ │ ├── FileWriter.cpp │ │ │ ├── FileReader.h │ │ │ └── FileWriter.h │ │ ├── Common │ │ │ ├── StreamHelper │ │ │ │ ├── StreamHelper.h │ │ │ │ └── StreamHelper.cpp │ │ │ ├── FilePath │ │ │ │ ├── FilePath.h │ │ │ │ └── FilePath.cpp │ │ │ └── StringView │ │ │ │ ├── StringView.h │ │ │ │ └── StringView.cpp │ │ ├── Csv │ │ │ ├── CsvFileReader.h │ │ │ ├── CsvFileWriter.h │ │ │ ├── CsvFileWriter.cpp │ │ │ └── CsvFileReader.cpp │ │ ├── Array │ │ │ ├── ArrayFileReader.h │ │ │ ├── ArrayFileWriter.h │ │ │ ├── ArrayFileWriter.cpp │ │ │ └── ArrayFileReader.cpp │ │ └── Binary │ │ │ ├── BinaryFileReader.h │ │ │ ├── BinaryFileWriter.h │ │ │ ├── BinaryFileWriter.cpp │ │ │ └── BinaryFileReader.cpp │ ├── Collections │ │ ├── README.md │ │ ├── image.png │ │ ├── PureNumbersCollections │ │ │ ├── PureNumbersCollection.h │ │ │ ├── Normal │ │ │ │ ├── NormalCollection.h │ │ │ │ └── NormalCollection.cpp │ │ │ └── Sorted │ │ │ │ └── SortedCollection.h │ │ ├── Collection.h │ │ ├── Interval │ │ │ ├── IntervalCollection.h │ │ │ └── IntervalCollection.cpp │ │ ├── Dynamic │ │ │ └── DynamicCollection.h │ │ └── Set │ │ │ ├── Set.cpp │ │ │ └── Set.h │ ├── Shapes │ │ ├── Rectangle.h │ │ ├── Circle.h │ │ ├── Triangle.h │ │ ├── Circle.cpp │ │ ├── Shape.h │ │ ├── Rectangle.cpp │ │ └── Triangle.cpp │ ├── StringView │ │ └── StringView.h │ └── Logger.cpp ├── img │ └── VTable.PNG └── Examples │ ├── PureVirtualDestructor.cpp │ ├── OverrideFinal.cpp │ └── VirtualFunctionsDestructors.cpp ├── Sem. 15 ├── Solutions │ ├── SingletonFactory │ │ ├── data.txt │ │ └── main.cpp │ └── ArrayManipulations │ │ ├── Containers │ │ └── IntVector.h │ │ ├── Command │ │ ├── SwapCommand.cpp │ │ ├── VectorCommand.h │ │ ├── SortCommand.h │ │ ├── SwapCommand.h │ │ └── SortCommand.cpp │ │ ├── CommandExecutor.h │ │ ├── CommandExecutor.cpp │ │ └── mainAndFactory.cpp ├── Design Patterns │ ├── Behavioural │ │ └── README.md │ ├── Structural │ │ └── Composite.cpp │ ├── Creational │ │ ├── Singleton.cpp │ │ └── FactoryAndFactoryMethod.cpp │ └── README.md └── README.md ├── Sem. 02 ├── Compilation │ └── Examples │ │ ├── LinkerErrorIncludingCpp │ │ ├── other.cpp │ │ └── main.cpp │ │ ├── LinkerErrorDefiningFunctionInHeader │ │ ├── header.h │ │ ├── other.cpp │ │ └── main.cpp │ │ ├── LinkerErrorMissingFunctionBody │ │ └── main.cpp │ │ └── Preprocessor │ │ └── PreprocessorDirectives.cpp ├── img │ ├── hierarchy.png │ ├── streams.png │ └── stream-flags.png ├── Solutions │ ├── CsvTestFile │ │ └── table.csv │ ├── PrintSourceCode.cpp │ ├── GetFileSize.cpp │ ├── CopyFileContent.cpp │ ├── NewLinesCount.cpp │ ├── ReplaceCharactersInFile.cpp │ └── StringToUnsignedConversion.cpp └── Examples │ ├── FlushExample.cpp │ ├── PrintFileContentWithGet.cpp │ ├── AdditionalExamples.cpp │ ├── UnformattedReadingAfterFormatted.cpp │ └── TogglingFileFlags.cpp ├── Sem. 12 ├── Solutions │ ├── Farm │ │ ├── AnimalFactory.h │ │ ├── Animal.cpp │ │ ├── Dog.h │ │ ├── Cat.h │ │ ├── Cat.cpp │ │ ├── Dog.cpp │ │ ├── Animal.h │ │ ├── AnimalFactory.cpp │ │ └── Farm.h │ ├── swap.hpp │ ├── SharedAndWeak │ │ └── main.cpp │ ├── FunctionsAverage.hpp │ ├── ShapeCollection │ │ ├── Rectangle │ │ │ └── Rectangle.h │ │ ├── Circle │ │ │ ├── Circle.h │ │ │ └── Circle.cpp │ │ ├── Triangle │ │ │ └── Triangle.h │ │ ├── ShapeCollection │ │ │ └── ShapeCollection.h │ │ └── Shape │ │ │ └── Shape.h │ ├── TemplateFunctionSpecification.hpp │ ├── SimpleVisitor-Buildings │ │ └── main.cpp │ └── SimpleVisitor-FileSystem │ │ └── main.cpp ├── Examples │ ├── TemplateInTwoFiles │ │ ├── Test.h │ │ ├── main.cpp │ │ └── Test.cpp │ ├── CallingMemberFunctionWithNullptr.cpp │ └── ObjectBehavingAsFunction.cpp └── SmartPointers │ ├── Weak_Ptr.png │ ├── Shared_Weak.png │ ├── Unique_Ptr.png │ ├── Shared_Weak1.png │ ├── shared_counter1.png │ └── shared_counter2.png ├── Sem. 01 ├── img │ ├── union.jpg │ ├── padding.png │ ├── memory-types.png │ └── selection-sort.png ├── Examples │ ├── IsLittleEndian.cpp │ ├── DifferentFormats.cpp │ ├── AccessingPadding.cpp │ ├── IPv4Address.cpp │ ├── Struct.cpp │ ├── ReadingIntegersBytes.cpp │ ├── Enums.cpp │ ├── Namespaces.cpp │ ├── Padding.cpp │ └── BitFields.cpp └── Revision_Examples │ ├── PtrAndRefs.cpp │ ├── HOF_GetCharactersCount.cpp │ ├── SwapRowsOfDynamicMatrix.cpp │ └── MemoryAllocation.cpp ├── Sem. 10 ├── img │ ├── inher-img.png │ ├── visibility.png │ └── class-hierarchy.png ├── Solutions │ ├── PersonTeacherStudent │ │ ├── Student.cpp │ │ ├── Student.h │ │ ├── Person.h │ │ └── Teacher.h │ ├── Bar │ │ ├── AlcoholDrink │ │ │ ├── AlcoholDrink.h │ │ │ └── AlcoholDrink.cpp │ │ ├── Drink │ │ │ ├── Drink.h │ │ │ └── Drink.cpp │ │ └── Bar │ │ │ ├── Bar.h │ │ │ └── Bar.cpp │ ├── UserAccountPseudoCode.cpp │ ├── Sets │ │ ├── SetByCriteria.h │ │ ├── SetByString.h │ │ ├── Bitset.h │ │ └── SetByCriteria.cpp │ └── GoogleDocumentAccessors.cpp └── Examples │ ├── WRONG-AccessDerivedFromBase.cpp │ ├── PrivateInheritancePassingToFunction.cpp │ ├── FinalAndInheritanceChain.cpp │ ├── PlacementNew.cpp │ └── TypesOfInheritance.cpp ├── Github Workflow └── images │ ├── fetch-origin.png │ ├── star-the-repo.png │ ├── repository-fork-settings.png │ └── repository-settings-menu.png ├── Sem. 08 ├── Examples │ ├── Static function │ │ ├── other.cpp │ │ └── main.cpp │ ├── UnhandledException │ │ └── main.cpp │ ├── Static variable │ │ ├── NaturalNumbersGenerator.cpp │ │ ├── StaticVariable.cpp │ │ └── GeneratorsExamples.cpp │ ├── Exceptions-ConstructorsAndDestructors.cpp │ ├── TerminationBecauseOfErrorInDestructor │ │ └── main.cpp │ ├── HandleBadAllocInConstructor.cpp │ └── MyCustomException │ │ └── main.cpp └── Solutions │ ├── StringPool │ ├── ExampleUsage.cpp │ ├── ImmutableString │ │ └── ImmutableString.h │ └── StringPool │ │ └── StringPool.h │ ├── CarDealerShip │ ├── Car │ │ ├── Car.h │ │ └── Car.cpp │ ├── ArrayOfObjectsImplementation │ │ └── CarDealership.h │ └── ArrayOfPointersImplementation │ │ └── CarDealership.h │ ├── Singleton │ ├── WithStaticVariable │ │ ├── Singleton.h │ │ ├── Singleton.cpp │ │ └── ExampleUsage.cpp │ └── WithPointer │ │ ├── Singleton.h │ │ ├── ExampleUsage.cpp │ │ └── Singleton.cpp │ ├── SelfCountingClass │ ├── SelfCounting.h │ ├── ExampleUsage.cpp │ └── SelfCounting.cpp │ ├── MyString (With Exceptions) │ └── MyString.h │ └── SwapCounter │ └── main.cpp ├── Sem. 09 ├── Examples │ └── OptimizedSwap.cpp └── Solutions │ ├── CarDealerShip (with move) │ ├── Car │ │ ├── Car.cpp │ │ └── Car.h │ ├── ArrayOfObjectsImplementation │ │ └── CarDealership.h │ └── ArrayOfPointersImplementation │ │ └── CarDealership.h │ ├── ClassOfStudents │ ├── Student.h │ ├── Student.cpp │ └── ClassOfStudents.h │ ├── UniqueTestPointer │ └── UniqueTestPointer.h │ └── MyString (with move) │ └── MyString-NoCapacity │ └── MyString.h ├── Sem. 04 ├── Examples │ ├── ConstructorDestructor │ │ ├── ConflictingNamesInConstructor.cpp │ │ ├── WhenIsConstructoCalled.cpp │ │ ├── Explicit.cpp │ │ ├── InitializerListVsAssigningInConstr.cpp │ │ ├── StructPointer.cpp │ │ └── Constructor.cpp │ ├── mutable.cpp │ ├── Student │ │ ├── Student.h │ │ └── Student.cpp │ └── MemberFunctions │ │ ├── MemberFunctions.cpp │ │ └── Encapsulation-FirstQuadrantPoint.cpp └── Solutions │ ├── Interval │ ├── HelperFunctions.h │ ├── Interval.h │ └── HelperFunctions.cpp │ └── Time │ ├── main.cpp │ └── Time.h ├── Sem. 07 ├── Solutions │ ├── PrimeIterator │ │ ├── ExampleUsage.cpp │ │ └── PrimeIterator.h │ └── MyString_Bonus_SmallStringOptimization │ │ └── MyStringSSO.h └── Examples │ └── Wrappers │ ├── CharWrapper │ ├── CharWrapper.cpp │ └── CharWrapper.h │ ├── ExampleUsage.cpp │ └── IntWrapper │ ├── IntWrapper.h │ └── IntWrapper.cpp ├── Sem. 06 ├── Examples │ ├── NotCompilingExamples.cpp │ └── RuleOf4Composition.cpp └── Solutions │ ├── User │ ├── ExampleUsage.cpp │ ├── User.h │ └── User.cpp │ ├── MyString │ ├── ExampleUsage.cpp │ ├── MyString.h │ └── MyString.cpp │ ├── Bitsets │ ├── Static-Bitset │ │ └── Bitset.h │ └── Dynamic-Bitset │ │ └── Bitset.h │ └── Student │ └── Student.h ├── Sem. 03 ├── Examples │ ├── ReusableFunctionForReadingWritingDynamicCharArr.cpp │ ├── ReadAndWriteIntFromBinary.cpp │ ├── ReadAndWriteTestStructureFromBinary.cpp │ ├── ReadAndWriteStudentToTextFile.cpp │ └── ReadAndWriteStudentArrayWithoutDynamicDataToBinaryFile.cpp └── Solutions │ └── ReplaceCharacterInFile.cpp ├── Sem. 14 └── Solutions │ └── ExpressionCalculator │ ├── StringView │ └── StringView.h │ └── MyString │ └── MyString.h └── LICENSE /Sem. 05/Examples/tba.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Practicum/README.md: -------------------------------------------------------------------------------- 1 | # Материали от практикум -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/FigureBase/Figure.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/TestFiles/numbers.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/TestFiles/numbers.arr: -------------------------------------------------------------------------------- 1 | [1 2 3 4] -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/TestFiles/numbers.csv: -------------------------------------------------------------------------------- 1 | 1,2,3,4 -------------------------------------------------------------------------------- /Sem. 15/Solutions/SingletonFactory/data.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 1 2 3 3 | -------------------------------------------------------------------------------- /Sem. 05/Examples/LinkerErrorIncludingCpp/other.cpp: -------------------------------------------------------------------------------- 1 | void f() { 2 | 3 | } -------------------------------------------------------------------------------- /Sem. 02/Compilation/Examples/LinkerErrorIncludingCpp/other.cpp: -------------------------------------------------------------------------------- 1 | void f() { 2 | 3 | } -------------------------------------------------------------------------------- /Sem. 05/Examples/LinkerErrorDefiningFunctionInHeader/header.h: -------------------------------------------------------------------------------- 1 | void f() { 2 | 3 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/README.md: -------------------------------------------------------------------------------- 1 | ### Class hierarchy 2 | 3 | ![alt text](image.png) -------------------------------------------------------------------------------- /Sem. 02/Compilation/Examples/LinkerErrorDefiningFunctionInHeader/header.h: -------------------------------------------------------------------------------- 1 | void f() { 2 | 3 | } -------------------------------------------------------------------------------- /Sem. 12/Solutions/Farm/AnimalFactory.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Animal.h" 3 | 4 | Animal* animalFactory(AnimalType type); -------------------------------------------------------------------------------- /Sem. 01/img/union.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 01/img/union.jpg -------------------------------------------------------------------------------- /Sem. 11/img/VTable.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 11/img/VTable.PNG -------------------------------------------------------------------------------- /Sem. 01/img/padding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 01/img/padding.png -------------------------------------------------------------------------------- /Sem. 02/img/hierarchy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 02/img/hierarchy.png -------------------------------------------------------------------------------- /Sem. 02/img/streams.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 02/img/streams.png -------------------------------------------------------------------------------- /Sem. 10/img/inher-img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 10/img/inher-img.png -------------------------------------------------------------------------------- /Sem. 12/Examples/TemplateInTwoFiles/Test.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | template 4 | class Test { 5 | public: 6 | Test(); 7 | }; -------------------------------------------------------------------------------- /Sem. 10/img/visibility.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 10/img/visibility.png -------------------------------------------------------------------------------- /Sem. 01/img/memory-types.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 01/img/memory-types.png -------------------------------------------------------------------------------- /Sem. 01/img/selection-sort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 01/img/selection-sort.png -------------------------------------------------------------------------------- /Sem. 02/img/stream-flags.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 02/img/stream-flags.png -------------------------------------------------------------------------------- /Sem. 05/Examples/LinkerErrorMissingFunctionBody/main.cpp: -------------------------------------------------------------------------------- 1 | void f(); 2 | 3 | int main() { 4 | f(); // linker error => no function definition 5 | } -------------------------------------------------------------------------------- /Sem. 10/img/class-hierarchy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 10/img/class-hierarchy.png -------------------------------------------------------------------------------- /Sem. 13/img/Diamond-problem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 13/img/Diamond-problem.png -------------------------------------------------------------------------------- /Practicum/Pract. 01/img/structC.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Practicum/Pract. 01/img/structC.jpg -------------------------------------------------------------------------------- /Practicum/Pract. 01/img/structD.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Practicum/Pract. 01/img/structD.jpg -------------------------------------------------------------------------------- /Sem. 12/SmartPointers/Weak_Ptr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 12/SmartPointers/Weak_Ptr.png -------------------------------------------------------------------------------- /Practicum/Pract. 01/img/transpose.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Practicum/Pract. 01/img/transpose.png -------------------------------------------------------------------------------- /Practicum/Pract. 08/blitz-test-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Practicum/Pract. 08/blitz-test-2.pdf -------------------------------------------------------------------------------- /Sem. 02/Compilation/Examples/LinkerErrorMissingFunctionBody/main.cpp: -------------------------------------------------------------------------------- 1 | void f(); 2 | 3 | int main() { 4 | f(); // linker error => no function definition 5 | } -------------------------------------------------------------------------------- /Sem. 12/SmartPointers/Shared_Weak.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 12/SmartPointers/Shared_Weak.png -------------------------------------------------------------------------------- /Sem. 12/SmartPointers/Unique_Ptr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 12/SmartPointers/Unique_Ptr.png -------------------------------------------------------------------------------- /Sem. 13/Solutions/README.md: -------------------------------------------------------------------------------- 1 | Всички решения и примери за шаблони се намират папките solutions и examples на предишния семинар [Sem. 12](../../Sem.%2012/) 2 | -------------------------------------------------------------------------------- /Github Workflow/images/fetch-origin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Github Workflow/images/fetch-origin.png -------------------------------------------------------------------------------- /Github Workflow/images/star-the-repo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Github Workflow/images/star-the-repo.png -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 11/Solutions/Collections/image.png -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Base/FileReader.cpp: -------------------------------------------------------------------------------- 1 | #include "FileReader.h" 2 | 3 | FileReader::FileReader(const MyString& fileName) : fileName(fileName) 4 | { } -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Base/FileWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "FileWriter.h" 2 | 3 | FileWriter::FileWriter(const MyString& fileName) : fileName(fileName) 4 | { } -------------------------------------------------------------------------------- /Sem. 12/SmartPointers/Shared_Weak1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 12/SmartPointers/Shared_Weak1.png -------------------------------------------------------------------------------- /Sem. 12/SmartPointers/shared_counter1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 12/SmartPointers/shared_counter1.png -------------------------------------------------------------------------------- /Sem. 12/SmartPointers/shared_counter2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Sem. 12/SmartPointers/shared_counter2.png -------------------------------------------------------------------------------- /Sem. 15/Solutions/ArrayManipulations/Containers/IntVector.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "MyVector.hpp" 3 | 4 | class IntVector : public Vector 5 | { }; 6 | 7 | -------------------------------------------------------------------------------- /Github Workflow/images/repository-fork-settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Github Workflow/images/repository-fork-settings.png -------------------------------------------------------------------------------- /Github Workflow/images/repository-settings-menu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeorgiTerziev02/Object-oriented_programming_FMI/HEAD/Github Workflow/images/repository-settings-menu.png -------------------------------------------------------------------------------- /Sem. 08/Examples/Static function/other.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void test() { 4 | std::cout << "Hello"; 5 | } 6 | 7 | static void test2() { 8 | std::cout << "Hello2"; 9 | } -------------------------------------------------------------------------------- /Sem. 12/Solutions/Farm/Animal.cpp: -------------------------------------------------------------------------------- 1 | #include "Animal.h" 2 | 3 | Animal::Animal(AnimalType type) : type(type) 4 | { } 5 | 6 | AnimalType Animal::getType() const { 7 | return type; 8 | } -------------------------------------------------------------------------------- /Sem. 12/Examples/TemplateInTwoFiles/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Test.h" 3 | 4 | int main() { 5 | Test t; 6 | Test t2; // not mentioned in the cpp file 7 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/PureNumbersCollections/PureNumbersCollection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "DynamicCollection.h" 4 | 5 | class PureNumbersCollection : public DynamicCollection { }; -------------------------------------------------------------------------------- /Sem. 12/Solutions/Farm/Dog.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Animal.h" 3 | class Dog : public Animal { 4 | public: 5 | Dog(); 6 | void roar() const override; 7 | Animal* clone() const override; 8 | }; 9 | -------------------------------------------------------------------------------- /Sem. 12/Examples/TemplateInTwoFiles/Test.cpp: -------------------------------------------------------------------------------- 1 | #include "Test.h" 2 | #include 3 | 4 | template 5 | Test::Test() { 6 | std::cout << "fdsfs"; 7 | } 8 | 9 | template Test; // !!! -------------------------------------------------------------------------------- /Sem. 12/Solutions/Farm/Cat.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Animal.h" 3 | class Cat : public Animal { 4 | public: 5 | Cat(); 6 | void roar() const override; 7 | Animal* clone() const override; 8 | }; 9 | 10 | -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/ConfigEnums/ConfigEnums.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum FigureType 4 | { 5 | KingFigure, 6 | QueenFigure, 7 | KnightFigure, 8 | BishopFigure, 9 | PawnFigure, 10 | RookFigure 11 | }; -------------------------------------------------------------------------------- /Sem. 02/Solutions/CsvTestFile/table.csv: -------------------------------------------------------------------------------- 1 | Ime,Familiya,Fakulteten nomer,Email 2 | Antonio,Petrov,0MI0600123,antoniosg@abv.bg.bg 3 | Ekaterina,Dimova,2MI0800100,kati@mail.bg 4 | Katerina,Vassileva,1MI0800555,katerina@gmail.com -------------------------------------------------------------------------------- /Sem. 09/Examples/OptimizedSwap.cpp: -------------------------------------------------------------------------------- 1 | #include "MyString.h" 2 | #include 3 | 4 | void swap(MyString& l, MyString& r) { 5 | MyString temp = std::move(l); 6 | l = std::move(r); 7 | r = std::move(l); 8 | } -------------------------------------------------------------------------------- /Sem. 12/Solutions/swap.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | template 5 | void swap(T& lhs, T& rhs) { 6 | T temp = std::move(lhs); 7 | lhs = std::move(rhs); 8 | rhs = std::move(lhs); 9 | } -------------------------------------------------------------------------------- /Sem. 10/Solutions/PersonTeacherStudent/Student.cpp: -------------------------------------------------------------------------------- 1 | #include "Student.h" 2 | 3 | Student::Student(const char* name, int age, size_t fn) : Person(name, age), fn(fn) 4 | { } 5 | 6 | size_t Student::getFn() const { 7 | return fn; 8 | } -------------------------------------------------------------------------------- /Sem. 01/Examples/IsLittleEndian.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool isLittleEndian() { 4 | union { 5 | unsigned int i; 6 | char c[sizeof(i)]; 7 | } test; 8 | test.i = 1; 9 | 10 | return test.i; 11 | } -------------------------------------------------------------------------------- /Sem. 05/Examples/LinkerErrorDefiningFunctionInHeader/other.cpp: -------------------------------------------------------------------------------- 1 | #include "header.h" 2 | 3 | // the preprocess will include the content of header.h here 4 | // which will result in a definition of f 5 | // which will exist in the obj file of other.cpp -------------------------------------------------------------------------------- /Sem. 08/Solutions/StringPool/ExampleUsage.cpp: -------------------------------------------------------------------------------- 1 | #include "ImmutableString/ImmutableString.h" 2 | 3 | int main() { 4 | ImmutableString s1("abc"); 5 | ImmutableString s2("abc"); 6 | ImmutableString s3 = s1; 7 | 8 | std::cout << (s1 == s2 && s2 == s3); 9 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Common/StreamHelper/StreamHelper.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace StreamHelper { 5 | int getCharCount(std::ifstream& in, char ch); 6 | 7 | size_t getFileSize(std::ifstream& in); 8 | }; 9 | -------------------------------------------------------------------------------- /Sem. 02/Compilation/Examples/LinkerErrorDefiningFunctionInHeader/other.cpp: -------------------------------------------------------------------------------- 1 | #include "header.h" 2 | 3 | // the preprocess will include the content of header.h here 4 | // which will result in a definition of f 5 | // which will exist in the obj file of other.cpp -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Csv/CsvFileReader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "FileReader.h" 3 | class CSVFileReader : public FileReader { 4 | public: 5 | CSVFileReader(const MyString& fileName); 6 | void read(int*& arr, size_t& size) const override; 7 | }; -------------------------------------------------------------------------------- /Sem. 01/Examples/DifferentFormats.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // writing in binary format 5 | int a = 0b101; 6 | // writing in hex format 7 | int b = 0xFF; 8 | std::cout << a << std::endl; 9 | std::cout << b << std::endl; 10 | } 11 | -------------------------------------------------------------------------------- /Sem. 05/Examples/LinkerErrorIncludingCpp/main.cpp: -------------------------------------------------------------------------------- 1 | #include "other.cpp" 2 | // the preprocess will include the content of other.cpp here 3 | // which will result of a seconds definition of f 4 | 5 | int main() { 6 | f(); // linker error => multiple definition of f 7 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Array/ArrayFileReader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "FileReader.h" 3 | 4 | class ArrayFileReader : public FileReader { 5 | public: 6 | ArrayFileReader(const MyString& fileName); 7 | void read(int*& arr, size_t& size) const override; 8 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Binary/BinaryFileReader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "FileReader.h" 3 | class BinaryFileReader : public FileReader { 4 | public: 5 | BinaryFileReader(const MyString& fileName); 6 | void read(int*& arr, size_t& size) const override; 7 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Csv/CsvFileWriter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "FileWriter.h" 3 | 4 | class CSVFileWriter : public FileWriter { 5 | public: 6 | CSVFileWriter(const MyString& fileName); 7 | void write(const int* arr, size_t size) const override; 8 | }; -------------------------------------------------------------------------------- /Sem. 10/Solutions/PersonTeacherStudent/Student.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Person.h" 3 | 4 | class Student : public Person { 5 | private: 6 | size_t fn = 0; 7 | public: 8 | Student(const char* name, int age, size_t fn); 9 | size_t getFn() const; 10 | }; 11 | 12 | -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/FigureFactory/FigureFactory.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Figures/FigureBase/Figure.h" 3 | #include "../ConfigEnums/ConfigEnums.h" 4 | 5 | class FigureFactory 6 | { 7 | public: 8 | static Figure* createFigure(bool isWhite, FigureType); //enum 9 | }; -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ChessBoard/ChessBoard.h" 3 | using namespace std; 4 | 5 | //ШАХ 6 | 7 | int main() 8 | { 9 | ChessBoard c; 10 | 11 | c.print(); 12 | 13 | c.moveFigure(0, 1, 2, 0); 14 | 15 | c.print(); 16 | } -------------------------------------------------------------------------------- /Sem. 02/Compilation/Examples/LinkerErrorIncludingCpp/main.cpp: -------------------------------------------------------------------------------- 1 | #include "other.cpp" 2 | // the preprocess will include the content of other.cpp here 3 | // which will result of a seconds definition of f 4 | 5 | int main() { 6 | f(); // linker error => multiple definition of f 7 | } -------------------------------------------------------------------------------- /Sem. 08/Solutions/CarDealerShip/Car/Car.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Car { 4 | private: 5 | // and many more members here 6 | size_t someValue; 7 | public: 8 | Car(); 9 | Car(size_t value); 10 | size_t getValue() const; 11 | void setValue(size_t value); 12 | }; 13 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Binary/BinaryFileWriter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "FileWriter.h" 3 | 4 | class BinaryFileWriter : public FileWriter { 5 | public: 6 | BinaryFileWriter(const MyString& fileName); 7 | void write(const int* arr, size_t size) const override; 8 | }; -------------------------------------------------------------------------------- /Sem. 08/Solutions/CarDealerShip/Car/Car.cpp: -------------------------------------------------------------------------------- 1 | #include "Car.h" 2 | 3 | Car::Car() : Car(0) { } 4 | Car::Car(size_t value): someValue(value) { } 5 | size_t Car::getValue() const { 6 | return someValue; 7 | } 8 | void Car::setValue(size_t value) { 9 | someValue = value; 10 | } -------------------------------------------------------------------------------- /Sem. 12/Solutions/Farm/Cat.cpp: -------------------------------------------------------------------------------- 1 | #include "Cat.h" 2 | #include 3 | 4 | Cat::Cat() : Animal(AnimalType::Cat) { } 5 | 6 | void Cat::roar() const { 7 | std::cout << "meow, moew" << std::endl; 8 | } 9 | 10 | Animal* Cat::clone() const { 11 | return new Cat(*this); 12 | } -------------------------------------------------------------------------------- /Sem. 12/Solutions/Farm/Dog.cpp: -------------------------------------------------------------------------------- 1 | #include "Dog.h" 2 | #include 3 | 4 | Dog::Dog() : Animal(AnimalType::Dog) { } 5 | 6 | void Dog::roar() const { 7 | std::cout << "Bark, bark" << std::endl; 8 | } 9 | 10 | Animal* Dog::clone() const { 11 | return new Dog(*this); 12 | } -------------------------------------------------------------------------------- /Sem. 09/Solutions/CarDealerShip (with move)/Car/Car.cpp: -------------------------------------------------------------------------------- 1 | #include "Car.h" 2 | 3 | Car::Car() : Car(0) { } 4 | Car::Car(size_t value): someValue(value) { } 5 | size_t Car::getValue() const { 6 | return someValue; 7 | } 8 | void Car::setValue(size_t value) { 9 | someValue = value; 10 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Array/ArrayFileWriter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "FileWriter.h" 3 | 4 | class ArrayFileWriter : public FileWriter { 5 | public: 6 | ArrayFileWriter(const MyString& fileName); 7 | void write(const int* arr, size_t size) const override; 8 | }; 9 | 10 | -------------------------------------------------------------------------------- /Sem. 04/Examples/ConstructorDestructor/ConflictingNamesInConstructor.cpp: -------------------------------------------------------------------------------- 1 | struct Test { 2 | int a; 3 | 4 | Test(int a) { 5 | // incorrect 6 | a = a; // a is the parameter of the constructor, not the data member 7 | // correct 8 | this->a = a; 9 | } 10 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/Collection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Collection { 4 | public: 5 | virtual void add(int) = 0; 6 | virtual void remove(int) = 0; 7 | virtual size_t count(int) const = 0; 8 | virtual bool contains(int) const = 0; 9 | virtual ~Collection() = default; 10 | }; 11 | -------------------------------------------------------------------------------- /Sem. 05/Examples/Preprocessor/PreprocessorDirectives.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // macros 4 | #define OOP { 5 | #define END } 6 | 7 | int main() OOP 8 | #if _DEBUG 9 | std::cout << "Debug mode" << std::endl; 10 | #else 11 | std::cout << "Release mode" << std::endl; 12 | #endif 13 | END -------------------------------------------------------------------------------- /Sem. 07/Solutions/PrimeIterator/ExampleUsage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "PrimeIterator.h" 4 | 5 | int main() { 6 | // print all the prime numbers from which are less than 100 7 | for (PrimeIterator it(2); *it < 100; it++) { 8 | std::cout << *it << " "; 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /Sem. 08/Examples/Static function/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void test(); 4 | void test2(); 5 | 6 | int main() { 7 | test(); 8 | // linker error 9 | test2(); // can't call test2 because it's static 10 | // it is related to the other .cpp and can not be called by another 11 | } -------------------------------------------------------------------------------- /Sem. 07/Examples/Wrappers/CharWrapper/CharWrapper.cpp: -------------------------------------------------------------------------------- 1 | #include "CharWrapper.h" 2 | 3 | CharWrapper::CharWrapper(char value) : value(value) {} 4 | 5 | char CharWrapper::getValue() const { 6 | return value; 7 | } 8 | 9 | CharWrapper::operator IntWrapper() const { 10 | return IntWrapper(value); 11 | } -------------------------------------------------------------------------------- /Sem. 02/Compilation/Examples/Preprocessor/PreprocessorDirectives.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // macros 4 | #define OOP { 5 | #define END } 6 | 7 | int main() OOP 8 | #if _DEBUG 9 | std::cout << "Debug mode" << std::endl; 10 | #else 11 | std::cout << "Release mode" << std::endl; 12 | #endif 13 | END -------------------------------------------------------------------------------- /Sem. 08/Examples/UnhandledException/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void test() { 4 | throw 3; 5 | } 6 | 7 | int main() { 8 | try { 9 | test(); 10 | } 11 | // does not catch ints 12 | catch(const std::exception& e) { 13 | std::cout << e.what(); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/Shapes/Rectangle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Shape.h" 3 | 4 | class Rectangle : public Shape 5 | { 6 | public: 7 | Rectangle(int x1, int y1, int x3, int y3); 8 | double getArea() const override; 9 | double getPerimeter() const override; 10 | bool isPointIn(int x, int y) const override; 11 | }; -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/King/King.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../FigureBase/Figure.h" 3 | 4 | class King : public Figure 5 | { 6 | 7 | public: 8 | King(bool isWhite); 9 | bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const override; 10 | void print() const override; 11 | }; -------------------------------------------------------------------------------- /Sem. 01/Examples/AccessingPadding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #pragma warning (disable:4996) 4 | 5 | struct A { 6 | int a; 7 | char s; 8 | char f[]; 9 | }; 10 | 11 | int main() { 12 | A a; 13 | 14 | char* ptr = (char*)&a; 15 | strcpy(ptr, "1234567"); 16 | std::cout << a.s; 17 | std::cout << a.f; 18 | } 19 | -------------------------------------------------------------------------------- /Sem. 05/Solutions/TextCensorer/SI/TextCensorer/TextCensorer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class TextCensorer { 4 | private: 5 | bool (*shouldCensor)(char) = nullptr; 6 | 7 | public: 8 | TextCensorer(bool (*shouldCensor)(char)); 9 | 10 | void print(const char* str) const; 11 | void print(char a) const; 12 | }; -------------------------------------------------------------------------------- /Sem. 04/Solutions/Interval/HelperFunctions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace HelperFunctions { 4 | bool isPrime(int number); 5 | 6 | int reverseNumber(int number); 7 | 8 | bool isPalindrome(int number); 9 | 10 | bool isPowOfTwo(int n); 11 | 12 | bool containsOnlyDistinctDigits(int number); 13 | }; -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/Knight/Knight.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../FigureBase/Figure.h" 3 | 4 | class Knight : public Figure 5 | { 6 | 7 | public: 8 | Knight(bool isWhite); 9 | bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const override; 10 | void print() const override; 11 | }; -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/Rook/Rook.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../FigureBase/Figure.h" 3 | 4 | class Rook : virtual public Figure 5 | { 6 | 7 | public: 8 | Rook(bool isWhite); 9 | bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const override; 10 | void print() const override; 11 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/Shapes/Circle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Shape.h" 3 | 4 | class Circle : public Shape 5 | { 6 | double radius; 7 | 8 | public: 9 | Circle(int x, int y, double radius); 10 | double getArea() const override; 11 | double getPerimeter() const override; 12 | bool isPointIn(int x, int y) const override; 13 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/Shapes/Triangle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Shape.h" 3 | 4 | class Triangle : public Shape 5 | { 6 | public: 7 | Triangle(int x1, int y1, int x2, int y2, int x3, int y3); 8 | double getArea() const override; 9 | double getPerimeter() const override; 10 | bool isPointIn(int x, int y) const override; 11 | }; -------------------------------------------------------------------------------- /Sem. 09/Solutions/ClassOfStudents/Student.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "MyString.h" 3 | 4 | class Student { 5 | private: 6 | MyString name; 7 | int age; 8 | public: 9 | Student(const MyString& name, int age); 10 | Student(MyString&& name, int age); 11 | 12 | const MyString& getName() const; 13 | unsigned getAge() const; 14 | }; -------------------------------------------------------------------------------- /Sem. 12/Examples/CallingMemberFunctionWithNullptr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Test { 4 | int a = 5; 5 | 6 | void f1() { 7 | std::cout << a; 8 | } 9 | 10 | void f2() { 11 | std::cout << 1; 12 | } 13 | }; 14 | 15 | int main() { 16 | Test* t = nullptr; 17 | // t->f1(); // error is thrown 18 | t->f2(); // 1 19 | } -------------------------------------------------------------------------------- /Sem. 02/Examples/FlushExample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const char FILE_NAME = "test.txt"; 4 | 5 | int main() { 6 | 7 | std::ofstream outfile(FILE_NAME); 8 | 9 | for (int n = 0; n < 100; ++n) 10 | { 11 | outfile << n; 12 | } 13 | // put debugger here and check the file 14 | outfile.close(); 15 | } 16 | -------------------------------------------------------------------------------- /Sem. 06/Examples/NotCompilingExamples.cpp: -------------------------------------------------------------------------------- 1 | class Test { 2 | private: 3 | // Compile error: Destructor is private 4 | ~Test() { } 5 | }; 6 | 7 | class Test2 { 8 | public: 9 | Test2() {} 10 | // Compile error: Copy constructor can't have parameter of type Test2 11 | // should be Test&! 12 | Test2(const Test2 t) {} 13 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Base/FileReader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Common/MyString/MyString.h" 3 | 4 | class FileReader { 5 | protected: 6 | MyString fileName; 7 | 8 | public: 9 | FileReader(const MyString& fileName); 10 | virtual void read(int*& arr, size_t& size) const = 0; 11 | virtual ~FileReader() = default; 12 | }; -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/Queen/Queen.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Bishop/Bishop.h" 3 | #include "../Rook/Rook.h" 4 | 5 | class Queen : public Bishop, public Rook 6 | { 7 | public: 8 | Queen(bool isWhite); 9 | bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const; 10 | void print() const; 11 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Base/FileWriter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Common/MyString/MyString.h" 3 | 4 | class FileWriter { 5 | protected: 6 | MyString fileName; 7 | 8 | public: 9 | FileWriter(const MyString& fileName); 10 | virtual void write(const int* arr, size_t size) const = 0; 11 | virtual ~FileWriter() = default; 12 | }; -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/Bishop/Bishop.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../FigureBase/Figure.h" 3 | 4 | class Bishop : virtual public Figure 5 | { 6 | 7 | public: 8 | Bishop(bool isWhite); 9 | bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const override; 10 | 11 | void print() const override; 12 | 13 | }; -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/Pawn/Pawn.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../FigureBase/Figure.h" 3 | 4 | class Pawn : virtual public Figure 5 | { 6 | bool isFirstMove; 7 | public: 8 | Pawn(bool isWhite); 9 | bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const override; 10 | void print() const override; 11 | }; -------------------------------------------------------------------------------- /Sem. 10/Examples/WRONG-AccessDerivedFromBase.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // violation example 4 | struct C { 5 | int x = 4; 6 | }; 7 | 8 | struct D : public C { 9 | int y = 5; 10 | }; 11 | 12 | void get(C& c) { 13 | int* ptr = (int*)&c; 14 | ++ptr; 15 | std::cout << *ptr; 16 | } 17 | 18 | int main() { 19 | D d; 20 | get(d); 21 | } 22 | -------------------------------------------------------------------------------- /Sem. 10/Solutions/Bar/AlcoholDrink/AlcoholDrink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Drink.h" 3 | 4 | class AlcoholDrink : public Drink { 5 | private: 6 | size_t alcPer; 7 | public: 8 | AlcoholDrink(); 9 | AlcoholDrink(const MyString& name, size_t ml, size_t alcPer); 10 | 11 | void setAlchoholPer(size_t alcPer); 12 | size_t getAlcPer() const; 13 | }; -------------------------------------------------------------------------------- /Sem. 15/Solutions/ArrayManipulations/Command/SwapCommand.cpp: -------------------------------------------------------------------------------- 1 | #include "SwapCommand.h" 2 | 3 | SwapCommand::SwapCommand(IntVector& v, size_t from, size_t to) 4 | : VectorCommand(v), from(from), to(to) { } 5 | 6 | void SwapCommand::execute() { 7 | std::swap(v[from], v[to]); 8 | } 9 | 10 | void SwapCommand::undo() { 11 | std::swap(v[to], v[from]); 12 | } -------------------------------------------------------------------------------- /Sem. 10/Examples/PrivateInheritancePassingToFunction.cpp: -------------------------------------------------------------------------------- 1 | class Base { 2 | public: 3 | int x; 4 | }; 5 | 6 | class Derived : private Base { }; 7 | 8 | void f(Base& b) { } 9 | 10 | int main() { 11 | Derived d; 12 | f(d); // Compilation error 13 | // Conversion from Derived to Base is inaccessible 14 | // because of private inheritance 15 | } -------------------------------------------------------------------------------- /Sem. 15/Solutions/ArrayManipulations/Command/VectorCommand.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "../Containers/IntVector.h" 4 | 5 | class VectorCommand { 6 | protected: 7 | IntVector& v; 8 | public: 9 | VectorCommand(IntVector& v); 10 | virtual ~VectorCommand() = default; 11 | virtual void execute() = 0; 12 | virtual void undo() = 0; 13 | }; -------------------------------------------------------------------------------- /Sem. 08/Solutions/Singleton/WithStaticVariable/Singleton.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Singleton { 4 | private: 5 | int dummy; 6 | Singleton(); 7 | public: 8 | Singleton(const Singleton& other) = delete; 9 | Singleton& operator=(const Singleton& other) = delete; 10 | 11 | int getDummy() const; 12 | void print(); 13 | 14 | static Singleton& getInstance(); 15 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/PureNumbersCollections/Normal/NormalCollection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "PureNumbersCollection.h" 4 | 5 | class NormalCollection : public PureNumbersCollection { 6 | public: 7 | void add(int) override; 8 | void remove(int) override; 9 | size_t count(int) const override; 10 | bool contains(int) const override; 11 | }; 12 | 13 | -------------------------------------------------------------------------------- /Sem. 05/Examples/LinkerErrorDefiningFunctionInHeader/main.cpp: -------------------------------------------------------------------------------- 1 | #include "header.h" 2 | 3 | // the preprocess will include the content of header.h here 4 | // which will result in a definition of f 5 | // which will exist in the obj file of main.cpp 6 | 7 | int main() { 8 | f(); // linker error => multiple definition of f 9 | // f is defined both in main.obj and other.obj 10 | } -------------------------------------------------------------------------------- /Sem. 05/Solutions/TextCensorer/main.cpp: -------------------------------------------------------------------------------- 1 | #include "TextCensorer/TextCensorer.h" 2 | #include "Text/Text.h" 3 | 4 | int main() { 5 | TextCensorer c1; 6 | c1.addLetter('e'); 7 | Text t1(c1, "hello"); 8 | Text t2(c1, "eeeeee"); 9 | 10 | t1.print(); 11 | t2.print(); 12 | 13 | // What happens if we make c1 dynamic, delete it here and then call t1.print()? 14 | } -------------------------------------------------------------------------------- /Sem. 15/Solutions/ArrayManipulations/Command/SortCommand.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "VectorCommand.h" 3 | #include "IntVector.h" 4 | 5 | class SortCommand : public VectorCommand { 6 | const IntVector* snapshot = nullptr; 7 | 8 | static void sortVector(IntVector& v); 9 | public: 10 | SortCommand(IntVector& v); 11 | 12 | void execute() override; 13 | void undo() override; 14 | }; -------------------------------------------------------------------------------- /Sem. 02/Examples/PrintFileContentWithGet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | constexpr char FILE_NAME[] = "OOP.cpp"; 5 | 6 | int main() { 7 | std::ifstream in(FILE_NAME); 8 | 9 | if (!in.is_open()) { 10 | return -1; 11 | } 12 | 13 | char symbol; 14 | while (!in.eof()) { 15 | in.get(symbol); 16 | std::cout << symbol; 17 | } 18 | 19 | in.close(); 20 | } -------------------------------------------------------------------------------- /Sem. 10/Solutions/Bar/Drink/Drink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "MyStringSso.h" 3 | 4 | class Drink { 5 | private: 6 | MyString name; 7 | size_t ml = 0; 8 | 9 | public: 10 | Drink() {}; 11 | Drink(const MyString& name, size_t ml); 12 | 13 | const MyString& getName() const; 14 | size_t getMl() const; 15 | 16 | void setName(const MyString& name); 17 | void setMl(size_t ml); 18 | }; -------------------------------------------------------------------------------- /Sem. 12/Solutions/Farm/Animal.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | enum class AnimalType { 4 | Dog = 0, 5 | Cat = 1 6 | }; 7 | 8 | class Animal { 9 | private: 10 | AnimalType type; 11 | 12 | public: 13 | Animal(AnimalType type); 14 | virtual void roar() const = 0; 15 | virtual Animal* clone() const = 0; 16 | AnimalType getType() const; 17 | 18 | virtual ~Animal() = default; 19 | }; 20 | 21 | -------------------------------------------------------------------------------- /Sem. 02/Compilation/Examples/LinkerErrorDefiningFunctionInHeader/main.cpp: -------------------------------------------------------------------------------- 1 | #include "header.h" 2 | 3 | // the preprocess will include the content of header.h here 4 | // which will result in a definition of f 5 | // which will exist in the obj file of main.cpp 6 | 7 | int main() { 8 | f(); // linker error => multiple definition of f 9 | // f is defined both in main.obj and other.obj 10 | } -------------------------------------------------------------------------------- /Sem. 08/Examples/Static variable/NaturalNumbersGenerator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t naturalNumbersGenerator() { 4 | static size_t current = 1; 5 | 6 | return current++; 7 | } 8 | 9 | int main() { 10 | std::cout << naturalNumbersGenerator(); 11 | std::cout << naturalNumbersGenerator(); 12 | std::cout << naturalNumbersGenerator(); 13 | std::cout << naturalNumbersGenerator(); 14 | } -------------------------------------------------------------------------------- /Sem. 10/Examples/FinalAndInheritanceChain.cpp: -------------------------------------------------------------------------------- 1 | class A { 2 | public: 3 | int a; 4 | }; 5 | 6 | class B : public A { 7 | void test() { a; } 8 | }; 9 | 10 | class C /*final*/ : protected B { 11 | void test() { a; } 12 | }; 13 | 14 | class D : private C { 15 | void test() { a; } 16 | }; 17 | 18 | int main() { 19 | A a; 20 | a.a; 21 | B b; 22 | b.a; 23 | C c; 24 | // c.a; //inaccessible 25 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Common/FilePath/FilePath.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../MyString/MyString.h" 3 | #include "../StringView/StringView.h" 4 | 5 | class FilePath { 6 | private: 7 | StringView name; 8 | StringView extension; 9 | 10 | public: 11 | FilePath(const MyString& fileName); 12 | 13 | const StringView& getName() const; 14 | const StringView& getExtension() const; 15 | }; -------------------------------------------------------------------------------- /Sem. 12/Solutions/Farm/AnimalFactory.cpp: -------------------------------------------------------------------------------- 1 | #include "AnimalFactory.h" 2 | #include "Dog.h" 3 | #include "Cat.h" 4 | #include 5 | 6 | Animal* animalFactory(AnimalType type) { 7 | switch (type) { 8 | case AnimalType::Dog: return new Dog(); 9 | break; 10 | case AnimalType::Cat: return new Cat(); 11 | break; 12 | } 13 | 14 | throw std::invalid_argument("Invalid animal type"); 15 | } -------------------------------------------------------------------------------- /Sem. 07/Examples/Wrappers/CharWrapper/CharWrapper.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../IntWrapper/IntWrapper.h" 3 | 4 | class CharWrapper { 5 | private: 6 | char value; 7 | public: 8 | CharWrapper(char value); 9 | char getValue() const; 10 | 11 | // Conversion operator 12 | // If we add explicit here you won't be able to call this implicitly 13 | operator IntWrapper() const; 14 | }; -------------------------------------------------------------------------------- /Sem. 08/Solutions/Singleton/WithPointer/Singleton.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Singleton { 4 | private: 5 | int dummy; 6 | Singleton(); 7 | 8 | static Singleton* instance; 9 | public: 10 | Singleton(const Singleton& other) = delete; 11 | Singleton& operator=(const Singleton& other) = delete; 12 | 13 | int getDummy() const; 14 | void print(); 15 | 16 | static Singleton& getInstance(); 17 | }; -------------------------------------------------------------------------------- /Sem. 08/Solutions/Singleton/WithStaticVariable/Singleton.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.h" 2 | #include 3 | 4 | Singleton::Singleton() : dummy(5) {} 5 | 6 | int Singleton::getDummy() const { 7 | return dummy; 8 | } 9 | 10 | void Singleton::print() { 11 | std::cout << "Hello"; 12 | } 13 | 14 | Singleton& Singleton::getInstance() { 15 | static Singleton instance; 16 | return instance; 17 | } 18 | -------------------------------------------------------------------------------- /Sem. 09/Solutions/ClassOfStudents/Student.cpp: -------------------------------------------------------------------------------- 1 | #include "Student.h" 2 | 3 | Student::Student(const MyString& name, unsigned age) 4 | : name(name), age(age) { } 5 | 6 | Student::Student(MyString&& name, unsigned age) 7 | : name(std::move(name)), age(age) { } 8 | 9 | const MyString& Student::getName() const { 10 | return name; 11 | } 12 | 13 | unsigned Student::getAge() const { 14 | return age; 15 | } -------------------------------------------------------------------------------- /Sem. 08/Solutions/SelfCountingClass/SelfCounting.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class SelfCounting { 4 | private: 5 | static size_t createdInstances; 6 | static size_t livingInstances; 7 | 8 | int localTempVariable; 9 | public: 10 | static size_t getLivingInstances(); 11 | static size_t getCreatedInstances(); 12 | 13 | SelfCounting(); 14 | SelfCounting(const SelfCounting& other); 15 | ~SelfCounting(); 16 | }; -------------------------------------------------------------------------------- /Sem. 10/Solutions/Bar/Drink/Drink.cpp: -------------------------------------------------------------------------------- 1 | #include "Drink.h" 2 | 3 | Drink::Drink(const MyString& name, size_t ml) 4 | : name(name), ml(ml) {} 5 | 6 | const MyString& Drink::getName() const { 7 | return name; 8 | } 9 | size_t Drink::getMl() const { 10 | return ml; 11 | } 12 | 13 | void Drink::setName(const MyString& name) { 14 | this->name = name; 15 | } 16 | void Drink::setMl(size_t ml) { 17 | this->ml = ml; 18 | } -------------------------------------------------------------------------------- /Sem. 05/Solutions/TextCensorer/IS/TextCensorer/TextCensorer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace TextCensorerConstants { 4 | constexpr int LETTERS_COUNT = 26; 5 | }; 6 | 7 | class TextCensorer { 8 | private: 9 | bool forbiddenLetters[TextCensorerConstants::LETTERS_COUNT] = { }; 10 | 11 | void printLetter(char a) const; 12 | public: 13 | void addLetter(char a); 14 | void print(const char* str) const; 15 | }; -------------------------------------------------------------------------------- /Sem. 10/Solutions/Bar/AlcoholDrink/AlcoholDrink.cpp: -------------------------------------------------------------------------------- 1 | #include "AlcoholDrink.h" 2 | 3 | AlcoholDrink::AlcoholDrink() : Drink() {} 4 | 5 | AlcoholDrink::AlcoholDrink(const MyString& name, size_t ml, size_t alcPer) 6 | : Drink(name, ml), alcPer(alcPer) {} 7 | 8 | void AlcoholDrink::setAlchoholPer(size_t alcPer) { 9 | this->alcPer = alcPer; 10 | } 11 | 12 | size_t AlcoholDrink::getAlcPer() const { 13 | return alcPer; 14 | } -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/Rook/Rook.cpp: -------------------------------------------------------------------------------- 1 | #include "Rook.h" 2 | 3 | Rook::Rook(bool isWhite) : Figure(isWhite, RookFigure) 4 | {} 5 | bool Rook::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const 6 | { 7 | return currentX == destX || currentY == destY; 8 | } 9 | void Rook::print() const 10 | { 11 | if (getIsWhite()) 12 | std::cout << 'R'; 13 | else 14 | std::cout << 'r'; 15 | } 16 | -------------------------------------------------------------------------------- /Sem. 08/Solutions/Singleton/WithPointer/ExampleUsage.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.h" 2 | #include 3 | 4 | int main() { 5 | Singleton& s1 = Singleton::getInstance(); 6 | Singleton& s2 = Singleton::getInstance(); 7 | 8 | // Error: trying CC is deleted 9 | // Singleton s3 = Singleton::getInstance(); 10 | std::cout << (&s1 == &s2); // true 11 | 12 | s1.print(); 13 | std::cout << (s1.getDummy() == s2.getDummy()); 14 | } -------------------------------------------------------------------------------- /Sem. 10/Solutions/UserAccountPseudoCode.cpp: -------------------------------------------------------------------------------- 1 | #include "MyString/MyString.h" 2 | 3 | class NotAuthenticatedUser { 4 | public: 5 | void playGameForFun() { } 6 | }; 7 | 8 | class AuthenticatedUser : public NotAuthenticatedUser { 9 | MyString password; 10 | MyString name; 11 | public: 12 | void playGameForRank() { } 13 | }; 14 | 15 | class PaidAccount : public AuthenticatedUser { 16 | public: 17 | void buyPayToWinStuff() { } 18 | }; -------------------------------------------------------------------------------- /Sem. 08/Solutions/Singleton/WithStaticVariable/ExampleUsage.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.h" 2 | #include 3 | 4 | int main() { 5 | Singleton& s1 = Singleton::getInstance(); 6 | Singleton& s2 = Singleton::getInstance(); 7 | 8 | // Error: trying CC is deleted 9 | // Singleton s3 = Singleton::getInstance(); 10 | std::cout << (&s1 == &s2); // true 11 | 12 | s1.print(); 13 | std::cout << (s1.getDummy() == s2.getDummy()); 14 | } -------------------------------------------------------------------------------- /Sem. 11/Examples/PureVirtualDestructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // This class is abstract because it has a pure virtual destructor 4 | class Test { 5 | public: 6 | int a; 7 | 8 | void f() { 9 | std::cout << "hello"; 10 | } 11 | 12 | virtual ~Test() = 0; 13 | }; 14 | 15 | Test::~Test() { } 16 | 17 | class Derived : public Test { }; 18 | 19 | int main() { 20 | // Test t; // error abstract class 21 | Derived d; 22 | d.a; 23 | } -------------------------------------------------------------------------------- /Sem. 06/Solutions/User/ExampleUsage.cpp: -------------------------------------------------------------------------------- 1 | #include "User.h" 2 | 3 | int main() { 4 | User u1("abcde", "fff"); 5 | 6 | // Think before using it 7 | // Two cases: 8 | 9 | // this is A NEW STRING which is a copy of the string 10 | MyString userName = u1.getUserName(); 11 | 12 | // this users the provided const& from the getter 13 | const MyString& userName = u1.getUserName(); 14 | 15 | std::cout << userName.c_str(); 16 | } -------------------------------------------------------------------------------- /Sem. 13/Examples/MemoryLeakWithVector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Base { 4 | virtual ~Base() = default; 5 | }; 6 | 7 | struct Derived1 : Base { }; 8 | 9 | struct Derived2 : Base { }; 10 | 11 | 12 | int main() { 13 | std::vector objects; 14 | 15 | objects.push_back(new Derived1()); 16 | objects.push_back(new Derived2()); 17 | 18 | // Memory leak 19 | // we should iterate through the pointers and delete them 20 | } -------------------------------------------------------------------------------- /Sem. 08/Solutions/SelfCountingClass/ExampleUsage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SelfCounting.h" 3 | 4 | int main() { 5 | SelfCounting s; 6 | SelfCounting* ptr = new SelfCounting(); 7 | std::cout << SelfCounting::getLivingInstances() << " " << SelfCounting::getCreatedInstances() << std::endl; 8 | delete ptr; 9 | SelfCounting s2 = s; 10 | std::cout << SelfCounting::getLivingInstances() << " " << SelfCounting::getCreatedInstances() << std::endl; 11 | } -------------------------------------------------------------------------------- /Sem. 05/Solutions/TextCensorer/IS/Text/Text.cpp: -------------------------------------------------------------------------------- 1 | #include "Text.h" 2 | #include 3 | 4 | void Text::setBuffer(const char *val) { 5 | if (strlen(val) > TextConstants::BUFF_SIZE) { 6 | return; 7 | } 8 | strcpy(buff, val); 9 | } 10 | 11 | Text::Text(TextCensorer &textCensorer, const char *buff) 12 | : censorer(textCensorer) 13 | { 14 | setBuffer(buff); 15 | } 16 | 17 | void Text::print() const { 18 | censorer.print(buff); 19 | } -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/King/King.cpp: -------------------------------------------------------------------------------- 1 | #include "King.h" 2 | #include 3 | 4 | King::King(bool isWhite) : Figure(isWhite, KingFigure) 5 | {} 6 | bool King::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const 7 | { 8 | return abs((int)currentX - (int)destX) <= 1 && abs((int)currentY - (int)destY) <= 1; 9 | } 10 | void King::print() const 11 | { 12 | if (getIsWhite()) 13 | std::cout << '%'; 14 | else 15 | std::cout << '$'; 16 | } -------------------------------------------------------------------------------- /Sem. 12/Solutions/SharedAndWeak/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "WeakPtr.hpp" 3 | 4 | 5 | struct A 6 | { 7 | A() { 8 | std::cout << "A()" << std::endl; 9 | } 10 | 11 | ~A() { 12 | std::cout << "~A()" << std::endl; 13 | } 14 | 15 | }; 16 | int main() 17 | { 18 | WeakPtr wp; 19 | { 20 | SharedPtr pt(new A()); 21 | wp = pt; 22 | 23 | std::cout << wp.expired() << std::endl; 24 | } 25 | 26 | std::cout << wp.expired() << std::endl; 27 | } 28 | -------------------------------------------------------------------------------- /Sem. 04/Examples/ConstructorDestructor/WhenIsConstructoCalled.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Test{ 4 | int a; 5 | Test(int a): a(a) { 6 | std::cout << "Test(int a) " << a << std::endl; 7 | } 8 | 9 | ~Test() { 10 | std::cout << "~Test() " << a << std::endl; 11 | } 12 | }; 13 | 14 | int main() { 15 | Test t(5); // Test(int a) 5 16 | Test* ptr = new Test(10); // Test(int a) 10 17 | delete ptr; // ~Test() 10 18 | } // ~Test() 5 -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/Bishop/Bishop.cpp: -------------------------------------------------------------------------------- 1 | #include "Bishop.h" 2 | #include 3 | 4 | Bishop::Bishop(bool isWhite) : Figure(isWhite, RookFigure) 5 | {} 6 | bool Bishop::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const 7 | { 8 | return abs((int)currentX - (int)destX) == abs((int)currentY - (int)destY); 9 | } 10 | void Bishop::print() const 11 | { 12 | if (getIsWhite()) 13 | std::cout << 'B'; 14 | else 15 | std::cout << 'b'; 16 | } 17 | -------------------------------------------------------------------------------- /Sem. 08/Solutions/Singleton/WithPointer/Singleton.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.h" 2 | #include 3 | 4 | Singleton* Singleton::instance = nullptr; 5 | 6 | Singleton::Singleton() : dummy(5) {} 7 | 8 | int Singleton::getDummy() const { 9 | return dummy; 10 | } 11 | 12 | void Singleton::print() { 13 | std::cout << "Hello"; 14 | } 15 | 16 | Singleton& Singleton::getInstance() { 17 | if (!instance) { 18 | instance = new Singleton(); 19 | } 20 | 21 | return *instance; 22 | } 23 | -------------------------------------------------------------------------------- /Sem. 12/Examples/ObjectBehavingAsFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | void invoke(T& obj) { 5 | obj(); 6 | } 7 | 8 | void printHello() { 9 | std::cout << "Hello from some func" << std::endl; 10 | } 11 | 12 | struct ObjAsFunc { 13 | int a; 14 | void operator()() const { 15 | std::cout << "Hello from obj " << a << std::endl; 16 | } 17 | }; 18 | 19 | int main() { 20 | invoke(printHello); 21 | ObjAsFunc oaf; 22 | oaf.a = 5; 23 | invoke(oaf); 24 | } 25 | -------------------------------------------------------------------------------- /Sem. 15/Design Patterns/Behavioural/README.md: -------------------------------------------------------------------------------- 1 | # Behavioral 2 | Улесняват комункацията между обекти 3 | 4 | ## Command 5 | Използва обект за да капсулира цялата логика за изпълнение на дадено действие 6 | - Плюсове 7 | - Single Responsibility 8 | - Open/Closed 9 | - Минуси 10 | - Нов layer в приложението 11 | 12 | TODO: 13 | 14 | ## **Iterator** 15 | Позволява да се обхожда колекция без да се знае от какъв тип е тя 16 | 17 | TODO: 18 | 19 | ## Visitor 20 | TODO: 21 | 22 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/Interval/IntervalCollection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Collection.h" 4 | #include "DynamicCollection.h" 5 | 6 | class IntervalCollection : public DynamicCollection { 7 | private: 8 | int start; 9 | int end; 10 | public: 11 | IntervalCollection(int start, int end); 12 | unsigned intervalLength() const; 13 | 14 | void add(int) override; 15 | void remove(int) override; 16 | size_t count(int) const override; 17 | bool contains(int) const override; 18 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Binary/BinaryFileWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "BinaryFileWriter.h" 2 | #include 3 | 4 | BinaryFileWriter::BinaryFileWriter(const MyString& fileName) : FileWriter(fileName) 5 | { } 6 | 7 | void BinaryFileWriter::write(const int* arr, size_t size) const { 8 | std::ofstream out(fileName.c_str(), std::ios::binary); 9 | if (!out.is_open()) { 10 | throw std::exception("Couldn't open file"); 11 | } 12 | 13 | out.write((const char*)arr, size * sizeof(int)); 14 | } -------------------------------------------------------------------------------- /Sem. 01/Examples/IPv4Address.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | union IPv4Address { 4 | unsigned int address; 5 | unsigned char octets[4]; // [sizeof(address)] 6 | }; 7 | 8 | void printIpAddress(const IPv4Address& ipAddress) { 9 | for (size_t i = 0; i < 4; i++) { 10 | std::cout << (int)ipAddress.octets[i]; 11 | if (i != 3) { 12 | std::cout << "."; 13 | } 14 | } 15 | } 16 | 17 | int main() { 18 | IPv4Address myAddress; 19 | myAddress.address = 16777343; 20 | 21 | printIpAddress(myAddress); 22 | } -------------------------------------------------------------------------------- /Sem. 05/Examples/ExplicitConstructor/main.cpp: -------------------------------------------------------------------------------- 1 | class ExplicitConstructor { 2 | public: 3 | explicit ExplicitConstructor(int a) { } 4 | }; 5 | 6 | class ImplicitConstructor { 7 | public: 8 | ImplicitConstructor(int a) { } 9 | }; 10 | 11 | int main() { 12 | // ExplicitConstructor ec = 5; // error: cannot convert 'int' to 'ExplicitConstructor' in initialization 13 | ExplicitConstructor ec2 = ExplicitConstructor(5); 14 | ExplicitConstructor ec3(5); 15 | ImplicitConstructor ic = 5; // OK 16 | } -------------------------------------------------------------------------------- /Sem. 02/Solutions/PrintSourceCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | constexpr char FILE_NAME[] = "OOP.cpp"; 5 | constexpr int BUFFER_SIZE = 1024; 6 | 7 | int main() { 8 | std::ifstream in(FILE_NAME); 9 | 10 | if (!in.is_open()) { 11 | std::cout << "Could not open file" << std::endl; 12 | return -1; 13 | } 14 | 15 | char buffer[BUFFER_SIZE]; 16 | while (!in.eof()) { 17 | in.getline(buffer, BUFFER_SIZE); 18 | std::cout << buffer << std::endl; 19 | } 20 | 21 | in.close(); 22 | } -------------------------------------------------------------------------------- /Sem. 02/Examples/AdditionalExamples.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void getExample() { 4 | char c; 5 | std::cin.putback('a'); 6 | std::cin.get(c); 7 | std::cout << c; 8 | } 9 | 10 | void readExample() { 11 | char c; 12 | std::cin.putback('a').putback('b'); 13 | char info[3] = { '\0' }; 14 | std::cin.read(info, 2); 15 | std::cout << info; 16 | } 17 | 18 | void infiniteLoop() { 19 | char c; 20 | std::cin.putback('a'); 21 | while (std::cin >> c) { 22 | std::cout << c; 23 | std::cin.putback(c); 24 | } 25 | } -------------------------------------------------------------------------------- /Sem. 05/Examples/InitializerListVsAssigningInConstructor/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Test1 { 4 | int x; 5 | public: 6 | Test1(int x) : x(x) { 7 | std::cout << this->x; 8 | } 9 | }; 10 | 11 | class Test2 { 12 | int x; 13 | public: 14 | Test2(int x) { 15 | std::cout << this->x; 16 | this->x = x; 17 | std::cout << this->x; 18 | } 19 | }; 20 | 21 | int main() { 22 | Test1 t1(5); // 5 23 | Test2 t2(5); // -8589934605 (garbage value) then 5 24 | } -------------------------------------------------------------------------------- /Sem. 15/Solutions/ArrayManipulations/Command/SwapCommand.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "VectorCommand.h" 3 | #include 4 | 5 | class SwapCommand : public VectorCommand { 6 | size_t from, to; 7 | // It will be good to have some isExecuted logic 8 | // because we should not execute undo() if execute() is not called 9 | //bool isExecuted = false; 10 | public: 11 | SwapCommand(IntVector& v, size_t from, size_t to); 12 | 13 | virtual void execute() override; 14 | virtual void undo() override; 15 | }; -------------------------------------------------------------------------------- /Sem. 04/Examples/ConstructorDestructor/Explicit.cpp: -------------------------------------------------------------------------------- 1 | class ExplicitConstructor { 2 | public: 3 | explicit ExplicitConstructor(int a) { } 4 | }; 5 | 6 | class ImplicitConstructor { 7 | public: 8 | ImplicitConstructor(int a) { } 9 | }; 10 | 11 | int main() { 12 | // ExplicitConstructor ec = 5; // error: cannot convert 'int' to 'ExplicitConstructor' in initialization 13 | ExplicitConstructor ec2 = ExplicitConstructor(5); 14 | ExplicitConstructor ec3(5); 15 | ImplicitConstructor ic = 5; // OK 16 | } 17 | -------------------------------------------------------------------------------- /Sem. 06/Solutions/MyString/ExampleUsage.cpp: -------------------------------------------------------------------------------- 1 | #include "MyString.h" 2 | 3 | int main() { 4 | // Constructor(const char* str) 5 | MyString s1("abcde"); 6 | 7 | // Constructor(const char* str) 8 | MyString s2 = "dasda"; 9 | 10 | // Copy constructor 11 | MyString s3 = s1; 12 | 13 | // Copy assignment operator 14 | MyString s4; 15 | s4 = s2; 16 | 17 | // Consturctor for the right side and op=!!! 18 | // "aaa" becomes MyString and after that it is copied to s3 19 | s3 = "aaa"; 20 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/PureNumbersCollections/Sorted/SortedCollection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "PureNumbersCollection.h" 4 | 5 | class SortedCollection : public PureNumbersCollection { 6 | private: 7 | // returns the leftmost index 8 | int lowerBound(int) const; 9 | 10 | // returns the rightmost index 11 | int upperBound(int) const; 12 | public: 13 | void add(int) override; 14 | void remove(int) override; 15 | size_t count(int) const override; 16 | bool contains(int) const override; 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /Sem. 04/Examples/ConstructorDestructor/InitializerListVsAssigningInConstr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Test1 { 4 | int x; 5 | public: 6 | Test1(int x) : x(x) { 7 | std::cout << this->x; 8 | } 9 | }; 10 | 11 | class Test2 { 12 | int x; 13 | public: 14 | Test2(int x) { 15 | std::cout << this->x; 16 | this->x = x; 17 | std::cout << this->x; 18 | } 19 | }; 20 | 21 | int main() { 22 | Test1 t1(5); // 5 23 | Test2 t2(5); // -8589934605 (garbage value) then 5 24 | } -------------------------------------------------------------------------------- /Sem. 04/Examples/mutable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Test { 4 | mutable unsigned xAccessCounter = 0; 5 | int x; 6 | public: 7 | Test(int x) : x(x) { } 8 | 9 | int getX() const { 10 | xAccessCounter++; // can be done because is mutable 11 | return x; 12 | } 13 | 14 | unsigned getXAccessCounter() const { 15 | return xAccessCounter; 16 | } 17 | }; 18 | 19 | int main() { 20 | Test t(5); 21 | t.getX(); 22 | 23 | std::cout << t.getXAccessCounter(); // 1 24 | } -------------------------------------------------------------------------------- /Sem. 13/Examples/DiamondProblemDuplicatedBaseClass.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int global = 1; 4 | 5 | struct A { 6 | int a; 7 | A() { 8 | a = global; 9 | global++; 10 | } 11 | }; 12 | 13 | struct B : /* virtual */ A { 14 | void printAFromB() { 15 | std::cout << a; 16 | } 17 | }; 18 | 19 | struct C : /* virtual */ A { 20 | void printAFromC() { 21 | std::cout << a; 22 | } 23 | }; 24 | 25 | struct D : B, C { }; 26 | 27 | int main() { 28 | D d; 29 | 30 | d.printAFromB(); 31 | d.printAFromC(); 32 | } 33 | -------------------------------------------------------------------------------- /Sem. 04/Examples/Student/Student.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Student { 4 | private: 5 | char* name = nullptr; 6 | int grade; 7 | 8 | void copyName(const char* newName); 9 | public: 10 | Student(int grade, const char* name); 11 | 12 | // delete generated functions by the compiler 13 | Student(const Student& other) = delete; 14 | Student& operator=(const Student& other) = delete; 15 | 16 | ~Student(); 17 | 18 | const char* getName() const; 19 | void setName(const char* newName); 20 | 21 | // TODO: grade get and set 22 | }; -------------------------------------------------------------------------------- /Sem. 05/Examples/Mutable/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Test { 4 | mutable unsigned xAccessCounter = 0; 5 | int x; 6 | public: 7 | Test(int x) : x(x) { } 8 | 9 | int getX() const { 10 | xAccessCounter++; // can be done because is mutable 11 | return x; 12 | } 13 | 14 | unsigned getXAccessCounter() const { 15 | return xAccessCounter; 16 | } 17 | }; 18 | 19 | int main() { 20 | Test t(5); 21 | t.getX(); 22 | 23 | std::cout << t.getXAccessCounter(); // 1 24 | } -------------------------------------------------------------------------------- /Sem. 04/Examples/ConstructorDestructor/StructPointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Test { 4 | public: 5 | Test() { 6 | std::cout << "Hello, from constructor!" << std::endl; 7 | } 8 | 9 | ~Test() { 10 | std::cout << "Hello, from destructor!" << std::endl; 11 | } 12 | }; 13 | 14 | int main() { 15 | // initilizing pointer which points to nullptr address 16 | Test* ptr = nullptr; // No constructor is called! 17 | Test* ptr2 = new Test(); // Constructor is called! 18 | delete ptr2; 19 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Csv/CsvFileWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "CSVFileWriter.h" 2 | #include 3 | 4 | CSVFileWriter::CSVFileWriter(const MyString& fileName) : FileWriter(fileName) 5 | { } 6 | 7 | void CSVFileWriter::write(const int* arr, size_t size) const { 8 | std::ofstream out(fileName.c_str()); 9 | if (!out.is_open()) { 10 | throw std::exception("Couldn't open file"); 11 | } 12 | 13 | for (size_t i = 0; i < size; i++) { 14 | out << arr[i]; 15 | 16 | if (i != size - 1) { 17 | out << ','; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/Queen/Queen.cpp: -------------------------------------------------------------------------------- 1 | #include "Queen.h" 2 | 3 | Queen::Queen(bool isWhite) : Bishop(isWhite), Rook(isWhite), Figure(isWhite, QueenFigure) 4 | {} 5 | 6 | bool Queen::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const 7 | { 8 | return Bishop::canBeMoved(currentX, currentY, destX, destY) 9 | || Rook::canBeMoved(currentX, currentY, destX, destY); 10 | } 11 | 12 | void Queen::print() const 13 | { 14 | if (getIsWhite()) 15 | std::cout << 'Q'; 16 | else 17 | std::cout << 'q'; 18 | } -------------------------------------------------------------------------------- /Sem. 05/Examples/StructPointerConstructorsAndDestructors/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Test { 4 | public: 5 | Test() { 6 | std::cout << "Hello, from constructor!" << std::endl; 7 | } 8 | 9 | ~Test() { 10 | std::cout << "Hello, from destructor!" << std::endl; 11 | } 12 | }; 13 | 14 | int main() { 15 | // initilizing pointer which points to nullptr address 16 | Test* ptr = nullptr; // No constructor is called! 17 | Test* ptr2 = new Test(); // Constructor is called! 18 | delete ptr2; 19 | } -------------------------------------------------------------------------------- /Sem. 06/Solutions/User/User.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../MyString/MyString.h" 3 | 4 | class User { 5 | private: 6 | MyString userName; 7 | MyString password; 8 | public: 9 | // No need of Big4(rule of three) the default behaviour is enough 10 | User(const MyString& userName, const MyString& password); 11 | 12 | const MyString& getUserName() const; 13 | // No getter for password, we don't want to expose it 14 | 15 | void setUserName(const MyString& userName); 16 | void setPassword(const MyString& password); 17 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/Shapes/Circle.cpp: -------------------------------------------------------------------------------- 1 | #include "Circle.h" 2 | 3 | namespace { 4 | constexpr double PI = 3.1415; 5 | } 6 | 7 | Circle::Circle(int x, int y, double radius) : Shape(1), radius(radius) 8 | { 9 | setPoint(0, x, y); 10 | } 11 | 12 | double Circle::getArea() const 13 | { 14 | return PI * radius * radius; 15 | } 16 | double Circle::getPerimeter() const 17 | { 18 | return 2 * PI * radius; 19 | } 20 | bool Circle::isPointIn(int x, int y) const 21 | { 22 | Shape::Point p(x, y); 23 | return p.getDistance(getPointAtIndex(0)) <= radius; 24 | } -------------------------------------------------------------------------------- /Sem. 05/Solutions/TextCensorer/SI/TextCensorer/TextCensorer.cpp: -------------------------------------------------------------------------------- 1 | #include "TextCensorer.h" 2 | #include 3 | 4 | TextCensorer::TextCensorer(bool (*shouldCensor)(char)) : shouldCensor(shouldCensor) {} 5 | 6 | void TextCensorer::print(const char *str) const { 7 | if (!str) { 8 | return; 9 | } 10 | 11 | while (*str) { 12 | print(*str); 13 | ++str; 14 | } 15 | 16 | std::cout << std::endl; 17 | } 18 | 19 | void TextCensorer::print(char a) const { 20 | std::cout << (shouldCensor(a) ? '*' : a); 21 | } -------------------------------------------------------------------------------- /Sem. 02/Solutions/GetFileSize.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | size_t getFileSize(std::ifstream& in) { 5 | size_t current = in.tellg(); 6 | in.seekg(0, std::ios::end); 7 | size_t size = in.tellg(); 8 | in.seekg(current, std::ios::beg); 9 | 10 | return size; 11 | } 12 | 13 | int main() { 14 | std::ifstream in("OOP.cpp"); 15 | if (!in.is_open()) { 16 | std::cout << "Could not open file" << std::endl; 17 | return -1; 18 | } 19 | 20 | std::cout << getFileSize(in) << std::endl; 21 | 22 | in.close(); 23 | } -------------------------------------------------------------------------------- /Sem. 08/Examples/Exceptions-ConstructorsAndDestructors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct A { 4 | static int counter; 5 | A() { 6 | std::cout << "A()"; 7 | counter++; 8 | if (counter == 3) { 9 | throw std::exception("fa"); 10 | } 11 | } 12 | 13 | ~A() { 14 | std::cout << "~A"; 15 | } 16 | }; 17 | 18 | int A::counter = 0; 19 | 20 | int main() { 21 | try { 22 | A arr[5]; // A()A()A() 23 | } 24 | catch (const std::exception&) { // if no try catch => only the constructors will be printed! 25 | // ~A~A 26 | std::cout << ""; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sem. 05/Solutions/TextCensorer/IS/Text/Text.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../TextCensorer/TextCensorer.h" 3 | 4 | namespace TextConstants { 5 | constexpr int BUFF_SIZE = 30; 6 | }; 7 | 8 | class Text { 9 | char buff[TextConstants::BUFF_SIZE + 1] = ""; 10 | // If we change it to Censor* we can then change 11 | // it every time with setter and also can have a default constructor 12 | TextCensorer& censorer; 13 | 14 | void setBuffer(const char* val); 15 | public: 16 | Text(TextCensorer& textCensorer, const char* buff); 17 | void print() const; 18 | }; -------------------------------------------------------------------------------- /Sem. 08/Examples/Static variable/StaticVariable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #pragma warning(disable: 4996) 6 | 7 | struct Test { 8 | Test() { 9 | std::cout << "Test"; 10 | } 11 | }; 12 | 13 | static Test a; 14 | 15 | Test& f() { 16 | static Test a; // created only once, on the first call 17 | return a; 18 | } 19 | 20 | Test& f2() { 21 | static Test a; // created only once, on the first call 22 | return a; 23 | } 24 | 25 | int main() { 26 | Test& t1 = f(); 27 | Test& t2 = f2(); 28 | 29 | std::cout << (&t1 != &t2); 30 | } -------------------------------------------------------------------------------- /Sem. 10/Solutions/Sets/SetByCriteria.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Bitset.h" 3 | 4 | class SetByCriteria : private Bitset { 5 | private: 6 | struct Criteria { 7 | bool(*includes)(unsigned n); 8 | bool(*excludes)(unsigned n); 9 | } criteria; 10 | 11 | void fillSet(); 12 | public: 13 | SetByCriteria(unsigned maxNumber, bool(*includes)(unsigned n), bool(*excludes)(unsigned n)); 14 | 15 | void setIncludes(bool(*includes)(unsigned n)); 16 | void setExcludes(bool(*excludes)(unsigned n)); 17 | 18 | bool contains(unsigned int n) const; 19 | void print() const; 20 | }; 21 | 22 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Array/ArrayFileWriter.cpp: -------------------------------------------------------------------------------- 1 | #include "ArrayFileWriter.h" 2 | #include 3 | 4 | ArrayFileWriter::ArrayFileWriter(const MyString& fileName) : FileWriter(fileName) 5 | { } 6 | 7 | void ArrayFileWriter::write(const int* arr, size_t size) const { 8 | std::ofstream out(fileName.c_str()); 9 | if (!out.is_open()) { 10 | throw std::exception("Couldn't open file"); 11 | } 12 | 13 | out << '['; 14 | for (size_t i = 0; i < size; i++) { 15 | out << arr[i]; 16 | if (i != size - 1) { 17 | out << ' '; 18 | } 19 | } 20 | 21 | out << ']'; 22 | } -------------------------------------------------------------------------------- /Sem. 13/Examples/VirtualInheritanceConstructors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct A { 4 | A() { 5 | std::cout << "A"; 6 | } 7 | 8 | virtual ~A() { 9 | std::cout << "~A"; 10 | } 11 | }; 12 | 13 | struct B : virtual A { 14 | B() { 15 | std::cout << "B"; 16 | } 17 | 18 | ~B() { 19 | std::cout << "~B"; 20 | } 21 | }; 22 | 23 | struct C : virtual A { 24 | C() { 25 | std::cout << "C"; 26 | } 27 | 28 | ~C() { 29 | std::cout << "~C"; 30 | } 31 | }; 32 | 33 | int main() { 34 | C c; 35 | } -------------------------------------------------------------------------------- /Sem. 12/Solutions/FunctionsAverage.hpp: -------------------------------------------------------------------------------- 1 | template 2 | class FunctionsAverage { 3 | private: 4 | const F1& first; 5 | const F2& second; 6 | public: 7 | FunctionsAverage(const F1& first, const F2& second); 8 | double average(int n) const; 9 | }; 10 | 11 | template 12 | FunctionsAverage::FunctionsAverage(const F1& first, const F2& second) 13 | :first(first), second(second) 14 | { } 15 | 16 | template 17 | int FunctionsAverage::average(int n) const { 18 | return (first(n) + second(n)) / 2.0; 19 | } 20 | -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/Pawn/Pawn.cpp: -------------------------------------------------------------------------------- 1 | #include "Pawn.h" 2 | #include 3 | Pawn::Pawn(bool isWhite) : Figure(isWhite, PawnFigure), isFirstMove(true) 4 | {} 5 | 6 | bool Pawn::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const 7 | { 8 | if (getIsWhite()) 9 | return currentY - 1 == destY && abs((int)currentX - (int)destX) <= 1; 10 | else 11 | return currentY + 1 == destY && abs((int)currentX - (int)destX) <= 1; 12 | } 13 | 14 | void Pawn::print() const 15 | { 16 | if (getIsWhite()) 17 | std::cout << 'P'; 18 | else 19 | std::cout << 'p'; 20 | } -------------------------------------------------------------------------------- /Sem. 13/Examples/VirtualInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct A { 4 | A() { 5 | std::cout << "A Default" << std::endl; 6 | } 7 | A(int b) { 8 | std::cout << "A With parameter" << std::endl; 9 | } 10 | }; 11 | 12 | struct B : virtual public A { 13 | B() { 14 | std::cout << "B constructor" << std::endl; 15 | } 16 | }; 17 | 18 | struct C : public B { 19 | C() : B(), A(6) { 20 | std::cout << "C constructor"; 21 | } 22 | }; 23 | 24 | struct D : public C { 25 | D() : A(5) { 26 | std::cout << "D constructor"; 27 | } 28 | }; 29 | 30 | int main1() { 31 | D d; 32 | } 33 | -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/FigureBase/Figure.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "../../ConfigEnums/ConfigEnums.h" 4 | 5 | class Figure 6 | { 7 | bool isWhite; 8 | FigureType type; 9 | 10 | public: 11 | Figure(bool isWhite, FigureType type) :isWhite(isWhite), type(type) 12 | {} 13 | 14 | virtual bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const = 0; 15 | virtual void print() const = 0; 16 | 17 | bool getIsWhite() const { return isWhite; }; 18 | FigureType getType() const { return type; } 19 | 20 | virtual ~Figure() = default; 21 | }; -------------------------------------------------------------------------------- /Sem. 15/Solutions/ArrayManipulations/CommandExecutor.h: -------------------------------------------------------------------------------- 1 | #include "Containers/MyStack.hpp" 2 | #include "Containers/MyQueue.hpp" 3 | #include "Command/VectorCommand.h" 4 | 5 | struct CommandExecutor { 6 | private: 7 | MyQueue toExecute; 8 | MyStack executed; 9 | public: 10 | CommandExecutor() = default; 11 | CommandExecutor(const CommandExecutor&) = delete; 12 | CommandExecutor& operator=(const CommandExecutor&) = delete; 13 | ~CommandExecutor(); 14 | 15 | void add(VectorCommand* c); 16 | void execute(); 17 | void executeAll(); 18 | void undo(); 19 | }; 20 | -------------------------------------------------------------------------------- /Sem. 08/Solutions/CarDealerShip/ArrayOfObjectsImplementation/CarDealership.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Car/Car.h" 3 | 4 | class CarDealership { 5 | private: 6 | Car* cars; 7 | size_t count; 8 | size_t capacity; 9 | 10 | void resize(); 11 | public: 12 | CarDealership(); 13 | CarDealership(const CarDealership&); 14 | CarDealership& operator=(const CarDealership&); 15 | ~CarDealership(); 16 | 17 | void addCar(const Car&); 18 | void deleteAtIndex(size_t index); 19 | const Car& getCar(size_t index) const; 20 | 21 | private: 22 | void free(); 23 | void copyFrom(const CarDealership&); 24 | }; 25 | -------------------------------------------------------------------------------- /Sem. 06/Solutions/User/User.cpp: -------------------------------------------------------------------------------- 1 | #include "User.h" 2 | 3 | User::User(const MyString &userName, const MyString &password) 4 | : userName(userName), password(password) 5 | { } 6 | 7 | const MyString& User::getUserName() const { 8 | return userName; 9 | } 10 | 11 | void User::setUserName(const MyString& userName) { 12 | if(userName.getLength() == 0) { 13 | return; 14 | } 15 | 16 | this->userName = userName; 17 | } 18 | void User::setPassword(const MyString& password) { 19 | if(password.getLength() == 0) { 20 | return; 21 | } 22 | 23 | this->password = password; 24 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Common/FilePath/FilePath.cpp: -------------------------------------------------------------------------------- 1 | #include "FilePath.h" 2 | 3 | FilePath::FilePath(const MyString& fileName) { 4 | const char* begin = fileName.c_str(); 5 | const char* iter = begin; 6 | const char* end = fileName.c_str() + fileName.getLength(); 7 | 8 | while (iter != end && *iter != '.') { 9 | iter++; 10 | } 11 | 12 | name = StringView(begin, iter); 13 | extension = StringView(iter, end); 14 | } 15 | 16 | const StringView& FilePath::getName() const { 17 | return name; 18 | } 19 | 20 | const StringView& FilePath::getExtension() const { 21 | return extension; 22 | } -------------------------------------------------------------------------------- /Sem. 01/Examples/Struct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Point{ 4 | int x = 0, y = 0; 5 | }; 6 | 7 | void readPoint(Point& p) { 8 | std::cin >> p.x >> p.y; 9 | } 10 | 11 | void printPoint(const Point& p) { 12 | std::cout << p.x << " " << p.y << std::endl; 13 | } 14 | 15 | int main() { 16 | Point p{1,2}; 17 | Point p2 = {1,2}; 18 | Point* p3 = new Point{1,2}; 19 | Point* arr = new Point[5]; // why having constant size in dynamic arr initialization is this bad? 20 | 21 | std::cout << p.x << " " << p.y << std::endl; 22 | 23 | delete p3; 24 | delete[] arr; 25 | } 26 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Array/ArrayFileReader.cpp: -------------------------------------------------------------------------------- 1 | #include "ArrayFileReader.h" 2 | #include 3 | #include "../StreamHelper/StreamHelper.h" 4 | 5 | ArrayFileReader::ArrayFileReader(const MyString& fileName) : FileReader(fileName) { } 6 | 7 | void ArrayFileReader::read(int*& arr, size_t& size) const { 8 | std::ifstream in(fileName.c_str()); 9 | if (!in.is_open()) { 10 | throw std::exception("Couldn't open file"); 11 | } 12 | 13 | size = StreamHelper::getCharCount(in, ' ') + 1; 14 | delete[] arr; 15 | arr = new int[size]; 16 | for (size_t i = 0; i < size; i++) { 17 | in >> arr[i]; 18 | } 19 | } -------------------------------------------------------------------------------- /Sem. 06/Solutions/MyString/MyString.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class MyString { 4 | private: 5 | char* str; 6 | size_t length; 7 | 8 | MyString(size_t length); 9 | public: 10 | MyString(); 11 | MyString(const char* str); 12 | MyString(const MyString& other); 13 | MyString& operator=(const MyString& other); 14 | ~MyString(); 15 | 16 | size_t getLength() const; 17 | const char* c_str() const; 18 | 19 | char& at(size_t index); 20 | char at(size_t index) const; 21 | // const char& at(size_t index) const; alternative 22 | private: 23 | void copyDynamic(const MyString& other); 24 | void freeDynamic(); 25 | }; 26 | 27 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Binary/BinaryFileReader.cpp: -------------------------------------------------------------------------------- 1 | #include "BinaryFileReader.h" 2 | #include 3 | #include "../StreamHelper/StreamHelper.h" 4 | 5 | BinaryFileReader::BinaryFileReader(const MyString& fileName) : FileReader(fileName) 6 | { } 7 | 8 | void BinaryFileReader::read(int*& arr, size_t& size) const { 9 | std::ifstream in(fileName.c_str(), std::ios::binary); 10 | if (!in.is_open()) { 11 | throw std::exception("Couldn't open file"); 12 | } 13 | 14 | size = StreamHelper::getFileSize(in) / sizeof(int); 15 | delete[] arr; 16 | arr = new int[size]; 17 | in.read((char*)arr, size * sizeof(int)); 18 | } -------------------------------------------------------------------------------- /Sem. 09/Solutions/CarDealerShip (with move)/Car/Car.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Car { 4 | private: 5 | // and many more members here 6 | // and imagine there is a dynamic memory 7 | // that is allocated in the constructor 8 | // (in order the move stuff to be usefull) 9 | size_t someValue; 10 | public: 11 | Car(); 12 | Car(size_t value); 13 | Car(const Car& other) = default; 14 | Car& operator=(const Car& other) = default; 15 | Car(Car&& other) noexcept = default; 16 | Car& operator=(Car&& other) noexcept = default; 17 | ~Car() = default; 18 | 19 | size_t getValue() const; 20 | void setValue(size_t value); 21 | }; 22 | -------------------------------------------------------------------------------- /Sem. 08/Solutions/CarDealerShip/ArrayOfPointersImplementation/CarDealership.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Car/Car.h" 3 | 4 | class CarDealership { 5 | private: 6 | Car** cars; 7 | size_t count; 8 | size_t capacity; 9 | 10 | void resize(); 11 | public: 12 | CarDealership(); 13 | CarDealership(const CarDealership& other); 14 | CarDealership& operator=(const CarDealership& other); 15 | ~CarDealership(); 16 | 17 | void addCar(const Car& newCar); 18 | void deleteAtIndex(size_t index); 19 | const Car& getCar(size_t index) const; 20 | 21 | private: 22 | void free(); 23 | void copyFrom(const CarDealership& other); 24 | }; 25 | -------------------------------------------------------------------------------- /Sem. 01/Revision_Examples/PtrAndRefs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void test(int* ptr) { 4 | *ptr = 10; 5 | } 6 | void test2(int& ref) { 7 | ref = 10; 8 | } 9 | void test3(int arr[]) { 10 | arr[0] = 10; 11 | } 12 | void test4(int* arr) { 13 | arr[0] = 10; 14 | } 15 | void test5(int ref) { 16 | ref = 10; 17 | } 18 | int main() { 19 | int a = 5; 20 | int b = 10; 21 | 22 | int& aRef = a; 23 | 24 | aRef++; 25 | 26 | int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 27 | int* arrPtr = arr; 28 | arrPtr++; 29 | arrPtr++; 30 | 31 | std::cout << *arrPtr; 32 | 33 | arrPtr = nullptr; 34 | 35 | // charptr++ vs intptr++; 36 | } 37 | -------------------------------------------------------------------------------- /Sem. 12/Solutions/Farm/Farm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Animal.h" 3 | 4 | class Farm { 5 | private: 6 | Animal** animals; 7 | size_t count; 8 | size_t capacity; 9 | 10 | void resize(); 11 | public: 12 | Farm(); 13 | Farm(const Farm& other); 14 | Farm(Farm&& other) noexcept; 15 | Farm& operator=(const Farm& other); 16 | Farm& operator=(Farm&& other) noexcept; 17 | ~Farm(); 18 | 19 | void addAnimal(AnimalType type); 20 | AnimalType getAnimalTypeAtIndex(size_t index) const; 21 | void roarAll() const; 22 | 23 | private: 24 | void free(); 25 | void copyFrom(const Farm& other); 26 | void moveFrom(Farm&& other); 27 | }; 28 | 29 | -------------------------------------------------------------------------------- /Sem. 03/Examples/ReusableFunctionForReadingWritingDynamicCharArr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // strlen 3 | 4 | namespace HelperFunctions { 5 | void readDynamicCharArrayFromBinaryFile(std::ifstream& in, char*& str) { 6 | size_t strLength; 7 | in.read((char*)&strLength, sizeof(strLength)); 8 | str = new char[strLength + 1]; 9 | in.read(str, strLength); 10 | str[strLength] = '\0'; 11 | } 12 | 13 | void writeDynamicCharArrayToBinaryFile(std::ofstream& out, const char* str) { 14 | size_t strLength = strlen(str); 15 | out.write((const char*)&strLength, sizeof(strLength)); 16 | out.write(str, strLength); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Sem. 10/Solutions/PersonTeacherStudent/Person.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | class Person { 3 | private: 4 | char* name = nullptr; 5 | int age = 0; 6 | 7 | void copyName(const char* name); 8 | public: 9 | Person(const char* name, int age); 10 | Person(const Person& other); 11 | Person(Person&& other) noexcept; 12 | Person& operator=(const Person& other); 13 | Person& operator=(Person&& other) noexcept; 14 | ~Person(); 15 | 16 | const char* getName() const; 17 | int getAge() const; 18 | 19 | void print() const; 20 | 21 | private: 22 | void copyFrom(const Person& other); 23 | void moveFrom(Person&& other) noexcept; 24 | void free(); 25 | }; 26 | 27 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Csv/CsvFileReader.cpp: -------------------------------------------------------------------------------- 1 | #include "CSVFileReader.h" 2 | #include 3 | #include 4 | #include "../StreamHelper/StreamHelper.h" 5 | 6 | CSVFileReader::CSVFileReader(const MyString& fileName) : FileReader(fileName) 7 | { } 8 | 9 | void CSVFileReader::read(int*& arr, size_t& size) const { 10 | std::ifstream in(fileName.c_str()); 11 | if (!in.is_open()) { 12 | throw std::exception("Couldn't open file"); 13 | } 14 | 15 | size = StreamHelper::getCharCount(in, ',') + 1; 16 | delete[] arr; 17 | arr = new int[size]; 18 | for (size_t i = 0; i < size; i++) { 19 | in >> arr[i]; 20 | in.ignore(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Sem. 13/Examples/MultipleInhDestructors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Base1 { 4 | Base1() { 5 | std::cout << "Base1"; 6 | } 7 | 8 | 9 | virtual ~Base1() { 10 | std::cout << "~Base1"; 11 | } 12 | }; 13 | 14 | struct Base2 { 15 | Base2() { 16 | std::cout << "Base2"; 17 | } 18 | 19 | ~Base2() { 20 | std::cout << "~Base2"; 21 | } 22 | }; 23 | 24 | struct Der : Base2, Base1 { 25 | virtual void d() { } 26 | 27 | ~Der() { 28 | std::cout << "~Der"; 29 | } 30 | }; 31 | 32 | int main() { 33 | Base1* ptr = new Der(); 34 | Base2* ptr2 = new Der(); 35 | 36 | delete ptr; // ~Base1~Base2~Der 37 | delete ptr2; // ~Base2 38 | } -------------------------------------------------------------------------------- /Sem. 12/Solutions/ShapeCollection/Rectangle/Rectangle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Shape/Shape.h" 4 | 5 | class Rectangle : public Shape { 6 | public: 7 | Rectangle(int x1, int y1, int x3, int y3); 8 | 9 | Shape* clone() const override; 10 | double getArea() const override; 11 | double getPer() const override; 12 | bool isPointIn(int x, int y) const override; 13 | 14 | bool intersectsWith(const Shape* other) const override; 15 | 16 | bool intersectsWithTriangle(const Triangle* other) const override; 17 | bool intersectsWithRect(const Rectangle* other) const override; 18 | bool intersectsWithCircle(const Circle* other) const override; 19 | }; 20 | 21 | -------------------------------------------------------------------------------- /Sem. 05/Solutions/EventCollection/EventCollection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Event/Event.h" 3 | 4 | namespace EventCollectionConstants { 5 | constexpr int MAX_EVENT_SIZE = 30; 6 | } 7 | 8 | class EventCollection { 9 | private: 10 | Event events[EventCollectionConstants::MAX_EVENT_SIZE]; 11 | size_t size = 0; 12 | 13 | EventCollection eventsOnDate(const Date& date) const; 14 | int findEventByName(const char* name) const; 15 | 16 | public: 17 | bool addEvent(const Event& event); 18 | bool removeEvent(const char* name); 19 | 20 | const Event& getByName(const char* name) const; 21 | EventCollection maxEvents(const Date& date) const; 22 | 23 | void print() const; 24 | }; -------------------------------------------------------------------------------- /Sem. 12/Solutions/ShapeCollection/Circle/Circle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Shape/Shape.h" 3 | 4 | class Circle : public Shape { 5 | private: 6 | double radius; 7 | public: 8 | Circle(int x, int y, double radius); 9 | 10 | Shape* clone() const override; 11 | 12 | double getArea() const override; 13 | double getPer() const override; 14 | bool isPointIn(int x, int y) const override; 15 | 16 | bool intersectsWith(const Shape* other) const override; 17 | 18 | bool intersectsWithTriangle(const Triangle* other) const override; 19 | bool intersectsWithRect(const Rectangle* other) const override; 20 | bool intersectsWithCircle(const Circle* other) const override; 21 | }; -------------------------------------------------------------------------------- /Sem. 04/Examples/Student/Student.cpp: -------------------------------------------------------------------------------- 1 | #include "Student.h" 2 | #include 3 | 4 | void Student::copyName(const char* newName) { 5 | size_t newNameLength = strlen(newName); 6 | this->name = new char[newNameLength + 1]; 7 | strcpy(this->name, newName); 8 | } 9 | 10 | Student::Student(int grade, const char* name) { 11 | this->grade = grade; 12 | copyName(name); 13 | } 14 | 15 | const char* Student::getName() const { 16 | return name; 17 | } 18 | 19 | void Student::setName(const char* newName) { 20 | if (strlen(newName) == 0) { 21 | return; 22 | } 23 | 24 | delete[] name; 25 | copyName(newName); 26 | } 27 | 28 | Student::~Student() { 29 | delete[] name; 30 | } 31 | -------------------------------------------------------------------------------- /Sem. 10/Solutions/PersonTeacherStudent/Teacher.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Person.h" 4 | 5 | class Teacher : public Person { 6 | private: 7 | char** subjects; 8 | size_t subjectsCount; 9 | 10 | void copySubjects(char** subjects, size_t subjectsCount); 11 | public: 12 | Teacher(const char* name, int age, char** subjects, size_t subjectsCount); 13 | 14 | Teacher(const Teacher& other); 15 | Teacher& operator=(const Teacher& other); 16 | 17 | Teacher(Teacher&& other) noexcept; 18 | Teacher& operator=(Teacher&& other) noexcept; 19 | ~Teacher(); 20 | 21 | private: 22 | void free(); 23 | void copyFrom(const Teacher& other); 24 | void moveFrom(Teacher&& other); 25 | }; 26 | 27 | -------------------------------------------------------------------------------- /Sem. 12/Solutions/ShapeCollection/Triangle/Triangle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Shape/Shape.h" 4 | 5 | class Triangle : public Shape { 6 | public: 7 | Triangle(int x1, int y1, int x2, int y2, int x3, int y3); 8 | 9 | Shape* clone() const override; 10 | 11 | double getArea() const override; 12 | double getPer() const override; 13 | bool isPointIn(int x, int y) const override; 14 | 15 | bool intersectsWith(const Shape* other) const override; 16 | 17 | bool intersectsWithTriangle(const Triangle* other) const override; 18 | bool intersectsWithRect(const Rectangle* other) const override; 19 | bool intersectsWithCircle(const Circle* other) const override; 20 | }; 21 | 22 | -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/Figures/Knight/Knight.cpp: -------------------------------------------------------------------------------- 1 | #include "Knight.h" 2 | #include 3 | 4 | Knight::Knight(bool isWhite) : Figure(isWhite, KnightFigure) 5 | {} 6 | bool Knight::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const 7 | { 8 | int xMovеment[] = { 1, 1, -1, -1, 2, -2, 2, -2 }; 9 | int yMovеment[] = { 2, -2, 2, -2, 1, 1, -1, -1 }; 10 | 11 | for (int i = 0; i < 8; i++) 12 | { 13 | if (currentX + xMovеment[i] == destX && 14 | currentY + yMovеment[i] == destY) 15 | return true; 16 | } 17 | return false; 18 | } 19 | 20 | void Knight::print() const 21 | { 22 | if (getIsWhite()) 23 | std::cout << 'K'; 24 | else 25 | std::cout << 'k'; 26 | } -------------------------------------------------------------------------------- /Sem. 01/Examples/ReadingIntegersBytes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a = 412288; // hex - 00 06 4A 80 = decimal - 0 6 74 128 5 | unsigned char* ptr = (unsigned char*)&a; 6 | 7 | // print every byte of int as number(0 - 255) reversed 8 | std::cout << (int)(*ptr) << std::endl; // 128 = 80 in hex // 8*16 + 0.1 9 | std::cout << (int)(*(ptr + 1)) << std::endl; // 74 = 4A in hex // 4*16 + 10*1 10 | std::cout << (int)(*(ptr + 2)) << std::endl; // 06 = 06 in hex // 0*16 + 6*1 11 | std::cout << (int)(*(ptr + 3)) << std::endl; // 00 = 00 in hex // 0*16 + 0*1 12 | 13 | // also can be written like 14 | for (size_t i = 0; i < 4; i++) { 15 | std::cout << (int)(ptr[i]) << " "; 16 | } 17 | } -------------------------------------------------------------------------------- /Sem. 08/Solutions/SelfCountingClass/SelfCounting.cpp: -------------------------------------------------------------------------------- 1 | #include "SelfCounting.h" 2 | 3 | size_t SelfCounting::livingInstances = 0; 4 | size_t SelfCounting::createdInstances = 0; 5 | 6 | size_t SelfCounting::getLivingInstances() { 7 | return livingInstances; 8 | } 9 | 10 | size_t SelfCounting::getCreatedInstances() { 11 | return createdInstances; 12 | } 13 | 14 | SelfCounting::SelfCounting() : localTempVariable(5) { 15 | createdInstances++; 16 | livingInstances++; 17 | } 18 | 19 | SelfCounting::SelfCounting(const SelfCounting& other) : localTempVariable(other.localTempVariable) { 20 | createdInstances++; 21 | livingInstances++; 22 | } 23 | 24 | SelfCounting::~SelfCounting() { 25 | livingInstances--; 26 | } -------------------------------------------------------------------------------- /Sem. 04/Examples/ConstructorDestructor/Constructor.cpp: -------------------------------------------------------------------------------- 1 | struct Test { 2 | int a; 3 | 4 | // initializing data member a with the value of the parameter a 5 | // using the member initializer list 6 | Test(int a) : a(a) { 7 | // at this point, this->a has the value of the parameter a 8 | } 9 | 10 | Test(int a, int b) { 11 | // at this point, this->a has the default int value; 12 | 13 | this->a = a; // we assign new value to a; 14 | } 15 | }; 16 | 17 | struct Test1 { 18 | int a; 19 | Test1() : Test1(0) { } 20 | Test1(int a) : a(a) { } 21 | }; 22 | 23 | struct Test2 { 24 | Test2(int a) {} 25 | } 26 | 27 | Test t[5]; // error: no default constructor => can not be initialized 28 | -------------------------------------------------------------------------------- /Sem. 05/Solutions/TextCensorer/SI/Text/Text.cpp: -------------------------------------------------------------------------------- 1 | #include "Text.h" 2 | #include 3 | 4 | Text::Text(const TextCensorer &textCensorer, const char *str) 5 | : censorer(textCensorer), str(str) {} 6 | 7 | void Text::print() const { 8 | censorer.print(str); 9 | } 10 | 11 | TextWithCensorerPointer::TextWithCensorerPointer(const char *str, const TextCensorer *censorer = nullptr) 12 | : str(str), censorer(censorer) { } 13 | 14 | void TextWithCensorerPointer::setCensorer(const TextCensorer *censorer) { 15 | this->censorer = censorer; 16 | } 17 | void TextWithCensorerPointer::print() const { 18 | if (!censorer) { 19 | std::cout << str << std::endl; 20 | return; 21 | } 22 | 23 | censorer->print(str); 24 | } -------------------------------------------------------------------------------- /Sem. 02/Examples/UnformattedReadingAfterFormatted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | constexpr int BUFFER_SIZE = 64; 4 | 5 | int main() { 6 | int a; 7 | std::cin >> a; // leaves a '\n' or ' ' in the input stream 8 | char arr[BUFFER_SIZE]; 9 | // cin.getline will read the left character in the input stream 10 | // => either arr will be empty string or will start with ' ' 11 | // to fix => std::cin.ignore(); - to ignore one character in the input stream 12 | // std::cin.ignore(N); - to ignore N symbols 13 | // cin.getline reads until it finds '\n' or BUFFER_SIZE symbols 14 | // + 1 for the terminating '\0' 15 | std::cin.ignore(); 16 | std::cin.getline(arr, BUFFER_SIZE + 1); 17 | std::cout << a << std::endl; 18 | } -------------------------------------------------------------------------------- /Sem. 03/Examples/ReadAndWriteIntFromBinary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace Constants { 5 | constexpr char FILE_NAME[] = "test.bin"; 6 | } 7 | 8 | int main() { 9 | { 10 | int n = 12345; 11 | std::ofstream ofs(Constants::FILE_NAME, std::ios::out | std::ios::binary); 12 | if (!ofs.is_open()) { 13 | return -1; 14 | } 15 | ofs.write((const char*)&n, sizeof(n)); 16 | ofs.close(); 17 | } 18 | { 19 | int n; 20 | std::ifstream ifs(Constants::FILE_NAME, std::ios::in | std::ios::binary); 21 | if (!ifs.is_open()) { 22 | return -1; 23 | } 24 | ifs.read((char*)&n, sizeof(n)); 25 | std::cout << n << std::endl; 26 | ifs.close(); 27 | } 28 | } -------------------------------------------------------------------------------- /Sem. 06/Solutions/Bitsets/Static-Bitset/Bitset.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace BitsetConstants { 4 | constexpr size_t MAX_SIZE = 1024; 5 | constexpr size_t BYTE_SIZE = 8; 6 | constexpr size_t BUCKETS_COUNT = MAX_SIZE / BYTE_SIZE; 7 | } 8 | 9 | class Bitset { 10 | private: 11 | unsigned char data[BitsetConstants::BUCKETS_COUNT] = { }; 12 | 13 | unsigned getBucketIndex(unsigned number) const; 14 | unsigned char getMask(unsigned number) const; 15 | public: 16 | Bitset() = default; 17 | 18 | bool contains(unsigned number) const; 19 | 20 | bool addNumber(unsigned number); 21 | bool removeNumber(unsigned number); 22 | 23 | void print() const; 24 | 25 | Bitset unionOfSets(const Bitset& rhs); 26 | Bitset intersectionOfSets(const Bitset& rhs); 27 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/Dynamic/DynamicCollection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Collection.h" 3 | 4 | class DynamicCollection : public Collection { 5 | protected: 6 | int* data = nullptr; 7 | size_t size = 0; 8 | size_t capacity = 8; 9 | void resize(size_t); 10 | 11 | public: 12 | DynamicCollection(); 13 | 14 | DynamicCollection(const DynamicCollection& other); 15 | DynamicCollection(DynamicCollection&& other) noexcept; 16 | 17 | DynamicCollection& operator=(const DynamicCollection& other); 18 | DynamicCollection& operator=(DynamicCollection&& other) noexcept; 19 | 20 | ~DynamicCollection(); 21 | 22 | private: 23 | void free(); 24 | void copyFrom(const DynamicCollection& other); 25 | void moveFrom(DynamicCollection&&); 26 | }; 27 | 28 | -------------------------------------------------------------------------------- /Sem. 02/Solutions/CopyFileContent.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | constexpr char FILE_NAME[] = "OOP.cpp"; 5 | constexpr char COPY_NAME[] = "CopyOfOOP.cpp"; 6 | constexpr int BUFFER_SIZE = 1024; 7 | 8 | int main() { 9 | std::ifstream in(FILE_NAME); 10 | if (!in.is_open()) { 11 | std::cout << "Could not open file to read" << std::endl; 12 | return -1; 13 | } 14 | 15 | std::ofstream out(COPY_NAME); 16 | if (!out.is_open()) { 17 | std::cout << "Could not open file to write" << std::endl; 18 | in.close(); // !!! 19 | return -1; 20 | } 21 | 22 | char buffer[BUFFER_SIZE]; 23 | while (!in.eof()) { 24 | in.getline(buffer, BUFFER_SIZE); 25 | out << buffer << std::endl; 26 | } 27 | 28 | in.close(); 29 | out.close(); 30 | } 31 | -------------------------------------------------------------------------------- /Sem. 11/Examples/OverrideFinal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Base { 4 | public: 5 | virtual ~Base() { 6 | std::cout << "~Base()" << std::endl; 7 | } 8 | 9 | virtual void test() { 10 | std::cout << 1 << std::endl; 11 | } 12 | }; 13 | 14 | class Derived : public Base { 15 | public: 16 | ~Derived() { 17 | std::cout << "~Derived()" << std::endl; 18 | } 19 | 20 | void test() override final { 21 | std::cout << 2 << std::endl; 22 | } 23 | }; 24 | 25 | class Derived2 : public Derived { 26 | public: 27 | // can't override test() because it is final in Derived 28 | void test() override { 29 | std::cout << 3 << std::endl; 30 | } 31 | }; 32 | 33 | int main() { 34 | Base* ptr = new Derived(); 35 | 36 | ptr->test(); 37 | delete ptr; 38 | } -------------------------------------------------------------------------------- /Sem. 05/Solutions/TextCensorer/IS/TextCensorer/TextCensorer.cpp: -------------------------------------------------------------------------------- 1 | #include "TextCensorer.h" 2 | #include 3 | 4 | namespace { 5 | bool isLower(char a) { 6 | return a >= 'a' && a <= 'z'; 7 | } 8 | }; 9 | 10 | void TextCensorer::printLetter(char a) const { 11 | std::cout << (isLower(a) && forbiddenLetters[a - 'a'] ? '*' : a); 12 | } 13 | 14 | void TextCensorer::addLetter(char a) { 15 | if (!isLower(a)) { 16 | return; 17 | } 18 | 19 | forbiddenLetters[a - 'a'] = true; 20 | } 21 | 22 | void TextCensorer::print(const char *str) const { 23 | if (!str) { 24 | return; 25 | } 26 | 27 | while (*str) { 28 | printLetter(*str); 29 | ++str; 30 | } 31 | 32 | std::cout << std::endl; 33 | } -------------------------------------------------------------------------------- /Sem. 01/Examples/Enums.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | enum class Color { 4 | Orange 5 | }; 6 | 7 | enum Color2 { 8 | Orange, 9 | }; 10 | 11 | enum Fruit { 12 | Orange 13 | }; 14 | 15 | // error: having same enumerator names 16 | enum Test { 17 | T1, 18 | T1 19 | }; 20 | 21 | // no problem having duplicate values 22 | enum Test { 23 | T0, // 0 24 | T1 = 1, 25 | T2, // 2, 26 | T3 = 1, 27 | T4, // 2 28 | }; 29 | 30 | // no need to write std:: before cout 31 | // after this line 32 | using std::cout; 33 | 34 | void main() { 35 | // error 36 | // int a = Orange; 37 | 38 | // overally it is not a good practice to compare different enums 39 | // error 40 | if(Color::Orange == Color2::Orange) { 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Sem. 15/Design Patterns/Structural/Composite.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "polymorphic_ptr.hpp" 3 | #include "MyVector.hpp" 4 | 5 | class FileSystemEntity { 6 | public: 7 | virtual size_t getSize() const = 0; 8 | virtual ~FileSystemEntity() = default; 9 | }; 10 | 11 | class File : public FileSystemEntity { 12 | size_t size = 0; 13 | public: 14 | size_t getSize() const { 15 | return size; 16 | } 17 | }; 18 | 19 | class Directory : public FileSystemEntity { 20 | Vector> children; 21 | public: 22 | size_t getSize() const { 23 | size_t size = 0; 24 | for (size_t i = 0; i < children.getSize(); i++) { 25 | size += children[i]->getSize(); 26 | } 27 | 28 | return size; 29 | } 30 | }; 31 | 32 | int main() { 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Sem. 04/Examples/MemberFunctions/MemberFunctions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void t(const Test& t) { 4 | t.func1(); // incorrect => error 5 | t.func2(); // incorrect => error 6 | t.constFunc(); // correct 7 | t.constFunc2(); // correct 8 | } 9 | 10 | struct Test{ 11 | int a; 12 | 13 | void func1() { 14 | std::cout << "func1" << std::endl; 15 | } 16 | 17 | void func2(); 18 | 19 | void constFunc() const { 20 | std::cout << "constFunc" << std::endl; 21 | } 22 | 23 | void constFunc2() const { 24 | std::cout << "constFunc2" << std::endl; 25 | constFunc(); // correct 26 | func1(); // incorrect! => error 27 | } 28 | }; 29 | 30 | void Test::func2() { 31 | std::cout << "func2" << std::endl; 32 | } -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/FigureFactory/FigureFactory.cpp: -------------------------------------------------------------------------------- 1 | #include "FigureFactory.h" 2 | #include "../Figures/Bishop/Bishop.h" 3 | #include "../Figures/King/King.h" 4 | #include "../Figures/Knight/Knight.h" 5 | #include "../Figures/Rook/Rook.h" 6 | #include "../Figures/Queen/Queen.h" 7 | #include "../Figures/Pawn/Pawn.h" 8 | 9 | Figure* FigureFactory::createFigure(bool isWhite, FigureType type) 10 | { 11 | 12 | switch (type) 13 | { 14 | case KingFigure: 15 | return new King(isWhite); 16 | case QueenFigure: 17 | return new Queen(isWhite); 18 | case KnightFigure: 19 | return new Knight(isWhite); 20 | case BishopFigure: 21 | return new Bishop(isWhite); 22 | case PawnFigure: 23 | return new Pawn(isWhite); 24 | case RookFigure: 25 | return new Rook(isWhite); 26 | } 27 | } -------------------------------------------------------------------------------- /Sem. 09/Solutions/CarDealerShip (with move)/ArrayOfObjectsImplementation/CarDealership.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Car/Car.h" 3 | 4 | class CarDealership { 5 | private: 6 | Car* cars; 7 | size_t count; 8 | size_t capacity; 9 | 10 | void resize(); 11 | public: 12 | CarDealership(); 13 | CarDealership(const CarDealership&); 14 | CarDealership(CarDealership&&) noexcept; 15 | CarDealership& operator=(const CarDealership&); 16 | CarDealership& operator=(CarDealership&&) noexcept; 17 | ~CarDealership(); 18 | 19 | void addCar(const Car&); 20 | void addCar(Car&&); 21 | void deleteAtIndex(size_t index); 22 | const Car& getCar(size_t index) const; 23 | 24 | private: 25 | void free(); 26 | void copyFrom(const CarDealership&); 27 | void moveFrom(CarDealership&&) noexcept; 28 | }; 29 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/Set/Set.cpp: -------------------------------------------------------------------------------- 1 | #include "Set.h" 2 | #include "NormalCollection.h" 3 | #include "SortedCollection.h" 4 | 5 | Set::Set(CollectionType type) { 6 | if (type == NORMAL_COLLECTION) { 7 | collection = new NormalCollection(); 8 | } 9 | else /*if (type == SORTED_COLLECTION)*/ { 10 | collection = new SortedCollection(); 11 | } 12 | } 13 | 14 | void Set::add(int elem) { 15 | if (!collection->contains(elem)) { 16 | collection->add(elem); 17 | } 18 | } 19 | 20 | void Set::remove(int elem) { 21 | collection->remove(elem); 22 | } 23 | 24 | size_t Set::count(int elem) const { 25 | return collection->count(elem); 26 | } 27 | 28 | bool Set::contains(int elem) const { 29 | return collection->contains(elem); 30 | } 31 | 32 | Set::~Set() { 33 | delete collection; 34 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Common/StreamHelper/StreamHelper.cpp: -------------------------------------------------------------------------------- 1 | #include "StreamHelper.h" 2 | 3 | namespace StreamHelper { 4 | int getCharCount(std::ifstream& ifs, char ch) { 5 | size_t currPos = ifs.tellg(); 6 | 7 | if (!ifs.is_open()) { 8 | return -1; 9 | } 10 | 11 | int count = 0; 12 | while (true) { 13 | char current = ifs.get(); 14 | 15 | if (ifs.eof()) { 16 | break; 17 | } 18 | 19 | if (current == ch) { 20 | count++; 21 | } 22 | } 23 | ifs.clear(); 24 | ifs.seekg(currPos); 25 | return count; 26 | } 27 | 28 | size_t getFileSize(std::ifstream& inFile) { 29 | size_t currPos = inFile.tellg(); 30 | inFile.seekg(0, std::ios::end); 31 | size_t res = inFile.tellg(); 32 | inFile.seekg(currPos); 33 | 34 | return res; 35 | } 36 | }; -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Common/StringView/StringView.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "MyString.h" 4 | 5 | class StringView { 6 | const char* _begin = nullptr; 7 | const char* _end = nullptr; //1 element after the final char; 8 | 9 | public: 10 | StringView() = default; 11 | StringView(const char* begin, const char* end); 12 | StringView(const char* str); //terminatedString 13 | StringView(const MyString& string); 14 | 15 | size_t length() const; 16 | char operator[](size_t ind) const; 17 | 18 | StringView substr(size_t from, size_t length) const; 19 | friend std::ostream& operator<<(std::ostream&, const StringView& strView); 20 | }; 21 | 22 | bool operator==(const StringView& lhs, const StringView& rhs); 23 | bool operator!=(const StringView& lhs, const StringView& rhs); -------------------------------------------------------------------------------- /Sem. 12/Solutions/TemplateFunctionSpecification.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | void print(T* arr, size_t size) { 5 | for (size_t i = 0; i < size; i++) { 6 | std::cout << arr[i] << " "; 7 | } 8 | std::cout << std::endl; 9 | } 10 | 11 | template<> 12 | void print(char* arr, size_t size) { 13 | for (size_t i = 0; i < size; i++) { 14 | std::cout << arr[i] << " ---- "; 15 | } 16 | std::cout << std::endl; 17 | } 18 | 19 | int main() { 20 | int arr1[] = { 1, 2 }; 21 | print(arr1, 2); 22 | char arr2[] = { 'a', 'b' }; 23 | print(arr2, 2); 24 | } 25 | 26 | // This is not a specialization 27 | // void print(char* arr, size_t size) { 28 | // for (size_t i = 0; i < size; i++) { 29 | // std::cout << arr[i] << " ---- "; 30 | // } 31 | // std::cout << std::endl; 32 | // } 33 | -------------------------------------------------------------------------------- /Sem. 06/Solutions/Student/Student.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Student { 4 | private: 5 | char* name = nullptr; 6 | int* grades = nullptr; 7 | size_t gradesCount = 0; 8 | 9 | void copyName(const char* newName); 10 | void copyGrades(const int* newGrades, size_t newGradesCount); 11 | 12 | void copyFrom(const Student& other); 13 | void free(); 14 | 15 | public: 16 | Student(const char* name, const int* grades, size_t gradesCount); 17 | 18 | Student() = default; 19 | Student(const Student& other); 20 | Student& operator=(const Student& other); 21 | ~Student(); 22 | 23 | const char* getName() const; 24 | const int* getGrades() const; 25 | 26 | const size_t getGradesCount() const; 27 | 28 | void setName(const char* newName); 29 | void setGrades(const int* newGrades, size_t newGradesCount); 30 | }; -------------------------------------------------------------------------------- /Sem. 01/Revision_Examples/HOF_GetCharactersCount.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t getCharCountByCondition(const char* str, bool(*predicate)(char ch)) { 4 | if (!str) { 5 | return 0; 6 | } 7 | 8 | size_t count = 0; 9 | while (*str) { 10 | if (predicate(*str)) { 11 | count++; 12 | } 13 | str++; 14 | } 15 | 16 | return count; 17 | } 18 | 19 | bool isLowerLetter(char ch) { 20 | return 'a' <= ch && ch <= 'z'; 21 | } 22 | 23 | size_t getLowerCount(const char* str) { 24 | return getCharCountByCondition(str, isLowerLetter); 25 | } 26 | 27 | size_t getDigitsCount(const char* str) { 28 | return getCharCountByCondition(str, [](char ch) { 29 | return '0' <= ch && ch <= '9'; 30 | }); 31 | } 32 | 33 | int main() { 34 | char str[] = "Hello World!123"; 35 | std::cout << getDigitsCount(str); 36 | } 37 | -------------------------------------------------------------------------------- /Sem. 07/Examples/Wrappers/ExampleUsage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntWrapper/IntWrapper.h" 3 | #include "CharWrapper/CharWrapper.h" 4 | 5 | int main() { 6 | // operators: 7 | // we can't overload: 8 | // priority/precedence 9 | // associativity 10 | // position according to the operands 11 | // arguments type and count 12 | // 13 | // we can overload: 14 | // return type - but it is not recommended, better stich to the expected return type of operator 15 | 16 | IntWrapper integer(5); 17 | std::cout << integer << std::endl; 18 | integer = 48; 19 | std::cout << integer << std::endl; 20 | 21 | CharWrapper character = integer; 22 | // if it was explicit 23 | CharWrapper character2 = (CharWrapper)integer; 24 | 25 | std::cout << character.getValue() << std::endl; 26 | } -------------------------------------------------------------------------------- /Sem. 13/Solutions/polymorphic/polymorphic_container.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "polymorphic_ptr.hpp" 3 | #include "MyVector.hpp" 4 | 5 | template 6 | class polymorphic_container { 7 | private: 8 | Vector> ptrs; 9 | 10 | public: 11 | void add(T* ptr); 12 | void execute(size_t index, void(*func)(T*)); 13 | void execute(size_t index, void(*func)(const T*)) const; 14 | }; 15 | 16 | template 17 | void polymorphic_container::add(T* ptr) { 18 | ptrs.push_back(ptr); 19 | } 20 | 21 | template 22 | void polymorphic_container::execute(size_t index, void(*func)(T*)) { 23 | func(ptrs[index].get()); 24 | } 25 | 26 | 27 | template 28 | void polymorphic_container::execute(size_t index, void(*func)(const T*)) const { 29 | func(ptrs[index].get()); 30 | } -------------------------------------------------------------------------------- /Sem. 09/Solutions/CarDealerShip (with move)/ArrayOfPointersImplementation/CarDealership.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Car/Car.h" 3 | 4 | class CarDealership { 5 | private: 6 | Car** cars; 7 | size_t count; 8 | size_t capacity; 9 | 10 | void resize(); 11 | public: 12 | CarDealership(); 13 | CarDealership(const CarDealership& other); 14 | CarDealership(CarDealership&& other) noexcept; 15 | CarDealership& operator=(const CarDealership& other); 16 | CarDealership& operator=(CarDealership&& other) noexcept; 17 | ~CarDealership(); 18 | 19 | void addCar(const Car& newCar); 20 | void addCar(Car&& newCar); 21 | void deleteAtIndex(size_t index); 22 | const Car& getCar(size_t index) const; 23 | 24 | private: 25 | void free(); 26 | void copyFrom(const CarDealership& other); 27 | void moveFrom(CarDealership&& other) noexcept; 28 | }; 29 | -------------------------------------------------------------------------------- /Sem. 10/Solutions/Sets/SetByString.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Bitset.h" 4 | 5 | class SetByString : private Bitset { 6 | private: 7 | char* str = nullptr; 8 | size_t length = 0; 9 | 10 | void fillSet(); 11 | void copyString(const char* str); 12 | public: 13 | SetByString(unsigned int max, const char* str); 14 | SetByString(const SetByString& other); 15 | SetByString(SetByString&& other) noexcept; 16 | SetByString& operator=(const SetByString& other); 17 | SetByString& operator=(SetByString&& other) noexcept; 18 | ~SetByString(); 19 | 20 | bool contains(unsigned n) const; 21 | void print() const; 22 | void setString(const char* str); 23 | void setAt(size_t index, char ch); 24 | 25 | private: 26 | void copyFrom(const SetByString& other); 27 | void free(); 28 | void moveFrom(SetByString&& other) noexcept; 29 | }; 30 | 31 | -------------------------------------------------------------------------------- /Sem. 02/Solutions/NewLinesCount.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace Constants { 5 | constexpr char FILE_NAME[] = "test.txt"; 6 | } 7 | 8 | int getCharCountFromFile(std::ifstream& ifs, char ch) { 9 | if (!ifs.is_open()) { 10 | return -1; 11 | } 12 | 13 | int count = 0; 14 | 15 | while (true) { 16 | char current = ifs.get(); 17 | if (ifs.eof()) { 18 | break; 19 | } 20 | 21 | if (current == ch) { 22 | count++; 23 | } 24 | } 25 | 26 | return count; 27 | } 28 | 29 | int getLinesCount(const char* fileName) { 30 | std::ifstream myFile(fileName); 31 | 32 | if (!myFile.is_open()) { 33 | return -1; 34 | } 35 | 36 | return getCharCountFromFile(myFile, '\n') + 1; 37 | } 38 | 39 | int main() { 40 | std::cout << getLinesCount(Constants::FILE_NAME) << std::endl; 41 | } 42 | -------------------------------------------------------------------------------- /Sem. 04/Solutions/Interval/Interval.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Interval { 4 | private: 5 | int start, end; 6 | 7 | unsigned getNumbersMeetingConditionCound(bool(*predicate)(int)) const; 8 | public: 9 | Interval(); 10 | Interval(int start, int end); 11 | 12 | int getStart() const; 13 | int getEnd() const; 14 | 15 | void setStart(int newValue); 16 | void setEnd(int newValue); 17 | 18 | unsigned getLength() const; 19 | bool isInInterval(int value) const; 20 | 21 | void print() const; 22 | 23 | unsigned getPrimeNumbersCount() const; 24 | unsigned getPalindromeNumbersCount() const; 25 | unsigned getOnlyUniqueDigitsNumbersCount() const; 26 | 27 | bool areStartAndEndPowOfTwo() const; 28 | Interval intersect(const Interval& other) const; 29 | 30 | bool isSubinterval(const Interval& other) const; 31 | }; -------------------------------------------------------------------------------- /Sem. 04/Solutions/Time/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Time.h" 2 | 3 | void stableSortTimes(Time* arr, size_t arrSize) { 4 | // insertion sort 5 | // https://visualgo.net/en/sorting 6 | for (int i = 1; i < arrSize; i++) { 7 | Time temp = arr[i]; 8 | 9 | int j = i - 1; 10 | while (j >= 0 && arr[j].compare(temp) > 0) { 11 | arr[j + 1] = arr[j]; 12 | j--; 13 | } 14 | 15 | arr[j + 1] = temp; 16 | } 17 | } 18 | 19 | int main() { 20 | constexpr size_t ARR_SIZE = 10; 21 | Time times[ARR_SIZE] = { Time(11, 30, 0), Time(12, 44, 29), Time(), Time(17, 15, 23), Time(23, 60, 0), Time(3, 14, 15), Time(2, 71, 82), Time(21, 22, 3), Time(17, 15, 23), Time(7, 31, 43) }; 22 | stableSortTimes(times, ARR_SIZE); 23 | 24 | for (int i = 0; i < ARR_SIZE; i++) { 25 | times[i].print(); 26 | } 27 | } -------------------------------------------------------------------------------- /Sem. 10/Solutions/Sets/Bitset.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Bitset { 4 | private: 5 | unsigned char* data; 6 | unsigned int maxNum; 7 | unsigned int bucketsCount; 8 | public: 9 | Bitset(unsigned int max); 10 | Bitset(const Bitset& other); 11 | Bitset(Bitset&& other) noexcept; 12 | Bitset& operator=(const Bitset& other); 13 | Bitset& operator=(Bitset&& other) noexcept; 14 | ~Bitset() noexcept; 15 | 16 | void add(unsigned int n); 17 | void remove(unsigned int n); 18 | bool contains(unsigned int n) const; 19 | void reset(); 20 | void print() const; 21 | 22 | unsigned getMaxNumber() const; 23 | 24 | friend Bitset unionOfSets(const Bitset& lhs, const Bitset& rhs); 25 | friend Bitset intersectionOfSets(const Bitset& lhs, const Bitset& rhs); 26 | 27 | private: 28 | void moveFrom(Bitset&& other) noexcept; 29 | void copyFrom(const Bitset& other); 30 | void free(); 31 | }; -------------------------------------------------------------------------------- /Sem. 08/Examples/TerminationBecauseOfErrorInDestructor/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Test { 4 | ~Test() { 5 | // 6 | std::cout << "Hello from destructor" << std::endl; 7 | throw 3; 8 | } 9 | }; 10 | 11 | void g() { 12 | // Test t; 13 | // first exception 14 | throw std::exception("Exception from g"); 15 | } 16 | 17 | void f() { 18 | Test t; // second exception when the stack is unwinding 19 | // => terminate() is called 20 | try { 21 | g(); 22 | } 23 | catch (int code) { 24 | std::cout << "Catched in f()"; 25 | throw code; 26 | } 27 | } 28 | 29 | int main() { 30 | try { 31 | f(); 32 | } 33 | catch (int code) { 34 | std::cout << "Catched in main()"; 35 | } 36 | catch (const std::exception& ex) { 37 | std::cout << ex.what(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Sem. 03/Examples/ReadAndWriteTestStructureFromBinary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace Constants { 5 | constexpr char FILE_NAME[] = "test.bin"; 6 | } 7 | 8 | struct Test { 9 | char ch; 10 | int n; 11 | }; 12 | 13 | int main() { 14 | { 15 | Test t = {'A', 12345}; 16 | std::ofstream ofs(Constants::FILE_NAME, std::ios::out | std::ios::binary); 17 | if (!ofs.is_open()) { 18 | return -1; 19 | } 20 | ofs.write((const char*)&t, sizeof(t)); 21 | ofs.close(); 22 | } 23 | { 24 | Test t; 25 | std::ifstream ifs(Constants::FILE_NAME, std::ios::in | std::ios::binary); 26 | if (!ifs.is_open()) { 27 | return -1; 28 | } 29 | ifs.read((char*)&t, sizeof(t)); 30 | std::cout << t.ch << " " << t.n << std::endl; 31 | ifs.close(); 32 | } 33 | } -------------------------------------------------------------------------------- /Sem. 05/Solutions/Event/Date/Date.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | // Some date implementation from last year 5 | class Date { 6 | unsigned MAX_DAYS[12] = 7 | { 8 | 31,28,31,30,31, 30, 31, 31, 30, 31, 30, 31 9 | }; 10 | unsigned day = 1; 11 | unsigned month = 1; 12 | unsigned year = 1; 13 | 14 | bool isLeapYear() const; 15 | 16 | mutable bool isModified = true; 17 | mutable int dayOfWeek = -1; 18 | 19 | public: 20 | Date(unsigned day, unsigned month, unsigned year); 21 | unsigned getDay() const; 22 | unsigned getMonth() const; 23 | unsigned getYear() const; 24 | 25 | void setDay(unsigned day); 26 | void setMonth(unsigned month); 27 | void setYear(unsigned year); 28 | 29 | bool isEqualTo(const Date& d) const; 30 | 31 | void print() const; 32 | void goToNextDay(); 33 | int getDayOfWeek() const; 34 | 35 | }; -------------------------------------------------------------------------------- /Sem. 05/Solutions/TextCensorer/SI/Text/Text.h: -------------------------------------------------------------------------------- 1 | #pragma once; 2 | 3 | #include "../TextCensorer/TextCensorer.h" 4 | 5 | // Version 1 6 | class Text { 7 | // We are using aggregation for both the str and the censorer! 8 | // we don't manage the memory of the str, we just use it 9 | const char* str; 10 | 11 | // If we change it to Censor* we can then change 12 | // it every time with setter and also can have a default constructor 13 | const TextCensorer& censorer; 14 | public: 15 | Text(const TextCensorer& textCensorer, const char* str); 16 | void print() const; 17 | }; 18 | 19 | // Version 2 20 | class TextWithCensorerPointer { 21 | const char* str; 22 | const TextCensorer* censorer; 23 | public: 24 | TextWithCensorerPointer(const char* str, const TextCensorer* censorer = nullptr); 25 | void setCensorer(const TextCensorer* censorer); 26 | void print() const; 27 | }; -------------------------------------------------------------------------------- /Sem. 13/Solutions/Chess/ChessBoard/ChessBoard.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Figures/FigureBase/Figure.h" 3 | 4 | const size_t BOARD_SIZE = 8; 5 | class ChessBoard 6 | { 7 | struct Cell 8 | { 9 | Figure* f = nullptr; 10 | Cell() = default; 11 | 12 | Cell(const Cell& other) = delete; 13 | Cell& operator=(const Cell& other) = delete; 14 | bool isEmpty() 15 | { 16 | return f == nullptr; 17 | } 18 | 19 | void print() const 20 | { 21 | if (f != nullptr) 22 | f->print(); 23 | else 24 | std::cout << ' '; 25 | } 26 | 27 | ~Cell() 28 | { 29 | delete f; 30 | } 31 | }; 32 | Cell board[BOARD_SIZE][BOARD_SIZE]; 33 | 34 | public: 35 | ChessBoard(); 36 | ChessBoard(const ChessBoard& other) = delete; 37 | ChessBoard& operator=(const ChessBoard&) = delete; 38 | 39 | void print() const; 40 | 41 | void moveFigure(size_t x, size_t y, size_t destX, size_t destY); 42 | 43 | }; 44 | -------------------------------------------------------------------------------- /Sem. 15/Solutions/ArrayManipulations/Command/SortCommand.cpp: -------------------------------------------------------------------------------- 1 | #include "SortCommand.h" 2 | 3 | SortCommand::SortCommand(IntVector& v) : VectorCommand(v) {} 4 | 5 | void SortCommand::sortVector(IntVector& v) { 6 | for (size_t i = 0; i < v.getSize() - 1; i++) { 7 | size_t minIndex = i; 8 | for (size_t j = i + 1; j < v.getSize(); j++) { 9 | if (v[j] < v[minIndex]) { 10 | minIndex = j; 11 | } 12 | } 13 | if (minIndex != i) { 14 | std::swap(v[i], v[minIndex]); 15 | } 16 | } 17 | } 18 | 19 | void SortCommand::execute() { 20 | if (snapshot) { 21 | delete snapshot; 22 | } 23 | snapshot = new IntVector(v); 24 | sortVector(v); 25 | } 26 | 27 | void SortCommand::undo() { 28 | if (snapshot) { 29 | v = std::move(*snapshot); 30 | delete snapshot; 31 | snapshot = nullptr; 32 | } 33 | } -------------------------------------------------------------------------------- /Sem. 07/Solutions/PrimeIterator/PrimeIterator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // TODO: can be refactored to accept a function pointer 4 | // then this class will be able to work with any function 5 | // that accepts size_t and return bool 6 | class PrimeIterator { 7 | private: 8 | size_t _value; 9 | 10 | PrimeIterator& goToNext(); 11 | PrimeIterator& goToPrev(); 12 | 13 | public: 14 | PrimeIterator(size_t value); 15 | PrimeIterator(); 16 | 17 | size_t getValue() const; 18 | void setValue(size_t value); 19 | 20 | PrimeIterator& operator++(); 21 | PrimeIterator operator++(int); 22 | 23 | PrimeIterator& operator--(); 24 | PrimeIterator operator--(int); 25 | 26 | // dereference operator 27 | unsigned operator*() const; 28 | }; 29 | 30 | // if we have == then we should have != 31 | bool operator==(const PrimeIterator& lhs, const PrimeIterator& rhs); 32 | bool operator!=(const PrimeIterator& lhs, const PrimeIterator& rhs); -------------------------------------------------------------------------------- /Sem. 10/Solutions/Bar/Bar/Bar.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "AlcoholDrink.h" 3 | #include "Drink.h" 4 | 5 | class Bar { 6 | private: 7 | const static short MAX_DRINKS_SIZE = 100; 8 | 9 | struct Statistics 10 | { 11 | size_t soldDrinks; 12 | size_t soldAlcoholDrinks; 13 | size_t mlSold; 14 | } stats; 15 | 16 | // the next two will be used as stacks 17 | AlcoholDrink alcDrinks[MAX_DRINKS_SIZE]; 18 | Drink drinks[MAX_DRINKS_SIZE]; 19 | size_t alcDrinksCount = 0; 20 | size_t drinksCount = 0; 21 | 22 | size_t alcDrinksCounter[MAX_DRINKS_SIZE] = {}; 23 | size_t drinksCounter[MAX_DRINKS_SIZE] = {}; 24 | 25 | public: 26 | Bar() = default; 27 | 28 | void addDrink(const Drink& drink, size_t count = 1); 29 | void addAlcoholDrink(const AlcoholDrink& drink, size_t count = 1); 30 | 31 | Drink getDrink(); // popBack; 32 | AlcoholDrink getAlcoholDrink(); // popBack 33 | 34 | const Statistics& getStats() const; 35 | }; -------------------------------------------------------------------------------- /Sem. 02/Examples/TogglingFileFlags.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void failExample() { 5 | std::cout << "Fail example" << std::endl; 6 | int a; 7 | std::cin >> a; // enter a letter 8 | std::cout << std::cin.fail() << std::endl; // true 9 | std::cout << std::cin.good() << std::endl; // false 10 | std::cin.clear(); 11 | std::cout << std::cin.fail() << std::endl; // false 12 | std::cout << std::cin.good() << std::endl; // true 13 | } 14 | 15 | void badExample() { 16 | std::cout << "Bad example" << std::endl; 17 | std::ofstream out("someFileThatDoesExist.cpp", std::ios::_Nocreate); 18 | out.put('a'); 19 | 20 | std::cout << out.bad() << std::endl; // true 21 | std::cout << out.good() << std::endl; // false 22 | out.clear(); 23 | std::cout << out.bad() << std::endl; // false 24 | std::cout << out.good() << std::endl; // true 25 | } 26 | 27 | int main() { 28 | failExample(); 29 | badExample(); 30 | } 31 | -------------------------------------------------------------------------------- /Sem. 10/Solutions/Sets/SetByCriteria.cpp: -------------------------------------------------------------------------------- 1 | #include "SetByCriteria.h" 2 | 3 | void SetByCriteria::fillSet() { 4 | for (size_t i = 0; i < getMaxNumber(); i++) { 5 | if (criteria.includes(i) && !criteria.excludes(i)) { 6 | add(i); 7 | } 8 | else { 9 | remove(i); 10 | } 11 | } 12 | } 13 | 14 | SetByCriteria::SetByCriteria(unsigned maxNumber, bool(*includes)(unsigned n), bool(*excludes)(unsigned n)) 15 | : Bitset(maxNumber), criteria({includes, excludes}) 16 | { 17 | fillSet(); 18 | } 19 | 20 | void SetByCriteria::setIncludes(bool(*includes)(unsigned n)) { 21 | criteria.includes = includes; 22 | fillSet(); 23 | } 24 | 25 | void SetByCriteria::setExcludes(bool(*excludes)(unsigned n)) { 26 | criteria.excludes = excludes; 27 | fillSet(); 28 | } 29 | 30 | bool SetByCriteria::contains(unsigned int n) const { 31 | return Bitset::contains(n); 32 | } 33 | 34 | void SetByCriteria::print() const { 35 | Bitset::print(); 36 | } 37 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/Shapes/Shape.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class Shape 5 | { 6 | protected: 7 | struct Point 8 | { 9 | int x = 0; 10 | int y = 0; 11 | 12 | Point(); 13 | Point(int x, int y); 14 | 15 | double getDistance(const Point& other) const; 16 | }; 17 | 18 | const Point& getPointAtIndex(size_t index) const; 19 | 20 | private: 21 | Point* points; 22 | size_t pointsCount; 23 | 24 | void copyFrom(const Shape& other); 25 | void moveFrom(Shape&& other); 26 | void free(); 27 | 28 | public: 29 | Shape(size_t pointsCount); 30 | Shape(const Shape& other); 31 | Shape(Shape&& other); 32 | Shape& operator=(const Shape& other); 33 | Shape& operator=(Shape&& other); 34 | virtual ~Shape(); 35 | 36 | void setPoint(size_t pointIndex, int x, int y); 37 | 38 | virtual double getArea() const = 0; 39 | virtual double getPerimeter() const = 0; 40 | virtual bool isPointIn(int x, int y) const = 0; 41 | }; -------------------------------------------------------------------------------- /Sem. 13/Solutions/polymorphic/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "polymorphic_ptr.hpp" 3 | #include "polymorphic_container.hpp" 4 | 5 | class Base { 6 | public: 7 | virtual void print() const = 0; 8 | virtual Base* clone() const = 0; 9 | virtual ~Base() = default; 10 | }; 11 | 12 | class Der1 : public Base { 13 | public: 14 | void print() const override { 15 | std::cout << "Der1" << std::endl; 16 | } 17 | Base* clone() const override { 18 | return new Der1(*this); 19 | } 20 | }; 21 | 22 | class Der2 : public Base { 23 | public: 24 | void print() const override { 25 | std::cout << "Der2" << std::endl; 26 | } 27 | Base* clone() const override { 28 | return new Der2(*this); 29 | } 30 | }; 31 | 32 | int main() { 33 | polymorphic_container pc; 34 | 35 | pc.add(new Der1()); 36 | pc.add(new Der2()); 37 | pc.execute(0, [](const Base* p) { p->print(); }); 38 | pc.execute(1, [](const Base* p) { p->print(); }); 39 | } -------------------------------------------------------------------------------- /Sem. 02/Solutions/ReplaceCharactersInFile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace Constants { 5 | const char FILE_NAME[] = "test.txt"; 6 | const char ERROR_MESSAGE[] = "Error"; 7 | }; 8 | 9 | void replaceCharacterInFile(const char* fileName, const char toReplace, const char replacement) { 10 | std::fstream file(fileName, std::ios::in | std::ios::out); 11 | 12 | if (!file.is_open()) { 13 | std::cout << Constants::ERROR_MESSAGE; 14 | return; 15 | } 16 | 17 | char current; 18 | while (true) { 19 | file.get(current); 20 | if (file.eof()) { 21 | break; 22 | } 23 | if (current == toReplace) { 24 | file.seekp(-1, std::ios::cur); 25 | file.put(replacement); 26 | file.flush(); 27 | // seekp also does a flush 28 | //file.seekp(file.tellp()); 29 | } 30 | } 31 | 32 | file.close(); 33 | } 34 | 35 | int main() { 36 | replaceCharacterInFile(Constants::FILE_NAME, 'b', 'c'); 37 | } 38 | -------------------------------------------------------------------------------- /Sem. 03/Solutions/ReplaceCharacterInFile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace Constants { 5 | const char FILE_NAME[] = "test.txt"; 6 | const char ERROR_MESSAGE[] = "Error"; 7 | }; 8 | 9 | void replaceCharacterInFile(const char* fileName, const char toReplace, const char replacement) { 10 | std::fstream file(fileName, std::ios::in | std::ios::out); 11 | 12 | if (!file.is_open()) { 13 | std::cout << Constants::ERROR_MESSAGE; 14 | return; 15 | } 16 | 17 | char current; 18 | while (true) { 19 | file.get(current); 20 | if (file.eof()) { 21 | break; 22 | } 23 | if (current == toReplace) { 24 | file.seekp(-1, std::ios::cur); 25 | file.put(replacement); 26 | file.flush(); 27 | // seekp also does a flush 28 | //file.seekp(file.tellp()); 29 | } 30 | } 31 | 32 | file.close(); 33 | } 34 | 35 | int main() { 36 | replaceCharacterInFile(Constants::FILE_NAME, 'b', 'c'); 37 | } 38 | -------------------------------------------------------------------------------- /Sem. 03/Examples/ReadAndWriteStudentToTextFile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace Constants { 5 | const size_t MAX_NAME_SIZE = 20; 6 | const char FILE_NAME[] = "test.txt"; 7 | } 8 | 9 | struct Student { 10 | int number; 11 | char name[Constants::MAX_NAME_SIZE + 1]; 12 | }; 13 | 14 | int main() { 15 | { 16 | Student s = { 5, "12345678901234567890" }; 17 | 18 | std::ofstream out(Constants::FILE_NAME); 19 | if (!out.is_open()) { 20 | return -1; 21 | } 22 | 23 | out << s.number << '\n' << s.name; 24 | 25 | out.close(); 26 | } 27 | 28 | { 29 | Student s; 30 | 31 | std::ifstream in(Constants::FILE_NAME); 32 | if (!in.is_open()) { 33 | return -1; 34 | } 35 | 36 | in >> s.number; 37 | in.ignore(); 38 | // getline includes the terminating zero in the count 39 | in.getline(s.name, Constants::MAX_NAME_SIZE + 1); 40 | 41 | std::cout << s.number << "\n" << s.name; 42 | 43 | in.close(); 44 | } 45 | } -------------------------------------------------------------------------------- /Sem. 01/Examples/Namespaces.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | namespace Constants { 3 | const double PI = 3.14159; 4 | const double E = 2.71828; 5 | } 6 | 7 | void f() { 8 | std::cout << "::f()" << std::endl; 9 | } 10 | 11 | namespace { 12 | void f1() { 13 | std::cout << "f1()" << std::endl; 14 | } 15 | } 16 | 17 | namespace A { 18 | void f() { 19 | std::cout << "A::f()" << std::endl; 20 | } 21 | } 22 | 23 | namespace B { 24 | void f() { 25 | // using ::f() you can access the global f function from here 26 | std::cout << "B::f()" << std::endl; 27 | } 28 | } 29 | 30 | // easy to confuse with the std::sqrt 31 | double sqrt(double x) { 32 | return x; 33 | } 34 | 35 | int main() { 36 | A::f(); 37 | B::f(); 38 | f(); 39 | std::cout << Constants::PI << std::endl; 40 | std::cout << Constants::E << std::endl; 41 | 42 | std::cout << sqrt(4) << std::endl; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Sem. 15/Design Patterns/Creational/Singleton.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class SingletonClass { 4 | private: 5 | SingletonClass() { 6 | std::cout << "SingletonClass was created" << std::endl; 7 | } 8 | 9 | ~SingletonClass() { 10 | std::cout << "SingletonClass was destroyed" << std::endl; 11 | } 12 | public: 13 | static SingletonClass& getInstance() { 14 | static SingletonClass instance; 15 | return instance; 16 | } 17 | 18 | SingletonClass(const SingletonClass& other) = delete; 19 | SingletonClass& operator=(const SingletonClass& other) = delete; 20 | 21 | void doStuff() { 22 | std::cout << this << std::endl; 23 | } 24 | }; 25 | 26 | int main() { 27 | { 28 | std::cout << "hello"; 29 | SingletonClass& s1 = SingletonClass::getInstance(); 30 | } 31 | SingletonClass& s2 = SingletonClass::getInstance(); 32 | s2.doStuff(); 33 | SingletonClass& s3 = SingletonClass::getInstance(); 34 | s3.doStuff(); 35 | SingletonClass& s4 = SingletonClass::getInstance(); 36 | } 37 | -------------------------------------------------------------------------------- /Sem. 09/Solutions/ClassOfStudents/ClassOfStudents.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Student.h" 3 | 4 | class ClassOfStudents { 5 | private: 6 | static constexpr size_t DEFAULT_SIZE = 16; 7 | 8 | Student** students = nullptr; 9 | size_t count = 0; 10 | size_t capacity = DEFAULT_SIZE; 11 | 12 | void resize(size_t leastCapacity); 13 | public: 14 | ClassOfStudents(); 15 | ClassOfStudents(const ClassOfStudents& other); 16 | ClassOfStudents(ClassOfStudents&& other) noexcept; 17 | ClassOfStudents& operator=(const ClassOfStudents&); 18 | ClassOfStudents& operator=(ClassOfStudents&&) noexcept; 19 | ~ClassOfStudents(); 20 | 21 | void addStudent(const Student&); 22 | void addStudent(Student&&); 23 | void insertAt(const Student&, size_t); 24 | void insertAt(Student&&, size_t); 25 | size_t getFirstFreeIndex() const; 26 | void removeAt(size_t index); 27 | 28 | private: 29 | void free(); 30 | void copyFrom(const ClassOfStudents&); 31 | void moveFrom(ClassOfStudents&&) noexcept; 32 | }; 33 | 34 | -------------------------------------------------------------------------------- /Sem. 11/Examples/VirtualFunctionsDestructors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Base { 4 | public: 5 | // !!!! virtual destructor - always 6 | virtual ~Base() { 7 | std::cout << "~Base()" << std::endl; 8 | } 9 | 10 | // virtual - the correct function will be called at runtime 11 | virtual void test() { 12 | std::cout << 1 << std::endl; 13 | } 14 | 15 | // this function is not virtual 16 | // the function from the base class will be called 17 | void test2() { 18 | std::cout << 1 << std::endl; 19 | } 20 | }; 21 | 22 | class Derived : public Base { 23 | public: 24 | ~Derived() { 25 | std::cout << "~Derived()" << std::endl; 26 | } 27 | 28 | virtual void test() override { 29 | std::cout << 2 << std::endl; 30 | } 31 | 32 | // shadowing the function from the base class! 33 | void test2() { 34 | std::cout << 2 << std::endl; 35 | } 36 | }; 37 | 38 | int main() { 39 | Base* ptr = new Derived(); 40 | 41 | ptr->test(); // 2 42 | ptr->test2(); // 1 43 | delete ptr; 44 | } -------------------------------------------------------------------------------- /Sem. 13/Examples/VirtualFunctionsAndDestructors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Base { 4 | public: 5 | // !!!! virtual destructor - always 6 | virtual ~Base() { 7 | std::cout << "~Base()" << std::endl; 8 | } 9 | 10 | // virtual - the correct function will be called at runtime 11 | virtual void test() { 12 | std::cout << 1 << std::endl; 13 | } 14 | 15 | // this function is not virtual 16 | // the function from the base class will be called 17 | void test2() { 18 | std::cout << 1 << std::endl; 19 | } 20 | }; 21 | 22 | class Derived : public Base { 23 | public: 24 | ~Derived() { 25 | std::cout << "~Derived()" << std::endl; 26 | } 27 | 28 | virtual void test() override { 29 | std::cout << 2 << std::endl; 30 | } 31 | 32 | // shadowing the function from the base class! 33 | void test2() { 34 | std::cout << 2 << std::endl; 35 | } 36 | }; 37 | 38 | int main() { 39 | Base* ptr = new Derived(); 40 | 41 | ptr->test(); // 2 42 | ptr->test2(); // 1 43 | delete ptr; 44 | } -------------------------------------------------------------------------------- /Sem. 01/Examples/Padding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Test { 4 | char test; 5 | char test2; 6 | int x; 7 | }; 8 | // sizeof(Test); // 8 9 | 10 | struct A { 11 | char a; 12 | int b; 13 | }; 14 | // sizeof(A); // 8 15 | 16 | // The important thing about the next example is to 17 | // check the visualization with the struct layout extension 18 | struct Nested { 19 | int b; 20 | A a; 21 | double d; 22 | }; 23 | // sizeof(Nested); // 24 24 | 25 | struct Test2 { 26 | int a; 27 | char b; 28 | // You can access the padding with the following, 29 | // NOTE: this can be written only in the end of the struct! 30 | char c[]; 31 | }; 32 | // sizeof(Test2); // 8 33 | 34 | struct Test3 { 35 | int a[50]; 36 | double b; 37 | }; 38 | // sizeof(Test3); // 208 39 | 40 | struct Test4{}; 41 | // sizeof(Test4); // 1 => because it needs to take some memory 42 | 43 | int main() { 44 | int a = 5; 45 | sizeof(int); // 4 46 | sizeof(a); // 4 47 | } 48 | -------------------------------------------------------------------------------- /Sem. 08/Solutions/StringPool/ImmutableString/ImmutableString.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "../StringPool/StringPool.h" 4 | 5 | class ImmutableString { 6 | private: 7 | const char* data; 8 | unsigned size; 9 | 10 | static StringPool pool; 11 | 12 | public: 13 | ImmutableString(); 14 | ImmutableString(const char* text); 15 | ImmutableString(const ImmutableString& other); 16 | ~ImmutableString(); 17 | 18 | char operator[](size_t index) const; 19 | 20 | ImmutableString& operator=(const ImmutableString& other); 21 | 22 | const char* c_str() const; 23 | 24 | size_t length() const; 25 | bool empty() const; 26 | 27 | // !!! 28 | friend bool operator==(const ImmutableString& lhs, const ImmutableString& rhs); 29 | // !!! 30 | friend ImmutableString operator+(const ImmutableString& lhs, const ImmutableString& rhs); 31 | }; 32 | 33 | bool operator!=(const ImmutableString& lhs, const ImmutableString& rhs); 34 | std::ostream& operator<<(std::ostream& os, const ImmutableString& str); -------------------------------------------------------------------------------- /Sem. 11/Solutions/Shapes/Rectangle.cpp: -------------------------------------------------------------------------------- 1 | #include "Rectangle.h" 2 | 3 | Rectangle::Rectangle(int x1, int y1, int x3, int y3) : Shape(4) 4 | { 5 | setPoint(0, x1, y1); 6 | setPoint(1, x1, y3); 7 | setPoint(2, x3, y3); 8 | setPoint(3, x3, y1); 9 | } 10 | double Rectangle::getArea() const 11 | { 12 | const Shape::Point& p0 = getPointAtIndex(0); 13 | const Shape::Point& p1 = getPointAtIndex(1); 14 | const Shape::Point& p2 = getPointAtIndex(2); 15 | 16 | return p0.getDistance(p1) * p1.getDistance(p2); 17 | 18 | } 19 | double Rectangle::getPerimeter() const 20 | { 21 | const Shape::Point& p0 = getPointAtIndex(0); 22 | const Shape::Point& p1 = getPointAtIndex(1); 23 | const Shape::Point& p2 = getPointAtIndex(2); 24 | 25 | return 2 * (p0.getDistance(p1) + p1.getDistance(p2)); 26 | } 27 | 28 | bool Rectangle::isPointIn(int x, int y) const 29 | { 30 | Shape::Point p(x, y); 31 | return p.x >= getPointAtIndex(0).x && p.x <= getPointAtIndex(2).x && 32 | p.y >= getPointAtIndex(0).y && p.y <= getPointAtIndex(2).y; 33 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/PureNumbersCollections/Normal/NormalCollection.cpp: -------------------------------------------------------------------------------- 1 | #include "NormalCollection.h" 2 | #include 3 | 4 | void NormalCollection::add(int n) { 5 | if (size == capacity) { 6 | resize(2 * size); 7 | } 8 | 9 | data[size++] = n; 10 | } 11 | 12 | void NormalCollection::remove(int n) { 13 | int index = -1; 14 | for (size_t i = 0; i < size; i++) { 15 | if (data[i] == n) { 16 | index = i; 17 | break; 18 | } 19 | } 20 | 21 | if (index == -1) { 22 | return; 23 | } 24 | 25 | for (size_t i = index; i < size - 1; i++) { 26 | data[i] = data[i + 1]; 27 | } 28 | size--; 29 | } 30 | 31 | size_t NormalCollection::count(int n) const { 32 | size_t counter = 0; 33 | for (size_t i = 0; i < size; i++) { 34 | if (data[i] == n) { 35 | counter++; 36 | } 37 | } 38 | 39 | return counter; 40 | } 41 | 42 | bool NormalCollection::contains(int n) const { 43 | for (size_t i = 0; i < size; i++) { 44 | if (data[i] == n) { 45 | return true; 46 | } 47 | } 48 | 49 | return false; 50 | } -------------------------------------------------------------------------------- /Sem. 06/Solutions/Bitsets/Dynamic-Bitset/Bitset.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace BitsetConstants { 4 | constexpr size_t BYTE_SIZE = 8; 5 | } 6 | 7 | // moved these functions to a namespace because they are not 8 | // using anything from the bitset instance(not using this) 9 | namespace BitsetHelpers { 10 | unsigned char getMask(unsigned int n); 11 | unsigned int getBucketIndex(unsigned int n); 12 | } 13 | 14 | class Bitset { 15 | private: 16 | unsigned char* data; 17 | unsigned int maxNum; 18 | unsigned int bucketsCount; 19 | public: 20 | Bitset(unsigned int max); 21 | Bitset(const Bitset& other); 22 | Bitset& operator=(const Bitset& other); 23 | ~Bitset(); 24 | 25 | void add(unsigned int n); 26 | void remove(unsigned int n); 27 | bool contains(unsigned int n) const; 28 | void print() const; 29 | 30 | friend Bitset unionOfSets(const Bitset& lhs, const Bitset& rhs); 31 | friend Bitset intersectionOfSets(const Bitset& lhs, const Bitset& rhs); 32 | 33 | private: 34 | void copyFrom(const Bitset& other); 35 | void free(); 36 | }; -------------------------------------------------------------------------------- /Sem. 15/Solutions/ArrayManipulations/CommandExecutor.cpp: -------------------------------------------------------------------------------- 1 | #include "CommandExecutor.h" 2 | 3 | CommandExecutor::~CommandExecutor() { 4 | while (!toExecute.empty()) { 5 | delete toExecute.peek(); 6 | toExecute.pop(); 7 | } 8 | while (!executed.empty()) { 9 | delete executed.peek(); 10 | executed.popBack(); 11 | } 12 | 13 | } 14 | 15 | void CommandExecutor::add(VectorCommand* c) { 16 | toExecute.push(c); 17 | } 18 | 19 | void CommandExecutor::execute() { 20 | if (toExecute.empty()) { 21 | return; 22 | } 23 | VectorCommand* c = toExecute.peek(); 24 | c->execute(); 25 | executed.pushBack(c); 26 | toExecute.pop(); 27 | } 28 | 29 | void CommandExecutor::executeAll() { 30 | while (!toExecute.empty()) { 31 | execute(); 32 | } 33 | } 34 | 35 | void CommandExecutor::undo() { 36 | if (executed.empty()) { 37 | return; 38 | } 39 | VectorCommand* c = executed.peek(); 40 | c->undo(); 41 | executed.popBack(); 42 | delete c; 43 | } -------------------------------------------------------------------------------- /Sem. 11/Solutions/StringView/StringView.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "MyString.h" 4 | 5 | class StringView 6 | { 7 | const char* start; 8 | const char* end; 9 | 10 | public: 11 | StringView(const char* str); 12 | StringView(const MyString& str); 13 | StringView(const char* start, const char* end); 14 | 15 | size_t length() const; 16 | char operator[](size_t index) const; 17 | StringView substr(size_t fromIndex, size_t length) const; 18 | 19 | friend std::ostream& operator<<(std::ostream& os, const StringView& view); 20 | 21 | friend int compare(const StringView& lhs, const StringView& rhs); 22 | }; 23 | 24 | bool operator<(const StringView& lhs, const StringView& rhs); 25 | bool operator<=(const StringView& lhs, const StringView& rhs); 26 | bool operator>=(const StringView& lhs, const StringView& rhs); 27 | bool operator>(const StringView& lhs, const StringView& rhs); 28 | bool operator==(const StringView& lhs, const StringView& rhs); 29 | bool operator!=(const StringView& lhs, const StringView& rhs); 30 | 31 | -------------------------------------------------------------------------------- /Sem. 10/Examples/PlacementNew.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // https://en.cppreference.com/w/cpp/language/new ctrl+f "placement new" for more info 4 | struct Test { 5 | static int x; 6 | int a; 7 | 8 | Test() { 9 | a = ++x; 10 | std::cout << a << std::endl; 11 | } 12 | 13 | ~Test() { 14 | --x; 15 | std::cout << "rip"; 16 | } 17 | }; 18 | 19 | int Test::x = 0; 20 | 21 | int main() { 22 | constexpr size_t capacity = 4; 23 | // allocate memory for two Test objects in advance 24 | Test* ptr =(Test*) new char[capacity * sizeof(Test)]; 25 | // allocate the test object at the address of ptr[0] 26 | Test* test1 = new (&ptr[0]) Test(); 27 | // allocate the test object at the address of ptr[1] 28 | Test* test2 = new (&ptr[1]) Test(); 29 | 30 | // you should explicitly call the destructor of the objects! 31 | // deleting the allocated memory will not call the destructors! 32 | test1->~Test(); 33 | test2->~Test(); 34 | // finally, delete the allocated memory 35 | delete[] ((char*)ptr); 36 | } -------------------------------------------------------------------------------- /Sem. 14/Solutions/ExpressionCalculator/StringView/StringView.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "MyString.h" 4 | 5 | class StringView { 6 | private: 7 | const char* start; 8 | const char* end; 9 | 10 | public: 11 | StringView(const char* str); 12 | StringView(const MyString& str); 13 | StringView(const char* start, const char* end); 14 | 15 | size_t length() const; 16 | char operator[](size_t index) const; 17 | StringView substr(size_t fromIndex, size_t length) const; 18 | 19 | friend std::ostream& operator<<(std::ostream& os, const StringView& view); 20 | 21 | friend int compare(const StringView& lhs, const StringView& rhs); 22 | }; 23 | 24 | bool operator<(const StringView& lhs, const StringView& rhs); 25 | bool operator<=(const StringView& lhs, const StringView& rhs); 26 | bool operator>=(const StringView& lhs, const StringView& rhs); 27 | bool operator>(const StringView& lhs, const StringView& rhs); 28 | bool operator==(const StringView& lhs, const StringView& rhs); 29 | bool operator!=(const StringView& lhs, const StringView& rhs); -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/Set/Set.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Collection.h" 3 | #include "PureNumbersCollection.h" 4 | 5 | enum CollectionType { 6 | NORMAL_COLLECTION, 7 | SORTED_COLLECTION 8 | }; 9 | 10 | /// 11 | /// Collection of unique elements only 12 | /// They might be sorted/unsorted 13 | /// 14 | class Set : public Collection { 15 | private: 16 | // We don't want a set of interval collection (because this will be a bitset, think about it) 17 | // so we create PureNumbersCollection base class 18 | // that will be inherited only by the collection we want to be in the set 19 | PureNumbersCollection* collection; 20 | 21 | public: 22 | Set(CollectionType); 23 | 24 | // for now deleted because we don't know how to create copy of collection 25 | // we don't know what there is behind 26 | Set(const Set&) = delete; 27 | Set& operator=(const Set&) = delete; 28 | 29 | void add(int) override; 30 | void remove(int) override; 31 | size_t count(int) const override; 32 | bool contains(int) const override; 33 | 34 | ~Set(); 35 | }; -------------------------------------------------------------------------------- /Sem. 15/Design Patterns/Creational/FactoryAndFactoryMethod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Base { 4 | public: 5 | virtual ~Base() = default; 6 | }; 7 | 8 | class Der1 : public Base {}; 9 | 10 | class Der2 : public Base {}; 11 | 12 | // Factory Method!!! 13 | class BaseFactory { 14 | public: 15 | virtual Base* create() const = 0; 16 | virtual ~BaseFactory() = default; 17 | }; 18 | 19 | class Der1Factory : public BaseFactory { 20 | public: 21 | Base* create() const override { 22 | return new Der1(); 23 | } 24 | }; 25 | 26 | class Der2Factory : public BaseFactory { 27 | public: 28 | Base* create() const override { 29 | return new Der2(); 30 | } 31 | }; 32 | 33 | // Factory !!! 34 | BaseFactory* factoryOfFactories(bool useDer1) { 35 | if (useDer1) { 36 | return new Der1Factory(); 37 | } 38 | else { 39 | return new Der2Factory(); 40 | } 41 | } 42 | 43 | 44 | void runApp(BaseFactory* factory) { 45 | 46 | } 47 | 48 | int main() { 49 | BaseFactory* factory = factoryOfFactories(true); 50 | runApp(factory); 51 | delete factory; 52 | } 53 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/Logger.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Logger { 5 | public: 6 | virtual void log(const char* message) = 0; 7 | virtual ~Logger() = default; 8 | }; 9 | 10 | class ConsoleLogger : public Logger { 11 | public: 12 | void log(const char* message) override { 13 | std::cout << message; 14 | } 15 | }; 16 | 17 | class FileLogger : public Logger { 18 | private: 19 | std::ofstream out; 20 | public: 21 | FileLogger(const char* fileName) : out(fileName) { 22 | if (!out.is_open()) { 23 | throw std::exception("Couldn't open file"); 24 | } 25 | } 26 | 27 | void log(const char* message) override { 28 | out << message; 29 | } 30 | }; 31 | 32 | void run(Logger* logger) { 33 | while (true) { 34 | try { 35 | //... 36 | } 37 | catch (const std::exception& ex) { 38 | logger->log(ex.what()); 39 | } 40 | } 41 | 42 | } 43 | 44 | int main() { 45 | #if _DEBUG 46 | Logger* logger = new ConsoleLogger(); 47 | #else 48 | Logger* logger = new FileLogger("logs.txt"); 49 | #endif 50 | run(logger); 51 | delete logger; 52 | } -------------------------------------------------------------------------------- /Sem. 12/Solutions/ShapeCollection/ShapeCollection/ShapeCollection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Shape.h" 3 | 4 | class ShapeCollection { 5 | private: 6 | Shape** shapes = nullptr; 7 | size_t size; 8 | size_t capacity; 9 | 10 | void resize(); 11 | void addShape(Shape*); 12 | public: 13 | ShapeCollection(); 14 | ShapeCollection(const ShapeCollection& other); 15 | ShapeCollection(ShapeCollection&& other) noexcept; 16 | ShapeCollection& operator=(const ShapeCollection& other); 17 | ShapeCollection& operator=(ShapeCollection&& other) noexcept; 18 | ~ShapeCollection(); 19 | 20 | void addTriangle(int x1, int y1, int x2, int y2, int x3, int y3); 21 | void addRectangle(int x1, int y1, int x3, int y3); 22 | void addCircle(int x, int y, double radius); 23 | 24 | double getPerOfFigureByIndex(size_t index) const; 25 | double getAreaOfFigureByIndex(size_t index) const; 26 | bool getIfPointInShapeByIndex(size_t index, int x, int y) const; 27 | private: 28 | void move(ShapeCollection&& other); 29 | void copyFrom(const ShapeCollection& other); 30 | void free(); 31 | }; 32 | 33 | -------------------------------------------------------------------------------- /Sem. 01/Examples/BitFields.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct S1 { 4 | // three-bit unsigned field, allowed values are 0...7 5 | unsigned int b : 3; 6 | // if the value exceeds 7, the result is implementation-defined 7 | }; 8 | 9 | struct S2 { 10 | // will occupy 2 bytes: 11 | unsigned char b1 : 3; // 1st 3 bits (in 1st byte) are b1 12 | unsigned char : 2; // next 2 bits (in 1st byte) are blocked out as unused 13 | unsigned char b2 : 6; // 6 bits for b2 - doesn't fit into the 1st byte => starts a 2nd 14 | unsigned char b3 : 2; // 2 bits for b3 - next (and final) bits in the 2nd byte 15 | }; 16 | // s2 comment 17 | // looks like --- 1110011110111 18 | // breakdown is: └┬┘├┘└─┬──┘└┤ 19 | // b1 u b2 b3 20 | 21 | struct S3 { 22 | // will occupy 2 bytes: 23 | // 3 bits: value of b1 24 | // 5 bits: unused 25 | // 2 bits: value of b2 26 | // 6 bits: unused 27 | unsigned char b1 : 3; 28 | unsigned char :0; // start a new byte !!! == the remaining bits of the current byte are blocked out as unused 29 | unsigned char b2 : 2; 30 | }; 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Georgi Terziev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Sem. 08/Examples/HandleBadAllocInConstructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Grade { 4 | int value; 5 | int randomStuff[50000]; 6 | }; 7 | 8 | // imagine we are not using any collections and strings 9 | class Student { 10 | private: 11 | char* userName = nullptr; 12 | Grade* grades = nullptr; 13 | int gradesCount; 14 | public: 15 | Student(int gradesCount): gradesCount(gradesCount) { 16 | userName = new char[50]; // no need to wrap in try catch, if it fails to allocated, we won't have memory leak 17 | 18 | try { 19 | grades = new Grade[gradesCount]; 20 | } 21 | catch (const std::exception& ex) { 22 | delete[] userName; // !!! 23 | std::cout << ex.what() << std::endl; // bad alloc 24 | throw ex; 25 | } 26 | } 27 | Student(const Student& other) = delete; 28 | Student& operator=(const Student& other) = delete; 29 | ~Student() { // in case of thrown error in the constructor => the destructor won't be called 30 | delete[] userName; 31 | delete[] grades; 32 | } 33 | }; 34 | 35 | int main() { 36 | try { 37 | Student s(5000000); 38 | } 39 | catch (const std::exception& ex) { 40 | std::cout << ""; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/Collections/Interval/IntervalCollection.cpp: -------------------------------------------------------------------------------- 1 | #include "IntervalCollection.h" 2 | #include 3 | 4 | IntervalCollection::IntervalCollection(int start, int end) { 5 | if (end < start) { 6 | std::swap(start, end); 7 | } 8 | 9 | this->start = start; 10 | this->end = end; 11 | 12 | // TODO: memory leak here!!!! 13 | data = new int[end - start + 1] {0}; 14 | } 15 | 16 | 17 | unsigned IntervalCollection::intervalLength() const { 18 | return end - start + 1; 19 | } 20 | 21 | void IntervalCollection::add(int elem) { 22 | if (elem < start || elem > end) { 23 | return; 24 | } 25 | 26 | ++data[elem - start]; 27 | } 28 | 29 | void IntervalCollection::remove(int elem) { 30 | if (elem < start || elem > end) { 31 | return; 32 | } 33 | 34 | int index = elem - start; 35 | 36 | if (data[index] == 0) { 37 | return; 38 | } 39 | 40 | --data[index]; 41 | } 42 | 43 | size_t IntervalCollection::count(int elem) const { 44 | if (elem < start || elem > end) { 45 | return 0; 46 | } 47 | 48 | return data[elem - start]; 49 | } 50 | 51 | bool IntervalCollection::contains(int elem) const { 52 | return count(elem) > 0; 53 | } -------------------------------------------------------------------------------- /Sem. 08/Examples/MyCustomException/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #pragma warning (disable:4996) 4 | 5 | class MyCustomException : public std::exception { 6 | private: 7 | char message[25] = ""; 8 | int code; 9 | public: 10 | // [My] message 11 | MyCustomException(const char* message, int code) : code(code) { 12 | strcat(this->message, "[My]"); 13 | strcat(this->message, message); 14 | } 15 | 16 | int getCode() const { 17 | return code; 18 | } 19 | 20 | char const* what() const { 21 | return message; 22 | } 23 | }; 24 | 25 | struct Test { 26 | ~Test() { 27 | // throw "dada"; 28 | std::cout << "hello" << std::endl; 29 | } 30 | }; 31 | 32 | void g() { 33 | Test t; 34 | throw MyCustomException("Exception from g", 8); 35 | } 36 | 37 | void f() { 38 | Test t; 39 | try { 40 | g(); 41 | } 42 | catch (int code) { 43 | std::cout << "Catched in f()"; 44 | throw code; 45 | } 46 | } 47 | 48 | int main() { 49 | try { 50 | f(); 51 | } 52 | catch (const MyCustomException& ex) { 53 | std::cout << ex.what() << " " << ex.getCode(); 54 | } 55 | catch (const std::exception& ex) { 56 | std::cout << ex.what(); 57 | } 58 | } -------------------------------------------------------------------------------- /Sem. 09/Solutions/UniqueTestPointer/UniqueTestPointer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | struct Test { 6 | int a; 7 | ~Test() { 8 | std::cout << "~"; 9 | } 10 | }; 11 | 12 | class UniqueTestPointer { 13 | private: 14 | Test* resource; 15 | 16 | public: 17 | UniqueTestPointer(Test* resource); 18 | UniqueTestPointer(const UniqueTestPointer&) = delete; 19 | UniqueTestPointer(UniqueTestPointer&&) noexcept; 20 | UniqueTestPointer& operator=(const UniqueTestPointer&) = delete; 21 | UniqueTestPointer& operator=(UniqueTestPointer&&) noexcept; 22 | ~UniqueTestPointer(); 23 | 24 | Test* get() const; // Returns the pointer 25 | void reset(Test* newResource); // Deletes the current pointer and assigns a new one 26 | Test* release(); // Releases the ownership of the pointer and returns it 27 | void swap(UniqueTestPointer& other); // Swaps the resources the pointers 28 | 29 | Test* operator->(); 30 | const Test* operator->() const; 31 | 32 | Test& operator*(); 33 | const Test& operator*() const; 34 | 35 | operator bool() const; 36 | private: 37 | void free(); 38 | void moveFrom(UniqueTestPointer&& other) noexcept; 39 | }; 40 | 41 | -------------------------------------------------------------------------------- /Sem. 04/Solutions/Time/Time.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Time { 4 | private: 5 | unsigned hours, minutes, seconds; 6 | 7 | Time(unsigned seconds); 8 | public: 9 | Time(); 10 | Time(unsigned hours, unsigned minutes, unsigned seconds); 11 | 12 | unsigned getHours() const; 13 | unsigned getMinutes() const; 14 | unsigned getSeconds() const; 15 | 16 | void setHours(unsigned newValue); 17 | void setMinutes(unsigned newValue); 18 | void setSeconds(unsigned newValue); 19 | 20 | void addSecond(); 21 | 22 | unsigned getTotalSeconds() const; 23 | 24 | void print() const; 25 | 26 | /// @brief 27 | /// @param other 28 | /// @return -1 if this is before other, 0 if they are equal, 1 if this is after other 29 | int compare(const Time& other) const; 30 | /// @brief 31 | /// @param other 32 | /// @return number less than 0 if this is before other, 0 if they are equal, number greater than 0 if this is after other 33 | int compare2(const Time& other) const; 34 | 35 | Time getDifference(const Time& other) const; 36 | 37 | Time getTimeToMidnight() const; 38 | bool isDinnerTime() const; 39 | bool isPartyTime() const; 40 | }; -------------------------------------------------------------------------------- /Sem. 05/Solutions/Event/Event.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Time/Time.h" 3 | #include "Date/Date.h" 4 | 5 | namespace EventConstants { 6 | constexpr size_t MAX_NAME_LENGTH = 20; 7 | } 8 | 9 | class Event { 10 | private: 11 | char name[EventConstants::MAX_NAME_LENGTH + 1] = "Unknown"; 12 | Date date; 13 | Time startTime; 14 | Time endTime; 15 | 16 | void validateTimes(); 17 | public: 18 | Event(); 19 | Event(const char* name, const Date& date, const Time& startTime, const Time& endTime); 20 | 21 | Event(const char* name, unsigned day, unsigned month, unsigned year, 22 | unsigned startTimeHours, unsigned startTimeMins, unsigned startTimeSecs, 23 | unsigned endTimeHours, unsigned endTimeMins, unsigned endTimeSecs); 24 | 25 | const char* getName() const; 26 | void setName(const char* str); 27 | 28 | const Date& getDate() const; 29 | // Question: 30 | // if there is no validation is there need of setter and getter 31 | void setDate(const Date& date); 32 | 33 | const Time& getStartTime() const; 34 | void setStartTime(const Time& startTime); 35 | 36 | const Time& getEndTime() const; 37 | void setEndTime(const Time& endTime); 38 | }; -------------------------------------------------------------------------------- /Sem. 05/Solutions/Event/Time/Time.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Time { 4 | private: 5 | unsigned hours, minutes, seconds; 6 | 7 | Time(unsigned seconds); 8 | public: 9 | Time(); 10 | Time(unsigned hours, unsigned minutes, unsigned seconds); 11 | 12 | unsigned getHours() const; 13 | unsigned getMinutes() const; 14 | unsigned getSeconds() const; 15 | 16 | void setHours(unsigned newValue); 17 | void setMinutes(unsigned newValue); 18 | void setSeconds(unsigned newValue); 19 | 20 | void addSecond(); 21 | 22 | unsigned getTotalSeconds() const; 23 | 24 | void print() const; 25 | 26 | /// @brief 27 | /// @param other 28 | /// @return -1 if this is before other, 0 if they are equal, 1 if this is after other 29 | int compare(const Time& other) const; 30 | /// @brief 31 | /// @param other 32 | /// @return number less than 0 if this is before other, 0 if they are equal, number greater than 0 if this is after other 33 | int compare2(const Time& other) const; 34 | 35 | Time getDifference(const Time& other) const; 36 | 37 | Time getTimeToMidnight() const; 38 | bool isDinnerTime() const; 39 | bool isPartyTime() const; 40 | }; -------------------------------------------------------------------------------- /Practicum/Pract. 07/README.md: -------------------------------------------------------------------------------- 1 | # OOП - Практикум 31.03.2025 - Седмица 7 2 | 3 | **Задача 1.** Да се разработи система за управление на книги в библиотека. 4 | 5 | Създайте клас `Book`, който описва книга в библиотеката. Всяка книга има: 6 | 7 | - Уникален идентификатор (ID), който се генерира автоматично 8 | - Заглавие (низ с произволна дължина) 9 | - Автор (низ с произволна дължина) 10 | - Статус (дали книгата е налична за заемане) 11 | 12 | Създайте клас `User`- описва потребител, който може да заема книги. Всеки потребител има: 13 | - потребителско име (до 8 символа) 14 | - телефонен номер за връзка (до 10 символа) 15 | 16 | Създайте клас `Library` - библиотека, която съдържа списък от книги (до 50). 17 | 18 | Реализирайте следните функционалности: 19 | - Добавяне на нова книга, ако капацитетът го позволява 20 | - Заемане на книга по дадено ID и име на потребителя – отбелязва книгата като "заета" и кой потребител я е заел 21 | - Връщане на книга по дадено ID – отбелязва книгата като "налична" 22 | - Извеждане на информация – принтира ID, заглавие, автор и статус 23 | 24 | При опит за заемане на вече заета книга или връщане на вече налична книга, се хвърля изключение. Всички възникнали изключения се обработват, за да не достигнат до крайния потребител. -------------------------------------------------------------------------------- /Sem. 03/Examples/ReadAndWriteStudentArrayWithoutDynamicDataToBinaryFile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace Constants { 5 | const size_t MAX_NAME_SIZE = 20; 6 | const char FILE_NAME[] = "test.txt"; 7 | } 8 | 9 | struct Student { 10 | int number; 11 | char name[Constants::MAX_NAME_SIZE + 1]; 12 | // 3 extra padding 13 | }; 14 | 15 | size_t getFileSize(std::ifstream& f) { 16 | size_t currentPos = f.tellg(); 17 | f.seekg(0, std::ios::end); 18 | size_t size = f.tellg(); 19 | 20 | f.seekg(currentPos); 21 | return size; 22 | } 23 | 24 | 25 | int main() { 26 | { 27 | Student s[] = { {155555, "12345678901234567890"}, {65, "123"}}; 28 | 29 | 30 | std::ofstream out(Constants::FILE_NAME, std::ios::binary); 31 | if (!out.is_open()) { 32 | return -1; 33 | } 34 | 35 | out.write((const char*)&s, sizeof(s)); 36 | 37 | out.close(); 38 | } 39 | 40 | { 41 | std::ifstream in(Constants::FILE_NAME, std::ios::binary); 42 | if (!in.is_open()) { 43 | return -1; 44 | } 45 | 46 | size_t fSize = getFileSize(in); 47 | Student* s = new Student[fSize / sizeof(Student)]; 48 | 49 | in.read((char*)s, fSize); 50 | 51 | std::cout << s[0].number << "\n" << s[0].name; 52 | 53 | in.close(); 54 | } 55 | 56 | 57 | } -------------------------------------------------------------------------------- /Sem. 07/Examples/Wrappers/IntWrapper/IntWrapper.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class IntWrapper { 5 | private: 6 | int value; 7 | 8 | public: 9 | IntWrapper(int value); 10 | 11 | int getValue() const; 12 | 13 | IntWrapper& operator=(int other); 14 | 15 | IntWrapper& operator+=(int other); 16 | IntWrapper& operator+=(IntWrapper& other); 17 | 18 | // Prefix increment 19 | IntWrapper& operator++(); 20 | // Postfix increment 21 | IntWrapper operator++(int); 22 | 23 | // Conversion operator 24 | operator bool() const; 25 | 26 | // For the students: you can try out this 27 | //void operator()(int a, int b) 28 | 29 | // this is first declaration of operator<< as a friend function 30 | // you can place the friend stuff also in the protected, private => it doesn't matter 31 | friend std::istream& operator>>(std::istream& in, IntWrapper& integer); 32 | }; 33 | 34 | IntWrapper operator+(const IntWrapper& lhs, const IntWrapper& rhs); 35 | 36 | std::ostream& operator<<(std::ostream& out, const IntWrapper& integer); 37 | 38 | // this is the same declaration of operator<< as a function 39 | // No need to write it two times only the friend one is enough 40 | // std::istream& operator>>(std::istream& in, IntWrapper& integer); -------------------------------------------------------------------------------- /Sem. 15/Solutions/ArrayManipulations/mainAndFactory.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Containers/IntVector.h" 3 | #include "Command/VectorCommand.h" 4 | #include "Command/SwapCommand.h" 5 | #include "Command/SortCommand.h" 6 | #include "CommandExecutor.h" 7 | 8 | void print(const IntVector& v) { 9 | for (size_t i = 0; i < v.getSize(); i++) { 10 | std::cout << v[i] << " "; 11 | } 12 | std::cout << std::endl; 13 | } 14 | 15 | struct Factory { 16 | public: 17 | static VectorCommand* create(IntVector& v, size_t commandNumber) { 18 | if (commandNumber == 1) { 19 | size_t from, to; 20 | std::cin >> from >> to; 21 | return new SwapCommand(v, from, to); 22 | } 23 | else if (commandNumber == 2) { 24 | return new SortCommand(v); 25 | } 26 | 27 | throw std::invalid_argument("Invalid command number"); 28 | } 29 | }; 30 | 31 | int main() { 32 | IntVector v; 33 | for (int i = 9; i >= 0; i--) { 34 | v.pushBack(i); 35 | } 36 | 37 | CommandExecutor ce; 38 | 39 | VectorCommand* vc1 = new SwapCommand(v, 4, 5); 40 | VectorCommand* vc2 = new SortCommand(v); 41 | 42 | ce.add(vc1); 43 | ce.add(vc2); 44 | print(v); 45 | ce.execute(); 46 | print(v); 47 | ce.execute(); 48 | print(v); 49 | ce.undo(); 50 | print(v); 51 | ce.undo(); 52 | print(v); 53 | } 54 | -------------------------------------------------------------------------------- /Sem. 08/Solutions/StringPool/StringPool/StringPool.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class StringPool { 4 | private: 5 | static constexpr int INITIAL_CAPACITY = 8; 6 | 7 | struct StringNode { 8 | char* data = nullptr; 9 | size_t referenceCount = 0; 10 | 11 | // This is internal class we can mark the operators as default (cc is deleted) 12 | // why? 13 | // as an internal class => only I can create create instances inside me 14 | // so I am the owner of these resources, I can delete them 15 | StringNode() = default; 16 | StringNode(const StringNode&) = delete; 17 | StringNode& operator=(const StringNode&) = default; 18 | ~StringNode() = default; 19 | 20 | void allocateData(const char* str); 21 | }; 22 | 23 | StringNode* nodes = nullptr; 24 | size_t size = 0; 25 | size_t capacity = INITIAL_CAPACITY; 26 | 27 | bool lowerBound(const char* str, int& index); 28 | const char* insert(const char* str, size_t index); 29 | void resize(); 30 | void removeStringFromPool(int index); 31 | public: 32 | StringPool(); 33 | StringPool(const StringPool& pool) = delete; 34 | StringPool& operator=(const StringPool& other) = delete; 35 | ~StringPool(); 36 | 37 | const char* getString(const char* str); 38 | void removeString(const char* str); 39 | 40 | void print() const; 41 | }; 42 | -------------------------------------------------------------------------------- /Sem. 15/README.md: -------------------------------------------------------------------------------- 1 | # Type casting. SOLID принципи. Въведение в Design Patterns. 2 | 3 | Със следващите линкове може да достъпите съответните материали: 4 | 5 | ### [Type Casting](./Type%20Casting) 6 | 7 | ### [SOLID Principles](./SOLID%20Principles) 8 | 9 | ### [Design Patterns](./Design%20Patterns) 10 | 11 | ## Задачи 12 | 13 | **Задача 1: Пример от конспект SingletonFactory** 14 | 15 | На база поредно прочетено число от файл решаваме какъв обект да създадем. Защо да е Singleton, няма нужда всеки път да отворяме поток към файла само, за да прочитаме едно число - зареждаме файла един път в началото и ползваме вече прочетен масив. 16 | 17 | **Задача 2: Array manipulations** 18 | 19 | Да се имплементира система, която изпълнява различни операции върху масив от числа. 20 | 21 | Ще имаме две операции: 22 | - Swap - която размества елемените на два индекса. 23 | - Sort - която сортира масива в нарастващ ред. 24 | 25 | Всяка изпълнена операция може да бъде отменена - т.е. да се върхем в предходното състояние на масива, 26 | преди изпълнението на операцията. 27 | 28 | Системата трябва да поддържа: 29 | - добавяне на операция към опашката от операции 30 | - изпълняване на операция от опашката 31 | - изпълнение на всички операции от опашката 32 | - отмяна на последно изпълнената операция 33 | -------------------------------------------------------------------------------- /Sem. 04/Examples/MemberFunctions/Encapsulation-FirstQuadrantPoint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // X and Y of every instance of FirstQuadrantPoint 5 | // should be non-negative 6 | struct FirstQuadrantPoint { 7 | private: // can not be accessed from outside 8 | int x, y; 9 | 10 | public: // can be accessed from outside 11 | FirstQuadrantPoint(int x, int y) : x(0), y(0) { 12 | setX(x); 13 | setY(y); 14 | } 15 | 16 | // returning copy so that the original value can not be changed 17 | int getX() const { 18 | return x; 19 | } 20 | 21 | // the only way to change the value of x 22 | // every time the validation is applied 23 | void setX(int value) { 24 | if (value < 0) { 25 | return; 26 | } 27 | x = value; 28 | } 29 | 30 | int getY() const { 31 | return y; 32 | } 33 | 34 | void setY(int value) { 35 | if (value < 0) { 36 | return; 37 | } 38 | y = value; 39 | } 40 | }; 41 | 42 | int main() { 43 | { 44 | FirstQuadrantPoint p(1, 2); 45 | p.setY(-100); 46 | p.setY(-100); 47 | p.setY(100); 48 | std::cout << p.getX() << " " << p.getY(); // 1 100 49 | } 50 | { 51 | FirstQuadrantPoint p(-1, -2); 52 | p.setY(-100); 53 | p.setY(-100); 54 | p.setY(100); 55 | std::cout << p.getX() << " " << p.getY(); // 0 100 56 | } 57 | } -------------------------------------------------------------------------------- /Sem. 12/Solutions/ShapeCollection/Shape/Shape.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Triangle; 4 | class Rectangle; 5 | class Circle; 6 | 7 | class Shape { 8 | protected: 9 | struct Point { 10 | int x, y; 11 | Point(); 12 | Point(int x, int y); 13 | }; 14 | 15 | friend double getDistance(const Point& p1, const Point& p2); 16 | 17 | Point* points = nullptr; 18 | size_t pointsCount = 0; 19 | 20 | public: 21 | Shape(size_t pointsCount); 22 | Shape(const Shape& other); 23 | Shape(Shape&& other) noexcept; 24 | Shape& operator=(const Shape& other); 25 | Shape& operator=(Shape&& other) noexcept; 26 | virtual ~Shape(); 27 | 28 | virtual Shape* clone() const = 0; 29 | 30 | const Point& getPoint(size_t index) const; 31 | void setPoint(size_t index, int x, int y); 32 | 33 | virtual double getArea() const = 0; 34 | virtual double getPer() const = 0; 35 | virtual bool isPointIn(int x, int y) const = 0; 36 | 37 | virtual bool intersectsWith(const Shape* other) const = 0; 38 | 39 | virtual bool intersectsWithTriangle(const Triangle* other) const = 0; 40 | virtual bool intersectsWithRect(const Rectangle* other) const = 0; 41 | virtual bool intersectsWithCircle(const Circle* other) const = 0; 42 | private: 43 | void move(Shape&& other); 44 | void copyFrom(const Shape& other); 45 | void free(); 46 | }; 47 | 48 | -------------------------------------------------------------------------------- /Sem. 10/Solutions/Bar/Bar/Bar.cpp: -------------------------------------------------------------------------------- 1 | #include "Bar.h" 2 | 3 | void Bar::addDrink(const Drink& drink, size_t count) { 4 | if (drinksCount >= Bar::MAX_DRINKS_SIZE) { 5 | return; 6 | } 7 | 8 | drinks[drinksCount] = drink; 9 | drinksCounter[drinksCount++] = count; 10 | } 11 | 12 | void Bar::addAlcoholDrink(const AlcoholDrink& drink, size_t count) { 13 | if (alcDrinksCount >= Bar::MAX_DRINKS_SIZE) { 14 | return; 15 | } 16 | 17 | alcDrinks[alcDrinksCount] = drink; 18 | alcDrinksCounter[alcDrinksCount++] = count; 19 | } 20 | 21 | Drink Bar::getDrink() { 22 | if (drinksCount == 0) { 23 | throw std::exception("Drinks array is empty"); 24 | } 25 | 26 | stats.mlSold += drinks[drinksCount - 1].getMl(); 27 | stats.soldDrinks++; 28 | 29 | if (--drinksCounter[drinksCount - 1] == 0) { 30 | drinksCount--; 31 | } 32 | 33 | return drinks[drinksCount]; 34 | } 35 | 36 | AlcoholDrink Bar::getAlcoholDrink() { 37 | if (alcDrinksCount == 0) { 38 | throw std::exception("Drinks array is empty"); 39 | } 40 | 41 | stats.mlSold += alcDrinks[alcDrinksCount - 1].getMl(); 42 | stats.soldAlcoholDrinks++; 43 | 44 | if (--alcDrinksCounter[alcDrinksCount - 1] == 0) { 45 | alcDrinksCount--; 46 | } 47 | 48 | return alcDrinks[alcDrinksCount]; 49 | } 50 | 51 | const Bar::Statistics& Bar::getStats() const { 52 | return stats; 53 | } -------------------------------------------------------------------------------- /Sem. 08/Solutions/MyString (With Exceptions)/MyString.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class MyString { 5 | private: 6 | char* data; 7 | size_t length; 8 | 9 | // for memory allocation only! 10 | // allocates length + 1 11 | explicit MyString(size_t length); 12 | 13 | public: 14 | MyString(); 15 | MyString(const char* arr); 16 | MyString(const MyString& other); 17 | MyString& operator=(const MyString& other); 18 | ~MyString(); 19 | 20 | const char* c_str() const; 21 | size_t getLength() const; 22 | 23 | char& operator[](size_t index); 24 | char operator[](size_t index) const; 25 | 26 | MyString& operator+=(const MyString& other); 27 | 28 | friend MyString operator+(const MyString& lhs, const MyString& rhs); 29 | friend std::istream& operator>>(std::istream& in, MyString& str); 30 | private: 31 | void free(); 32 | void copyFrom(const MyString& other); 33 | }; 34 | 35 | std::ostream& operator<<(std::ostream& out, const MyString& str); 36 | bool operator<(const MyString& lhs, const MyString& rhs); 37 | bool operator<=(const MyString& lhs, const MyString& rhs); 38 | bool operator>=(const MyString& lhs, const MyString& rhs); 39 | bool operator>(const MyString& lhs, const MyString& rhs); 40 | bool operator==(const MyString& lhs, const MyString& rhs); 41 | bool operator!=(const MyString& lhs, const MyString& rhs); -------------------------------------------------------------------------------- /Sem. 14/Solutions/ExpressionCalculator/MyString/MyString.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class MyString { 5 | private: 6 | char* data; 7 | size_t length; 8 | 9 | // for memory allocation only! 10 | // allocates length + 1 11 | explicit MyString(size_t length); 12 | 13 | public: 14 | MyString(); 15 | MyString(const char* arr); 16 | MyString(const MyString& other); 17 | MyString& operator=(const MyString& other); 18 | ~MyString(); 19 | 20 | const char* c_str() const; 21 | size_t getLength() const; 22 | 23 | char& operator[](size_t index); 24 | char operator[](size_t index) const; 25 | 26 | MyString& operator+=(const MyString& other); 27 | 28 | friend MyString operator+(const MyString& lhs, const MyString& rhs); 29 | friend std::istream& operator>>(std::istream& in, MyString& str); 30 | private: 31 | void free(); 32 | void copyFrom(const MyString& other); 33 | }; 34 | 35 | std::ostream& operator<<(std::ostream& out, const MyString& str); 36 | bool operator<(const MyString& lhs, const MyString& rhs); 37 | bool operator<=(const MyString& lhs, const MyString& rhs); 38 | bool operator>=(const MyString& lhs, const MyString& rhs); 39 | bool operator>(const MyString& lhs, const MyString& rhs); 40 | bool operator==(const MyString& lhs, const MyString& rhs); 41 | bool operator!=(const MyString& lhs, const MyString& rhs); -------------------------------------------------------------------------------- /Sem. 02/Solutions/StringToUnsignedConversion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace HelperFunctions { 5 | bool isDigit(char ch) { 6 | return '0' <= ch && ch <= '9'; 7 | } 8 | 9 | unsigned getDigitFromChar(char ch) { 10 | return ch - '0'; 11 | } 12 | }; 13 | 14 | enum class ErrorCode { 15 | NoError, 16 | NullptrProvided, 17 | EmptyStringProvided, 18 | StringIsNotANumber 19 | }; 20 | 21 | struct ConversionResult { 22 | ErrorCode errorCode; 23 | unsigned result; 24 | }; 25 | 26 | ConversionResult strToUnsignedInt(const char* str) { 27 | if (!str) { 28 | return { ErrorCode::NullptrProvided, 0 }; 29 | } 30 | 31 | if (!*str) { 32 | return { ErrorCode::EmptyStringProvided, 0 }; 33 | } 34 | 35 | unsigned result = 0; 36 | while (*str) { 37 | if (!HelperFunctions::isDigit(*str)) { 38 | return { ErrorCode::StringIsNotANumber, 0 }; 39 | } 40 | 41 | result *= 10; 42 | result += HelperFunctions::getDigitFromChar(*str); 43 | str++; 44 | } 45 | 46 | return { ErrorCode::NoError, result }; 47 | } 48 | 49 | int main() { 50 | ConversionResult conversionResult = strToUnsignedInt("123b45"); 51 | 52 | if (conversionResult.errorCode == ErrorCode::NoError) { 53 | std::cout << conversionResult.result; 54 | } 55 | else { 56 | std::cout << "Error occurred with status: " << (int)conversionResult.errorCode; 57 | } 58 | } -------------------------------------------------------------------------------- /Sem. 13/Examples/VTableMultipleInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A { 5 | virtual void f() { 6 | std::cout << "A::f()" << std::endl; 7 | } 8 | }; 9 | struct B { 10 | virtual void g() { 11 | std::cout << "B::g()" << std::endl; 12 | } 13 | }; 14 | struct C : A, B { 15 | virtual void h() { 16 | std::cout << "C::h()" << std::endl; 17 | } 18 | }; 19 | 20 | template 21 | void printVtable(T* t, size_t size) { 22 | void (**vt)() = *(void (***)())t; 23 | // auto funcAddress1 = vt[0]; 24 | // auto funcAddress2 = vt[1]; 25 | 26 | for (size_t i = 0; i < size; i++) { 27 | std::cout << vt[i] << std::endl; 28 | // invoke the function 29 | vt[i](); 30 | } 31 | std::cout << std::endl; 32 | } 33 | 34 | int main() { 35 | std::cout << "Print object of type A vtable" << std::endl; 36 | A* aTableTest = new A(); 37 | printVtable(aTableTest, 1); 38 | std::cout << "Print object of type B vtable" << std::endl; 39 | B* bTableTest = new B(); 40 | printVtable(bTableTest, 1); 41 | 42 | std::cout << "Print object of type C vtable coming from inheriting A" << std::endl; 43 | C* cTableTest = new C(); 44 | printVtable(cTableTest, 2); 45 | 46 | std::cout << "Print object of type C vtable coming from inheriting B" << std::endl; 47 | B* bTableFromCTest = (C*)cTableTest; 48 | printVtable(bTableFromCTest, 1); 49 | } 50 | -------------------------------------------------------------------------------- /Sem. 07/Solutions/MyString_Bonus_SmallStringOptimization/MyStringSSO.h: -------------------------------------------------------------------------------- 1 | #include 2 | #pragma once 3 | 4 | // Idea: for strings of size <= SSO_MAX_SIZE, store the string in the already allocated memory for _size and data; 5 | // For larger strings, use dynamic memory allocation. 6 | class MyString { 7 | static const short SSO_MAX_SIZE = sizeof(char*) + sizeof(size_t) - 1; 8 | 9 | union { 10 | struct { 11 | char* _data; 12 | size_t _size; 13 | }; 14 | char ssoData[MyString::SSO_MAX_SIZE + 1]{ '\0' }; 15 | }; 16 | 17 | bool isSso() const; 18 | void move(MyString&& other); 19 | void copyFrom(const MyString& other); 20 | void free(); 21 | 22 | explicit MyString(size_t size); 23 | 24 | void notUsingSso(); 25 | public: 26 | 27 | MyString(); 28 | MyString(const char* data); 29 | 30 | MyString(const MyString& other); 31 | MyString& operator=(const MyString& other); 32 | 33 | MyString(MyString&& other) noexcept; 34 | MyString& operator=(MyString&& other) noexcept; 35 | 36 | MyString& operator+=(const MyString& other); 37 | 38 | const char* c_str() const; 39 | size_t length() const; 40 | 41 | char& operator[](size_t index); 42 | char operator[](size_t index) const; 43 | 44 | ~MyString(); 45 | 46 | friend MyString operator+(const MyString& lhs, const MyString& rhs); 47 | }; 48 | 49 | std::ostream& operator<<(std::ostream& os, const MyString& obj); -------------------------------------------------------------------------------- /Sem. 12/Solutions/SimpleVisitor-Buildings/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class House; 4 | class Factory; 5 | class Bank; 6 | 7 | // Visitor 8 | class Person { 9 | public: 10 | void visit(const House* house) { 11 | std::cout << "sleep"; 12 | } 13 | void visit(const Factory* factory) { 14 | std::cout << "work"; 15 | } 16 | void visit(const Bank* bank) { 17 | std::cout << "withdraw"; 18 | } 19 | }; 20 | 21 | // Accepts the visitor 22 | class Building { 23 | public: 24 | virtual void accept(Person* person) = 0; 25 | virtual ~Building() = default; 26 | }; 27 | 28 | class House : public Building { 29 | public: 30 | void accept(Person* person) override { 31 | person->visit(this); 32 | } 33 | }; 34 | 35 | class Factory : public Building { 36 | public: 37 | void accept(Person* person) override { 38 | person->visit(this); 39 | } 40 | }; 41 | 42 | class Bank : public Building { 43 | public: 44 | void accept(Person* person) override { 45 | person->visit(this); 46 | } 47 | }; 48 | 49 | int main() { 50 | Person person; 51 | House house; 52 | Factory factory; 53 | Bank bank; 54 | 55 | Building* buildings[] = {&house, &factory, &bank}; 56 | 57 | for (size_t i = 0; i < 3; i++) { 58 | buildings[i]->accept(&person); 59 | std::cout << std::endl; 60 | } 61 | } -------------------------------------------------------------------------------- /Sem. 15/Solutions/SingletonFactory/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Base { 4 | public: 5 | virtual ~Base() = default; 6 | }; 7 | class A : public Base { }; 8 | class B : public Base { }; 9 | 10 | class SingletonFactory { 11 | SingletonFactory() { 12 | std::ifstream in("data.txt"); 13 | if (!in.is_open()) { 14 | throw std::exception("Could not open data.txt"); 15 | } 16 | 17 | in >> numbersSize; 18 | numbers = new size_t[numbersSize]; 19 | size_t index = 0; 20 | while (!in.eof()) { 21 | in >> numbers[index++]; 22 | } 23 | } 24 | 25 | ~SingletonFactory() { 26 | delete[] numbers; 27 | } 28 | 29 | size_t current = 0; 30 | size_t numbersSize = 0; 31 | size_t* numbers = nullptr; 32 | public: 33 | Base* create() { 34 | if (current >= numbersSize) { 35 | return nullptr; 36 | } 37 | 38 | if (numbers[current++] % 2 == 0) { 39 | return new A(); 40 | } 41 | else { 42 | return new B(); 43 | } 44 | } 45 | 46 | static SingletonFactory& getInstance() { 47 | static SingletonFactory instance; 48 | return instance; 49 | } 50 | 51 | SingletonFactory(const SingletonFactory& other) = delete; 52 | SingletonFactory& operator=(const SingletonFactory& other) = delete; 53 | }; 54 | 55 | int main() { 56 | SingletonFactory& sf = SingletonFactory::getInstance(); 57 | 58 | sf.create(); 59 | sf.create(); 60 | sf.create(); 61 | sf.create(); 62 | } 63 | -------------------------------------------------------------------------------- /Sem. 11/Solutions/Shapes/Triangle.cpp: -------------------------------------------------------------------------------- 1 | #include "Triangle.h" 2 | #include 3 | 4 | Triangle::Triangle(int x1, int y1, int x2, int y2, int x3, int y3) : Shape(3) 5 | { 6 | setPoint(0, x1, y1); 7 | setPoint(1, x2, y2); 8 | setPoint(2, x3, y3); 9 | } 10 | double Triangle::getArea() const 11 | { 12 | const Shape::Point& p0 = getPointAtIndex(0); 13 | const Shape::Point& p1 = getPointAtIndex(1); 14 | const Shape::Point& p2 = getPointAtIndex(2); 15 | 16 | return abs(p0.x * p1.y + p1.x * p2.y + p2.x * p0.y - p0.y * p1.x - p1.y * p2.x - p2.y * p0.x) / 2.00; 17 | } 18 | double Triangle::getPerimeter() const 19 | { 20 | const Shape::Point& p0 = getPointAtIndex(0); 21 | const Shape::Point& p1 = getPointAtIndex(1); 22 | const Shape::Point& p2 = getPointAtIndex(2); 23 | 24 | return p0.getDistance(p1) + p1.getDistance(p2) + p2.getDistance(p0); 25 | } 26 | 27 | bool Triangle::isPointIn(int x, int y) const 28 | { 29 | Shape::Point p(x, y); 30 | Triangle t1(getPointAtIndex(0).x, getPointAtIndex(0).y, getPointAtIndex(1).x, getPointAtIndex(1).y, p.x, p.y); 31 | Triangle t2(getPointAtIndex(2).x, getPointAtIndex(2).y, getPointAtIndex(1).x, getPointAtIndex(1).y, p.x, p.y); 32 | Triangle t3(getPointAtIndex(2).x, getPointAtIndex(2).y, getPointAtIndex(0).x, getPointAtIndex(0).y, p.x, p.y); 33 | 34 | return abs(t1.getArea() + t2.getArea() + t3.getArea() - getArea()) <= std::numeric_limits::epsilon(); 35 | } -------------------------------------------------------------------------------- /Sem. 07/Examples/Wrappers/IntWrapper/IntWrapper.cpp: -------------------------------------------------------------------------------- 1 | #include "IntWrapper.h" 2 | 3 | IntWrapper::IntWrapper(int value) : value(value) {} 4 | 5 | int IntWrapper::getValue() const { 6 | return value; 7 | } 8 | 9 | IntWrapper& IntWrapper::operator=(int newValue) { 10 | value = newValue; 11 | return *this; 12 | } 13 | 14 | 15 | IntWrapper& IntWrapper::operator+=(int other) { 16 | this->value += other; 17 | return *this; 18 | } 19 | 20 | IntWrapper& IntWrapper::operator+=(IntWrapper& other) { 21 | this->value += other.value; 22 | return *this; 23 | } 24 | 25 | IntWrapper& IntWrapper::operator++() { 26 | value += 1; 27 | return *this; 28 | } 29 | 30 | IntWrapper IntWrapper::operator++(int) { 31 | IntWrapper copy(*this); 32 | value++; 33 | return copy; 34 | } 35 | 36 | IntWrapper::operator bool() const { 37 | return value != 0; 38 | } 39 | 40 | // No need to make it friend we can use the getter 41 | std::ostream &operator<<(std::ostream &out, const IntWrapper &integer) { 42 | return out << integer.getValue(); 43 | } 44 | 45 | std::istream &operator>>(std::istream &in, IntWrapper &integer) { 46 | // as it is defined as a friend we can directly access the private member 47 | return in >> integer.value; 48 | } 49 | 50 | IntWrapper operator+(const IntWrapper& lhs, const IntWrapper& rhs) { 51 | return IntWrapper(lhs.getValue() + rhs.getValue()); 52 | } -------------------------------------------------------------------------------- /Sem. 10/Solutions/GoogleDocumentAccessors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "MyString/MyString.h" 3 | 4 | // Google docs 5 | struct Comment { 6 | MyString text; 7 | MyString createdBy; 8 | }; 9 | 10 | class GoogleDocsDocument { 11 | private: 12 | MyString data; 13 | Comment comments[30]; 14 | size_t count = 0; 15 | 16 | public: 17 | const MyString& getData() const { 18 | return data; 19 | } 20 | 21 | void addComment(const Comment& comment) { 22 | if(count == 30) { 23 | return; 24 | } 25 | 26 | comments[count++] = comment; 27 | } 28 | 29 | void addComment(Comment&& comment) { 30 | if(count == 30) { 31 | return; 32 | } 33 | 34 | comments[count++] = std::move(comment); 35 | } 36 | 37 | void setText(const MyString& text) { 38 | data = text; 39 | } 40 | 41 | void setText(MyString&& text) { 42 | data = std::move(text); 43 | } 44 | }; 45 | 46 | class Viewer { 47 | public: 48 | void viewDocument(const GoogleDocsDocument& doc) const { 49 | std::cout << doc.getData(); 50 | } 51 | }; 52 | 53 | class Commenter : public Viewer { 54 | MyString userName; 55 | public: 56 | void addComment(GoogleDocsDocument& doc) { 57 | doc.addComment({ "ada", userName }); 58 | } 59 | }; 60 | 61 | class Editor : public Commenter { 62 | public: 63 | void edit(GoogleDocsDocument& doc) { 64 | doc.setText("wleeeeeeeeeee"); 65 | } 66 | }; -------------------------------------------------------------------------------- /Sem. 12/Solutions/ShapeCollection/Circle/Circle.cpp: -------------------------------------------------------------------------------- 1 | #include "Circle.h" 2 | #include 3 | 4 | namespace { 5 | const double PI = 3.1415; 6 | } 7 | 8 | Circle::Circle(int x, int y, double radius) : Shape(1), radius(radius) { 9 | setPoint(0, x, y); 10 | } 11 | 12 | Shape* Circle::clone() const { 13 | return new Circle(*this); 14 | } 15 | 16 | double Circle::getArea() const { 17 | return PI * radius * radius; 18 | } 19 | 20 | double Circle::getPer() const { 21 | return 2 * PI * radius; 22 | } 23 | 24 | bool Circle::isPointIn(int x, int y) const { 25 | Shape::Point point(x, y); 26 | return getDistance(point, getPoint(0)) <= radius; 27 | } 28 | #include "Circle.h" 29 | 30 | const double PI = 3.1415; 31 | Circle::Circle(int x, int y, double radius) : Shape(1), radius(radius) 32 | { 33 | setPoint(0, x, y); 34 | } 35 | 36 | bool Circle::intersectsWith(const Shape* other) const { 37 | return other->intersectsWithCircle(this); 38 | } 39 | bool Circle::intersectsWithTriangle(const Triangle* other) const { 40 | std::cout << "Formula for circle with triangle" << std::endl; 41 | return true; 42 | } 43 | bool Circle::intersectsWithRect(const Rectangle* other) const { 44 | std::cout << "Formula for circle with rect" << std::endl; 45 | return true; 46 | } 47 | bool Circle::intersectsWithCircle(const Circle* other) const { 48 | std::cout << "Formula for circle with circle" << std::endl; 49 | return true; 50 | } -------------------------------------------------------------------------------- /Practicum/Pract. 08/String/String.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class String 5 | { 6 | char* data; 7 | unsigned len; 8 | unsigned capacity; 9 | 10 | void copyFrom(const String& other); 11 | void free(); 12 | void resize(); 13 | 14 | explicit String(unsigned capacity); 15 | 16 | public: 17 | String(); 18 | String(const char* str); 19 | String(const String& other); 20 | String& operator=(const String& other); 21 | ~String(); 22 | 23 | unsigned length() const; 24 | const char* c_str() const; 25 | String& concat(const String& other); 26 | 27 | String& operator+=(const String& other); 28 | String& operator+=(char ch); 29 | 30 | char& operator[](size_t index); 31 | char operator[](size_t index) const; 32 | 33 | friend String operator+(const String& lhs, const String& rhs); 34 | friend std::istream& operator>>(std::istream& is, String& str); 35 | }; 36 | 37 | std::ostream& operator<<(std::ostream& os, const String& str); 38 | 39 | bool operator<(const String& lhs, const String& rhs); 40 | bool operator<=(const String& lhs, const String& rhs); 41 | bool operator>=(const String& lhs, const String& rhs); 42 | bool operator>(const String& lhs, const String& rhs); 43 | bool operator==(const String& lhs, const String& rhs); 44 | bool operator!=(const String& lhs, const String& rhs); 45 | 46 | unsigned getNextPowerOfTwo(unsigned n); -------------------------------------------------------------------------------- /Sem. 11/Solutions/ReadWriteFiles/Common/StringView/StringView.cpp: -------------------------------------------------------------------------------- 1 | #include "StringView.h" 2 | 3 | StringView::StringView(const char* begin, const char* end) : _begin(begin), _end(end) 4 | { } 5 | 6 | StringView::StringView(const char* str) : StringView(str, str + strlen(str)) 7 | { } 8 | 9 | StringView::StringView(const MyString& str) : StringView(str.c_str()) 10 | { } 11 | 12 | size_t StringView::length() const { 13 | return _end - _begin; 14 | } 15 | 16 | char StringView::operator[](size_t index) const { 17 | return _begin[index]; 18 | } 19 | 20 | StringView StringView::substr(size_t from, size_t length) const { 21 | if (_begin + from + length > _end) { 22 | throw std::length_error("Error, Substr out of range"); 23 | } 24 | 25 | return StringView(_begin + from, _begin + from + length); 26 | } 27 | 28 | std::ostream& operator<<(std::ostream& os, const StringView& strView) { 29 | const char* iter = strView._begin; 30 | 31 | while (iter != strView._end) { 32 | os << *iter; 33 | iter++; 34 | } 35 | return os; 36 | } 37 | 38 | bool operator==(const StringView& lhs, const StringView& rhs) 39 | { 40 | if (lhs.length() != rhs.length()) { 41 | return false; 42 | } 43 | for (size_t i = 0; i < lhs.length(); i++) { 44 | if (lhs[i] != rhs[i]) { 45 | return false; 46 | } 47 | } 48 | return true; 49 | } 50 | 51 | bool operator!=(const StringView& lhs, const StringView& rhs) { 52 | return !operator==(lhs, rhs); 53 | } -------------------------------------------------------------------------------- /Sem. 08/Examples/Static variable/GeneratorsExamples.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | constexpr unsigned MAX_FIBONACCI = 5; 4 | 5 | int fibonacci(unsigned number) { 6 | static unsigned cache[MAX_FIBONACCI] = { 1, 1 }; 7 | if (number >= MAX_FIBONACCI) { 8 | throw std::out_of_range("Number too big!"); 9 | } 10 | if (cache[number] == 0) { 11 | cache[number] = fibonacci(number - 1) + fibonacci(number - 2); 12 | } 13 | 14 | return cache[number]; 15 | } 16 | 17 | const int& swap(int a, int b) { 18 | static unsigned swaps = 0; 19 | int temp = a; 20 | a = b; 21 | b = temp; 22 | 23 | return swaps; 24 | } 25 | 26 | int nextFibonacci() { 27 | static unsigned previous = 0; 28 | static unsigned current = 1; 29 | 30 | int temp = current; 31 | current = previous + current; 32 | previous = temp; 33 | return temp; 34 | } 35 | 36 | int idGenerator() { 37 | static int id = 0; 38 | return id++; 39 | } 40 | 41 | int main() { 42 | std::cout << nextFibonacci() << std::endl; 43 | std::cout << nextFibonacci() << std::endl; 44 | std::cout << nextFibonacci() << std::endl; 45 | std::cout << nextFibonacci() << std::endl; 46 | std::cout << nextFibonacci() << std::endl; 47 | 48 | std::cout << fibonacci(0) << std::endl; 49 | std::cout << fibonacci(1) << std::endl; 50 | std::cout << fibonacci(2) << std::endl; 51 | std::cout << fibonacci(3) << std::endl; 52 | std::cout << fibonacci(4) << std::endl; 53 | std::cout << fibonacci(5) << std::endl; 54 | } -------------------------------------------------------------------------------- /Sem. 15/Design Patterns/README.md: -------------------------------------------------------------------------------- 1 | # Design Patterns 2 | 3 | Design patterns са стандартни решения на често срещани проблеми в софтуерния дизайн. 4 | Те не са специфичен код, който да може да copy paste-нете във вашия проект, а са концепция по какъв начин да решите даден проблем. 5 | Всеки design pattern е шаблон, по който да се реши различен проблем. 6 | 7 | ### Плюсове: 8 | - Общ език между различните членове на екипа 9 | - Вече тествани решения на често срещани проблеми в софтуерния дизайн 10 | - Увеличават абстракцията 11 | - Увеличават гъвкавостта и преизползването на код 12 | - Улесняват development-a при скалиране на приложението 13 | 14 | ### Минуси: 15 | - Използване без да се разбират достатъчно добре 16 | - [Golden Hammer](https://sourcemaking.com/antipatterns/golden-hammer) ``` If all you have is a hammer, everything looks like a nail. ``` 17 | 18 | ## Видове 19 | Могат да бъдат категоризирани по тяхната цел и предназначение. 20 | Техните категории са три - Creational, Structural и Behavioral. 21 | 22 | **Note:** В съответните папки можете да намерите със същите имена може да намерите примери и повече информация за различните видове Design Patterns. 23 | 24 | Полезни линкове: 25 | - [Refactoring Guru](https://refactoring.guru/design-patterns) - design patterns обяснени на достъпен език с доста примери 26 | - [SourceMaking](https://sourcemaking.com/) - design patterns обяснени на достъпен език с доста примери, подобен на горния, но има разлики 27 | -------------------------------------------------------------------------------- /Sem. 01/Revision_Examples/SwapRowsOfDynamicMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void mySwap(int*& a, int*& b) { 4 | int* temp = a; 5 | a = b; 6 | b = temp; 7 | } 8 | 9 | bool isValidRow(int rows, int row) { 10 | return row >= 0 && row < rows; 11 | } 12 | 13 | void swapRows(int** arr, int rows, int row1, int row2) { 14 | if(!isValidRow(rows, row1) || !isValidRow(rows, row2)) { 15 | return; 16 | } 17 | 18 | // TODO: 19 | // Why this is not optimal? 20 | // What is the difference between this and the other implementation? 21 | // When each of them should be used? 22 | // for (size_t i = 0; i < rows; i++) { 23 | // int temp = arr[row1][i]; 24 | // arr[row1][i] = arr[row2][i]; 25 | // arr[row2][i] = temp; 26 | // } 27 | mySwap(arr[row1], arr[row2]); 28 | } 29 | 30 | void freeMatrix(int** arr, int rows) { 31 | for (size_t i = 0; i < rows; i++) { 32 | delete[] arr[i]; 33 | } 34 | 35 | delete[] arr; 36 | } 37 | 38 | int main() { 39 | 40 | const int ROWS = 3; 41 | const int COLS = 3; 42 | 43 | // TODO: how would you handle the following case? 44 | // Each row of the matrix has different size 45 | int** arr = new int* [ROWS]; 46 | for (size_t i = 0; i < ROWS; i++) { 47 | arr[i] = new int[COLS]; 48 | } 49 | 50 | // ... do some stuff with the matrix 51 | swapRows(arr, ROWS, 0, 1); 52 | 53 | freeMatrix(arr, ROWS); 54 | } 55 | -------------------------------------------------------------------------------- /Sem. 09/Solutions/MyString (with move)/MyString-NoCapacity/MyString.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class MyString { 5 | private: 6 | char* data; 7 | size_t length; 8 | 9 | // for memory allocation only! 10 | // allocates length + 1 11 | explicit MyString(size_t length); 12 | 13 | public: 14 | MyString(); 15 | MyString(const char* arr); 16 | MyString(const MyString& other); 17 | MyString(MyString&& other) noexcept; 18 | MyString& operator=(const MyString& other); 19 | MyString& operator=(MyString&& other) noexcept; 20 | ~MyString(); 21 | 22 | const char* c_str() const; 23 | size_t getLength() const; 24 | 25 | char& operator[](size_t index); 26 | char operator[](size_t index) const; 27 | 28 | MyString& operator+=(const MyString& other); 29 | 30 | friend MyString operator+(const MyString& lhs, const MyString& rhs); 31 | friend std::istream& operator>>(std::istream& in, MyString& str); 32 | private: 33 | void free(); 34 | void copyFrom(const MyString& other); 35 | void moveFrom(MyString&& other) noexcept; 36 | }; 37 | 38 | std::ostream& operator<<(std::ostream& out, const MyString& str); 39 | bool operator<(const MyString& lhs, const MyString& rhs); 40 | bool operator<=(const MyString& lhs, const MyString& rhs); 41 | bool operator>=(const MyString& lhs, const MyString& rhs); 42 | bool operator>(const MyString& lhs, const MyString& rhs); 43 | bool operator==(const MyString& lhs, const MyString& rhs); 44 | bool operator!=(const MyString& lhs, const MyString& rhs); -------------------------------------------------------------------------------- /Practicum/Pract. 08/README.md: -------------------------------------------------------------------------------- 1 | # OOП - Практикум 7.04.2025 - Седмица 8 2 | 3 | ## Задачи за практикум 4 | 5 | **Задача 1.** Реализирайте клас **StringVector**, който представлява динамична колекция от `String` обекти и има минимум следните функции: 6 | 7 | * [**push_back**](https://cplusplus.com/reference/vector/vector/push_back/) - добавя елемент в края на вектора 8 | * [**pop_back**](https://cplusplus.com/reference/vector/vector/pop_back/) - премахва последния елемент на вектора 9 | * [**insert**](https://cplusplus.com/reference/vector/vector/insert/) - добавя елемент на даден индекс във вектора 10 | * [**erase**](https://cplusplus.com/reference/vector/vector/erase/) - изтрива елемент на даден индекс във вектора 11 | * [**clear**](https://cplusplus.com/reference/vector/vector/clear/) - изчиства данните във вектора 12 | * [**at**](https://cplusplus.com/reference/vector/vector/operator[]/) - позволява индексация на елементите във вектора (работи в константно и неконстантно) 13 | * [**empty**](https://cplusplus.com/reference/vector/vector/empty/) - връща дали векторът е празен 14 | * [**size**](https://cplusplus.com/reference/vector/vector/size/) - връща броя на елементите във вектора 15 | 16 | *Bonus:* добавете каквито прецените други фунцкии и оператори, които биха били смислени и полезни. (Пр: оператори за вход и изход от поток) 17 | 18 | За реализацията на класа използвайте предоставения клас String и неговите методи. При невалидни входни данни, погрижете се класът да сигнализира с подходящи изключения. -------------------------------------------------------------------------------- /Sem. 01/Revision_Examples/MemoryAllocation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // How much memory is allocated in the following expressions and where? 5 | // We assume that our os is x32 and the pointer takes 4 bytes 6 | short s = 1; // Stack: 2 Heap: 0 7 | int a = 2; // Stack: 4 Heap: 0 8 | bool b = true; // Stack: 1 Heap: 0 9 | bool* p = new bool; // Stack: 4 Heap: 1 10 | char c = '\0'; // Stack: 1 Heap: 0 11 | double d = 11.1; // Stack: 8 Heap: 0 12 | int arr1[40] = { 0 }; // Stack: 160 Heap: 0 13 | // arr1 is array in the stack => 40 * 4 bytes 14 | 15 | int* arr2 = new int(40); // Stack: 4 Heap: 4 16 | int* arr3 = new int[40]; // Stack: 4 Heap: 160 17 | char test[] = "abcd"; // Stack: 5 Heap: 0 18 | int** matrix = new int*[10]; // Stack: 4 Heap: 10 * 4 19 | for (size_t i = 1; i < 10; i++) { 20 | matrix[i] = new int[i]; // new int(i) vs new int[i] 21 | } 22 | // line 18 to 21 in two situations 23 | // 1) new int(i) 24 | // Stack: 4 Heap: 10 * 4 + 9 * 4 25 | // 2) new int[i] 26 | // Stack: 4 Heap: 10 * 4 + 4(1 + 2 .... + 9) 27 | 28 | //int arr1[40] = { 0 }; // Stack: 160 Heap: 29 | //int* arr2 = new int(40); // Stack: 4 Heap: 4 30 | //int* arr3 = new int[40]; // Stack: 4 Heap: 160 31 | // Which of the following statements is/are incorrect? 32 | // delete[] arr1; - arr1 is not a dynamic array 33 | // delete[] arr2; - arr2 is a pointer to a single int, not to a int[] 34 | // delete[] arr3; - correct 35 | } 36 | -------------------------------------------------------------------------------- /Sem. 05/Solutions/EventCollection/EventCollection.cpp: -------------------------------------------------------------------------------- 1 | #include "EventCollection.h" 2 | 3 | EventCollection EventCollection::eventsOnDate(const Date& date) const { 4 | EventCollection res; 5 | 6 | for (size_t i = 0; i < size; i++) { 7 | if (date.isEqualTo(events[i].getDate()) == 0) { 8 | res.addEvent(events[i]); 9 | } 10 | } 11 | 12 | return res; 13 | } 14 | 15 | int EventCollection::findEventByName(const char* name) const { 16 | for (size_t i = 0; i < size; i++) { 17 | if (strcmp(events[i].getName(), name) == 0) { 18 | return i; 19 | } 20 | } 21 | 22 | return -1; 23 | } 24 | 25 | bool EventCollection::addEvent(const Event& event) { 26 | if (size >= EventCollectionConstants::MAX_EVENT_SIZE) { 27 | return false; 28 | } 29 | 30 | events[size] = event; 31 | size++; 32 | return true; 33 | } 34 | 35 | EventCollection EventCollection::maxEvents(const Date& date) const { 36 | //TODO 37 | } 38 | 39 | bool EventCollection::removeEvent(const char* name) { 40 | int index = findEventByName(name); 41 | 42 | if (index == -1) { 43 | return false; 44 | } 45 | 46 | events[index] = events[size - 1]; 47 | size--; 48 | return true; 49 | } 50 | 51 | const Event& EventCollection::getByName(const char* name) const { 52 | int index = findEventByName(name); 53 | if (index == -1) { 54 | return Event(); 55 | } 56 | 57 | return events[index]; 58 | } 59 | 60 | void EventCollection::print() const { 61 | for (size_t i = 0; i < size; i++) { 62 | std::cout << events[i].getName() << std::endl; 63 | } 64 | } -------------------------------------------------------------------------------- /Sem. 06/Solutions/MyString/MyString.cpp: -------------------------------------------------------------------------------- 1 | #include "MyString.h" 2 | #include 3 | #include 4 | 5 | #pragma warning (disable : 4996) 6 | 7 | MyString::MyString(size_t length): length(length) { 8 | str = new char[length + 1]; 9 | } 10 | 11 | MyString::MyString() : MyString((size_t)0) { 12 | str[0] = '\0'; 13 | } 14 | //MyString::MyString() : MyString("") { 15 | //} 16 | 17 | MyString::MyString(const char* str): MyString(strlen(str)) { 18 | strcpy(this->str, str); 19 | } 20 | 21 | MyString::MyString(const MyString& other): length(other.length) { 22 | copyDynamic(other); 23 | } 24 | 25 | MyString& MyString::operator=(const MyString& other) { 26 | if (this != &other) { 27 | freeDynamic(); 28 | length = other.length; 29 | copyDynamic(other); 30 | } 31 | return *this; 32 | } 33 | 34 | MyString::~MyString() { 35 | freeDynamic(); 36 | } 37 | 38 | size_t MyString::getLength() const { 39 | return length; 40 | } 41 | 42 | const char* MyString::c_str() const { 43 | return str; 44 | } 45 | 46 | char& MyString::at(size_t index) { 47 | if (index >= length) { 48 | throw std::out_of_range("...."); 49 | } 50 | return str[index]; 51 | } 52 | 53 | char MyString::at(size_t index) const { 54 | if (index >= length) { 55 | throw std::out_of_range("...."); 56 | } 57 | 58 | return str[index]; 59 | } 60 | 61 | void MyString::copyDynamic(const MyString& other) { 62 | str = new char[other.length + 1]; 63 | strcpy(str, other.str); 64 | } 65 | 66 | void MyString::freeDynamic() { 67 | delete[] str; 68 | } 69 | -------------------------------------------------------------------------------- /Sem. 04/Solutions/Interval/HelperFunctions.cpp: -------------------------------------------------------------------------------- 1 | #include "HelperFunctions.h" 2 | 3 | #include 4 | 5 | namespace { 6 | // anonymous namespace 7 | // you cant access this variables inside from another file 8 | constexpr int DIGITS_COUNT = 10; 9 | } 10 | 11 | bool HelperFunctions::isPrime(int number) { 12 | if (number == 2) { 13 | return true; 14 | } 15 | if (number < 2 || number % 2 == 0) { 16 | return false; 17 | } 18 | 19 | double sqrtN = sqrt(number); 20 | for (int i = 3; i <= sqrtN; i += 2) { 21 | if (number % i == 0) { 22 | return false; 23 | } 24 | } 25 | return true; 26 | } 27 | 28 | int HelperFunctions::reverseNumber(int number) { 29 | int reversed = 0; 30 | while (number != 0) { 31 | (reversed *= 10) += number % 10; 32 | number /= 10; 33 | } 34 | 35 | return reversed; 36 | } 37 | 38 | bool HelperFunctions::isPalindrome(int number) { 39 | return number == reverseNumber(number); 40 | } 41 | 42 | bool HelperFunctions::isPowOfTwo(int n) { 43 | if (n <= 0) { 44 | return false; 45 | } 46 | return (n & n - 1) == 0; 47 | } 48 | 49 | bool HelperFunctions::containsOnlyDistinctDigits(int number) { 50 | bool digits[DIGITS_COUNT] = {}; 51 | while (number != 0) { 52 | int lastDigit = number % 10; 53 | if (digits[lastDigit]) { 54 | return false; 55 | } 56 | digits[lastDigit] = true; 57 | number /= 10; 58 | } 59 | return true; 60 | } -------------------------------------------------------------------------------- /Sem. 08/Solutions/SwapCounter/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class SwapCounter { 4 | private: 5 | static int count; 6 | public: 7 | static void swap(int& a, int& b) { 8 | int temp = a; 9 | a = b; 10 | b = temp; 11 | count++; 12 | } 13 | 14 | static int getCount() { 15 | return count; 16 | } 17 | 18 | static void resetCount() { 19 | count = 0; 20 | } 21 | }; 22 | 23 | void bubbleSort(int* arr, size_t size) { 24 | for (int i = 0; i < size - 1; i++) { 25 | bool isSwapped = false; 26 | for (int j = 0; j < size - 1 - i; j++) { 27 | if (arr[j + 1] < arr[j]) { 28 | SwapCounter::swap(arr[j], arr[j + 1]); 29 | isSwapped = true; 30 | } 31 | } 32 | if (!isSwapped) { 33 | return; 34 | } 35 | } 36 | } 37 | 38 | void selectionSort(int* arr, size_t size) { 39 | for (size_t i = 0; i < size; i++) { 40 | int minElementIndex = i; 41 | for (size_t j = i + 1; j < size; j++) { 42 | if (arr[j] < arr[minElementIndex]) { 43 | minElementIndex = j; 44 | } 45 | } 46 | 47 | if (minElementIndex != i) { 48 | SwapCounter::swap(arr[minElementIndex], arr[i]); 49 | } 50 | } 51 | } 52 | 53 | int SwapCounter::count = 0; 54 | 55 | int main() { 56 | { 57 | int arr[10] = { 10,9,8,7,6,5,4,3,2,1 }; 58 | bubbleSort(arr, 10); 59 | std::cout << "Bubble sort swaps: " << SwapCounter::getCount() << std::endl; 60 | } 61 | { 62 | SwapCounter::resetCount(); 63 | int arr[10] = { 10,9,8,7,6,5,4,3,2,1 }; 64 | selectionSort(arr, 10); 65 | std::cout << "Selection sort swaps: " << SwapCounter::getCount() << std::endl; 66 | } 67 | } -------------------------------------------------------------------------------- /Sem. 06/Examples/RuleOf4Composition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct A { 4 | A() { 5 | std::cout << "A constructor" << std::endl; 6 | } 7 | 8 | A(const A& other) { 9 | std::cout << "A copy-constructor" << std::endl; 10 | } 11 | 12 | A& operator=(const A& other) { 13 | std::cout << "A operator=" << std::endl; 14 | return *this; 15 | } 16 | 17 | ~A() { 18 | std::cout << "A destructor" << std::endl; 19 | } 20 | }; 21 | 22 | struct B { 23 | B() { 24 | std::cout << "B constructor" << std::endl; 25 | } 26 | 27 | B(const B& other) { 28 | std::cout << "B copy-constructor" << std::endl; 29 | } 30 | 31 | B& operator=(const B& other) { 32 | std::cout << "B operator=" << std::endl; 33 | return *this; 34 | } 35 | 36 | ~B() { 37 | std::cout << "B destructor" << std::endl; 38 | } 39 | }; 40 | 41 | struct C { 42 | A a; 43 | B b; 44 | 45 | C() { 46 | std::cout << "C constructor" << std::endl; 47 | } 48 | 49 | // you have to explicitly call a and b copy constr 50 | // otherwise the default constructors will be called 51 | C(const C& other) : a(other.a), b(other.b) { 52 | std::cout << "C copy-constructor" << std::endl; 53 | } 54 | 55 | // you have to explicitly call a and b operator= 56 | // otherwise nothing will be called 57 | C& operator=(const C& other) { 58 | a = other.a; 59 | b = other.b; 60 | std::cout << "C operator=" << std::endl; 61 | return *this; 62 | } 63 | 64 | ~C() { 65 | std::cout << "C destructor" << std::endl; 66 | } 67 | }; 68 | 69 | int main() { 70 | C c2, c3; 71 | C c1 = c2; 72 | c2 = c3; 73 | } -------------------------------------------------------------------------------- /Sem. 12/Solutions/SimpleVisitor-FileSystem/main.cpp: -------------------------------------------------------------------------------- 1 | // The whole example is for demo purpose 2 | // treat it like a pseudo code! 3 | #include 4 | #include // NOT ALLOWED IN THE COURSE ! 5 | 6 | class FileSystemEntityVisitor; 7 | 8 | class FileSystemEntity { 9 | std::string name; 10 | public: 11 | FileSystemEntity(std::string name) : name(name) {} 12 | const std::string& getName() const { 13 | return name; 14 | } 15 | 16 | virtual void accept(FileSystemEntityVisitor* visitor) = 0; 17 | 18 | virtual ~FileSystemEntity() = 0; 19 | }; 20 | FileSystemEntity::~FileSystemEntity() {} 21 | 22 | class File : public FileSystemEntity { 23 | public: 24 | void accept(FileSystemEntityVisitor* visitor) override { 25 | visitor->visitFile(this); 26 | } 27 | }; 28 | 29 | class Directory : public FileSystemEntity { 30 | public: 31 | FileSystemEntity** children; 32 | size_t size; 33 | // !!!not good encapsulation 34 | // !!!missing stuff here 35 | void accept(FileSystemEntityVisitor* visitor) override { 36 | visitor->visitDirectory(this); 37 | } 38 | }; 39 | 40 | class FileSystemEntityVisitor { 41 | public: 42 | void visitFile(File* f) { 43 | std::cout << f->getName(); 44 | } 45 | 46 | void visitDirectory(Directory* d) { 47 | std::cout << d->getName(); 48 | // then iterate through the children and start printing their names 49 | for (size_t i = 0; i < d->size; i++) { 50 | // each children migth be directory or file! so we pass the visitor to check the child 51 | d->children[i]->accept(this); 52 | } 53 | } 54 | }; 55 | 56 | int main() { } 57 | -------------------------------------------------------------------------------- /Sem. 10/Examples/TypesOfInheritance.cpp: -------------------------------------------------------------------------------- 1 | class A { 2 | private: 3 | int a; 4 | protected: 5 | int b; 6 | public: 7 | int c; 8 | }; 9 | 10 | // default is private 11 | class PrivateDerived : A{ 12 | void test() { 13 | // private members are not accessible inside derived classes 14 | // a inaccessible 15 | // b and c are inherited as private members 16 | b; 17 | c; 18 | } 19 | }; 20 | 21 | class ProtectedDerived : protected A { 22 | void test() { 23 | // private members are not accessible inside derived classes 24 | // b and c inherited as protected members 25 | b; 26 | c; 27 | } 28 | }; 29 | 30 | class ProtectedOfProtectedDerived : protected ProtectedDerived { 31 | void test2() { 32 | // b and c inherited as protected members 33 | b; 34 | c; 35 | } 36 | }; 37 | 38 | class PublicDerived : public A { 39 | void test() { 40 | // private members are not accessible inside derived classes 41 | // b is protected 42 | // c is public 43 | b; 44 | c; 45 | } 46 | }; 47 | 48 | void myMain() { 49 | PrivateDerived privateDerived; 50 | ProtectedDerived protectedDerived; 51 | PublicDerived publicDerived; 52 | 53 | // privateDerived.a; // inaccessible 54 | // privateDerived.b; // private for the class 55 | // privateDerived.c; // private for the class 56 | 57 | // protectedDerived.a; // inaccessible 58 | // protectedDerived.b; // protected for the class 59 | // protectedDerived.c; // protected for the class 60 | 61 | //publicDerived.a; // inacessible 62 | //publicDerived.b; // protected for the class 63 | publicDerived.c; // public for the class 64 | } --------------------------------------------------------------------------------