1

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

KutengF
  • 51
  • 6
  • 4
    You should take a look at a good book [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – t.niese Jun 02 '20 at 08:09
  • `I know this is the constructor` is not **the** constructor but **a** constructor (there can be more than one). So `CBook()` is also a constructor. – t.niese Jun 02 '20 at 08:10
  • @t.niese Okay, will do . Thanks. Sorry I've 0 concept about objects. – KutengF Jun 02 '20 at 08:13

1 Answers1

1

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.

NixoN
  • 661
  • 1
  • 6
  • 19