SlideShare ist ein Scribd-Unternehmen logo
1 von 36
DATA TYPES
      By:-Nokesh prabhakar.
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.
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
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
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.
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.
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.
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 .
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.
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.
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 .
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 .
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
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.
Type        Approximate      Minimal
                size(in bytes)    range
Char                 1           -128 to 127

Unsigned char        1           0 to 255

Signed char          1            same as char
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.
TYPE         approximate Digit of
              size(in bytes) precision
Float             4             7


Double            8             15

Long double       10            19
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.
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].
   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
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.
   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
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
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.
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
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.
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;
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.
An example: Classes and
Objects
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;
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.
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.
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!
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;
};
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
}
Data types

Weitere ähnliche Inhalte

Was ist angesagt? (20)

data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Array in c
Array in cArray in c
Array in c
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
String functions in C
String functions in CString functions in C
String functions in C
 
Array in c++
Array in c++Array in c++
Array in c++
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Structure in C
Structure in CStructure in C
Structure in C
 
C functions
C functionsC functions
C functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Data types in C
Data types in CData types in C
Data types in C
 
DBMS: Types of keys
DBMS:  Types of keysDBMS:  Types of keys
DBMS: Types of keys
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Data types in java
Data types in javaData types in java
Data types in java
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
 

Ähnlich wie Data types

cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfYRABHI
 
java Basic Programming Needs
java Basic Programming Needsjava Basic Programming Needs
java Basic Programming NeedsRaja Sekhar
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Typesk v
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2rohassanie
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 
datatypes-200723165518 (1).pptx
datatypes-200723165518 (1).pptxdatatypes-200723165518 (1).pptx
datatypes-200723165518 (1).pptxNaniBhai3
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++Neeru Mittal
 
variablesfinal-170820055428 data type results
variablesfinal-170820055428 data type resultsvariablesfinal-170820055428 data type results
variablesfinal-170820055428 data type resultsatifmugheesv
 
What is Data Types and Functions?
What is Data Types and Functions?What is Data Types and Functions?
What is Data Types and Functions?AnuragSrivastava272
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++ Hridoy Bepari
 
C Sharp Nagina (1)
C Sharp Nagina (1)C Sharp Nagina (1)
C Sharp Nagina (1)guest58c84c
 

Ähnlich wie Data types (20)

Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
Datatypes
DatatypesDatatypes
Datatypes
 
Data Handling
Data HandlingData Handling
Data Handling
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdf
 
C++ data types
C++ data typesC++ data types
C++ data types
 
java Basic Programming Needs
java Basic Programming Needsjava Basic Programming Needs
java Basic Programming Needs
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
datatypes-200723165518 (1).pptx
datatypes-200723165518 (1).pptxdatatypes-200723165518 (1).pptx
datatypes-200723165518 (1).pptx
 
Cpprm
CpprmCpprm
Cpprm
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
variablesfinal-170820055428 data type results
variablesfinal-170820055428 data type resultsvariablesfinal-170820055428 data type results
variablesfinal-170820055428 data type results
 
C96e1 session3 c++
C96e1 session3 c++C96e1 session3 c++
C96e1 session3 c++
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 
Data types
Data typesData types
Data types
 
What is Data Types and Functions?
What is Data Types and Functions?What is Data Types and Functions?
What is Data Types and Functions?
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
 
C Sharp Nagina (1)
C Sharp Nagina (1)C Sharp Nagina (1)
C Sharp Nagina (1)
 

Kürzlich hochgeladen

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Kürzlich hochgeladen (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Data types

  • 1. DATA TYPES By:-Nokesh prabhakar.
  • 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.
  • 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. 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.
  • 29. An example: Classes and Objects
  • 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!
  • 34. 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; };
  • 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 }