Beruflich Dokumente
Kultur Dokumente
Programming
• Identifiers cannot match any keyword of the C++ language nor your compiler's specific
ones, which are reserved
Some standard reserved keywords
Some of the standard reserved keywords are:
bool, break, case, catch, char, class, const, const_cast, operator,
continue, asm, default, delete, do, double, dynamic_cast, else,
enum, explicit, export, extern, false, float, for, friend, goto, if,
inline, int, long, auto, mutable, namespace, new, private,
protected, public, register, reinterpret_cast, return, short, signed,
sizeof, static, static_cast, struct, switch, template, this, throw, true,
try, typedef, typeid, typename, union, unsigned, using, virtual,
void,volatile, wchar_t, while, and, and_eq, bitand, bitor, compl,
not, not_eq, or, or_eq, xor, xor_eq etc. etc.
Remember!
The C++ language is a "case sensitive" language.
Different data types are not going to be interpreted the same way.
Data Types
Below are the basic fundamental data types in C++:
Char
short int
Int
long int
Bool
float
double
Data Type - Char
The char data type is used to store character or small integer.
true or false
Data Type - float
The float data type is used to store Floating point number (fractions
or decimals).
3.4x10-38 to 3.4x1038
Data Type - double
The double data type is used to store Double precision floating point
number (fractions or longer decimals).
3.4x10-308 to 3.4x10308
Declaration of variables
In order to use a variable in C++, we must first
declare it specifying which data type we want it
to be.
Global scope
Local scope.
Global and Local
// operating with variables
Variables
#include <iostream>
using namespace std;
//global variables
int integer;
char acharacter;
float afraction;
int main()
{
// Local variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
// print out the result:
cout << result;
// terminate the program:
return 0;
Global Variables
A global variable is a variable declared in the
main body of the source code, outside all
functions.
For example,
int a = 0;
Constructor initialization
For example:
int a (0);
Initialisation of Variables
// initialization of variables
#include <iostream>
using namespace std;
int main ()
{
int a=5; // initial value = 5
int b(2); // initial value = 2
int result; // initial value undetermined
a = a + 3;
result = a - b;
cout << result;
return 0;
}
Strings
Strings are Variables that can store non-numerical values
that are longer than one single character.
<string>
Strings can also perform all the other basic operations that
fundamental data types can, like being declared without
an initial value and being assigned values during
execution:
Constants
for example:
a = 5;
Defined constants
You can define your own names for constants that you use very often
without having to resort to memory-consuming variables, simply by
using the #define preprocessor directive.
For example:
#define PI 3.14159265
#define NEWLINE '\n'
Defined constants – cont.
Once they are defined, you can use them in the rest of the code as if
they were any other regular constant.
Operators