Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
That's because of this part of the code: Book(string aTitle, string aAuthor, int aPages){ aTitle = title; aAuthor = author; aPages = pages; } You're attibuting the properties from the cl...
Answer
#2: Post edited
- That's because of this part of the code:
- ```
- Book(string aTitle, string aAuthor, int aPages){
- aTitle = title;
- aAuthor = author;
- aPages = pages;
- }
- ```
- You're attibuting the properties from the class to the function parameter values. As the properties aren't initialized yet, they're getting just random values. It should have been the oposite. Like this:
- ```
Book(string aTitle, string aAuthor, int aPages){- title = aTitle;
- author = aAuthor;
- pages = aPages;
- }
- ```
- That's because of this part of the code:
- ```
- Book(string aTitle, string aAuthor, int aPages){
- aTitle = title;
- aAuthor = author;
- aPages = pages;
- }
- ```
- You're attibuting the properties from the class to the function parameter values. As the properties aren't initialized yet, they're getting just random values. It should have been the oposite. Like this:
- ```
- Book(string aTitle, string aAuthor, int aPages) {
- title = aTitle;
- author = aAuthor;
- pages = aPages;
- }
- ```
- you can also use member initializer syntax:
- ```
- Book(string aTitle, string aAuthor, int aPages) : title(aTitle), author(aAuthor), pages(aPages) {
- }
- ```
- or like this:
- ```
- Book(string aTitle, string aAuthor, int aPages) : title{aTitle}, author{aAuthor}, pages{aPages} {
- }
- ```
#1: Initial revision
That's because of this part of the code: ``` Book(string aTitle, string aAuthor, int aPages){ aTitle = title; aAuthor = author; aPages = pages; } ``` You're attibuting the properties from the class to the function parameter values. As the properties aren't initialized yet, they're getting just random values. It should have been the oposite. Like this: ``` Book(string aTitle, string aAuthor, int aPages){ title = aTitle; author = aAuthor; pages = aPages; } ```