C++ is one of the most versatile languages in the world. It is used nearly everywhere for everything… systems programming (operating systems, device drivers, database engines, embedded, Internet of Things, etc.)

Enter Header Image Headline Here

Wednesday 5 April 2017

C++ Comments

                                        C+ is Comments 


What is Comments in C++ ?


Comment in C++ Programming is similar as that of in C. Each and every language will provide this great feature which is used to document source code. We can create more readable and eye catching program structure using comments. We should use as many as comments in C++ program. Comment is non-executable Statement in the C++.



Types of Comment in C++ Programming -

We can have two types of comment in programming - 

  • 1. Single Line Comment
  • 2. Multiple Line Comment

Single Line Comment in C++  -

cout<<"Hello"; //Print Hello Word
 
cout<<"languagecplusplus.com"; //Website
 
cout<<"aradhana"; //Author

  • Single Line comment starts with "//" symbol.
  • Remaining line after "//" symbol is ignored by browser.
  • End of Line is considered as End of the comment.

Multiple Line Comments in C++  -

int main()
{
/* this comment
        can be considered
           as 
   multiple line comment */
 
cout << "Hello C++ Programming";
 
return(0);
}


  • Multi Line comment starts with "/*" symbol.
  • Multi Line comment ends with  "*/" symbol.

How to use Comment more efficiently in C++Programming  -



  • Comment for User Documentation.
  • Hiding Non- usable Code.
  • Hiding Single Statement.
  • Comment Used to explain particular statement.

1. Comment used for Documentation


/* Name : Main Function
 * Parameter : None
 * Return Value : Integer
 */
 
int main()
{
 
cout << "Hello C++ Programming";
 
return(0);
}


Above comment is used for documentation. We use This type of Documentation to summarize C++ code.

2. Comment used to Hide Non- usable line.

<script type="text/javascript">
// document.write("<h1>Heading</h1>");
 
document.write("<p>Google</p>");
document.write("<p>Yahoo</p>");
</script>

We have used comment to prevent execution of single line.

// document.write("<h1>Heading</h1>");

3.  Comment used to Hide Non-usable Multiline Code  -


 
int main()
{
person p1,p2;
 
p1.getData();
p2.putData();
// p3.getData();
 
return(0);
}

4. Comment written after Statement  -



int main()
{
person p1,p2;
 
p1.getData(); // Get Personal Info
p2.putData(); // Save Personal Info
 
return(0);
}


Something About Comment : 

  • Comment are non Executable Statements.
  • Comments are always neglected by compiler.
  • Comments are replaced by white spaces during the preprocessor phase.
  • Comment can be written anywhere and any number of times.
  • Comments can be used for documentation.
  • Comments can be Single Line or multiple line.


Bad Habits of Using Comments in C++ :

1. Don't Write Comment for statement for that can be easily understood.


int main()
{
int age=0;
//initialize age to 0
 
return(0);
}



0 comments:

Post a Comment

Popular Posts

Recent Posts

Categories

Unordered List

Text Widget