2. Data Types
Data types are means to identify the type
of data and associated operation of
handling it .
C++ has three types of data types :-
• Built in data type.
• Derived data type.
• User defined data type.
3. C++ Data
Types
User-defined Derived type
type
structure Built-in array
function
union
class
type pointer
reference
enumeration
Integral Floating
Void
type type
int char float double
4. Type Bytes Range
char 1 -128 to 127
signed: -128 to 127
unsigned: 0 to 255
short int 2 -31768 to 32767
signed: -32768 to 32767
unsigned: 0 to 65535
int 2 -32768 to 32767
signed: -31768 to 32767
unsigned: 0 to 65535
long int 4 -2147483648 to 2147483647
signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
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
5. Built in data type
Built in data types are those who are
not composed of other data types.
There are mainly 5 kinds of build in
data type :-
1.int type.
2.char type.
3.float type.
4.double type.
5.void type.
6. Void data type
The void data type specifies an
empty set of values .
It is used as the return type for
functions that do not return a
value.
7. Int data type
Integers are whole number such as 5,39,-
1917,0 etc.
they have no fractional part.
Integers can have positive as well as
negative value .
An identifiers declared as int cannot have
fractional part.
8. Char data type
characters can store any member of
the c++ implementation’s basic
character set .
An identifiers declared as char
becomes character variable .
char set is often said to be a integer
type .
9. Float data type
A number having a fractional part is
a floating-point number .
the decimal point shows that it is a
floating-point number not an integer.
for ex-31.0 is a floating-point
number not a integer but simply 31 is
a integer.
10. Double data type
It is used for handling floating-point
numbers.
It occupies twice as memory as float.
It is used when float is too small or
insufficienly precise.
11. Data type modifiers
The basic data type has modifiers
preceding them .we use modifier to alter
the meaning of the base type to fit various
situation more precisely.
There are 3 types of modifiers:-
1.Integer type modifiers.
2.Character type modifiers .
3.Float type modifiers .
12. Integer type modifiers
By using different number of bytes to
store values , c++ offers 3 types of
integers :short , int and long that can
represent upto three different integer
sizes.
A short integer is at least 2 bytes .
A int integer is at least as big as short .
A long integer is at least 4 bytes .
13. APPROXIMATE
TYPE SIZE(IN BYTES)
MINIMAL RANGE
short 2 -32768 to 32767
Unsigned short 2 0 to 65,535
Signed short 2 same as short
Int 2 -32768 to 32767
Unsigned int 2 0 to 65,535
Signed int 2 same as int
Long 4 -2,147,483,648 to
2,147,483,647
Unsigned long 4 0 to 4,294,967,295
14. character type modifiers
The char type can also be signed or
unsigned .
The unsigned char represent the range 0
to 255.
The signed char represent the range -128
to 127.
15. Type Approximate Minimal
size(in bytes) range
Char 1 -128 to 127
Unsigned char 1 0 to 255
Signed char 1 same as char
16. Floating-point type modifiers
C++ has three floating-point types : float
, double and long double.
float type occupies 4 byte.
Double occupies 8 byte .
Long double occupies 10 byte.
17. TYPE approximate Digit of
size(in bytes) precision
Float 4 7
Double 8 15
Long double 10 19
18. Derived Data Types
From the built in data types other types
can be derived called derived data types.
There are 5 types of derived data
types :-
1.Arrays.
2.Functions.
3.Pointers.
4.References.
5.Constant.
19. ARRAYS
Values of similar type stored in continuous
memory locations.
int a[10]; char string[3]=“xyz”;
Array can be one dimensional , two
dimensional , multi dimensional.
For ex-float a[3]; //declares array of three
floats :a[0],a[1],a[2].
Int b[2][4]; //declares a 2 dimension array
of integer:b[0][0], b[0][1], b[0][2], b[0][3],
b[1][0], b[1][1], b[1][2], b[1][3].
20. Two ways to make multidimensional arrays
◦ Both examples from Ada
◦ Construct specifically as multidimensional.
matrix: array (1..10, 1..10) of real;
-- Reference example: matrix(7, 2)
Looks nice, but has limited functionality.
◦ Construct as being an array of arrays.
matrix: array (1..10) of array (1..10) of real;
-- Reference example: matrix(7)(2)
Multidimensional Arrays
20
21. Functions
Set of statements to perform specific
tasks.
A piece of code that perform specific
task.
Introduces modularity in the code.
Reduces the size of program.
C++ has added many new features
to the functions to make them more
reliable and flexible.
It can be overloaded.
23. Pointers
Pointers can be declared and initialized as in
C.
int * ip; // int pointer
ip = &x; // address of x assigned to ip
*ip = 10; // 10 assigned to x through
indirection
24. References
A reference is an alternative name of
an object.
Constant
A constant is a data item whose data value
can never change during the program run.
25. Classes and Objects
Class is a way to bind the data and
procedures that operates on data.
Class declaration:
class class_name
{
private:
variable declarations;//class
function declarations;//members
public:
variable declarations;//class
function declarations;//members
};//Terminates with a semicolon
26. Classes and Objects
Class members that have been declared as
private can be accessed only from within
the class.
Public class members can be accessed
from outside the class also.
Supports data-hiding and data
encapsulation features of OOP.
27. Classes and Objects
Objects are run time instance of a class.
Class is a representation of the object, and
Object is the actual run time entity which
holds data and function that has been
defined in the class.
Object declaration:
class_name obj1;
class_name obj2,obj3;
class class_name
{……}obj1,obj2,obj3;
28. Classes and Objects
Accessing class members
◦ Object-name.function-name(actual-arguments);
◦ obj1.setdata(100,34.4);
Defining Member Functions
◦ Outside the class definition.
return-type class-name::function-name
(argument declaration)
{
Function body;
}
◦ Inside the class definition.
Same as normal function declaration.
30. Structures
Structures Revisited
◦ Makes convenient to handle a group of logically
related data items.
struct student //declaration
{
char name[20];
int roll_number;
float total_marks;
};
struct student A;// C declaration
student A; //C++ declaration
A.roll_number=999;
A.total_marks=595.5;
Final_Total=A.total_marks + 5;
31. Structures
Limitations
◦ C doesn’t allow it to be treated like built-in data
types.
struct complex{float x; float y;};
struct complex c1,c2,c3;
c3=c1+c2;//Illegal in C
◦ They do not permit data hiding.
32. Structures in C++
Can hold variables and functions as
members.
Can also declare some of its members as
‘private’.
C++ introduces another user-defined type
known as ‘class’ to incorporate all these
extensions.
33. Unions
A union is like a record
◦ But the different fields take up the same space
within memory
union foo {
int i;
float f;
char c[4];
}
Union size is 4 bytes!
35. void CheckEndian() {
Another union
union {
char charword[4];
unsigned int intword;
example
} check;
check.charword[0] = 1;
check.charword[1] = 2;
check.charword[2] = 3;
check.charword[3] = 4;
#ifdef BIG_ENDIAN
if (check.intword != 0x01020304) { /* big */
cout << "ERROR: Host machine is not Big Endian.nExiting.n";
exit (1);
}
#else
#ifdef LITTLE_ENDIAN
if (check.intword != 0x04030201) { /* little */
cout << "ERROR: Host machine is not Little Endian.nExiting.n";
exit (1);
}
#else
cout << "ERROR: Host machine not defined as Big or Little Endian.n";
cout << "Exiting.n";
exit (1);
#endif // LITTLE_ENDIAN
#endif // BIG_ENDIAN
}