Reputation: 51
My teacher gave me a sample code, but I'm a bit confused.
class CBook
{
...
pubic:
CBook();
CBook(string Title, string Auther, int Year); // I know this is the constructor
~CBook(); // and this is the destructor
};
I wish to know the use of CBook();
, is this line of code really necessary?
Thanks
Upvotes: 1
Views: 53
Reputation: 683
Actually compiler will call this constructor in case you will do like that:
CBook obj;
in another words, you will not pass any arguments to it.
So, if you delete it and try for example:
CBook obj;
compiler will give you an error like "no default constructor"
Sometimes we need object without any of its members filled to fill the later.
Upvotes: 1