Check your g++ version by typing:
> g++ --version
It should be >= 7.0
> mkdir build
> cd build
> cmake ..
> make
> ./modern_cpp
Or use QT Creator... (or other IDE).
nullptr
: Change allNULL
tonullptr
susing
alias: Changetypedef
tousing
alias- Scoped
enum
: Write a new scopedenum
namedColor
and define in it 3 colors of your choice. Inherit from unsigned char. Add a new field:Color color
in theShape
class, so that every shape has it's own defined color. auto
: Useauto
, wherever you should.- Range-based for loop: Use range-based for loops, wherever possible.
default
,delete
: Mark copy constructors asdefault
. DeletegetY()
method inSquare
and all default constructors of shapesfinal
,override
: MarkCircle
class asfinal
MarkgetX()
inRectangle
asfinal
. What is the problem? Mark all overridden virtual methods. Can you spot the problem?constexpr
: Write a function that calculates n-th Fibonacci's number. Do not mark itconstexpr
. In the first line ofmain()
add computing 45-th Fibonacci's number. Measure the time of program execution (time ./modern_cpp
) Mark fibonacci function asconstexpr
, compile the program and measure the time of execution once again. If you can't see a big difference assign the result to the constexpr variable.- Uniform initialization:
Use
initializer_list
to initialize the collection. Add a new constructor to Shape -Shape(Color c)
. What happens? Use constructor inheritance to allow initialization of all shapes providing only aColor
as a parameter. Create some shapes providingColor
only param. Add in-class field initialization for all shapes to safely use inherited constructor. - Lambda functions:
Change functions from
main.cpp
into lambdas (sortByArea
,perimeterBiggerThan20
,areaLessThan10
) Change lambdaareaLessThan10
into lambdaareaLessThanX
, which takesx = 10
on a capture list. What is the problem? Usestd::function
to solve the problem.
- Attributes:
Add a new method
double getPi()
inCircle
class, which returns a PI number. Mark it as deprecated. noexcept
: Mark somegetArea()
andgetPerimeter()
methods asnoexcept
- Move semantics:
Add move constructors and move assignment operators to all shapes.
Mark them as
noexcept
. What about Rule of 5? Move some shapes into the collection. - Delegating constructors:
Add a new constructor, which takes also the previously defined Color of a shape. You can use a default parameter for
Color
. Delegate a call in the old constructor to the new one. - Variadic templates:
Write a factory method which should work like
std::make_shared
. It should have below signature:Inside, it should create atemplate<class DerivedType, class... Arguments> std::shared_ptr<Shape> make_shape(Arguments&&... args);
shared_ptr
toDerivedType
and pass all arguments into constructor ofDerivedType
via perfect forwarding.