Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lesson_9/SmartPointer/Car.cpp
Original file line number Diff line number Diff line change
@@ -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";
}
14 changes: 14 additions & 0 deletions Lesson_9/SmartPointer/Car.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once


class Car
{
public:
Car(const std::string& color);
~Car();

void Drive();

private:
std::string color_;
};
8 changes: 8 additions & 0 deletions Lesson_9/SmartPointer/CarFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "stdafx.h"
#include "CarFactory.h"
#include "Car.h"

std::unique_ptr<Car> CarFactory::BuildCar(const std::string& color)
{
return std::make_unique<Car>(new Car(color));
}
8 changes: 8 additions & 0 deletions Lesson_9/SmartPointer/CarFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include "Car.h"

class CarFactory
{
public:
std::unique_ptr<Car> BuildCar(const std::string& color);
};
39 changes: 39 additions & 0 deletions Lesson_9/SmartPointer/Driver.cpp
Original file line number Diff line number Diff line change
@@ -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<CarFactory> factory)
: factory_(factory)
, name_(name)
{
}

void Driver::BuyCar(const std::string& color)
{
car_ = factory_->BuildCar(color);
}

std::unique_ptr<Car> Driver::SellCar()
{
return std::make_unique<Car>(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";
}
}
22 changes: 22 additions & 0 deletions Lesson_9/SmartPointer/Driver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include "Car.h"
class CarFactory;

class Driver
{
public:
Driver(const std::string& name, std::shared_ptr<CarFactory> factory);

void BuyCar(const std::string& color);

std::unique_ptr<Car> SellCar();
void BuyUsedCar(Driver* d);

void Go();

private:
std::unique_ptr<Car> car_;
std::shared_ptr<CarFactory> factory_;
std::string name_;
};

70 changes: 3 additions & 67 deletions Lesson_9/SmartPointer/SmartPointer.cpp
Original file line number Diff line number Diff line change
@@ -1,70 +1,6 @@
#include <iostream>
#include <string>
#include <memory>

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<Car> BuildCar(const std::string& color)
{
return std::unique_ptr<Car>(new Car(color));
}
};

class Driver
{
public:
Driver(const std::string& name, std::shared_ptr<CarFactory> 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> car_;
std::shared_ptr<CarFactory> factory_;
std::string name_;
};
#include "stdafx.h"
#include "CarFactory.h"
#include "Driver.h"

int main()
{
Expand Down
20 changes: 15 additions & 5 deletions Lesson_9/SmartPointer/SmartPointer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@
<ProjectGuid>{F6CAC44C-D7A8-48BA-B457-F0FD6A929F1C}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>SmartPointer</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down Expand Up @@ -151,8 +151,18 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="a.cpp" />
<ClCompile Include="Car.cpp" />
<ClCompile Include="CarFactory.cpp" />
<ClCompile Include="Driver.cpp" />
<ClCompile Include="SmartPointer.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Car.h" />
<ClInclude Include="CarFactory.h" />
<ClInclude Include="Driver.h" />
<ClInclude Include="stdafx.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
26 changes: 26 additions & 0 deletions Lesson_9/SmartPointer/SmartPointer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,31 @@
<ClCompile Include="SmartPointer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Car.cpp">
<Filter>Resource Files</Filter>
</ClCompile>
<ClCompile Include="CarFactory.cpp">
<Filter>Resource Files</Filter>
</ClCompile>
<ClCompile Include="Driver.cpp">
<Filter>Resource Files</Filter>
</ClCompile>
<ClCompile Include="a.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Car.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="stdafx.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CarFactory.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Driver.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
Empty file added Lesson_9/SmartPointer/a.cpp
Empty file.
5 changes: 5 additions & 0 deletions Lesson_9/SmartPointer/stdafx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <iostream>
#include <string>
#include <memory>
10 changes: 5 additions & 5 deletions Lesson_9/StdThread/StdThread.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@
<ProjectGuid>{52F68E52-72B0-44B0-84D7-CBE3CAB4537B}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>StdThread</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down