I have confusion after reading from some places.
I'm doing example from this page let's say there's a class(Book) which has an object(Author) as one of its member. Here's the constructor:
Book::Book(string name, Author author, double price, int qtyInStock)
: name(name), author(author) { **// Must use member initializer list to construct object**
// Call setters to validate price and qtyInStock
setPrice(price);
setQtyInStock(qtyInStock);
}
I tried to declare the object author inside the constructor instead of initializer lisr. It gave me error. no matching function to call Author::Author()
--> which is the default constuctor of Author.
After reading from these stackoverflow posts : this1 and this2 What I understand is that, summarizing from those 2:
an object is considered & must be fully initialised when execution enters the body of the constructor
An object has all of its members initialised in the initialisation list. Even if you do not explicitly initialise them there, the compiler will happily do so for you
native types like int or double do have a default constructor
So from all of those above, what I understand is a user defined object/class DOES NOT automatically have DEFAULT CONSTRUCTOR, unlike the primitive types. That's why it gives error, if I do not use member initializer list (which calls copy constructor) , cause the compiler tries to initialize it using default consturctor which it(user defined class) does not have.
And so possible solutions are: define a default constructor for the class, or use member initializer
AND THEN I read this post on stackoverflow saying that: "How Many default methods does a class have?" and the answer mentions that It HAS DEFAULT CONSTRUCTOR
1. If it has default consturctor, Why does my first case( the book and author classes) give error?
Also I read from this page, lets say I defined a class Point before, then page wrote:
Point p1; // **Invoke default constructor**
// OR Point p1 = Point(); NOT Point p1();
2. So when we declare like above, does it INVOKE DEFAULT CONSTRUCTOR? Why it does not give an error if so? I thought a class does not have a default constructor?
Appreciate if you can help me to clarify things here, and answer the two questons above (italic)