2. C and C++

download 2. C and C++

of 86

Transcript of 2. C and C++

  • 7/27/2019 2. C and C++

    1/86

    Programming in C++ -C and C++

    1

    C and C++

    Objectives

    First C++ Program

    Declaring Variables

    Explaining keywords in C++

    Explaining basic data types in C++

    Explaining arithmetic operators and operator precedence

    Explaining Type conversion

    Explaining arrays

    contd on the next slide..

  • 7/27/2019 2. C and C++

    2/86

    Programming in C++ -C and C++

    2

    Explaining strings

    Explaining enumerated data types

    Explaining structures in C++

    Explaining pointers

    Explaining new and delete operators

  • 7/27/2019 2. C and C++

    3/86

    Programming in C++ -C and C++

    3

    Your First C++ ProgramListing: The Hello World program.

    #include // a preprocessor directive

    void main(void){cout

  • 7/27/2019 2. C and C++

    4/86

    Programming in C++ -C and C++

    4

    Your First C++ Program (Cont.)

    main() is the entry point into the program. The statement,

    cout

  • 7/27/2019 2. C and C++

    5/86

    Programming in C++ -C and C++

    5

    Output Using cout

    In the previous listing, the statement

    cout

  • 7/27/2019 2. C and C++

    6/86

    Programming in C++ -C and C++

    6

    cout Revisited

    Listing: A C++ program with variable along with cout.

    #include void main(void){

    int var;var=100;cout

  • 7/27/2019 2. C and C++

    7/86

    Programming in C++ -C and C++

    7

    The previous program will print the following result:

    Value of the variable var=100

  • 7/27/2019 2. C and C++

    8/86

    Programming in C++ -C and C++

    8

    Cascading

  • 7/27/2019 2. C and C++

    9/86

    Programming in C++ -C and C++

    9

    Defining a Variable at the Point Of Use

    In C++ variables can be declared/defined at any point in thebody of a program before it is used for the first time as shownin the next program.

  • 7/27/2019 2. C and C++

    10/86

    Programming in C++ -C and C++

    10

    Listing: Defining a variable at the point of use.

    #include void main(void){int var1;var1=100;

    cout

  • 7/27/2019 2. C and C++

    11/86

    Programming in C++ -C and C++

    11

    The previous program produces the following output:

    Value of the variable var1=100

    Value of the variable var2=200

  • 7/27/2019 2. C and C++

    12/86

    Programming in C++ -C and C++

    12

    Input Using cin and >> Operator

    The input counterpart of cout and >.

    The cin object and the extraction operator >> are definedin the header file iostream.h.

    The next listing shows the use of cin object and extraction

    operator >>.

  • 7/27/2019 2. C and C++

    13/86

    Programming in C++ -C and C++

    13

    Listing: Input using cin object and extraction operator >>.

    #include void main(void){coutage;cout

  • 7/27/2019 2. C and C++

    14/86

    Programming in C++ -C and C++

    14

    When the program is run, it displays the prompt

    Enter your age: _

    At this point type a number and press the RETURN key.

    Enter your age: 30 RETURN

    Now the following output will appear:

    My age is 30 years

  • 7/27/2019 2. C and C++

    15/86

    Programming in C++ -C and C++

    15

    The > Operators

    The statement,

    cout >var;

    extracts data from the cin object and puts it in thevariable var.

  • 7/27/2019 2. C and C++

    16/86

    Programming in C++ -C and C++

    16

    cout and cin are Smart Objects

    A stream corresponds to a flow of data.

    An output stream corresponds to a flow of data from the

    program. A destination is associated with output stream.cout object corresponds to the standard output stream.

    The standard output stream is the output stream to thestandard output device i.e. the display unit.

  • 7/27/2019 2. C and C++

    17/86

    Programming in C++ -C and C++

    17

    An input stream corresponds to a flow of data into theprogram. A source is associated with the input stream. cinobject corresponds to the standard input stream.

    The standard input stream is an input stream of data from

    the standard input device which is the keyboard.

    The unit of data in an input stream or in an output streamis one character.

  • 7/27/2019 2. C and C++

    18/86

    Programming in C++ -C and C++

    18

    C++ Keywords

    Key words are identifiers that have special meaning to the

    compiler. They should not be used as a variable name. Thetable given in the next slide shows the list of C++ keywords.

  • 7/27/2019 2. C and C++

    19/86

    Programming in C++ -C and C++

    19

    asm auto break case catch

    _cdecl cdecl char class constcontinue _cs default delete do

    double _ds else enum _es

    _export extern _far far float

    for friend goto huge if inline int interrupt long _near

    near new operator private static

    protected public return _seg _pascal

    short signed sizeof _ss _saveregs

    struct switch template this trytypedef union unsigned virtual void

    volatile while register _loadds

    Table of C++ keywords

  • 7/27/2019 2. C and C++

    20/86

    Programming in C++ -C and C++

    20

    Basic Data Types in C++ In C++ there are two basic data types. They are integerdata types and floating point data types.

    Integers are whole numbers like 10,22,0,-323,...... Theyhave no fractional part.

    Floating point numbers are numbers with a fractional part

    for example 10.82, -3.28, ....

  • 7/27/2019 2. C and C++

    21/86

    Programming in C++ -C and C++

    21

    Again, we have 4 different integer data types each having

    both signed and unsigned versions.

    1. char 2. unsigned char

    3. short4. unsigned short5. int6. unsigned int7. long

    8. unsigned long

  • 7/27/2019 2. C and C++

    22/86

    Programming in C++ -C and C++

    22

    Various C++ integer types differ in the amount of memory

    they use to hold an integer data.

    A char is 1 byte long

    A short integer is at least 2 bytes long

    An integer is at least as big as a short integer

    A long integer is at least four bytes and at least as big as

    an int

  • 7/27/2019 2. C and C++

    23/86

    Programming in C++ -C and C++

    23

    The signed data types split its range approximately equallybetween positive and negative values.

    An integer variable(having two bytes space) can holdvalues in the range -32768 to 32767 as illustrated taking animaginary four-bit-space data type in the next figure.

  • 7/27/2019 2. C and C++

    24/86

    Programming in C++ -C and C++

    24

    0 0 0 0 1 1 1 1

    The four bit values can vary

    from to

    When the sign-bit or left-most-bit is 0 then the four bits can vary

    from

    0 0 0 0= 0 to = 7

    0 1 1 1

    When the sign-bit or left-most-bit is 1 then the four bits can vary

    from 1 0 0 0 1 1 1 1= -8 to = -1

    Figure: Range of values of an imaginary 4-bit signed data type

    ( values are obtained through complementing arithmetic)

  • 7/27/2019 2. C and C++

    25/86

    Programming in C++ -C and C++

    25

    The unsigned data type has the advantage of doubling themaximum value a signed variable can hold.

    A signed int can hold values in the range -32768 to 32767,

    the maximum value the variable can hold is 32767. But itsunsigned version can hold numbers in the range 0 to 65535as illustrated taking an imaginary four-bit-space data type inthe next figure.

  • 7/27/2019 2. C and C++

    26/86

    Programming in C++ -C and C++

    26

    For an unsigned four-bit data type the values can vary

    from 0 0 0 0 1 1 1 1= 0 to = 15

    Figure: Range of values of an imaginary 4-bit unsigned

    data type

  • 7/27/2019 2. C and C++

    27/86

    Programming in C++ -C and C++

    27

    A char type is actually another integer type. A char type can

    be considered as a one byte equivalent of an integer.

    char type variable is used to store characters.

    Each character is assigned a number code; a number codein the range of 0 to 255.

    The most commonly used character code is the ASCII.Every char type value has its corresponding ASCII value.

  • 7/27/2019 2. C and C++

    28/86

    Programming in C++ -C and C++

    28

    Listing: A C++ program to print ASCII value of a givencharacter and vice versa.

    #include void main(void) {char ch;int ii;

    ch=P;ii=ch;cout

  • 7/27/2019 2. C and C++

    29/86

    Programming in C++ -C and C++ 29

    The previous program will print the followingoutput:

    ASCII code for P is 80

    ASCII code for Q is 81

  • 7/27/2019 2. C and C++

    30/86

    Programming in C++ -C and C++ 30

    That a char type is really a one-byte equivalent of signed intcan be illustrated through the following program.

  • 7/27/2019 2. C and C++

    31/86

    Programming in C++ -C and C++ 31

    Listing: A C++ program to illustrate that the char type is onebyte equivalent of a signed int.

    #include void main(void){int ii;

    char ch;ch=200;ii=ch;cout

  • 7/27/2019 2. C and C++

    32/86

    Programming in C++ -C and C++ 32

    The above program will print the following output: -56

    A char type variable being one-byte equivalent of int type

    can hold values in the range -128 to 127. So, 200 will bestored in one byte as shown below:

    The left most bit, the sign bit is set to 1. This number will beinterpreted as a -ve number. In 2's complement notation this-ve number is -56.

    By taking an unsigned char type variable this problem canbe tackled.

  • 7/27/2019 2. C and C++

    33/86

    Programming in C++ -C and C++ 33

    Table: Summary of characteristics of different C++ integertype data

  • 7/27/2019 2. C and C++

    34/86

    Programming in C++ -C and C++ 34

    Floating point numbers have a fractional part. There arethree floating-point data types viz float, double and longdouble.

  • 7/27/2019 2. C and C++

    35/86

    Programming in C++ -C and C++ 35

    Number Systems

    Integer constants in a C++ program can be expressed in anyof the three number systems viz decimal, hexadecimal andoctal.

    an integer data item without any symbol attached to it at theleft is interpreted as decimal integer. Thus,

    var = 123;

    will store the decimal number 123 in var.

  • 7/27/2019 2. C and C++

    36/86

    Programming in C++ -C and C++ 36

    a symbol 0x or 0X at the beginning of a data constantwill cause the compiler to interpret it as hexadecimal

    number. Thus,

    var = 0x123

    will store the decimal number 291 in var.

    a leading 0 will interpret the data as octal. Thus,

    var = 0123

    will store decimal 83 in var.

  • 7/27/2019 2. C and C++

    37/86

    Programming in C++ -C and C++ 37

    Escape SequenceCharacters that have special meaning to the editor orcompiler can be made to behave like normal characters bypreceding them with the back slash (\) and as a result its

    special character status is lost. Such a sequence ofcharacters is called an escape sequence.

  • 7/27/2019 2. C and C++

    38/86

    Programming in C++ -C and C++ 38

    An escape sequence is stored internally as a singlecharacter even though it is typed using more than onecharacters.

    An escape sequence can be used as a character constantby putting single quotes around it as shown below:

    var = \n;var = \t;

  • 7/27/2019 2. C and C++

    39/86

    Programming in C++ -C and C++ 39

    Symbolic Constants and const Qualifier

    The #define directive enables you to define symbolicconstants in a C++ program as shown below:

    #define PI 3.14159

    Once a symbolic constant is defined you can use it in placeof the value it represents. That is, instead of writing,

    area = 3.14159*r*r

    You can write

    area = PI*r*r

  • 7/27/2019 2. C and C++

    40/86

    Programming in C++ -C and C++ 40

    C++ Arithmetic Operators

    C++ operators follow an order of precedence which refers to

    the order in which the operators operates.

    An operator of higher precedence will be executed first.

    Consider the following expression:

    10 + 2 * 3 = 10 + (2 * 3) = 10 + 6 = 16

    Here the operator * has a higher precedence over + operator.

    For details about different operators and their precedencelevels refer to table 5 of chapter on C and C++.

  • 7/27/2019 2. C and C++

    41/86

    Programming in C++ -C and C++ 41

    Type Conversion

    When a value of one data type is assigned to a variable ofanother data type, C++ automatically converts the datatype of the value to the data type of the variable.

    Assigning a short value to a long variable does notchange the value.

    Assigning a long int to a short int may result in copyingthe lower byte.

  • 7/27/2019 2. C and C++

    42/86

    Programming in C++ -C and C++ 42

    When mixed types are combined in an arithmeticexpression type conversion takes place.

    C++ converts char, unsigned char, signed char and shortvalues in an expression into int. This is called integral

    promotion(implicit type conversion).

    When an operation involves two types, smaller type isconverted into the larger type.

  • 7/27/2019 2. C and C++

    43/86

    Programming in C++ -C and C++ 43

    Type Casting

    Type casting is used to enforce type conversion explicitly.Suppose var is an integer type variable. To convert thisint data into float we use type casting as shown below:

    (float) var; OR float (var);

    The next C++ program listing illustrates the use of typecasting.

  • 7/27/2019 2. C and C++

    44/86

    Programming in C++ -

    C and C++

    44

    Listing: A C++ program to illustrate type casting.

    #include

    void main(void){int xx,yy,zz;xx=21.99+16.88;

    yy=int(21.99)+int(16.88);zz=(int)21.99+(int)16.88;cout

  • 7/27/2019 2. C and C++

    45/86

    Programming in C++ -

    C and C++

    45

    The above program prints the following output:

    xx=38 yy=37 zz=37

    ASCII code of A is =65

  • 7/27/2019 2. C and C++

    46/86

    Programming in C++ -

    C and C++

    46

    Arrays

    C++ offers something called derived data type. Deriveddata types are data types built from basic data types. An

    array is one such data type.

    An array is a data form that can hold several values, allof them of one type.

  • 7/27/2019 2. C and C++

    47/86

    Programming in C++ -

    C and C++

    47

    An array declaration statement should indicate three things.

    1. Type of the values to be stored2. The name of the array3. Number of elements in the array

    An array declaration has the following general format.

    type_name array_name[array_size];

    Example:

    char name[30];int num[10];

  • 7/27/2019 2. C and C++

    48/86

    Programming in C++ -

    C and C++

    48

    Listing: A C++ program to demonstrate an array.

    #include

    void main(void){int age[4];age[0]=30;

    age[1]=28;age[2]=4;coutage[3];cout

  • 7/27/2019 2. C and C++

    49/86

    Programming in C++ -

    C and C++

    49

    The last program prints the following output.

    Enter value for age[3] : 2 RETURN

    Total age = 64Size of the array = 8 bytes

  • 7/27/2019 2. C and C++

    50/86

    Programming in C++ -

    C and C++

    50

    Strings

    A string is a sequence of alpha numerals to be treated as asingle unit.

    In a program, the string is denoted by a sequence of alphanumerals enclosed within double quotes.

    Thus CMC Ltd., is a string

  • 7/27/2019 2. C and C++

    51/86

    Programming in C++ -

    C and C++

    51

    Inside the memory a string is stored in consecutivememory locations with a null character (\0)

    attached to it at the end.

    Thus the string "CMC Ltd" is stored in memory as:

    C M C L t d \0

  • 7/27/2019 2. C and C++

    52/86

    Programming in C++ -

    C and C++

    52

    Strings and cout Object

    The statements

    char name[20] = CMC Ltd CMC Centrename[7] = \0;

    cout

  • 7/27/2019 2. C and C++

    53/86

    Programming in C++ -

    C and C++

    53

    Strings and cin Object

    cin object can be used to initialize an array.

    The statements,

    char name[SIZE]; /* where size is any integer */cin>>name;

    will prompt you to initialize the array named name. When youpress enter to terminate the array, the null (\0) character

    automatically gets inserted at the end of the line of characters.

  • 7/27/2019 2. C and C++

    54/86

    Programming in C++ -

    C and C++

    54

    Two Dimensional Array

    A two dimensional array can be used to store data that wetypically record on paper as a table of data.

    Array format:

    data_type array_name[row_size] [col_size]

    Example:

    char name[10][20];int num[10][15];

  • 7/27/2019 2. C and C++

    55/86

    Programming in C++ -

    C and C++

    55

    Enumerated Data Types

    An enumerated data type is a user defined data type,declared as shown below.

    enum name{list of possible values};

    Here enum is a keyword, name is the user definedname given to the data type, list of possible valuesis the finite list of values a variable of type name cantake on.

    Enumerated data types work fine when you know inadvance the finite list of values a variable can take on.

  • 7/27/2019 2. C and C++

    56/86

    Programming in C++ -

    C and C++

    56

    Enumerated data types are treated internally asintegers.

    Example:

    enum days_of_the_week{ Sun, Mon, Tue, Wed,Thu, Fri, Sat};

    Sun is 0Mon is 1Tue is 2

    .and so on.

  • 7/27/2019 2. C and C++

    57/86

    Programming in C++ -

    C and C++

    57

    Structures

    A structure is a data type that can hold a large numberof different types of data.

    A structure is a type with a template serving to definethe types data properties. Once you define the templateyou can create variables of that type.

  • 7/27/2019 2. C and C++

    58/86

    Programming in C++ -

    C and C++

    58

    A structure template is declared as shown in thefollowing example:

    struct person{char name[20];int age ;

    float height ;};

    Here struct is a keyword, person is the templates name.

    age, height, etc. are called structure members.

    The semicolon after the closing braces is mandatory.

    St t i bl f th b t i d fi d h

  • 7/27/2019 2. C and C++

    59/86

    Programming in C++ -

    C and C++

    59

    Structure variable of the above type is defined as shownbelow:

    person p;

    Individual structure members can be referenced using dot(.) operator as shown below:

    p.namep.name[5]p.name[15]p.agep.height

    A program in the next slide explains how to create and usea structure.

  • 7/27/2019 2. C and C++

    60/86

    Programming in C++ -

    C and C++

    60

    Listing: A C++ program to show the usage of structures.

    #include #include

    struct person{

    char name[20];int age;float height;};

    contd. on the next slide..

  • 7/27/2019 2. C and C++

    61/86

    Programming in C++ -

    C and C++

    61

    void main(void){

    person p;strcpy(p.name,Bill);p.age=40;p.height=170.5;

    cout

  • 7/27/2019 2. C and C++

    62/86

    Programming in C++ -

    C and C++

    62

    The last program prints the following output:

    The person's name is Bill

    The persons age is 40 yearsThe persons height is 170.5 cm

  • 7/27/2019 2. C and C++

    63/86

    Programming in C++ -

    C and C++

    63

    Pointers

    Pointers are variables that can store addresses of variables.

    The pointer declaration should reflect what type of data thepointer points to.

    A pointer type variable by itself requires two bytes of memory.

  • 7/27/2019 2. C and C++

    64/86

    Programming in C++ -

    C and C++

    64

    A pointer variable pointing to an integer type value can bedeclared as shown below:

    int *ptr;

    Here ptr is the variable name.The * indicates that it is a pointer type variable.The int indicates that the pointer points to an integer type data.

    Pointers (Contd.)

  • 7/27/2019 2. C and C++

    65/86

    Programming in C++ -

    C and C++

    65

    Initializing Pointers

    Consider the following two declarations:

    int *ptr;int num = 288;

    &num gives the address of the variable num.So, the ptr variable can be initialized with &num as:

    ptr=#

    The following figure illustrates the initialization of pointers.

  • 7/27/2019 2. C and C++

    66/86

    Programming in C++ -

    C and C++

    66

    Figure: A pointer variable ptr points to the address 100 wherethe integer value 288 is stored. Theaddress values are arbitrary.

    Listing: A C++ program to illustrate pointer variable

  • 7/27/2019 2. C and C++

    67/86

    Programming in C++ -

    C and C++

    67

    Listing: A C++ program to illustrate pointer variabledeclaration and initialization.

    #include

    void main(void){int *int_ptr; int var=288; int_ptr=&var;

    cout

  • 7/27/2019 2. C and C++

    68/86

    Programming in C++ -

    C and C++

    68

    The last program printed the following output in one run:

    Value of var = 288Address of var = ox1b97fff2Value of int_ptr = ox1b97fff2Size of int_ptr = 2 bytes

    The pointer variable int_ptr is of size 2 bytes.In the address 0x1b97fff2, the segment address part is 0x1b97and the offset address part is 0xfff2.The variable int_ptr holds only the offset part of the address.

  • 7/27/2019 2. C and C++

    69/86

    Programming in C++ -

    C and C++

    69

    Pointers and Numbers

    Computers typically handle addresses as unsigned integers.But pointers being of distinct type from integers the followingassignment is not valid in C++.

    int *ptr;ptr = 0xb800;

    Nothing in the above statement tells that 0xb800 is an

    address.

  • 7/27/2019 2. C and C++

    70/86

    Programming in C++ -

    C and C++

    70

    If you want to use a numeric value as an address you musttype cast it as shown below:

    int *ptr;ptr = (int *)0xb800;

    OR

    char *ptr;ptr = (char *)0xb800;

  • 7/27/2019 2. C and C++

    71/86

    Programming in C++ -

    C and C++

    71

    Indirection Operator (*)

    If ptr is a pointer type variable, then the indirection operation*ptr refers to the content of the memory location pointed by ptr.

    This operator * is known as indirection operator or

    dereferencing operator.

    x=*ptr;

    x is a variable and ptr is a pointer variable. Here the content of

    the location pointed to by ptr is assigned to the variable x.

  • 7/27/2019 2. C and C++

    72/86

    Programming in C++ -

    C and C++

    72

    Figure: Illustrating the indirection operation x = *ptr. Address

    value shown here is arbitrary

  • 7/27/2019 2. C and C++

    73/86

    Programming in C++ -

    C and C++

    73

    Pointer to Constant and Constant Pointer

    You can use the const keyword in two different ways withpointers.

    The first way is to make a pointer point to a constant object.

    The second way is to make the pointer itself constant.

  • 7/27/2019 2. C and C++

    74/86

    Programming in C++ -

    C and C++

    74

    A pointer pointing to a constant object can be declared asshown below:

    int age = 40;const int*ptr = &age;

    This declaration means that the value of age variable cannot

    be altered through ptr. But you can surely change the valueusing the variable age itself. So,

    age = 50; // valid* ptr =50; // invalid

  • 7/27/2019 2. C and C++

    75/86

    Programming in C++ -

    C and C++

    75

    The second way to use const keyword makes the pointeritself a constant. It makes it impossible to change the value

    of the pointer.

    It is declared as shown below:

    int age = 40;int * const ptr = &age;

    The value of age in the above statement can be changedthrough assignment statement or through ptr.

    However, the value of ptr cannot be changed.

  • 7/27/2019 2. C and C++

    76/86

    Programming in C++ -

    C and C++

    76

    Pointer Arithmetic

    Let CONST_INT_PTR be pointing to an integer value. So,CONST_INT_PTR is pointing to an address value like(int *) 0xa000.

    Now (CONST_INT_PTR+1) will be an address offset from(int *) 0xa000 by an amount equal to the size in bytesof the data type CONST_INT_PTR is pointing to.

    Listing: A C++ program to illustrate pointer arithmetic

  • 7/27/2019 2. C and C++

    77/86

    Programming in C++ -

    C and C++

    77

    Listing: A C++ program to illustrate pointer arithmetic.

    #include

    void main(void){int x;int *ptr;ptr=&x;

    cout

  • 7/27/2019 2. C and C++

    78/86

    Programming in C++ -

    C and C++

    78

    The last program printed the following result in oneparticular run:

    Value of ptr = 0x8f6afff4Value of ptr+1= 0x8f6afff6Value of incremented ptr= 0x8f6afff6

    Pointers and Arrays

  • 7/27/2019 2. C and C++

    79/86

    Programming in C++ -

    C and C++

    79

    Pointers and Arrays

    Consider the following array declaration:

    int num[5];

    num gives the starting address of the array

    num is a constant value num++ is invalid since it is a constant value num is not a variable num has a datatype (int *). num is the same as &num[0] *(num+n) is the same as num[n]

    (where 0

  • 7/27/2019 2. C and C++

    80/86

    Programming in C++ -

    C and C++

    80

    If ptr is an integer pointer, we can initialize the pointervariable ptr with array name as shown below:

    ptr = num;

    Now,

    *(ptr +n) is the same as *(num+n) = num[n]*(ptr+n) can be interchanged with ptr[n].

  • 7/27/2019 2. C and C++

    81/86

    Programming in C++ -

    C and C++

    81

    Pointers and StructuresConsider a structure template

    struct person{char name[20];int age;double height;};

    A ariable of person t pe str ct re can be declared and

  • 7/27/2019 2. C and C++

    82/86

    Programming in C++ -

    C and C++

    82

    A variable of person type structure can be declared andinitialized as shown below:

    person p = {Bill, 40, 170.5};

    The address_of operator (&) can be used to extract theaddress of the structure variable p.

    Pointer to a person type structure data can be declaredas shown below:

    person * ptr;

    The size of ptr is 2 bytes.

  • 7/27/2019 2. C and C++

    83/86

    Programming in C++ -

    C and C++

    83

    You can initialize ptr with the address of p. Thus

    ptr = &p;

    To access individual structure members using pointer tostructure variable we use the -> operator as shown below:

    ptr->nameptr->ageptr->height

  • 7/27/2019 2. C and C++

    84/86

    Programming in C++ -

    C and C++

    84

    Pointers and Strings

    String constants can be used to initialize pointers as shownbelow.

    char *ptr = Hello World;

    The above statement creates a variable ptr which is initializedwith the starting address of the memory location where thestring Hello World is stored with a null character appended to

    it at the end.

  • 7/27/2019 2. C and C++

    85/86

    Programming in C++ -

    C and C++

    85

    The new Operator

    You can allocate memory at run time to store values. Thismemory is unnamed and pointers become the only meansto access that memory. The C++ operator new can beused for this purpose. For example,

    int * ptr = new int;

    The above statement creates a pointer variable ptr first.The new operator then reserves two consecutive bytes ofmemory and returns the starting address of this chunk ofmemory to the variable ptr.

  • 7/27/2019 2. C and C++

    86/86

    The delete Operator

    The new operator is used to reserve memory when you needit. When you have finished your work with this memory youcan delete it using the delete operator.

    int *ptr=new int;delete ptr;

    This releases the memory ptr is pointing to. It does notremove ptr itself. ptr can be reused.