+ 1

Can anyone explain what is the works of blank constructor here Mother() {};

#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(); }

21st Oct 2019, 4:40 AM
Malem Yengkhom
Malem Yengkhom - avatar
3 Antworten
+ 3
It is called as default constructor . Every class has constructor within, whether you declares it or not . If you declare it , compiler will not provide you. Since in your code you haven't made any other constructor even if you drop this nothing will happen . But if you make your own constructor then it's must to make default constructor explicitly otherwise you can't do this action Mother m; //to this action we need //default constructor
21st Oct 2019, 5:35 AM
Nikhil Sharma
Nikhil Sharma - avatar
+ 2
This is called default constructor. You can initialize anything inside that. You can write print statement also. It will invoked by the object by default no need to call. Check this example. Here I didn't call the constructor:- Mother() {}; #include <iostream> using namespace std; class Mother { public: Mother() { cout << "Default Constructor"; }; }; class Daughter: public Mother { public: Daughter() {}; }; int main() { Daughter d; } output is :- Default Constructor https://www.google.com/amp/s/www.geeksforgeeks.org/c-internals-default-constructors-set-1/amp/
21st Oct 2019, 5:25 AM
A͢J
A͢J - avatar
+ 1
Thanks for the help ❣️
21st Oct 2019, 5:33 AM
Malem Yengkhom
Malem Yengkhom - avatar