diff --git a/Lesson_9/SmartPointer/Car.cpp b/Lesson_9/SmartPointer/Car.cpp new file mode 100644 index 0000000..8cad4d7 --- /dev/null +++ b/Lesson_9/SmartPointer/Car.cpp @@ -0,0 +1,18 @@ +#include "stdafx.h" +#include "Car.h" + +Car::Car(const std::string& color) +: color_(color) +{ + std::cout << color_ << " car has been created\n"; +} + +Car::~Car() +{ + std::cout << color_ << " car has been destroied\n"; +} + +void Car::Drive() +{ + std::cout << color_ << " car in move\n"; +} diff --git a/Lesson_9/SmartPointer/Car.h b/Lesson_9/SmartPointer/Car.h new file mode 100644 index 0000000..f3a904e --- /dev/null +++ b/Lesson_9/SmartPointer/Car.h @@ -0,0 +1,14 @@ +#pragma once + + +class Car +{ +public: + Car(const std::string& color); + ~Car(); + + void Drive(); + +private: + std::string color_; +}; \ No newline at end of file diff --git a/Lesson_9/SmartPointer/CarFactory.cpp b/Lesson_9/SmartPointer/CarFactory.cpp new file mode 100644 index 0000000..8294990 --- /dev/null +++ b/Lesson_9/SmartPointer/CarFactory.cpp @@ -0,0 +1,8 @@ +#include "stdafx.h" +#include "CarFactory.h" +#include "Car.h" + +std::unique_ptr CarFactory::BuildCar(const std::string& color) +{ + return std::make_unique(new Car(color)); +} diff --git a/Lesson_9/SmartPointer/CarFactory.h b/Lesson_9/SmartPointer/CarFactory.h new file mode 100644 index 0000000..bf6a71a --- /dev/null +++ b/Lesson_9/SmartPointer/CarFactory.h @@ -0,0 +1,8 @@ +#pragma once +#include "Car.h" + +class CarFactory +{ +public: + std::unique_ptr BuildCar(const std::string& color); +}; diff --git a/Lesson_9/SmartPointer/Driver.cpp b/Lesson_9/SmartPointer/Driver.cpp new file mode 100644 index 0000000..bc9381a --- /dev/null +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -0,0 +1,39 @@ +#include "stdafx.h" +#include "Driver.h" +#include "CarFactory.h" +#include "Car.h" + +Driver::Driver(const std::string& name, std::shared_ptr factory) + : factory_(factory) + , name_(name) +{ +} + +void Driver::BuyCar(const std::string& color) +{ + car_ = factory_->BuildCar(color); +} + +std::unique_ptr Driver::SellCar() +{ + return std::make_unique(car_.release()); +} + +void Driver::BuyUsedCar(Driver * d) +{ + car_.reset(); + d->SellCar(); +} + +void Driver::Go() +{ + if (car_ != nullptr) + { + std::cout << name_ << " I have a car "; + car_->Drive(); + } + else + { + std::cout << name_ << ": I'll go on foot\n"; + } +} diff --git a/Lesson_9/SmartPointer/Driver.h b/Lesson_9/SmartPointer/Driver.h new file mode 100644 index 0000000..469bde9 --- /dev/null +++ b/Lesson_9/SmartPointer/Driver.h @@ -0,0 +1,22 @@ +#pragma once +#include "Car.h" +class CarFactory; + +class Driver +{ +public: + Driver(const std::string& name, std::shared_ptr factory); + + void BuyCar(const std::string& color); + + std::unique_ptr SellCar(); + void BuyUsedCar(Driver* d); + + void Go(); + +private: + std::unique_ptr car_; + std::shared_ptr factory_; + std::string name_; +}; + diff --git a/Lesson_9/SmartPointer/SmartPointer.cpp b/Lesson_9/SmartPointer/SmartPointer.cpp index 3e3666f..e52b4b2 100644 --- a/Lesson_9/SmartPointer/SmartPointer.cpp +++ b/Lesson_9/SmartPointer/SmartPointer.cpp @@ -1,70 +1,6 @@ -#include -#include -#include - -class Car -{ -public: - Car(const std::string& color) - : color_(color) - { - std::cout << color_ << " car has been created\n"; - } - ~Car() { std::cout << color_ << " car has been destroied\n"; } - - void Drive() - { - std::cout << color_ << " car in move\n"; - } - -private: - std::string color_; -}; - -class CarFactory -{ -public: - std::unique_ptr BuildCar(const std::string& color) - { - return std::unique_ptr(new Car(color)); - } -}; - -class Driver -{ -public: - Driver(const std::string& name, std::shared_ptr factory) - : factory_(factory) - , name_(name) - { - } - - void BuyCar(const std::string& color) - { - car_ = factory_->BuildCar(color); - } - - // SellCar - // BuyUsedCar - - void Go() - { - if (car_ != nullptr) - { - std::cout << name_ << " I have a car "; - car_->Drive(); - } - else - { - std::cout << name_ << ": I'll go on foot\n"; - } - } - -private: - std::unique_ptr car_; - std::shared_ptr factory_; - std::string name_; -}; +#include "stdafx.h" +#include "CarFactory.h" +#include "Driver.h" int main() { diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj b/Lesson_9/SmartPointer/SmartPointer.vcxproj index 6150fc1..03140ff 100644 --- a/Lesson_9/SmartPointer/SmartPointer.vcxproj +++ b/Lesson_9/SmartPointer/SmartPointer.vcxproj @@ -23,32 +23,32 @@ {F6CAC44C-D7A8-48BA-B457-F0FD6A929F1C} Win32Proj SmartPointer - 10.0.17763.0 + 10.0 Application true - v141 + v142 Unicode Application false - v141 + v142 true Unicode Application true - v141 + v142 Unicode Application false - v141 + v142 true Unicode @@ -151,8 +151,18 @@ + + + + + + + + + + diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters b/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters index 46986af..beb0311 100644 --- a/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters +++ b/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters @@ -18,5 +18,31 @@ Source Files + + Resource Files + + + Resource Files + + + Resource Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Lesson_9/SmartPointer/a.cpp b/Lesson_9/SmartPointer/a.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Lesson_9/SmartPointer/stdafx.h b/Lesson_9/SmartPointer/stdafx.h new file mode 100644 index 0000000..b6c332f --- /dev/null +++ b/Lesson_9/SmartPointer/stdafx.h @@ -0,0 +1,5 @@ +#pragma once + +#include +#include +#include \ No newline at end of file diff --git a/Lesson_9/StdThread/StdThread.vcxproj b/Lesson_9/StdThread/StdThread.vcxproj index d2e65cc..0375706 100644 --- a/Lesson_9/StdThread/StdThread.vcxproj +++ b/Lesson_9/StdThread/StdThread.vcxproj @@ -23,32 +23,32 @@ {52F68E52-72B0-44B0-84D7-CBE3CAB4537B} Win32Proj StdThread - 10.0.17763.0 + 10.0 Application true - v141 + v142 Unicode Application false - v141 + v142 true Unicode Application true - v141 + v142 Unicode Application false - v141 + v142 true Unicode