Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

download Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

of 22

Transcript of Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    1/22

    C++ Data Types

    Built-In Type

    Integral type - int, char

    Floating typefloat, double

    Void

    Used to specify the return type of a function

    when it is not returning any value

    To indicate an empty argument list to a

    function. Example : void fun (void);

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    2/22

    C++ Data Types

    Used in declaration of generic pointers. Void *gp;

    A generic pointer can be assigned a pointer

    value of any basic data type, but it may not

    be dereferenced.

    For ex: int * ip; gp = ip; // assigns int

    pointer to void pointer.

    But, *ip = *gp; is illegal;It would not make sense to dereference a pointer

    to a void value.

    LOOPS

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    3/22

    C++ Data Types

    User-defined type

    1. Structure

    2. Union3. Class

    4. Enumeration

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    4/22

    C++ Data Types

    Structure and Union in C.

    They are used in C++ with some added

    features of object oriented languages.

    Class is used in c++.

    The class variables are known as objects.

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    5/22

    Enumerated Data type

    It is user defined data type which provides a

    way for attaching names to numbers.

    It increases the comprehensibility of the

    code.

    The enum keyword automatically

    enumerates a list of words by assigning themvalues 0,1,2 and so on.

    It is used to create symbolic constants.

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    6/22

    Enumerated Data type

    The enum statement is similar to the structstatement.

    Ex : enum shape {circle, square, triangle}

    enum color{ red, blue, green, yellow}

    They differ from enum in C .

    In C++, they can be used as new type names.

    By using, tag names we can declare new

    variables.

    shape ellipse; // ellipse is of type shape.

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    7/22

    Enumerated Data type

    ANSI C defines the types of enums to be ints. In c++, each enumerated data type retain its

    own separate type.

    C++ does not permit an int value to beautomatically converted to an enum value.

    Ex : color background = blue; // allowed

    color background = 7; // error in c++ color background = (color) 7; // ok

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    8/22

    Enumerated Data type

    An enumerated value can be used in placeof an int value.

    Ex : int c = red; // valid, color type

    promoted to int By default, enumerators are assigned

    integer values starting from 0. We can

    override these values by :enum color {red= 8, blue = 4, green};

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    9/22

    Enumerated Data type

    C++ also permits the creation of anonymous

    enums ( enums with out tag names)

    Ex : enum {off, on};

    int switch_1 = off;

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    10/22

    Derived data types

    Array : contiguous/ continuous memory space.

    Character arrays are initialized in different

    way:

    Ex in C :char string[3] = xyz // programmer

    intends to leave out the null character \0.

    Ex in C++ : char string[4] = XYZ

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    11/22

    Derived data types

    Function: A block of code that is to beexecuted to serve some purpose.

    Pointers:

    int * ip; // int pointer

    ip = &x; // address of x assigned to p;

    *ip = 10; // 10 assigned to x through

    indirection.

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    12/22

    Difference Between The Two?

    Constant pointer

    Pointer to a constant

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    13/22

    Constants

    Constants refer to fixed values that do notchange during the execution of a program.

    They include integers, characters, floating

    point numbers and strings.Literal constant do not have memory locations.

    Examples:

    123 // decimal integer

    12.34 // floating point integer

    037 // Octal integer

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    14/22

    Constants

    0x2 //hexadecimal integer C++ //string constant

    A // character constant

    Lab // wide-character constant wchar-_t type is a wide-character literal

    introduced by ANSI C++ and is used for character

    sets that cannot fit a character into a single byte.

    Wide-character literals begin with the letter L.

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    15/22

    Symbolic constants

    They can be created as follows:

    1. Using the qualifier const, and

    2. Defining a set of integer constants using enum

    keyword.

    Any value declared as const cannot be modifiedby the program in any way.

    ex : Const int size = 10;

    C++ requires a constant to be initialized. ANSI C does not require an initialization, it will

    give a default value 0.

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    16/22

    Symbolic constants

    Enumeration example :enum { x, y, z};

    const x = 0;

    const y = 1;

    const z =2;

    Or explicitly, enum {x=100, y = 50,z=200};

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    17/22

    Control Structures

    Three control structures: Sequence structure ( straight line)

    Selection structure (branching)

    Loop structure ( iteration or repetition)

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    18/22

    Selection structure (branching)

    Simple If statement

    if (condition){ // code

    }

    Ifelse statementIf (condition)

    {

    // code}

    Else

    {// code}

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    19/22

    Switch statement

    This is multiple branching statement, basedon a condition.

    The control is transferred to one of themany possible points.

    Syntax:

    switch (expression)

    {

    case 1:

    { //code

    }

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    20/22

    Switch statement

    case 2:{ //code

    }

    Default:

    {// code

    }

    }

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    21/22

    LOOPS

    Do While :exit controlled loopDo

    {

    // code

    }while (condition is true);

  • 7/31/2019 Basic of c &c++ (Rohan Khanna's Conflicted Copy 2012-05-23)

    22/22

    LOOPS

    While Loop : entry controlled loop

    while (condition is true)

    { // code

    } For Loop : entry controlled and action will

    repeated a predetermined no. of times

    for( initial value; test; increment)

    { // code

    }