Data types

36
DATA TYPES By:-Nokesh prabhakar.

description

contents has all information about data types in c++.

Transcript of Data types

Page 1: Data types

DATA TYPESBy:-Nokesh prabhakar.

Page 2: Data types

Data TypesData 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.

Page 3: Data types

Built-in type

Integral type

int char

Void Floating type

float double

Derived typearray

functionpointer

reference

Page 4: Data types

Type Bytes Range

char 1 -128 to 127signed: -128 to 127unsigned: 0 to 255

short int 2 -31768 to 32767signed: -32768 to 32767unsigned: 0 to 65535

int 2 -32768 to 32767signed: -31768 to 32767unsigned: 0 to 65535

long int 4 -2147483648 to 2147483647signed: -2147483648 to 2147483647unsigned: 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

Page 5: Data types

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.

Page 6: Data types

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.

Page 7: Data types

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.

Page 8: Data types

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 .

Page 9: Data types

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.

Page 10: Data types

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.

Page 11: Data types

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 .

Page 12: Data types

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 .

Page 13: Data types

TYPE APPROXIMATE 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

Page 14: Data types

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.

Page 15: Data types

Type Approximate size(in bytes)

Minimal range

Char 1 -128 to 127

Unsigned char 1 0 to 255

Signed char 1 same as char

Page 16: Data types

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.

Page 17: Data types

TYPE approximate size(in bytes)

Digit of precision

Float 4 7

Double 8 15

Long double 10 19

Page 18: Data types

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.

Page 19: Data types

ARRAYSValues 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].

Page 20: Data types

20

Multidimensional Arrays

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)

Page 21: Data types

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.

Page 22: Data types

Function declaration◦ return-type function-name (argument-list);◦void show();◦float volume(int x,float y,float z);

Function definitionreturn-type function-name(argument-list){statement1;statement2;

} Function call

◦ function-name(argument-list);◦ volume(a,b,c);

Functions

Page 23: Data types

Pointers

Pointers can be declared and initialized as in C.

int * ip; // int pointerip = &x; // address of x assigned to ip*ip = 10; // 10 assigned to x through indirection

Page 24: Data types

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.

Page 25: Data types

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;//classfunction declarations;//members

public:variable declarations;//classfunction declarations;//members

};//Terminates with a semicolon

Page 26: Data types

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.

Page 27: Data types

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;

Page 28: Data types

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.

Page 29: Data types

An example: Classes and Objects

Page 30: Data types

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 declarationstudent A; //C++ declarationA.roll_number=999;A.total_marks=595.5;Final_Total=A.total_marks + 5;

Page 31: Data types

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.

Page 32: Data types

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.

Page 33: Data types

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!

Page 34: Data types

Union example (from an assembler)union DisasmInst {#ifdef BIG_ENDIAN struct { unsigned char a, b, c, d; } chars;#else struct { unsigned char d, c, b, a; } chars;#endif int intv; unsigned unsv; struct { unsigned offset:16, rt:5, rs:5, op:6; } itype; struct { unsigned offset:26, op:6; } jtype; struct { unsigned function:6, sa:5, rd:5, rt:5, rs:5,

op:6; } rtype;};

Page 35: Data types

Another union example

void CheckEndian() { union { char charword[4]; unsigned int intword; } 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}

Page 36: Data types

Thankyou