Coding understandings by comments

Hi

I have little idea that what is going on.. But I want to make my points strong on each line

Following are the code with comments in each line.. Please review and correct those comments which needs better explanation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<conio.h>

using namespace std;
class example { // Class declared
private:        // Private coding initiative.
    int a, b;   // declared 2 private variables.
public:         // Public coding initiative
    example() { // Constructor started.
        a=10;   // First variable under constructor
        b=20;   // Second variable under constructor
        cout<<"This is COnstructor Block";  // Message to display when constructor provoked
    }
    void display(){    // declare void function to display result
        cout<<"Values :"<<a <<"\t"<<b; // Display result of variable which values assigned by Constructor.
    }
};
int main(){
    example Object;    // Class object declared.
    Object.display();  // display function under class initialized.
    getch();           // End program by pressing Enter.
    return 0;          // Program ended successfully. 
}
Looks ok, how about one comment at the top that says what the program does.
Sir

Can you please elaborate difference between std::cout vs cout

:: stand for ?

and why std write



Sorry, but there are a number of issues. You are also using slightly non-standard grammar.

line 5

That's a class definition, not a declaration.

line 6

"Private coding initiative" does mean anything to me? Perhaps "Private members" (as the public block includes variables and functions.)

line 10, 11

what you are doing here is initializing the variables

(Note it's better form to use the constructor initializer list for this purpose, e.g.

1
2
    example() : a(0), b(0) {
    }
)

line 12

you invoke a constructor rather than provoke it

line 14

You have defined the member function display (though you're declared it as the same time.) Compare with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class example { // Class declared
private:        // Private coding initiative.
    int a, b;   // declared 2 private variables.
public:         // Public coding initiative
    example() { // Constructor started.
        a=10;   // First variable under constructor
        b=20;   // Second variable under constructor
        cout<<"This is COnstructor Block";  // Message to display when constructor provoked
    }
    void display(); // declare void function to display result
};

void example::display(){    // define void function to display result
    cout<<"Values :"<<a <<"\t"<<b; // Display result of variable which values assigned by Constructor.
}


line 15

The use of the word result implies a calculation, which is not the case here. You are just displaying the values of the variables as assigned in the constructor (or set in the constructor.

line 18

You've defined Object here (as a variable of type example.)

line 20

you've called the function, or invoked it, but not initialized it

line 21

getch() doesn't end the program (it waits for the user to press a key)

(Note that getch is a non-standard function, from the point of view of C++, like everything else in conio.h.)

Andy

PS Might be useful for you to read?

Declarations, Prototypes, Definitions, and Implementations
http://www.cplusplus.com/articles/yAqpX9L8/
Last edited on
Can you please elaborate difference between std::cout vs cout

:: stand for ?

std is a namespace

And :: is the scope resolution operator

(here you're resolving the identifier cout in the std namespace.)

See "Namespaces" section on this page:

Name visibility
http://www.cplusplus.com/doc/tutorial/namespaces/

and

Scope resolution operator
https://en.wikipedia.org/wiki/Scope_resolution_operator

Andy
Last edited on
andywestken wrote:
line 5

That's a class definition, not a declaration.

Technically a definition is a declaration, but the word "definition" carries a bit more information so I agree it should be preferred in this situation.
A definition is always a declaration, but not vice versa.

definition = introduce name into the namespace. So the class could be declared simply as

class example; // declared but not defined

But above the members of the class are being defined, so it's also (and more importantly) a definition.

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class B; // declaration

class A { // class definition (and declaration)
    B* pb;
    // etc
public:
    A(); // constructor declaration
    ~A(); // destructor declaration
    void invoke() { // method definition (and declaration)
        // do something which doesn't involve B
    }
};

class B { // definition (and redeclaration)
    // whatever...
};

A::A() : pb(new C);{} // constructor definition

A::~A() { delete pb; } // destructor definition 


Last edited on
Topic archived. No new replies allowed.