diff --git a/chapters/chapter_02/CMakeLists.txt b/chapters/chapter_02/CMakeLists.txt index 2ffd009..666d680 100644 --- a/chapters/chapter_02/CMakeLists.txt +++ b/chapters/chapter_02/CMakeLists.txt @@ -9,7 +9,8 @@ add_library( src/structured_binding.cpp src/type_inference.cpp src/variadic_templates.cpp - src/delegate_constructor.cpp) + src/delegate_constructor.cpp + src/inheritance_constructor.cpp) target_compile_features(chapter_02 PUBLIC cxx_std_20) diff --git a/chapters/chapter_02/include/inheritance_constructor.h b/chapters/chapter_02/include/inheritance_constructor.h new file mode 100644 index 0000000..7256484 --- /dev/null +++ b/chapters/chapter_02/include/inheritance_constructor.h @@ -0,0 +1,11 @@ +#pragma once + +#include "chapter_interface.h" + +class InheritanceConstructor : public ChapterTest { +public: + virtual void run() override; + virtual const char *name() const override { + return "Inheritance constructor test"; + }; +}; diff --git a/chapters/chapter_02/src/inheritance_constructor.cpp b/chapters/chapter_02/src/inheritance_constructor.cpp new file mode 100644 index 0000000..4ad4252 --- /dev/null +++ b/chapters/chapter_02/src/inheritance_constructor.cpp @@ -0,0 +1,22 @@ +#include "inheritance_constructor.h" +#include + +class BaseClass { +private: + int value; + +public: + BaseClass(int value) : value(value){}; + void print() { std::cout << value; }; +}; + +class SubClass : public BaseClass { +public: + using BaseClass::BaseClass; +}; + +void InheritanceConstructor::run() { + std::cout << "SubClass(23).print(): "; + SubClass(23).print(); + std::cout << std::endl; +} diff --git a/src/main.cpp b/src/main.cpp index e74c5a9..ba15876 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include "delegate_constructor.h" #include "if_constexpr.h" #include "ifswitch.h" +#include "inheritance_constructor.h" #include "initlist.h" #include "null.h" #include "range_based_for.h" @@ -32,6 +33,7 @@ int main(int argc, char **argv) { chapter_2_tests.emplace_back(std::make_unique()); chapter_2_tests.emplace_back(std::make_unique()); chapter_2_tests.emplace_back(std::make_unique()); + chapter_2_tests.emplace_back(std::make_unique()); for (auto &test : chapter_2_tests) { std::cout << "\n" << test->name() << ":" << std::endl;