Data types

2,589 views
2,342 views

Published on

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

Published in: Education
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
2,589
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
132
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Data types

  1. 1. DATA TYPES By:-Nokesh prabhakar.
  2. 2. Data TypesData types are means to identify the typeof data and associated operation ofhandling it .C++ has three types of data types :- • Built in data type. • Derived data type. • User defined data type.
  3. 3. C++ Data TypesUser-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. 4. Type Bytes Rangechar 1 -128 to 127 signed: -128 to 127 unsigned: 0 to 255short int 2 -31768 to 32767 signed: -32768 to 32767 unsigned: 0 to 65535int 2 -32768 to 32767 signed: -31768 to 32767 unsigned: 0 to 65535long int 4 -2147483648 to 2147483647 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295float 4 3.4E-38 to 3.4E+38double 8 1.7E-308 to 1.7E+308long double 10 3.4E-4932 to 1.1E+4932
  5. 5. Built in data typeBuilt in data types are those who arenot composed of other data types.There are mainly 5 kinds of build indata type :-1.int type.2.char type.3.float type.4.double type.5.void type.
  6. 6. Void data type The void data type specifies anempty set of values . It is used as the return type forfunctions that do not return avalue.
  7. 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 asnegative value . An identifiers declared as int cannot havefractional part.
  8. 8. Char data type characters can store any member ofthe c++ implementation’s basiccharacter set . An identifiers declared as charbecomes character variable . char set is often said to be a integertype .
  9. 9. Float data type A number having a fractional part isa floating-point number . the decimal point shows that it is afloating-point number not an integer. for ex-31.0 is a floating-pointnumber not a integer but simply 31 isa integer.
  10. 10. Double data type It is used for handling floating-pointnumbers. It occupies twice as memory as float. It is used when float is too small orinsufficienly precise.
  11. 11. Data type modifiersThe basic data type has modifierspreceding them .we use modifier to alterthe meaning of the base type to fit varioussituation more precisely.There are 3 types of modifiers:- 1.Integer type modifiers. 2.Character type modifiers . 3.Float type modifiers .
  12. 12. Integer type modifiersBy using different number of bytes tostore values , c++ offers 3 types ofintegers :short , int and long that canrepresent upto three different integersizes. 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. 13. APPROXIMATE TYPE SIZE(IN BYTES) MINIMAL RANGEshort 2 -32768 to 32767Unsigned short 2 0 to 65,535Signed short 2 same as shortInt 2 -32768 to 32767Unsigned int 2 0 to 65,535Signed int 2 same as intLong 4 -2,147,483,648 to 2,147,483,647Unsigned long 4 0 to 4,294,967,295
  14. 14. character type modifiersThe char type can also be signed orunsigned .The unsigned char represent the range 0to 255.The signed char represent the range -128to 127.
  15. 15. Type Approximate Minimal size(in bytes) rangeChar 1 -128 to 127Unsigned char 1 0 to 255Signed char 1 same as char
  16. 16. Floating-point type modifiersC++ 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. 17. TYPE approximate Digit of size(in bytes) precisionFloat 4 7Double 8 15Long double 10 19
  18. 18. Derived Data TypesFrom the built in data types other typescan be derived called derived data types.There are 5 types of derived datatypes :-1.Arrays.2.Functions.3.Pointers.4.References.5.Constant.
  19. 19. ARRAYSValues of similar type stored in continuousmemory locations.int a[10]; char string[3]=“xyz”;Array can be one dimensional , twodimensional , multi dimensional.For ex-float a[3]; //declares array of threefloats :a[0],a[1],a[2].Int b[2][4]; //declares a 2 dimension arrayof 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. 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. 21. FunctionsSet of statements to perform specifictasks.A piece of code that perform specifictask.Introduces modularity in the code.Reduces the size of program.C++ has added many new featuresto the functions to make them morereliable and flexible.It can be overloaded.
  22. 22.  Function declaration ◦ return-type function-name (argument-list); ◦ void show(); ◦ float volume(int x,float y,float z); Function definition return-type function-name(argument-list) { statement1; statement2; } Function call ◦ function-name(argument-list); ◦ volume(a,b,c);Functions
  23. 23. PointersPointers can be declared and initialized as inC.int * ip; // int pointerip = &x; // address of x assigned to ip*ip = 10; // 10 assigned to x throughindirection
  24. 24. ReferencesA reference is an alternative name ofan object. ConstantA constant is a data item whose data valuecan never change during the program run.
  25. 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. 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. 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. 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.
  29. 29. An example: Classes andObjects
  30. 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. 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. 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. 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!
  34. 34. Union example (from anassembler)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;};
  35. 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}

×