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 { };

2nd Feb 2018, 6:00 AM
Amit Kumar
Amit Kumar - avatar
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.
2nd Feb 2018, 6:01 AM
BlazingMagpie
BlazingMagpie - avatar