首页 > 代码库 > c++学习笔记4:如何写好注释

c++学习笔记4:如何写好注释

1.单行注释一般放在语句的上方:

// cout and endl live in the iostream library
std::cout << "Hello world!" << std::endl;

2.多行注释写法:

/* This is a multi-line comment.
 * the matching asterisks to the left
 * can make this easier to read
 */

3.注释的用处

.At the library, program, or function level, describe what
.Inside the library, program, or function, describe how
.At the statement level, describe why

通俗点就是:

(1)描述该块代码是什么;

(2)描述该块代码如何实现;

(3)描述该块代码为什么要如此实现;

 

接下来举些例子:

(1)what

// This program calculate the student‘s final grade based on his test and homework scores.

 

(2)how

/* To calculate the final grade, we sum all the weighted midterm and homework scores
    and then divide by the number of scores to assign a percentage.  This percentage is
    used to calculate a letter grade. 
*/
// To generate a random item, we‘re going to do the following:
// 1) Put all of the items of the desired rarity on a list
// 2) Calculate a probability for each item based on level and weight factor
// 3) Choose a random number
// 4) Figure out which item that 

(3)why

// We need to divide items by 2 here because they are bought in pairs
cost = items / 2 * storePrice;

 

c++学习笔记4:如何写好注释