From 9ea0424c41f62a63e453e1e3124a87512814a0e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AF=D1=80=D0=BE=D1=81=D0=BB=D0=B0=D0=B2=20=D0=96=D1=83?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BB=D1=8C=D0=BE=D0=B2?= Date: Tue, 9 Jun 2020 20:02:56 +0300 Subject: [PATCH 1/2] divided the program into separate class files --- Lesson_9/SmartPointer/Car.cpp | 18 ++++++ Lesson_9/SmartPointer/Car.h | 14 +++++ Lesson_9/SmartPointer/CarFactory.cpp | 8 +++ Lesson_9/SmartPointer/CarFactory.h | 8 +++ Lesson_9/SmartPointer/Driver.cpp | 28 +++++++++ Lesson_9/SmartPointer/Driver.h | 22 +++++++ Lesson_9/SmartPointer/SmartPointer.cpp | 70 +--------------------- Lesson_9/SmartPointer/SmartPointer.vcxproj | 10 ++-- Lesson_9/SmartPointer/a.cpp | 0 Lesson_9/SmartPointer/stdafx.h | 5 ++ Lesson_9/StdThread/StdThread.vcxproj | 10 ++-- 11 files changed, 116 insertions(+), 77 deletions(-) create mode 100644 Lesson_9/SmartPointer/Car.cpp create mode 100644 Lesson_9/SmartPointer/Car.h create mode 100644 Lesson_9/SmartPointer/CarFactory.cpp create mode 100644 Lesson_9/SmartPointer/CarFactory.h create mode 100644 Lesson_9/SmartPointer/Driver.cpp create mode 100644 Lesson_9/SmartPointer/Driver.h create mode 100644 Lesson_9/SmartPointer/a.cpp create mode 100644 Lesson_9/SmartPointer/stdafx.h 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..2da61cd --- /dev/null +++ b/Lesson_9/SmartPointer/CarFactory.cpp @@ -0,0 +1,8 @@ +#include "CarFactory.h" +#include "stdafx.h" +#include "Car.h" + +std::unique_ptr CarFactory::BuildCar(const std::string& color) +{ + return std::unique_ptr(new Car(color)); +} diff --git a/Lesson_9/SmartPointer/CarFactory.h b/Lesson_9/SmartPointer/CarFactory.h new file mode 100644 index 0000000..fb32b62 --- /dev/null +++ b/Lesson_9/SmartPointer/CarFactory.h @@ -0,0 +1,8 @@ +#pragma once +class Car{}; + +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..50d0473 --- /dev/null +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -0,0 +1,28 @@ +#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); +} + +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..6bd3f45 --- /dev/null +++ b/Lesson_9/SmartPointer/Driver.h @@ -0,0 +1,22 @@ +#pragma once +class CarFactory{}; +class Car{}; + +class Driver +{ +public: + Driver(const std::string& name, std::shared_ptr factory); + + void BuyCar(const std::string& color); + + // SellCar + // BuyUsedCar + + 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..ab775e4 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 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 From a1ef1ec25deca5822a5bded44ee4f556b60fb5c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AF=D1=80=D0=BE=D1=81=D0=BB=D0=B0=D0=B2=20=D0=96=D1=83?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BB=D1=8C=D0=BE=D0=B2?= Date: Tue, 9 Jun 2020 20:55:56 +0300 Subject: [PATCH 2/2] problems with class 'Car' --- Lesson_9/SmartPointer/CarFactory.cpp | 4 +-- Lesson_9/SmartPointer/CarFactory.h | 2 +- Lesson_9/SmartPointer/Driver.cpp | 11 ++++++++ Lesson_9/SmartPointer/Driver.h | 8 +++--- Lesson_9/SmartPointer/SmartPointer.vcxproj | 10 +++++++ .../SmartPointer/SmartPointer.vcxproj.filters | 26 +++++++++++++++++++ 6 files changed, 54 insertions(+), 7 deletions(-) diff --git a/Lesson_9/SmartPointer/CarFactory.cpp b/Lesson_9/SmartPointer/CarFactory.cpp index 2da61cd..8294990 100644 --- a/Lesson_9/SmartPointer/CarFactory.cpp +++ b/Lesson_9/SmartPointer/CarFactory.cpp @@ -1,8 +1,8 @@ -#include "CarFactory.h" #include "stdafx.h" +#include "CarFactory.h" #include "Car.h" std::unique_ptr CarFactory::BuildCar(const std::string& color) { - return std::unique_ptr(new Car(color)); + return std::make_unique(new Car(color)); } diff --git a/Lesson_9/SmartPointer/CarFactory.h b/Lesson_9/SmartPointer/CarFactory.h index fb32b62..bf6a71a 100644 --- a/Lesson_9/SmartPointer/CarFactory.h +++ b/Lesson_9/SmartPointer/CarFactory.h @@ -1,5 +1,5 @@ #pragma once -class Car{}; +#include "Car.h" class CarFactory { diff --git a/Lesson_9/SmartPointer/Driver.cpp b/Lesson_9/SmartPointer/Driver.cpp index 50d0473..bc9381a 100644 --- a/Lesson_9/SmartPointer/Driver.cpp +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -14,6 +14,17 @@ 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) diff --git a/Lesson_9/SmartPointer/Driver.h b/Lesson_9/SmartPointer/Driver.h index 6bd3f45..469bde9 100644 --- a/Lesson_9/SmartPointer/Driver.h +++ b/Lesson_9/SmartPointer/Driver.h @@ -1,6 +1,6 @@ #pragma once -class CarFactory{}; -class Car{}; +#include "Car.h" +class CarFactory; class Driver { @@ -9,8 +9,8 @@ class Driver void BuyCar(const std::string& color); - // SellCar - // BuyUsedCar + std::unique_ptr SellCar(); + void BuyUsedCar(Driver* d); void Go(); diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj b/Lesson_9/SmartPointer/SmartPointer.vcxproj index ab775e4..03140ff 100644 --- a/Lesson_9/SmartPointer/SmartPointer.vcxproj +++ b/Lesson_9/SmartPointer/SmartPointer.vcxproj @@ -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