Check your g++ version by typing:
> g++ --version
It should be >= 7.0
> mkdir build
> cd build
> cmake ..
> make
> ./modern_cpp-
static_assert: Assert thatM_PIused inCircle.cppfile is not equal to3.14 -
usingalias: Changetypedeftousingalias -
scoped
enum: Write a new scoped enum named Color and define in it 3 colors of your choice. Inherit from unsigned char. Add a new field:Color colorin theShapeclass, 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.
-
move semantics: Group task: Implement your own unique_ptr. Requirements:
- Template class
- RAII
- Copy operations not allowed
- Move operations allowed
- Interface functions - at least:
- T* get()
- T& operator*()
- T* operator->()
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. -
default,delete: Mark copy constructors asdefault. DeletegetY()method inSquareand all default constructors of shapes -
final,override: MarkCircleclass asfinalMarkgetX()inRectangleasfinal. 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_listto 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 aColoras a parameter. Create some shapes providingColoronly param. Add in-class field initialization for all shapes to safely use inherited constructor. -
SFINAE Write a function that allows inserting only subclasses of Shape to the collection. Other parameter types should not compile. Use SFINAE. Find proper type_traits.
-
attributes: Add a new method
double getPi()inCircleclass, which returns a PI number. Mark it as deprecated. -
noexcept: Mark somegetArea()andgetPerimeter()methods asnoexcept -
alignas,alignofChange the alignment of theCircleclass to 128. Print the alignment inmain()function. Change the alignment to 2. Print the alignment. -
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.
-
lambda functions: Change functions from
main.cppinto lambdas (sortByArea,perimeterBiggerThan20,areaLessThan10) Change lambdaareaLessThan10into lambdaareaLessThanX, which takesx = 10on a capture list. What is the problem? Usestd::functionto solve the problem. -
variadic templates: Write a factory method which should work like
std::make_shared. It should have below signature:template<class DerivedType, class... Arguments> std::shared_ptr<Shape> make_shape(Arguments&&... args);
Inside, it should create a
shared_ptrto DerivedType and pass all arguments into constructor of DerivedType via perfect forwarding.