Code writing style in a languageC++
Principles of writing code inC++
In the beginning, you should know that all you will learn in this lesson is a theoretical explanation of the methods of writing code.
So in this lesson, you will learn how to write codeC++Correctly understood by the computer and understood by anyone trying to read the code that you will write in the future when building your own applications.
Case Sensitivity in C Plus
languageC++apply concepttheCase Sensitivity,This means that it distinguishes between uppercase and lowercase letters.
Example: note
They Note
are not the same thing.
Strictly Typed in C Plus Plus
languageC++apply concepttheStrictly Typed,This means that the type of the variable must be specified directly when defining it (such as an integer, a character, a decimal number, etc..) and the type of data stored in it cannot be changed later, as is the case in the Java languageCAnd theC#,Unlike some other languages such as Python and JavaScript.
The name of the class in C Plus Plus
It is preferable that the name of the class begins with a capital letter, and if the name of the class consists of more than one word, make the first letter of each word a capital letter.
Examples
• In the event that the name of the class consists of one word.
class Car{ }
• In the event that the name of the class consists of more than one word.
class CppFirstClass{ }
Variable name in C++
Use lowercase letters when choosing names for variables. If the variable name consists of more than one word, start each word with a capital letter starting from the second word.
Examples
• In the event that the variable name consists of one word.
int color;
• If the variable name consists of more than one word.
int firstCarColor;
The name of the function in C++
Use lowercase letters when choosing names for functions. If the name of the function consists of more than one word, start each word with a capital letter starting from the second word.
Examples
• If the function name consists of one word.
void color();
• If the function name consists of more than one word.
void changeCarColor();
Comments on C Plus Plus
We use comments to only make notes about the code we wrote, so we don't forget how we programmed the code in case we want to review or modify it after a long time.
Comments do not affect the written code at all, and in languageC++There are two methods of posting comments:
Comment on one line.
Comment on multiple lines.
Examples
• If the comment consists of one line, we put the // symbol before writing the comment.
// this is a one line comment
• In the event that the comment consists of several lines, we start the comment with the symbol /* and end it with the symbol*/ .
/* This is multilines comment This is a comment Consists of several lines */
Remember: you are not obligated to put comments in your programs. But we recommend that you always put comments to help you understand the code you wrote.
Characters used to put names inC++
Any name we give to a variable, function, class, object etc.. is calledidentifierin programming.
inC++Each element we want to create we have to give it a special name, that is, we have to specifytheidentifierfor him.
So a distinction is made between the elements inC++Through their names, that is, throughtheIdentifiers.
Mandatory rules when giving names
alltheIdentifiersThey must begin with a capital letter in
A-Z
, a lowercase letter in betweena-z
, or a slash_
.prevents starttheIdentifiersby number.
It is forbidden to usetheLiteralswhich values
true
,false
.It is forbidden to use any of the reserved words(Keywords).
do not forget toC++apply principletheCase Sensitive.
Focus on the first letter only
Examples of allowed nouns : H armash , n ame , _ number
Examples of prohibited nouns : 1 st , - cash , @user
What are the reserved words?C++
All of the following words are reserved for the languageC++, which cannot be usedKIdentifiers.
alignof and
and_eq
auto
bitand
bitor
bool
break
case
catch
char
char32_t
class
compl
const
constexpr
const_cast
continue
decltype
default
delete
do
dynamic_cast
else
enum
explicit
export
extern
false
float
for
friend
goto
inline
int
long
mutable
namespace
new
noexcept
not
not_eq
nullptr
operator
or_eq
private
protected
Public
register
reinterpret_cast
requires
return
short
signed
sizeof
static_assert
static_cast
struct
switch
template
this
thread_local
throw
true
try
typedef
typename
union
unsigned
using
virtual
void
volatile
wchar_t
while
xor
xor_eq