C++ Identifiers.

C++ Programming Tutorial

C++ Identifiers.


The name that we give to an entity in the program so that it can be uniquely identified, is called "identifier". The names of variables, functions, labels of a class, and other user-defined objects are called 'identifiers'. ‘Identifiers’ cannot be used as keywords.






List of identifiers.

  1. Constants.

  2. Variables

  3. Functions

  4. Labels

  5. Defined data types


Rules for identifiers declaraion.

  1. Only alphabetic characters, digits, and underscores are allowed.

  2. The identifier name cannot start with a digit.

  3. Therefore, we can say that C++ identifiers are case-sensitive. For eg int 'a' and int 'A' are differnt variables.

  4. A declared keyword cannot be used as a variable name.





Operators in C++.

C++ Programming Tutorial

Operators in C++.


The operator is a symbol that tells the compiler to perform any mathematical and logical operation.

Such symbols which are used to perform any type of mathematical and logical operation. They are called operators in C++ programming. In programming, operators are used to manipulating data and variables.






Operators work together with one or more variables, constants, or operands. Variable, constant, operands, function, and operators merge all these together to form an expression.

Types of operators in C++ programming.

In C++ programming there are lots of built-in operators that have been divided into different types.

  1. Arithmetic Operators.

  2. Assignment Operators.

  3. Relational Operators.

  4. Logical Operators.

  5. Conditional Operators (Ternary Operators).

  6. Bitwise Operators.

  7. Increment / Decrement Operators.

  8. Special Operators.


1.Arithmetic Operators

Arithmetic operators are the most basic operators. These operators are used to perform mathematical operations such as Addition (addition), Subtraction (subtraction), Multiplication (multiplication), Division (division / division), Modulus division (modulus division).

Operators Explanation

+ (Addition)

It adds two operands.

- (Subtraction)

This removes the left operand from the right operand.

* (Multiplication)

It multiplies the two operands.

/ (Division)

It divides left operand by the right operand.

% (Modulus)

It extracts the remainder by dividing left operand by the right operand.

#include <iostream> using namespace std; int main() { int number1,number2,res; cout<<"\n Enter first number:"; cin>>number1; cout<<"\n Enter second number:"; cin>>number2; res=number1+number2; cout<<"\n Sum of two numbers : "<<res; res=number1-number2; cout<<"\n Subtraction of two numbers : "<<res; res=number1*number2; cout<<"\n Multiplication of two numbers : "<<res; res=number1/number2; cout<<"\n Division of two numbers : "<<res; res=number1%number2; cout<<"\n Remainder of two numbers : "<<res; return 0; }
Output:
Enter first number:5 Enter second number:3 Sum of two numbers : 8 Subtraction of two numbers : 2 Multiplication of two numbers : 15 Division of two numbers : 1 Remainder of two numbers : 2



2.Assignment Operators

The assignment operator is used to assign a value to a variable. Some operators come in the list of these operators, whose list is given in the table below. For example x=x+y same as x+=y.

Operators Examples

= (assignment)

c = a + b

+= (add assignment)

c += a same as c = c + a

-= (subtract assignment)

c -= a same as c = c - a

*= (multiply assignment)

c *= a same as c = c * a

/= (divide assignment)

c /= a same as c = c / a

%= (modulus assignment)

c %= a same as c = c % a

&= (AND assignment)

c &= a same as c = c & a

|= (OR assignment)

c |= a same as c = c | a

^= (XOR assignment)

c ^= a same as c = c ^ a

<<= (Left Shift assignment)

c <<= a same as c = c << a

>>= (Right Shift assignment)

c >>= a same as c = c >> a

#include <iostream> using namespace std; int main() { int number1,number2; cout<<"\n Enter first number:"; cin>>number1; cout<<"\n Enter second number:"; cin>>number2; number1+=number2; cout<<"\n Sum of two numbers : "<<number1; number1-=number2; cout<<"\n Subtraction of two numbers : "<<number1; number1*=number2; cout<<"\n Multiplication of two numbers : "<<number1; number1/=number2; cout<<"\n Division of two numbers : "<<number1; number1%=number2; cout<<"\n Remainder of two numbers : "<<number1; number2 &= 2; cout<<"\nvalue of number2 &= 2 is "<< number2<<endl; number2 |= 2; cout<<"value of 'number2 |= 2' is "<< number2<<endl; number2 ^= 2; cout<<"value of number2 ^= 2 is "<< number2<<endl; number2 <<= 2; cout<<"value of number2 <<= 2 is "<< number2<<endl; number2 >>= 2; cout<<"value of number2 >>= 2 is "<< number2<<endl; return 0; }
Output:
Enter first number:5 Enter second number:3 Sum of two numbers : 8 Subtraction of two numbers : 5 Multiplication of two numbers : 15 Division of two numbers : 5 Remainder of two numbers : 2 value of number2 &= 2 is 2 value of 'number2 |= 2' is 2 value of number2 ^= 2 is 0 value of number2 <<= 2 is 0 value of number2 >>= 2 is 0 Enter first number:20 Enter second number:12 Sum of two numbers : 32 Subtraction of two numbers : 20 Multiplication of two numbers : 240 Division of two numbers : 20 Remainder of two numbers : 8 value of number2 &= 2 is 0 value of 'number2 |= 2' is 2 value of number2 ^= 2 is 0 value of number2 <<= 2 is 0 value of number2 >>= 2 is 0

3.Relational Operators

Relational Operator is used to compare the value of two operands, hence it is also called a Comparison operator. In the table below, lists of Relational Operators are given.

Operator Description

==

This operator is called equal to the operator. This operator is used to equal check two values. If both values are equal, then it returns true.

!=

This operator is called Not equal to the operator. It is used to check that two operands are not equal. Meaning that this operator is used to check the value of two operands, if the value of both operands is not equal then it returns true.

>

This operator is called Greater than the operator. It is used to check the value of the first operand greater than the value of the second operand. If the value of the first operand is greater than the value of the second operand, then it returns true like (5> 2) return true.

<

This operator is called Less than the operator. It is used to check the value of the first operand less than the value of the second operand. If the value of the first operand is smaller than the value of the second operand, then it returns true such as (3 <4) return true.

>=

This operator is called Greater than equal to the operator. It is used to check the value of the first operand greater than and equal to the value of the second operand. If the value of the first operand is greater than or equal to the value of the second operand, then it returns true as (5> = 5) return true.

<=

This operator is called Less than equal to the operator. It is used to check the value of the first operand less than the value of the second operand. If the value of the first operand is smaller than or equal to the value of the second operand, it returns true such that (5 <= 5) return true.

#include <iostream> using namespace std; int main(){ int number1, number2; cout<<"\n Enter first number : "; cin>>number1; cout<<"\n Enter second number : "; cin>>number2; if(number1< number2) { cout<<" number1 is less than number2"<<endl; } else { cout<<" number1 is greater than number2"<<endl; } if(number1<= number2) { cout<<" number1 is less than number2"<<endl; } else{ cout<<" number1 is greater than number2"<<endl; } if(number1> number2) { cout<<" number1 is greater than number2"<<endl; } else { cout<<" number1 is less than number2"<<endl; } if(number1>= number2) { cout<<" number1 is greater than number2"<endl; } else { cout<<" number1 is less than number2"<<endl; } if(number1== number2) { cout<<" number1 is equal to number2"<<endl; } else { cout<<" number1 is not equal to number2"<<endl; } return 0; }
Output:
Enter first number : 31 Enter second number : 36 number1 is less than number2 number1 is less than number2 number1 is less than number2 number1 is less than number2 number1 is not equal to number2 Enter first number : 31 Enter second number : 31 number1 is greater than number2 number1 is less than number2 number1 is less than number2 number1 is greater than number2 number1 is equal to number2



4.Logical Operators.

Logical operators are used to combining two expressions and a condition. These operators are used to perform logical operations.

Operators Explanation

&& (logical &&)

If both the conditions are true then it will return true.for e.g. (5 <6) && (6> 5)

|| (logical OR)

If either one is true, then it will return true. For e.g. (5 <6) || (6> 5)

! (logical not)

If the condition is true, it makes it false.for e.g. ! ((5 <6) && (6> 5)) ! ((5 <6) || (6> 5))

#include <iostream> using namespace std; int main() { int number1,number2,number3,number4; cout<<"\n Enter first number :"; cin>>number1; cout<<"\n Enter second number :"; cin>>number2; cout<<"\n Enter third number :"; cin>>number3; cout<<"\n Enter fourth number :"; cin>>number4; if((number1<number2) && (number3>number4)) { cout<<"Condition is true ( AND operator)."<<endl; } else { cout<<"Condition is false ( AND operator)."<<endl; } if((number1<number2) || (number3>number4)) { cout<<"Condition is true ( OR operator)."<<endl; } else { cout<<"Condition is false ( OR operator)."<<endl; } if(!((number1<number2) && (number3>number4))) { cout<<"Condition is true ( NOT operator)."<<endl; } else { cout<<"Condition is false ( NOT operator)."<<endl; } return 0; }
Output:
Enter first number :10 Enter second number :20 Enter third number :15 Enter fourth number :8 Condition is true ( AND operator). Condition is true ( OR operator). Condition is false ( NOT operator).

5.Conditional Operators (Ternary Operators)

Conditional operators are used to checking the condition. This operator has only two options TRUE and FALSE. If the given condition satisfies, then TRUE will return otherwise FALSE will be returned.

Syntax of conditional operators.
Condition ? True Expression : False Expression;
#include <iostream> using namespace std; int main() { int number; cout<<"\n Enter any number :"; cin>>number; ( number%2==0) ? cout<<"\n It is Even number" : cout<<"\n It is Odd number" ; return 0; }
Output:
Enter any number :31 It is Odd number Enter any number :36 It is Even number



6.Bitwise Operators.

Bitwise operator is used to manipulating BIT level data. This operator is used for shifting from right to left and left to right bit. The bitwise operator does not apply to float and double data type.

The full form of Bit is a binary digit which is 0 and 1. The bitwise operator is calculated at 0 and 1. Whenever we manipulate the decimal number by the bitwise operator, the processor first converts it in the form of 0 and 1 and then calculates it.

Operator Description

&

Bitwise AND

|

Bitwise OR

^

Bitwise exclusive OR

<<

Left Shift

>>

Right Shift

Bitwise operator &, |, ^

p q p & q p | q p ^ q

0

0

0

0

0

0

1

0

1

1

1

0

0

1

1

1

1

1

1

0

Operation on AND(p&q)
Decimal Value Binary Value If p = 20, q = 12, then 20 0 0 0 1 0 1 0 0 12 0 0 0 0 1 1 0 0 -------------------------- 4 0 0 0 0 0 1 0 0 --------------------------
Operation on OR(p|q)
Decimal Value Binary Value If p = 20, q = 12, then 20 0 0 0 1 0 1 0 0 12 0 0 0 0 1 1 0 0 -------------------------- 28 0 0 0 1 1 1 0 0 --------------------------
Operation on XOR(a^b)
Decimal Value Binary Value If a = 20, b = 12, then 20 0 0 0 1 0 1 0 0 12 0 0 0 0 1 1 0 0 -------------------------- 24 0 0 0 1 1 0 0 0 --------------------------

Binary Left Shift( << ) and Right Shift( >> )

Left Shift (<<) for e.g. a = 20; / * 0001 0100 * / a << Shifts every binary number in binary value of numeric value in 2 with 2 binary numbers left side. For e.g.a = 20; / * 0001 0100 * / So this means 0101 0000 will be 80.

Right Shift (>>) for e.g. a = 20; / * 0001 0100 * / This is completely opposite from Left shift. Right Shift a >> shifts every binary number from the right side to 2 binary numbers in binary value of numeric value in 2. for e.g.a = 20; / * 0001 0100 * / So its 0000 0101 means 5.

Complement Operator (~)

This operator does all the bit reverse.

This operator reduces 0 to 1 and 0 to 0.

Operation on Complement( ~ )
Decimal Value Binary Value ~12 0 0 0 0 1 1 0 0 -------------------------- 243 1 1 1 1 0 0 1 1 -------------------------- Why did 243 come here instead of Output -13?

2's Complement of 243 -(reverse of 243 in binary + 1)

Operation on 2's Complement( ~ )

Decimal Value Binary Value 2's Complement

243 1111 0011 -(0000 1100+1) = -(0000 1101) = -13
#include <iostream> using namespace std; int main() { int number1,number2,result; cout<<"\n Enter first number:"; cin>>number1; cout<<"\n Enter second number:"; cin>>number2; result=number1&number2; cout<<"value of result(number1&number2) is "<<result<<endl; result=number1|number2; cout<<"value of result(number1|number2) is "<<result<<endl; result=number1^number2; cout<<"value of result(number1^number2) is "<<result<<endl; result=number1<<2; cout<<"value of result(number1<<2) is "<<result<<endl; result=number1>>2; cout<<"value of result(number1>>2) is "<<result<<endl; cout<<"value of result(~number1) is "<<~number1<<endl; return 0; }
Output:
Enter first number:20 Enter second number:12 value of result(number1&number2) is 4 value of result(number1|number2) is 28 value of result(number1^number2) is 24 value of result(number1<<2) is 80 value of result(number1>>2) is 5 value of result(~number1) is -21 Enter first number:31 Enter second number:36 value of result(number1&number2) is 4 value of result(number1|number2) is 63 value of result(number1^number2) is 59 value of result(number1<<2) is 124 value of result(number1>>2) is 7 value of result(~number1) is -32

7.Increment / Decrement Operators.

The increment / decrement operator is used to increase and decrease the value of the variable once. For example, if the variable already has a 5 value store, then its value will be reduced to 6 by increment operator and its value will be 4 by decrement operator.

There are two types of increase / decrement operator.

  1. Pre increment.

  2. Pre decrement.

  3. Post increment.

  4. Post decrement.


Pre increment / decrement operator.

In the pre-increment/decrement operator, first, the value increase and decrease occur and then the further calculation is performed.

Syntax of pre increment / decrement operator.
++ (variable name), – – (variable name)

Post increment / decrement operator.

In the post-increment/decrement operator, the calculation is first performed and then the value change occurs. Post means later. There is a value increase and decrease after the calculation is performed.

Syntax of post increment / decrement operator.
(variable name) ++, (variable name) – –
#include <iostream> using namespace std; int main() { int x; cout<<"\n Enter x value :"; cin>>x; cout<<"Print Value of Pre-increment : "<<++x<<endl; cout<<"Value of x : "<<x<<endl; cout<<"Print Value of Pre-decrement : "<<--x<<endl; cout<<"Value of x : "<<x<<endl; cout<<"Print Value of Post increment : "<<x++<<endl; cout<<"Value of x : "<<x<<endl; cout<<"Print Value of Post decrement : "<<x--<<endl; cout<<"Value of x : "<<x<<endl; return 0; }
Output:
Enter x value :31 Print Value of Pre-increment : 32 Value of x : 32 Print Value of Pre-decrement : 31 Value of x : 31 Print Value of Post increment : 31 Value of x : 32 Print Value of Post decrement : 32 Value of x : 31



Reserved Words or Keywords in C++.

C++ Programming Tutorial

Reserved Words or Keywords in C++.


These words have special meaning for C++ Compiler, hence they are called Keyword or Reserve Words. Each Reserve Word has its own special meaning and every Reserve Word is used only to accomplish a particular task in a particular situation. We cannot use a Reserve Word as a common identifier.






A list of 32 Keywords in C++ Language which are also available in C language is given below.

auto

break

case

char

const

continue

default

do

double

else

enum

extern

float

for

goto

if

int

long

register

return

short

signed

sizeof

static

struct

switch

typedef

union

unsigned

void

volatile

while

A list of 30 Keywords in C++ Language which are not available in C language is given below.

asm

dynamic_cast

namespace

reinterpret_cast

bool

explicit

new

static_cast

false

catch

operator

template

friend

private

class

this

inline

public

throw

const_cast

delete

mutable

protected

true

try

typeid

typename

using

virtual

wchar_t

C++ Keywords Description

asm

Used to write assembly language.

auto

Storage is a type of class. Which is also used for Local variables?

bool

The Boolean variable is used to declare. For eg. True or False.

break

Used to break loops or statements.

case

The switch case statement is to be used.

catch

Used to handle exceptions from the throw.

char

The character variable is used to declare.

class

Used to declare classes.

const

Const This is used for a constant variable.

const_cast

The Constant variable is cast.

continue

The loop is iterated.

default

The switch is used in case statements.

delete

Used for dynamic memory allocation.

do

Is a loop type. With which, while loop is used.

double

The floating-point is the data-type.

dynamic_cast

Used with the pointer.

else

The statement with if is used.

enum

Enumeration is the 'Keyword' of the data type.

explicit

Constructor is used to convert.

export

The definition of the template is exported.

extern

Storage is a type of class. Whose scope is global?

false

Boolean has a value.

float

The floating-point variable is used to declare.

for

Is a type of loop?

friend

Non-member function; Used to access private data.

goto

There is a statement that contains a label.

if

Is a statement. Which shows whether the condition is true or false?

inline

Is used for the function.

int

The integer variable is used to declare.

long

The long integer variable is used to declare.

mutable

Is the type of storage class?

namespace

The same identifiers (variables, functions, classes) are used to tell differently.

new

Used for dynamic memory allocation.

operator

Used to declare overloaded operators.

private

Used to declare private members of a class.

protected

Used to declare protected members of the class.

public

Used to declare public members of the class.

register

Is the type of storage class.

reinterpret_cast

The cast-type Variable is used to change.

return

Is used for the function.

short

The short integer variable is used to declare.

signed

Is a variable modifier.

sizeof

The size of the variable is used to return.

static

Is the type of storage class?

static_cast

Is a type conversion.

struct

The structure is used to define or declare.

switch

Is a statement.

template

Used to write the Generic Program.

this

Brings the pointer to the current object.

throw

The exception is used for handling.

true

Boolean has value.

try

The exception is used for handling.

typedef

The data type is used to give alias name.

typeid

The object is described.

typename

The alternative to the class is.

union

This keyword is assigned to its members at the same memory location.

unsigned

The unsigned integer variable is used to declare.

using

Used with a namespace.

virtual

Runtime is used for polymorphism.

void

It does not return anything.




C++ Data Types.

C++ Programming Tutorial

C++ Data Types.


Introduction of Data Types

We are creating variables to store data in the C++ language. These data variables are called data types.






Types Data Types

Basic Data Type.

int, char, float, double, etc.

Derived Data Type.

array, pointer, etc.

Enumeration Data Type.

enum.

User Defined Data Type.

structure and class.

Basic Data Types.

Basic data types of data types are found in all programming languages. Because these are the basic data types. Which are mandatory to use. They are.

  1. Integer data type

  2. Float data type

  3. Character data type


Integer data type.

The integer data type only stores whole numbers. The data type found in the integer data type.

Float data type.

Which contains a decimal data stored data type is called float data type.

Character data type.

These data types stores the character in the variables is called a character data type.

User Defined Data Types.

Structure and union in C language are a user-defined data type. This type of data type is also completely allowed in C ++. And at the same time, C ++ provides us the capabilities to create some new user define data types which are known as object-oriented programming.

Class.

Class is an object-oriented programming base data type. And Class is a user-defined data type. The class is also like structure.

class num { your variables and function };

Enum Data Type.

Enum data type is also user define data types. To create an enum data type, we use the enum keyword. And whenever we create Enum data types. So numbers are attached to each of the names. That is, using names like enum keyword automatically assigns names from 0 to values.

enum num {values1, values2, values3};

Derived Data Types.

Derived data types are derived from basic data types.

  1. Arrays.

  2. Functions.

  3. Pointers , etc.


Basic Data Types.

The basic data types are integer-based, floating-point, and character-based. C++ language supports both signed and unsigned literals.

The memory size of basic data types may change according to a 32 or 64-bit operating system.

Let's see the basic data types. It size is given according to 32 bit OS.

Data Types Memory Size Range

char

1 byte

-128 to 127

signed char

1 byte

-128 to 127

unsigned char

1 byte

0 to 127

short

2 byte

-32,768 to 32,767

signed short

2 byte

-32,768 to 32,767

unsigned short

2 byte

0 to 32,767

int

2 byte

-32,768 to 32,767

signed int

2 byte

-32,768 to 32,767

unsigned int

2 byte

0 to 32,767

short int

2 byte

-32,768 to 32,767

signed short int

2 byte

-32,768 to 32,767

unsigned short int

2 byte

0 to 32,767

int

2

-32768 To 32767

Short int

2

-31768 To 32767

Long int

4

-2147483648 To 2147483647

signed int

2

-31768 To 32767

unsigned int

2

0 To 65535

float

4

3.4E-38 to 3.4E+38

double

8

1.7E-308 to 1.7E+308

long double

10

3.4E-4932 to 1.1E+4932