Lec03_Basic Elements C++

Embed Size (px)

Citation preview

  • 8/13/2019 Lec03_Basic Elements C++

    1/28

    BASIC ELEMENTSOF

    C++

  • 8/13/2019 Lec03_Basic Elements C++

    2/28

    OBJECTIVES

    To be able to use C++

    input/outputand formatting

    To become familiar with the

    C++standard

  • 8/13/2019 Lec03_Basic Elements C++

    3/28

    Outcome

    Know how to write your firstprogram in C++ using basicelements of C++.

  • 8/13/2019 Lec03_Basic Elements C++

    4/28

    Hello C++!

    // This is a C++ program. It prints the sentence:// Welcome to C++ Programming.

    #include using namespace std;

    int main(){

    cout

  • 8/13/2019 Lec03_Basic Elements C++

    5/28

    Your First Program Explained

  • 8/13/2019 Lec03_Basic Elements C++

    6/28

    Your First Program Explained

    #include- read the file iostreamthat contains the definition for the stream

    input/output package.

    using namespace std;- all names in theprogram belong to the "standard namespace"

  • 8/13/2019 Lec03_Basic Elements C++

    7/28

    cout

  • 8/13/2019 Lec03_Basic Elements C++

    8/28

    New Line \n or endl

    #include using namespace std;

    int main(){

    cout

  • 8/13/2019 Lec03_Basic Elements C++

    9/28

    Comments

    Comments can be written in two styles: Single line:

    double can_volume = 0.355; // Liters in a 12-ouncecan

    Multiline for longer comments:

    /*This program computes the volume (in liters)of a six-pack of soda cans.

    */

  • 8/13/2019 Lec03_Basic Elements C++

    10/28

    Basic data types in C++Data type C++ keyword Range

    Integer int -32768 to 32767

    Long integer long -4294967296 to 4294967295

    Short integer short -128 to 127

    Unsigned integer unsigned 0 to 65535

    Character char 0 to 255

    Floating point float 6 digits of precision

    Double floating point double 12 digits of precision

    Example of declaration

    float amount; //Declares amount as a floating-point variable

    int number; //Declares number as an integer variable

    char ch; //Declares ch as a character variable

  • 8/13/2019 Lec03_Basic Elements C++

    11/28

    C++ operators

    Several classes of operators

    Arithmetic

    Relational

    Logical

    Assignment

  • 8/13/2019 Lec03_Basic Elements C++

    12/28

    Arithmetic operators

    Seven arithmetic operators in C++ ( E.g. int a = 7, b =2; )

    Operators Action

    - Substraction

    + Addition

    * Multiplication

    / Division

    % Modulus division

    - - Decrement

    + + Increment

    Expression Value

    a - b 5a + b 9

    a * b 14

    a / b 3

    a % b 1

    a-- 6

    b++ 3

  • 8/13/2019 Lec03_Basic Elements C++

    13/28

    Arithmetic Operators (Integer)

    #include using namespace std;

    int main(){

    // Integers Expression

    cout

  • 8/13/2019 Lec03_Basic Elements C++

    14/28

    Arithmetic Operators (Float)

    #include using namespace std;

    int main(){

    // Floating-point expression

    cout

  • 8/13/2019 Lec03_Basic Elements C++

    15/28

    Arithmetic Operators (Mix)

    #include using namespace std;

    int main()

    { // Mix expression

    cout

  • 8/13/2019 Lec03_Basic Elements C++

    16/28

    Relational operators

    Six relational operators in C++

    The results is either TRUE(1)or FALSE (0)

    E.g.

    if a = 7andb = 5, then a < byields 0and a != byields 1Operator Meaning

    < Less than

    Greater than

    >= Greater than or equal

    = = Equal

    != Not equal

  • 8/13/2019 Lec03_Basic Elements C++

    17/28

    Logical Operators

    &&(logicalAND) Returns trueif both conditions are true

    ||(logical OR)

    Returns trueif either of its conditions are true

    !(logicalNOT, logical negation) Reverses the truth/falsity of its condition

    Returns truewhen its condition is false

    Logical operators used as conditions in loops

    Expression Resulttrue && false falsetrue || false true!false true

  • 8/13/2019 Lec03_Basic Elements C++

    18/28

    Assignment Operators

    Most commonly used assignment operator is =(e.g. int Q = 5; )

    Assignment expression abbreviations

    c = c + 3;can be abbreviated as c += 3;using the additionassignment operator

    Examples of other assignment operators include:d -= 4 (d = d - 4)

    e *= 5 (e = e * 5)f /= 3 (f = f / 3)g %= 9 (g = g % 9)

  • 8/13/2019 Lec03_Basic Elements C++

    19/28

    Increment and Decrement Operators

    Find the value of i and j (Assume i = 1)

    int j = ++i // j is 2 , i is 2

    int j = i++ // j is 1, I is 2

    int j = --i // j is 0, I is 0

    int j = i-- //j is 1, I is 0

    T C i (C ti )

  • 8/13/2019 Lec03_Basic Elements C++

    20/28

    Type Conversion (Casting)#include using namespace std;

    int main(){

    cout

  • 8/13/2019 Lec03_Basic Elements C++

    21/28

    Allocating Memory with Constants

    const doubleCONVERSION = 2.54;

    const intNO_OF_STUDENTS = 20;

    const charBLANK = ;

    const doublePAY_RATE = 15.75;

    Constant?A memory location whose content is not

    allowed to change during program execution.

  • 8/13/2019 Lec03_Basic Elements C++

    22/28

    Allocating Memory with Variables

    doublesale; intnum1, num2;

    charfirst;

    stringstr;

    Variable?A memory location whose content maychange during program execution.

    num1 = 4; num2 = 4*5-11;

    sale = 0.02 * 1000;

    first = D; str = It is a sunny day.;

    i l d i

  • 8/13/2019 Lec03_Basic Elements C++

    23/28

    #include using namespace std;

    int main(){

    const intNO_STUD = 20;

    stringstr;str = All are good students;

    cout

  • 8/13/2019 Lec03_Basic Elements C++

    24/28

    Input (Read) Statement#include using namespace std;

    int main()

    {int feet, inches;

    cout feet >> inches;cout

  • 8/13/2019 Lec03_Basic Elements C++

    25/28

    Increment & Decrement Operators#include using namespace std;

    int main()

    {int num = 0, num2 = 0, tot = 5;

    num++;

    num2 = num2 + 1;cout

  • 8/13/2019 Lec03_Basic Elements C++

    26/28

    Preprocessor Directives

    #include #include using namespace std;

    int main(){

    int num;string name;

    coutnum;

    coutname;

    return 0; }

  • 8/13/2019 Lec03_Basic Elements C++

    27/28

    Lets Try!

    Write a program that prompts the user to enter twotest scores and then prints the average test score.(Assume test scores are decimal numbers.)

  • 8/13/2019 Lec03_Basic Elements C++

    28/28

    Lets Try!

    Write a program that converts Fahrenheit degrees toCelsius using the formula :

    Celcius = 5/9 * (Fahrenheit 32)