首页 > 代码库 > 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:如何写好注释
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。