+ 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(); }
3 odpowiedzi
+ 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
+ 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/
+ 1
Thanks for the help ❣️