conceptofcdatatypes-090925045031-phpapp02

Upload: jithendra-babu

Post on 08-Aug-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    1/30

    MADE BY:-RAFIYA SIRIN

    XI-B

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    2/30

    DATA TYPES

    Data types are means to identify the type of

    data and associated operations of handling

    it. C++ provides a predefined set of data

    types for handling the data it uses. Whenvariables are declared of a particular data

    type then the variable becomes the place

    where the data is stored and data types is

    the type of value(data) stored by thatvariable. Data can be of may types such as

    character, integer, real etc. since the data to

    be dealt with are of may types, a

    programming language must provide

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    3/30

    DATA TYPES

    FUNDAMENTAL DATATYPES

    USER DEFINED DATATYPES

    DATA TYPE MODIFIERS

    DERIVED DATA TYPES

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    4/30

    FUNDAMENTAL DATA TYPES

    INTEGER

    CHARACTER

    DOUBLE

    VOID

    FLOAT

    FUNDAMENTAL DATA TYPES ARE

    THOSE THAT ARE NOT

    COMPOSED OF OTHER DATATYPES

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    5/30

    Integers are whole numbers with a machinedependent range of values. A good programminglanguage as to support the programmer bygiving a control on a range of numbers and

    storage space. C has 3 classes of integerstorage namely short int, int and long int. All ofthese data types have signed and unsignedforms. A short int requires half the space than

    normal integer values. Unsigned numbers arealways positive and consume all the bits for themagnitude of the number. The long andunsigned integers are used to declare a longerrange of values.

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    6/30

    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 isequivalent to the integer code of thatcharacter. Character data type is oftencalled as integer data type because

    the memory implementation of chardata type is in terms of the numbercode.

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    7/30

    FLOAT DATA TYPE

    A number having fractional part is a floating- point number. Anidentifier declared as float becomes a floating-point variable and canhold 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.

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    8/30

    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.

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    9/30

    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 typevoid may be declared. It is used when

    program or calculation does not require

    any value but the syntax needs it.

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    10/30

    DATA TYPE MODIFIERS

    INTEGER TYPEMODIFIERS

    CHARACTER TYPEMODIFIERS

    FLOATING-POINTMODIFIERS

    THEY CHANGE SOME

    PROPERTIES OF THE

    DATA TYPE

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    11/30

    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 integernot to hold negative values.

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    12/30

    TYPE APPROXIMATESIZE

    MINIMAL RANGE

    SHORT 2 -32768 to 32767

    UNSIGNEDSHORT

    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

    UNSIGNEDLONG

    4 0 to 4,294,967,295

    SIGNED LONG 4 Same as long

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    13/30

    CHARACTER TYPE

    MODIFIER The char can also be signed or unsigned. unlike

    int,char is not signed or unsigned by default. It is latermodified to best fit the type to the hardwareproperties.

    TYPE APPROXIMATE SIZE MINIMAL RANGE

    CHAR 1 -128 to 127

    UNSIGNED CHAR 1 0 to 255

    SIGNED CHAR 1 Same as char

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    14/30

    FLOATING POINT TYPE

    MODIFIERS

    There are three floating-point types: float, double, and long

    double. These types represent minimum allowable range of

    types.

    Note:-dont use commas in numeric values assigned tovariables.

    TYPE APPROXIMA

    TE SIZE

    DIGITS

    OFPRECISIO

    N

    FLOAT 4 7

    LONG

    DOUBLE

    8 15

    LONG 10 19

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    15/30

    Name Description Size* Range*

    char Character or small integer. 1bytesigned: -128 to 127

    unsigned: 0 to 255

    short int (short) Short Integer. 2bytessigned: -32768 to 32767

    unsigned: 0 to 65535

    int Integer. 4bytes

    signed: -2147483648 to

    2147483647

    unsigned: 0 to 4294967295

    long int (long) Long integer. 4bytes

    signed: -2147483648 to

    2147483647

    unsigned: 0 to 4294967295

    float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)

    doubleDouble precision floating point

    number.8bytes +/- 1.7e +/- 308 (~15 digits)

    long doubleLong double precision floating

    point number.8bytes +/- 1.7e +/- 308 (~15 digits)

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    16/30

    DERIVED DATA TYPES

    ARRAYS

    POINTERS

    REFERENCES

    CONSTANTS

    FUNCTIONS

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    17/30

    ARRAYSAn array is a series of elements of the same type placed in

    contiguous memory locations that can be individuallyreferenced by adding an index to a unique identifier.

    That means that, for example, we can store 5 values of type intin an array without having to declare 5 different variables, eachone with a different identifier. Instead of that, using an array wecan 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 isused. A typical declaration for an array in C++ is:type name [elements];

    NOTE: The elements field within brackets [ ] which represents thenumber of elements the array is going to hold, must be a constantvalue, since arrays are blocks of non-dynamic memory whose size mustbe determined before execution.

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    18/30

    VALID OPERATIONS WITH ARRAYS:-

    billy[0] = a;

    billy[a] = 75;

    b = billy [a+2];

    billy[billy[a]] = billy[2] + 5;

    PROGRAM:-

    // arrays example#include

    using namespace std;

    int billy [] = {16, 2, 77, 40, 12071};

    int n, result=0;

    int main () OUTPUT:- 12206

    {

    for ( n=0 ; n

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    19/30

    FUNCTIONS:-Function groups a number of program statements into a unit and gives it a

    name. This unit can be invoked from other parts of a program. Acomputer program cannot handle all the tasks by it self. Instead itsrequests other program like entities called functions in C to get itstasks done. A function is a self contained block of statements thatperform a coherent task of same kind.

    The name of the function is unique in a C Program and is Global. It meansthat a function can be accessed from any location with in a C Program.We pass information to the function called arguments specified whenthe function is called. And the function either returns some value to thepoint it was called from or returns nothing.

    We can divide a long C program into small blocks which can perform a

    certain task. A function is a self contained block of statements thatperform a coherent task of same kind.

    We first declare the function and then at the end of the program we definethe function.

    http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/http://www.mycplus.com/tag/function/
  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    20/30

    POINTERS:- The memory of your computer can be imagined as a

    succession of memory cells, each one of the minimal sizethat computers manage (one byte). These single-bytememory cells are numbered in a consecutive way, so as,

    within any block of memory, every cell has the samenumber as the previous one plus one.

    This way, each cell can be easily located in the memorybecause it has a unique address and all the memory cellsfollow a successive pattern. For example, if we are looking

    for cell 1776 we know that it is going to be right betweencells 1775 and 1777, exactly one thousand cells after 776and exactly one thousand cells before cell 2776.

    The general form of declaring the pointer istype*ptr;

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    21/30

    REFERENCES:-

    It is an alternative name for an object. Areference variable provides an alias for apreviously defined variable. Its declaration

    consists of a base type, an &(ampersand), areference variable name equated to avariable name.

    the general form of declaring is:-type&ref-var = var-name;

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    22/30

    CONSTANTS:-

    C++ constants are not very different from any C++ variable. Theyare defined in a similar way and have the same data types and thesame memory limitations. However, there is one major difference -once a constant has been created and value assigned to it then that

    value may not be changed. Defining Constants with C++

    There are actually three ways of defining a constant in a C++ program:A. by using the preprocessorB. by using the constkey wordC. by using enumerators - these will have a range of integer valuesIt's also worth noting that there are two types of constant: literal and

    symbolic.the general form of declaring a variable is:-

    const int upperage = 50;

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    23/30

    USER DEFINED DERIVED DATA TYPES

    CLASS

    STRUCTURE

    UNION

    ENUMERATION

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    24/30

    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.

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    25/30

    Classes are generally declared using thekeyword class, with the followingformat:

    class class_name { access_specifier_1:member1;

    access_specifier_2:

    member2; ...} object_names;

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    26/30

    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 ofvalid 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 valididentifier as its name.

    Structure is different from an array in the sense that an array represents an aggregateof elements of same type whereas a structure represents an aggregate of elements ofarbitrary types..

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    27/30

    Unions allow one same portion of memory to be accessed as different data types, sinceall of them are in fact the same location in memory. Its declaration and use is similar tothe 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. Itssize 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 theelements 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 orstructures of smaller elements.

    The exact alignment and order of the members of a union in memory is platformdependant. Therefore be aware of possible portability issues with this type of use.

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    28/30

    ENUMERATION:-

    It can be used to assign names to integer constants.//Program to illustrate Enumerator

    #include

    void main(void)

    {

    enum type{POOR,GOOD,EXCELLENT};//this is the syntax of enumerator

    int var;

    var=POOR;//this makes programs more understandable

    cout

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    29/30

    We have so many data types to allow theprogrammer to take advantage of hardwarecharacteristics. Machines are significantlydifferent in their memory requirements,memory access times, and computation speeds.

  • 8/22/2019 conceptofcdatatypes-090925045031-phpapp02

    30/30