Ist Lecture on C++

Embed Size (px)

Citation preview

  • 7/31/2019 Ist Lecture on C++

    1/26

    Structure of a C++ Program

    Date: 17/01/11

  • 7/31/2019 Ist Lecture on C++

    2/26

    Outlines

    Introduction

    Components of a C++ Program

    Escape Sequences Variable declaration & Memory allocation

    Namespaces

    User Interactive Programs Formatting the Output

  • 7/31/2019 Ist Lecture on C++

    3/26

    Introduction

    C++ improves on many of Cs features.

    C++ provides object-oriented programming(OOP). C++ is a superset to C.

  • 7/31/2019 Ist Lecture on C++

    4/26

    History

    C was designed by Ritchi, Tompson, Kernighan

    It was fast, good & powerful

    but programs maintance was a nightmare!

    Solution: ... Object-Oriented Programming

  • 7/31/2019 Ist Lecture on C++

    5/26

    Some New Operators in C++

    >> Extraction Operator extracts data from

    input stream.

  • 7/31/2019 Ist Lecture on C++

    6/26

    Components of C++ program

    Header files

    INT MAIN() or VOID MAIN()

    Output Statement- Use Of COUT

    The CIN

  • 7/31/2019 Ist Lecture on C++

    7/26

    Simple C++ Program

    #include

    int main()

    {Std :: cout

  • 7/31/2019 Ist Lecture on C++

    8/26

    HEADER FILES

    #include

    #include #include

  • 7/31/2019 Ist Lecture on C++

    9/26

    INT MAIN OR VOID MAIN

    #include

    int main()

    {Statements ;

    If(expression)

    {statement ;

    ---------------;

    }

    Statement ; // commentreturn 0 ;

    }

  • 7/31/2019 Ist Lecture on C++

    10/26

    Output Statements

    The first output statement of previous programstd :: cout

  • 7/31/2019 Ist Lecture on C++

    11/26

    Using cout and Insertion operator

    Screen

    cout

  • 7/31/2019 Ist Lecture on C++

    12/26

    Some character preceded by slash character(\)

    like the one at the end of the above output

    statement, i.e. \n having a special meaning

    for the compiler.

    Back Slash (\) is an escape character.

    \n is new line character.

    Thus, std::cout

  • 7/31/2019 Ist Lecture on C++

    13/26

    Escape Character Description of action

    \a Bell or beep is generated by the computer on

    program execution.

    \b Back space. Move cursor to previous position

    \f From feed Advance cursor to next page

    \n Shift to next line

    \r Carriage return. Position the cursor to the

    beginning of current line.

    \t Horizontal tab.

    \v Vertical tab.

    \\ Displays a back slash character (\)

    \ Displays a single quote character()

    \? Displays a question mark(?).\0 Null termination character. Signifies end of a

    character string.

    \o Code for octal numbers.

    \x Code for hexadecimal numbers respectively

    \ Displays a double quote character().

  • 7/31/2019 Ist Lecture on C++

    14/26

    Application of some escape sequence

    #include

    int main()

    {

    std::cout

  • 7/31/2019 Ist Lecture on C++

    15/26

    Output

    The expected output is :

    Hello, Welcome to programming with C++!

    Hello,\ Welcome to programming with C++!\Hello Welcome to programming with C++!

    Hello Welcome to programming with C++!

  • 7/31/2019 Ist Lecture on C++

    16/26

    Comments

    A comment introduced by first putting doubleslash (//) followed by the comment up to the endof the line.

    If the comment is long and goes to the next line,another double slash (//) is needed before thenext line.

    Alternatively, a long comment may as wellas be

    between the C type comment symbols,i.e. /*before the start of comment and */ at the end ofthe comment.

  • 7/31/2019 Ist Lecture on C++

    17/26

    How to include comments and use of

    void main() in place of int main()// A program with comments

    #include

    Void main()

    // Anything written after double slash is neglected up to the

    // end of line. The comment can be put anywhere in the program.

    {std::cout

  • 7/31/2019 Ist Lecture on C++

    18/26

    The cin

    To put in some data we make use of function cin

    This is used along with the operator >> which is

    called extraction operator.

    This is followed by the name of variable in whose

    memory block the data has to be stored.

    After typing the data for cin, do press the enter-

    key, otherwise, the computer will not proceedfurther.

  • 7/31/2019 Ist Lecture on C++

    19/26

    Using cin and Extraction operator

    cin >> number

    Keyboard

  • 7/31/2019 Ist Lecture on C++

    20/26

    Application of cin in a user interactive

    program

    #include

    int main()

    {

    int length = 0 ; // length is the name of variable of type intInt width = 0, area =0; // width and area are also names of variables

    std:: cout

  • 7/31/2019 Ist Lecture on C++

    21/26

    Namespaces

    It enables the programmer to group a set ofobjects or function under one name.

    All classes, objects and functions of C++

    Standard Library are defined undernamespace std.

    We can simplify the writing ofstd :: coutto

    coutand std :: cin to cin by including followingthe directive in the program:

    using namespace std ;

  • 7/31/2019 Ist Lecture on C++

    22/26

    Application of namespaces

    #include

    Using namespace std ; // use of std

    namespace

    namespace NS1 // no semicolon at the end

    of line

    {

    int n = 3 ;

    float m = 2.5 ;

    int k = 2 ;

    double R = n*m*k ; }

    namespase NS2 // no semicolon at the end of

    line{ float n = 4.0 ; // Names are same as in NS1

    //but values are different .

    int m =2 ;

    double k = 3.0 ;

    double R = n*m*k ; }

    int main()

    { int Squatre ;

    int Product ;

    using namespace NS2 ;

    Square = n*n + m*m ; // values under NS2 are used

    Product = NS1 :: k*NS2 :: m ; // k belongs to NS1 and//m to NS2

    cout

  • 7/31/2019 Ist Lecture on C++

    23/26

    User Interactive Programs

    #include

    using namespace std ;

    int main()

    { int D ;

    float PI ;

    // Here D stands for//diameter and PI stands

    //for .

    double A , C ;

    // A stands for area and C

    //stands for circumference

    cout > D >> PI ;

    // statement for input D and PI

    cout

  • 7/31/2019 Ist Lecture on C++

    24/26

    Formatting The Output

    In next Class ..

  • 7/31/2019 Ist Lecture on C++

    25/26

    Conclusion

    We tried to grasp the fundamentals of C++

    programming.

  • 7/31/2019 Ist Lecture on C++

    26/26

    Thanks for listening.