0
Why to add public: Daughter() {}; in this example
I just want to know whether its necessary or a best practice to add this statement "public: Daughter() {};" after declaring a child class in this example #include <iostream> using namespace std; class Mother { public: Mother() {}; void sayHi() { cout << "Hi"; } }; class Daughter: public Mother { public: Daughter() {}; }; int main() { Daughter d; d.sayHi(); } //Outputs "Hi" Even without adding that statement I am getting the same result. Am I missing something. I just used class Daughter: public Mother { };
1 Answer
+ 3
If you don't add "public:", the constructor will be private. If there's no available public constructor, a default one will be made, which does exactly the same thing your current one does.