C++ is a programming language 

C++ is a programming language with grammar rules and legal vocabulary, much like any other foreign language. To use it:
a. You create a source code file – a plain text file – containing a “program” written according to the C++ language rules (grammar and vocabulary). Source code is usually saved in a file with a .cpp extension.
b. A special program called a compiler checks the source code file for grammar and vocabulary errors. If there are no errors, the compiler creates the “object file”, a file that contains the original instructions in a machine language format. The object file is not “human readable”. The object file is usually named “somename.obj”.
c. A second program called the linker combines your object file with possibly other object files to form an “executable” file. The executable file contains instructions in a format that the operating system can understand and execute, i.e. this is a real program you can run.
Note that a compiler is not very smart when translating your source code. Every rule of the C++ language must be followed exactly, the compiler does not “know what you mean” unless you specifically and grammatically correctly write down exactly what you mean.

General Formatting:
a. C++ is does distinguish between uppercase and lowercase letters (i.e., it’s case sensitive).
b. Statements must end in a semi-colon.
c. Curly braces { and } are used to group a set of expressions together
d. Comments should be added to your code to clarify it (using either // for short comments or /* … */ for longer comments).

Libraries
C++ provides several libraries of pre-written functions for you that perform frequently needed operations.  You access these libraries by placing #include statements at the top of your source code file.  The most commonly used libraries are listed below.
a. iostream : provides cin and cout
b. cmath : provides math functions like sin(), sqrt() and log10()
c. cstdlib : provides miscellaneous routines like rand()
d. ctime : provides function for manipulating time
e. string : allows use of the string type
f. vector : allows use of the vector type
g. list : allows use of the list type

Variable Declaration
To declare a variable, you must give it a name and a type. 
Syntax:
type varName =  [initialValue]; 
Variable (and function) names should consist of letters, numbers, and the underscore character. They must start with a letter. Spaces are illegal. Note: variable names should always be meaningful. All declared variable have a value. C++ allocates memory and “labels” it when it encounters a declaration.  You may optionally give a variable an initial value when it is declared. 
Example variable declarations:
double pi = 3.14159;
int seconds;
string myName = “Norbert Weiner”;
All variables must be declared before they can be used. Note: Declare variables close to where they are needed and initialize them immediately.

Scope and Scope Rules
The scope of a variable refers to that part of the program where the variable is accessible. There are two scope rules governing the scope of variables (and symbols):
a. The scope of a variable is the codeblock in which it is defined, i.e. a variable is only valid within the block in which it is defined.
b. If two variables by the same name are defined in a block, then the closest declaration counts and hides the original variable.
Global variables are variables whose scope is the entire program. That means that they must be defined outside any function, typically at the top of the program. Every function can access and change a global variable.  Avoid using global variables without good reason.

Constants
Constants are variables whose value cannot change. They are declared the same as variables except that they must be prefaced by the keyword const and must be given a value when they are declared. Syntax;
const type varName = initialValue;
The initial value cannot change throughout the program. As a rule of thumb, never use unnamed constants (plain values) if there is a meaning attached to them. Instead, define a global constant whose name reflects the meaning and use that name instead of the value.


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: