MELJUN Intro to C++ Programming

download MELJUN Intro to C++ Programming

of 47

Transcript of MELJUN Intro to C++ Programming

  • 8/14/2019 MELJUN Intro to C++ Programming

    1/47

    Introduction To C++Introduction To C++Programming LanguageProgramming Language

    LECTURE MELJUN CORTES, MBA,MPAMELJUN CORTES,

    MBA,MPA

  • 8/14/2019 MELJUN Intro to C++ Programming

    2/47

    What is C++

    C++ is a high-level language and itis evolved from C over a period ofseveral years starting in 1980.

    The standard for C++ was jointlydeveloped by the American NationalStandards Institute (ANSI) and the

    International Standards Organization(ISO).

    a set of rules, symbols, and special

    words used to construct a computer

  • 8/14/2019 MELJUN Intro to C++ Programming

    3/47

    Structure of a C++ Program

    Figure 3.1 Structure of a C++ Program

  • 8/14/2019 MELJUN Intro to C++ Programming

    4/47

    A Typical C++ Program

    // A typical C++ Program

    # include

    using namespace std;

    # define PI 3.142

    int Integer;

    int main ( )

    {

    double radius, area;

    radius = 7;

    area = radius * radius * PI;

    return 0;

    }

    Preprocessor Directive

    Comment

    Main

    function

    Global Declaration

    Local declaration

    Statements

  • 8/14/2019 MELJUN Intro to C++ Programming

    5/47

  • 8/14/2019 MELJUN Intro to C++ Programming

    6/47

    Token

    Token : combination of the characters in C++

    Categorised into:

    Identifiers Reserved words/keywords

    Constants

    Literal String

    Punctuators Operators

  • 8/14/2019 MELJUN Intro to C++ Programming

    7/47

    Identifiers

    Allows programmers to name data and other objects in theprogram-variable, constant, function etc.

    Can use any capital letter A through Z, lowercase letters athrough z, digits 0 through 9 and also underscore ( _ )

    Rules for identifier The first character must be alphabetic character orunderscore

    It must consists only of alphabetic characters, digits andunderscores, cannot contain spaces

    It cannot duplicate any reserved word C++ is case-sensitive; this means that CASE, Case, case,

    and CaSe are four completely different words.

  • 8/14/2019 MELJUN Intro to C++ Programming

    8/47

    Valid and Invalid Identifiers

    Valid names

    Astudent_name_aSystemNam

    e

    pialstdntNm_anthrSysNmPI

    Invalid names

    $sum // $ is illegal2names // cant start with

    2stdnt Nmbr // cant have

    space int // reserved word

  • 8/14/2019 MELJUN Intro to C++ Programming

    9/47

    Reserved word/Keywords

    A word that has special meaning inC++.

    Keywords cannot be used to nameidentifiers.

  • 8/14/2019 MELJUN Intro to C++ Programming

    10/47

    Constant

    Data values that cannot be changedduring the execution of a program

    Types of constant: Literals constantDefined constantsDeclared constants

  • 8/14/2019 MELJUN Intro to C++ Programming

    11/47

    Literals Constant

    If the data cannot be changed, we can simply codethe data value itself in a statement

    Eg: discount = 0.10 ;

    Categorised into: Integer Numerals ( eg: 178, -9, 0113, 0x4b)Floating-Point Numerals (eg: 3.14159,6.02e23,1.6e-19 ,3.0

    Characters ( eg: A, p)

    Strings ( eg; Hello World)

    Boolean (eg: true , false).

    0.10 is a literalconstant

  • 8/14/2019 MELJUN Intro to C++ Programming

    12/47

    Defined Constant (#define)

    Use the#define preprocessor directive Format: #define identifier value

    Eg : #define EPF_RATE 0.11 Placed at the beginning of the program

    #include using namespace std;#define EPF_RATE 3.142

    int main(){

    ;nett = salary ( salary * EPF_RATE);;

    }

  • 8/14/2019 MELJUN Intro to C++ Programming

    13/47

    Declared Constants

    se a type const qualifier to indicate that datacannot be changed and to fix the contents of

    the memory location

    g: const float pi = 3.1416;

    eclared inside a function

    #include using namespace std;#define EPF_RATE 3.142

    int main(){

    const double socso_rate = 0.05;nett = salary ( salary * EPF_RATE * socso_rate);;

    }

  • 8/14/2019 MELJUN Intro to C++ Programming

    14/47

    Data Types in C++

    Type defines a set of value and operationsthat can be applied on those values

    Set of values for each type is known as thedomain for the type

    Functions also have types which is

    determined by the data it returns

  • 8/14/2019 MELJUN Intro to C++ Programming

    15/47

    Data Types

    Standard

    They serves as the basic building blocks for derivedtypes (complex structures that are built using thestandard types

    Serves as the basic building blocks for derived types

  • 8/14/2019 MELJUN Intro to C++ Programming

    16/47

    Data Types

    Derived

  • 8/14/2019 MELJUN Intro to C++ Programming

    17/47

    Data Type : Void

    Typed as void Has no values and operations Both set of values are empty Usually used in functionsEg: void printSum()

  • 8/14/2019 MELJUN Intro to C++ Programming

    18/47

    Data Type : Char

    Used to hold characters or very smallinteger values

    Usually 1 byte of memory CODE:

    char letter;

    letter = 'C';

  • 8/14/2019 MELJUN Intro to C++ Programming

    19/47

    Data Type : Integer

    Coded as int A number without a fraction part C++ supports three different sizes of

    integershort intintlong int

    Can be signed and unsigned

  • 8/14/2019 MELJUN Intro to C++ Programming

    20/47

    Data Type : Integer

    Type Byte Size MinimumValue

    Maximum Value

    short int 2 -32,768 32,767

    unsigned short int 2 0 65,535

    int 4 -2,147,483,648 2,147,483,647

    unsigned int 4 0 4,294,967,295

    long int 4 -2,147,483,648 2,147,483,647

    unsigned long int 4 0 4,294,967,295

  • 8/14/2019 MELJUN Intro to C++ Programming

    21/47

    Data Type : Float

    A number with fractional part such as 43.32, - 2.33

    C++ supports three types of float

    floatdouble

    long float Stored in a form similar to scientific notation Can be represented in Fixed point (decimal) notation:

    31.4159 0.0000625E notation:

    3.14159E1 6.25e-5

  • 8/14/2019 MELJUN Intro to C++ Programming

    22/47

    Data Type Float

    Are double by default Can be forced to be float (3.14159f) or

    long double (0.0000625L)

    All floating-point numbers are signed

    Type Byte Size Precision Range

    float 4 7 10-37 ..1038

    double 8 15 10-307 ..10308

    long double 8 15 10-307 ..10308

  • 8/14/2019 MELJUN Intro to C++ Programming

    23/47

    Data Type : Boolean

    Represents values that are true or false

    bool variables are stored as small integers

    falseis represented by 0,

    trueby 1:

    bool allDone = true;

    bool finished = false;

    allDone finished

    0

  • 8/14/2019 MELJUN Intro to C++ Programming

    24/47

    Variables

    A storage location in memory whose contents can change whileprogram is running

    Has an identifier and a type of data it can hold

    Variable declaration syntax :

    type identifier [= initial_value]

    eg : int itemsOrdered;

    A variable name should represent the purpose of the variable.To hold the number of items

    ordered.

  • 8/14/2019 MELJUN Intro to C++ Programming

    25/47

    Variables

  • 8/14/2019 MELJUN Intro to C++ Programming

    26/47

  • 8/14/2019 MELJUN Intro to C++ Programming

    27/47

    Variables Assignment

    An assignment statement uses the = operator tostore a value in a variable.item = 12;

    This statement assigns the value 12 to the itemvariable.

    The variable receiving the value must appear onthe left side of the = operator.

    This will NOT work: // ERROR!12 = item;

  • 8/14/2019 MELJUN Intro to C++ Programming

    28/47

    Variables Scope

    The scope of a variable: the part of the programin which the variable can be accessed

    A variable cannot be used before it is defined

  • 8/14/2019 MELJUN Intro to C++ Programming

    29/47

    Variables Scope

    Global Variables

    Local Variable

    Local Variable

  • 8/14/2019 MELJUN Intro to C++ Programming

    30/47

    Variables Scope

    Global scope a global variable is a variable declared in the main

    body of the source code, outside all functions

    Global variables can be referred from anywhere in the

    code, even inside functions, whenever it is after itsdeclaration.

    Local Scopea local variable is one declared within the body of a

    function or a block. is limited to the block enclosed in braces ({}) where

    they are declared.

  • 8/14/2019 MELJUN Intro to C++ Programming

    31/47

    Operators

    C ++ uses a set of built in operators ( Eg : +,-, / etc).

    Four classes of operators :

    ArithmeticRelationalLogical

    Assignment

  • 8/14/2019 MELJUN Intro to C++ Programming

    32/47

    Arithmetic Operators

    Assume int a=4, b= 5, d;

    C++

    Operation

    Arithmetic

    Operator

    C++

    Expression

    Value of d

    after

    assignmentAddition + d = a + b 9

    Substraction - d = b - 2 3

    Multiplication * d = a * b 20

    Division / d = a/2 2

    Modulus % d = b%3 2

  • 8/14/2019 MELJUN Intro to C++ Programming

    33/47

    Assignment Operators

    Assume x=4, y=5, z=8;Assignment

    Operator

    Sample

    Expression

    Similar

    Expression

    Value of variable

    after assignment

    += x += 5 x = x + 5 x=9

    -= y -= x y = y - x y=1

    *= x *= z x = x*z x=32

    /= z /=2 z = z/2 z = 4

    %= y %=x y = y%x y=1

    R l ti l & E lit

  • 8/14/2019 MELJUN Intro to C++ Programming

    34/47

    Relational & EqualityOperators Assume y = 6, x =5

    Relational Operators Sample Expression Value

    > y > x T

    < y < 2 F

    >= x >= 3 T

  • 8/14/2019 MELJUN Intro to C++ Programming

    35/47

    Logical Operators

    Logical Operators Called Sample Operation

    && AND expression1 && expression 2

    | | OR expression1 | | expression2

    ! NOT !expression

    Example :Assume int x = 50

    expression !expression Sample Expression

    F T !(x == 60)

    T F !(x != 60)

  • 8/14/2019 MELJUN Intro to C++ Programming

    36/47

    Logical Operators

    Assume x=4, y=5, z=8

    expression1 expression2 expression1 &&

    expression2

    Sample Expression

    F F F ( y > 10) && ( z

  • 8/14/2019 MELJUN Intro to C++ Programming

    37/47

    Increment and DecrementOperators

    Operator Called Sample

    Expression

    Similar

    Expression

    Explanation

    ++ preincrement ++a a = a +1

    a += 1

    Increment a by 1, then use

    the new value of a in

    expression in which a reside

    ++ postincrement a++ a = a +1

    a += 1

    Use the current value of a in

    the expression which a

    reside, then increment a by

    1

    - - predecrement - - a a = a -1

    a -= 1

    Decrement a by 1, then usethe new value of a in

    expression in which a reside

    - - postdecrement a - - a = a -1

    a -= 1

    Use the current value of a in

    the expression which a

    reside, then decrement a by1

  • 8/14/2019 MELJUN Intro to C++ Programming

    38/47

    Operator Precedence

    Operators Associative

    ( ) Left to right

    ++ - - + - ! Right to left

    * / % Left to right

    + - Left to right

    < >= Left to right

    = = != Left to right

    && Left to right

    | | Left to right

    *= += - = /= %= Right to left

  • 8/14/2019 MELJUN Intro to C++ Programming

    39/47

    Operator Precedence

    Example 1:

    int a=10, b=20, c=15, d=8;

    a*b/(-c*31%13)*d

    1. a*b/(-15*31%13)*d

    2. a*b/(-465%13)*d

    3. a*b/(-10)*d

    4. 200/(-10)*d5. -20*d

    6. -160

    Example 2:

    int a=15, b=6, c=5, d=4;

    d *= ++b a/3 + c

    1. d *= ++b a/3+ c

    2. d*=7- a/3+c

    3. d*=7- 5+c

    4. d*=2 + c5. d*= 7

    6. d = d*7

    7. d = 28

  • 8/14/2019 MELJUN Intro to C++ Programming

    40/47

    Example of a program

    // Operating with variables#include

    using namespace std;

    int main(){

    //variables declarationint no1;int no2;

    int value_div;int value_mod;

    cout > no1 >> no2;value_div= no1 / no2;

    cout

  • 8/14/2019 MELJUN Intro to C++ Programming

    41/47

    / Evaluate two complex expression /

    #include

    using namespace std;

    void main ( )

    {

    int a =3, b=4, c = 5, x, y;

    cout

  • 8/14/2019 MELJUN Intro to C++ Programming

    42/47

    Formatting Output

    Output is made more readable usingmanipulator

    Must include header file.Manipulators Use

    endl

    dec

    oct

    hex

    fixedshowpoint

    setw()

    setprecision

    setfill()

    New line

    Formats output as decimal

    Formats output as octal

    Formats output as hexadecimal

    Set floating-point decimalsShows decimal in floating-point values

    Sets width of output fields

    Specifies number of decimals for floating point

    Specifies fill character

  • 8/14/2019 MELJUN Intro to C++ Programming

    43/47

    Formatting Output

    Set Width (setw) Set the minimum width for an outputTwo alignment : right justified and left

    justified Set Fill Character (setfill) If the output width is greater than the data

    values placed in it, a fill character can be

    used to fill the empty spaces wheneverrequired

  • 8/14/2019 MELJUN Intro to C++ Programming

    44/47

    Formatting Output

    Three integer manipulators:Decimal (dec)The default

    Value in decimal form Octal (oct) Values are displayed in octal numbering system

    Hexadecimal (hex) Values are in hexadecimal format

  • 8/14/2019 MELJUN Intro to C++ Programming

    45/47

    Formatting Output

    Three Floating-point manipulators: Fixed (fixed)

    Displays floating-point numbers (eg:1.234568e+6) in the fixed-point format(1234567.875)

    Set precision (setprecision)Used to control the number of the decimal

    places to be displayed Showpoint (showpoint)To show value with the decimal point

    //demonstrate the output manipulator

  • 8/14/2019 MELJUN Intro to C++ Programming

    46/47

    46

    //demonstrate the output manipulator

    #include

    #include

    using namespace std;

    int main( )

    {

    char aChar;

    int integer;

    float dlrAmnt;

    cout integer>>dlrAmnt >> aChar;

    cout

  • 8/14/2019 MELJUN Intro to C++ Programming

    47/47

    THANK YOU!THANK YOU!