Click here to Skip to main content
15,505,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am going through a video series in C++, and have created a project. I have commented some lines of code, and would like to know if my comments are an accurate reflect of the code following the comments.

Would someone explain to me what I have done, regarding the lines without comments?

#include <iostream>

using namespace std;

class Book {
    string title;
    string author;
    Book() { cout << "Book created." << endl; };
    Book(const Book& other) : title(other.title) { cout << "Book created by copying.\n" << endl; };
    void setTitle(string title) { this->title = title; };
    void display() const { cout << "Book title: " << title << endl; }

int main()
    Book book1;

    // Calling the copy constructor using the equal sign
    Book book2 = book1;
    cout << "Book 1 called through copy constructor: " << endl;

    // Object (book2) of the class Book being passed to a function called "display()"

    // Set title for book 2
    cout << endl;

    cout << "Book 1" << endl;
    cout << endl;

    cout << "Calling Book 2 via copy constructor through Book 4:" << endl;
    Book book4 = book2;
    cout << endl;

    // Another way to invoke the copy constructor directly
    cout << "Book 1: Direct invocation" << endl;
    Book book3(book1);

    cout << "Calling the copy constructor through the object, book 3: " << endl;

    return 0;

What I have tried:

I have done research on how to understand the lines of code, however there are questions which are not specific enough for me to determine if I am using the terminology correctly.
Updated 10-Mar-21 21:07pm

No. The comments are useless: they describe the line of code instead of what the code is doing, or why it's doing it:
// Calling the copy constructor using the equal sign

Or they are factually inaccurate:
// Object (book2) of the class Book being passed to a function called "display()"

Use comments to explain why code does something when it not that obvious, or to document what a function does.
Share this answer
CPallini 11-Mar-21 1:58am    
As noted by Griff in his solution, you should not replicate in comments what is obvious just seeing the code. Comments should complete what the code is already telling to you. They can, for instance, explain the reason why the code is as such. Or,as another example, they may shed a bit of light of some obscure-looking parts of the code .

I am wondering why did you comment the code in the main function, instead of the one defining the class Book (usually it is the opposite: Consumers of the class need some documentation).
For example, your class features the unused variable author, you should explain, with appropriate remarks, the reason why there is and is, so far, unused.
Share this answer
bob_smith_0101 14-Mar-21 7:02am    

Very much appreciate the insight, much more tactful in response to my query. I commented the code in the main function because I have no idea how what is the proper "coding comment" etiquette, as I was not exposed to this in grad school (i.e., not MY major).

What is meant by "consumers" of the class?

As far as my questions go (related to the whole "etiquette protocol" for comments -- any resource links? YouTube videos? Would be very much appreciated.
CPallini 14-Mar-21 10:51am    
A consumer of your class is anyone who uses it.
I don't suggest any 'etiquette protocol', I suggest common sense. If you repeat what is already in code, then that is a redundant info. On the other hand, if your comment sheds some light on what cannot be inferred by code inspection, then you are adding value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900