Implementing Constructors on Inheritance with using Keyword on C++
using keyword is the new way to implement a constructor to the child class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> #include <string> class MyParent{ public: MyParent(){ std::cout << "MyParent()" << std::endl; } MyParent(std::string inp){ std::cout << "MyParent(" << inp << ")" << std::endl; } ~MyParent(){ std::cout << "~MyParent()" << std::endl; } }; class MyChild : public MyParent{ public: using MyParent::MyParent; //implements all Constructors }; int main() { MyChild p{"Hello"}; return EXIT_SUCCESS; } |