Basic Constructs in C++
Basic Constructs in C++
DATA TYPES
DATA TYPES
DERIVED DATA TYPES
CHARACTER
FUNDAMENTAL DATA
FLOAT
TYPES
VOID
INTEGER DATA TYPE
• Integers are whole numbers with a machine dependent range
of values. A good programming language as to support the
programmer by giving a control on a range of numbers and
storage space. C has 3 classes of integer storage namely short
int, int and long int. All of these data types have signed and
unsigned forms. A short int requires half the space than normal
integer values. Unsigned numbers are always positive and
consume all the bits for the magnitude of the number. The long
and unsigned integers are used to declare a longer range of
values.
CHARACTER DATA TYPE
• It can store any member of the C++ implementation's
basic character set. If a character from this set is
stored in a character variable, its value is equivalent
to the integer code of that character. Character data
type is often called as integer data type because the
memory implementation of char data type is in terms
of the number code.
FLOAT DATA TYPE
• A number having fractional part is a floating- point
number. An identifier declared as float becomes a
floating-point variable and can hold floating-point
numbers. floating point variables represent real numbers.
They have two advantages over integer data types:-
1: they can represent values between integers.
2: they can represent a much greater range of values.
they have one disadvantage also, that is their operations are
usually slower.
DOUBLE DATA TYPE
• The data type double is also used for handling floating-point
numbers. But it is treated as a distinct data type because, it
occupies twice as much memory as type float, and stores
floating-point numbers with much larger range and precision.
It is slower that type float.
VOID DATA TYPE
• It specifies an empty set of values. It is used as the return type
for functions that do not return a value. No object of type void
may be declared. It is used when program or calculation does
not require any value but the syntax needs it.
INTEGER TYPE
MODIFIERS
FLOATING-POINT
MODIFIERS
INTEGER TYPE MODIFIERS
• C++ offers three types of integer data type:-
1:- short integer- at least two bytes.
2:- int integer – at least as big as short.
3:- long integer-at least four bytes.
the prefix signed makes the integer type hold negative
values also. Unsigned makes the integer not to hold
negative values.
TYPE APPROXIMATE MINIMAL RANGE
SIZE
SHORT 2 -32768 to 32767
UNSIGNED 2 0 to 65,535
SHORT
SIGNED SHORT 2 Same as short
UNSIGNED 4 0 to 4,294,967,295
LONG
SIGNED LONG 4 Same as long
CHARACTER TYPE MODIFIER
• The char can also be signed or unsigned.
unlike int,char is not signed or unsigned by
default. It is later modified to best fit the type
to the hardware properties.
TYPE APPROXIMATE MINIMAL
SIZE RANGE
CHAR 1 -128 to 127
UNSIGNED 1 0 to 255
CHAR
SIGNED CHAR 1 Same as char
FLOATING POINT TYPE
MODIFIERS
• There are three floating-point types: float, double,
and long double. These types represent minimum
allowable range of types.
Note:- don’t use commas in numeric values assigned to
variables.
TYPE APPROXIMAT DIGITS OF
E SIZE PRECISION
FLOAT 4 7
LONG 8 15
DOUBLE
LONG 10 19
DOUBLE
Name Description Size* Range*
signed: -2147483648 to
int Integer. 4bytes 2147483647
unsigned: 0 to 4294967295
signed: -2147483648 to
long int (long) Long integer. 4bytes 2147483647
unsigned: 0 to 4294967295
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
FUNCTIONS
REFERENCES
CONSTANTS
ARRAYS
• An array is a series of elements of the same type placed in
contiguous memory locations that can be individually referenced by
adding an index to a unique identifier.
That means that, for example, we can store 5 values of type int in an
array without having to declare 5 different variables, each one with a
different identifier. Instead of that, using an array we can store 5
different values of the same type, int for example, with a unique
identifier.
• Like a regular variable, an array must be declared before it is used. A
typical declaration for an array in C++ is:
type name [elements];
STRUCTURE
UNION
ENUMERATION
CLASS
• Class: A class is a collection of variables and
function under one reference name. it is the
way of separating and storing similar data
together. Member functions are often the
means of accessing, modifying and operating
the data members (i.e. variables). It is one of
the most important features of C++ since OOP
is usually implemented through the use of
classes.
• Classes are generally declared using the keyword
class, with the following format:
class class_name { access_specifier_1:
member1;
access_specifier_2:
member2; ...
} object_names;
STRUCTURES:-
• A data structure is a group of data elements grouped together under one name.
These data elements, known as members, can have different types and different
lengths.
• Data structures are declared in C++ using the following syntax:
struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
• where structure_name is a name for the structure type, object_name can be a set of
valid identifiers for objects that have the type of this structure. Within braces { }
there is a list with the data members, each one is specified with a type and a valid
identifier as its name.
• Structure is different from an array in the sense that an array represents an
aggregate of elements of same type whereas a structure represents an aggregate of
elements of arbitrary types..
UNION:-
• Unions allow one same portion of memory to be accessed as different data types,
since all of them are in fact the same location in memory. Its declaration and use is
similar to the one of structures but its functionality is totally different:
union union_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
• All the elements of the union declaration occupy the same physical space in memory.
Its size is the one of the greatest element of the declaration.
• all of them are referring to the same location in memory, the modification of one of
the elements will affect the value of all of them. We cannot store different values in
them independent of each other.
One of the uses a union may have is to unite an elementary type with an array or
structures of smaller elements.
• The exact alignment and order of the members of a union in memory is platform
dependant. Therefore be aware of possible portability issues with this type of use.
ENUMERATION:-
• It can be used to assign names to integer constants.
//Program to illustrate Enumerator
#include<iostream.h>
void main(void)
{
enum type{POOR,GOOD,EXCELLENT};//this is the syntax of enumerator
int var;
var=POOR;//this makes programs more understandable
cout<<var<<endl;
var=GOOD;
cout<<var<<endl;
var=EXCELLENT;
cout<<var;
}
//poor=0
good=1
excellent=2
CONCLUSION:-
• We have so many data types to allow the programmer to take
advantage of hardware characteristics. Machines are
significantly different in their memory requirements, memory
access times, and computation speeds.
Operators & Expressions
Contents
Introduction
Arithmetic operations
Arithmetic expressions
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Type conversions in expressions
31
Introduction
An operator is a symbol that tells the computer to perform certain
manipulations.
An expression is a sequence of operands and operators that
reduces to a single value.
C operators can be classified into a number of categories.
◦ Arithmetic operators
◦ Relational operators
◦ Logical operators
◦ Assignment operators
◦ Increment and decrement operators
◦ Conditional operators
◦ Bitwise operators
◦ Special operators
32
Arithmetic operators
• The arithmetic operators in C
Operator meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% modulo division
33
• Note:, Arithmetic operators
– Integer division truncates remainder
– The % operator cannot be applied to a float or double.
– The precedence of arithmetic operators
• Unary + or -
•* / %
•+ -
34
Arithmetic expressions
• An arithmetic expression is a combination of variables,
constants, and operators.
• For example,
• a*b-c a*b-c
• (m+n)(x+y) (m+n)*(x+y)
• ax2+bx+c a*x*x+b*x+c
35
36
Relational Operators
• The relational operators in C are :
Operator Meaning
< less that
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
Relational Operators
• A relational expression yields a value of 1 or
0.
–5<6 1
– -34 + 8 > 23 - 5 0
– if a=3, b=2, c =1; then a > b > c is ?
37
Logical operators
• C has the following three logical operators
– && meaning logical and
– || meaning logical or
– ! meaning logical not ( unary operator )
• Expressions connected by && or || are
evaluated left to right, and evaluation stops as
soon as the truth or falsehood of the result is
known.
38
Assignment operators
• The use of shorthand assignment operators
has three advantages:
– 1. What appears on the left-hand side need not be
repeated and therefore it becomes easier to write.
– 2. The statement is more concise and easier to
read.
– 3. The statement is more efficient.
39
Increment and decrement
operators
C provides two unusual operators for
incrementing and decrementing variables.
The increment operator ++ adds 1 to its operand,
while the decrement operator -- subtracts 1.
The unusual aspect is that ++ and -- may be used
either as prefix operators (before the variable, as
in ++n), or postfix operators (after the variable:
n++).
In both cases, the effect is to increment n. But
the expression ++n increments n before its value
is used, while n++ increments n after its value has
been used.
40
The increment and decrement operators can be
used in complex statements. Example:
m=n++ -j +10;
43
Bitwise operator
• C supports bitwise operators for manipulation of data at bit
level.
• Bitwise operators may not be applied to float or double.
• Bitwise operators are as follows:
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left
>> shift right
~ One’s Complements
Special operators
1. The Comma Operator
The comma operator can be used to link the related
expressions together. A comma-linked list of
expressions is evaluated left to right and the value of
right-most expression is the value of the combined
expression. For example, the statement
value = (x=10, y=5, x+y);
first assigns the value 10 to x, then assigns 5 to y, and
finally assigns 15 to value. Since comma operator has
the lowest precedence of all operators, the
parentheses are necessary.
45
Sizeof operator
48
Precedence of operators
• Precedence establishes the hierarchy of one set of operators
over another when an arithmetic expression has to be
evaluated.
• It refers to the order in which c evaluates operators.
• The evaluation of operators in an arithmetic
expression takes place from left to right for operators having
equal precedence .
Exam. k = 2 * 3/4 + 4/4 + 8–2 + 5/8 ;
O/P => 8
Precedence of operators
BODMAS RULE-
Brackets of Division Multiplication Addition Subtraction
Brackets will have the highest precedence and have to be evaluated
first, then comes of , then comes division, multiplication, addition
and finally subtraction.
C language uses some rules in evaluating the expressions and they r
called as precedence rules or sometimes also referred to as
hierarchy of operations, with some operators with highest
precedence and some with least.
The 2 distinct priority levels of arithmetic operators in c are-
Highest priority : * / %
Lowest priority : + -
Associativity of operators
• Associativity tells how an operator associates with its operands.
for eg:
1. The unary minus associates minus with the quantity to its right.
2. The assignment operator = associates from right to left.
• Hence the expression on the right is evaluated first and its value is
assigned to the variable on the left.
• Associativity also refers to the order in which c evaluates operators in
an expression having same precedence.
• Such type of operator can operate either left to right or vice versa.
• The operator () function call has highest precedence & the comma
operator has lowest precedence
• All unary , conditional & assignment operators associate RIGHT TO
LEFT .
• All other remaining operators associate LEFT TO RIGHT
Rules for evaluation of expression
1. First parenthesized sub expression from left to right are
evaluated.
2. If parentheses are nested, the evaluation begins with the
innermost sub expression
3. The precedence rule is applied in determining the order of
application of operators in evaluating sub expressions
4. The associatively rule is applied when 2 or more operators of
the same precedence level appear in a sub expression.
5. Arithmetic expressions are evaluated from left to right using
the rules of precedence
6. When parentheses are used, the expressions within parentheses
assume highest priority
Hierarchy of operators
Operator Description Associativity
( ), [ ] Function call, array element Left to Right
reference
+, -, ++, - - Unary plus, minus, increment,
,!,~,*,& decrement, logical negation,
1’s complement, pointer Right to Left
reference, address
2/22/2019