您的位置:首页 > 编程语言 > C语言/C++

Simple Tips on C++(对于C++的一些建议)

2015-09-15 00:05 507 查看
Introduction(简介)

For being a powerful object-oriented programming language, C++ is used almost everywhere including graphics and games section.

(C++作为一个强有力的面向对象语言,应用于包括图形、游戏等所有领域中。)

Complex features like multiple inheritance, template programming has made the language more powerful.

(向多重继承、泛型编程等特征使这门语言更加的强有力。)

So if you want to create powerful computer software or games, you should learn C++ properly and effectively.

(如果你想创造强有力的电脑软件和游戏,你应该正确高效的学习C++。)

To the novice programmer, C++ is always more complex and hard programming language than it actually is. I am saying this from my own experience. It is true that C++ is a complex featured programming language. But do not be afraid, once you have realized the importance of all the features of this language, you will feel the pleasure of learning this language.

(对于新手程序员,C++总是更加复杂和艰苦的编程语言,它实际就是这样。我从我自己的经验说这句话的。的确,C ++是一个复杂的功能的编程语言。但是不要害怕,一旦你已经意识到了这种语言的所有功能的重要性,你会觉得学习这门语言的乐趣。)

What IDE Should You Use to Write a C++ Program?

Well, if you are using Windows as your platform and looking for a C++ IDE, then I suggest the Microsoft Visual Studio. It has a fully-featured powerful C++ programming environment. Its various features such as the IntelliSense will help you to write programs more quickly and correctly. Its debugging system will let you correct your program. You will feel comfortable writing C++ program using this IDE.

(你应该使用什么样的IDE来写C ++程序?

这样,如果你使用的是Windows为平台,寻找一个C ++ IDE,那么我建议微软的Visual Studio。它有一个全功能强大的C++编程环境。它的各种功能,如智能感知将帮助您更快速,准确地编写程序。其调试系统将让你纠正你的程序。使用这个IDE编写C ++程序你会感到很舒服。)

How Can You Write a Good C++ Program?

A good program must be sufficiently easy to read, modify and extend. So your program should be easier to read by at least by yourself and there should not be any confusion when reading the program.

(你怎么能写一个好的C ++程序?

一个好的程序必须有足够的易阅性,可修改性和扩展性。所以,至少对于你自己来说,你的程序应该是更易于阅读,在读取程序时,不应该有任何混淆。)

If you are not be able to read your program easily, then how can you know whether yours program is going to work or not?

(如果你不能轻松的阅读自己的程序,你如何知道你的程序是否能正常工作呢?)

If any portion of your code is looking confusing or disturbing, your mind then deletes it and writes it again properly.

(如果你代码的任何部分看上去使人迷惑和令人不安,你的想法应该是立刻删除它,并且重新编写。)

C++ is a case-sensitive programming language. So always be careful about that.

(C ++是一种区分大小写的编程语言。所以,一定要加倍小心这一点。)

The following things will help you to improve readability of your program.

(接下来的事情将会帮助你提高你的程序的可阅读性。)

Variable Naming

(变量命名)

Use a deserving name for each variable of your program so that it will be easier for you to understand what purpose that variable was made for.

(为您的程序的每个变量使用一个值得的名称,这样它会更容易让你了解这个变量是为什么目的。)

Horrible variable naming can be also a great cause of program bug, at least for a novice programmer.

(至少对于一个新手程序员来说,恐怖的变量命名会引起程序很大的bug。)

int Money = 100;
const char* Name = "Shuvo";
printf("%s has only %d taka!\n", Name, Money);


Instead of:

(替换)

int Var1 = 100;
const char* Var2 = "Shuvo";
printf("%s has only %d taka!\n", Var2, Var1);
Use 'g_' or 'g' prefix for global variable and use 'm_' or 'm' prefix for member variable.


Use different prefix for each data-type such as ‘n’ or ‘i’ for integer variables, ‘f’ for floating type variables, ‘u’ for unsigned type, ‘p’ for any kind of pointers.

Use ‘str’ prefix for string type ( std::string ) variables.

(对于不同的变量类型,使用不同的前缀。比如说对于整形变量使用’n’或’i’,对于浮点型使用’f’,对于无符号类型使用’u’,对于各种类型的指针可以使用’p’。)

Use of data-type prefix will help you to figure out variables data-type, especially when your Code Editor does not support IntelliSense like feature.

(所有的数据类型的加前缀都会使你分清变量类型,尤其是你的编译器不支持智能推断。)

I am not saying that you must have to use those prefix and of course you can use your own variable name prefix that will help you much more than those prefixes.

(我不是说你必须使用这些前缀进行命名,你可以使用你自己的方式进行前缀命名。)

Function Naming

(函数命名)

Use a suitable name for each function of your program so that it will be easier for you to understand what intention the function was made for and what kind of job is doing the function.

(为您的程序的每个函数使用一个合适的名称,这样它会更容易让你明白为什么写这个函数,这个函数所做的工作是什么样子的。)

Use a different naming style for global and member functions that will help you to remember when you invoke the function. Although you can use the ‘::’ operator of C++ language for that purpose.

(对于全局函数和成员函数,使用不同的命名方式会帮助你记住合适调用这个函数。尽管在C++中你可以使用操作符::来显现类成员函数。)

Use of Comment

(使用注释)

Use comments at any place of your program where there is a possibility of confusion. It will help you remember which statement of your program is written for what purpose. It also helps when you later try to update your source code.

(在你的程序容易引起困惑的地方需要进行注释。这将会帮你记住你写此程序的意图。同样也会帮你记住你最后一次修改这段代码是什么时候。)

In C++, ‘// …..’ is used for single-line comment and ‘/* ….. */’ is used for multi-line comment.

(在C++中’// …..’ 用于单行注释,’/* ….. */’用于多行注释)

If you are working with several projects, I strongly suggest that you use enough comments inside each project source code.

(如果你的工作有几个工程,我强烈建议你在源代码中加入足够的注释。)

Keeping the Memory Free

(不要内存泄露)

This is the most painful task for a C++ programmer. Novice C++ programmer often forgets to deallocate the allocated memory and this makes a program buggy and memory cost.

(这是一个C ++程序员最痛苦的任务。初学C++程序员经常忘记释放分配的内存,这使得程序缺陷和存储成本。)

So do not let your program crash the user computer as well as your computer. Think more about memory management of your program.

(所以,不要让你的程序崩溃用户电脑,以及您的计算机。多想想你的程序的内存管理。)

When freeing a pointer, always check whether that pointer actually exists so that reduces program crash. You can do it in this way:

(当释放指针,请务必检查指针是否实际存在,这样减少了程序崩溃。你能做到这样:)

if ( ptr != NULL )
{
    delete ptr;
    ptr  = NULL;
}


Instead of:

(替换)

delete ptr;


Organize Your Project Source Code

(组织你的工程源代码)

If you are going to develop a big project that will need over thousand lines of source code, then do not gather all your source code into a single CPP source file. It will force your C++ compiler to compile the whole source-code even for every little modification.

(如果你要开发将需要超过千行源代码的大项目,那么就不要收集所有的源代码到一个单一的CPP源文件。它会迫使你的C ++编译器编译整个源代码,甚至对每个小的修改。)

Divide your source code into several CPP source and header files. This will let your compiler compile only those source files that you have modified. So it will save your time.

(把你的源代码为几个CPP源文件和头文件。这将让你的编译器编译只已修改的源文件。因此,这将节省您的时间。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: