Last time
1 / 27

Last Time…. - PowerPoint PPT Presentation

  • Uploaded on

Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements Introduction to Library Functions. Precedence of Operators. Evaluation the following Expressions: a) 4 + 7 * 6 / 3 – 5

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Last Time….' - evadne

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Last time
Last Time….

  • Operators

    • Arithmetic Operators

    • Assignment Operators

    • Increment/Decrement Operators

    • Relational Operators

    • Logical Operators

  • Expression

  • Statements

  • Introduction to Library Functions

  • Precedence of operators
    Precedence of Operators

    Evaluation the following Expressions:

    a) 4 + 7 * 6 / 3 – 5

    b) ++ 23 / - 3 + 3 --

    Defining constants
    Defining constants

    • const Qualifier

    • #define directive

    Automatic type conversion and casts
    Automatic Type Conversion and Casts

    int count = 100;

    float avgWeight = 70F;

    double totalWeight = count * avgWeight;

    cout<<“The total weight in this room = ”<<totalWeight;

    Automatic Type Conversion , Compiler handles the type conversion

    Static casts
    Static Casts

    int result;

    float a = 4.0;

    result = a * 3.56;

    <<Compiler Signals a WARNING!>>

    How do you cast to another type?



    Implicit type conversion
    Implicit Type Conversion

    • When an operator’s operands are of different data types, C++ will automatically convert them to the same data type.

    • When a value is converted to a higher data type, it is said to be prompted.

    • To demote a value means to convert it to a lower data type.

    Two rules
    Two Rules

    Rule 1: When an operator works with two values of different values of different data types, the lower-ranking value is prompted to the type of the higher-ranking value.

    Rule 2: When the final value of and expression is assigned to a variable, it will be converted to the data type of that variable.

    Explicit type conversion
    Explicit Type Conversion

    [A type cast expression lets you manually promote or demote a value in the same way that automatic conversion takes place.]


    A variable or Literal value

    static_cast <DataType>(Value)

    Data Type you wish to convert it to

    How would you prevent the INTEGER DIVISION?

    int main()


    int books, months;

    double booksPerMonth;

    cout<<“How many books do you plan to read?”


    cout<<“How many month will it take you to read them?”


    booksPerMonth = books/months;

    cout<<“That is”<<booksPerMonth<<“books per month.\n”;


    return 0;


    Quiz 1 implement an pseudocode
    Quiz _1 : Implement an Pseudocode

    Write a program that implements the following algorithm.


    Read the total hours the employee has worked, TotalHours

    Read the hourly rate of pay for the employee, HourlyRate

    GrossSalary= TotalHours* HourlyRate

    Tax = GrossSalary * 0.1

    NetSalary = GrossSalary - Tax

    Display NetSalary


    Quiz 2 currency
    Quiz _2:Currency

    [Write a program that will convert U.S. dollar amounts to Japanese yen and to euros. The conversion factors to use are 1dollar = 108.5 yen and 1 dollar = 0.8218 euros]

    Library functions

    Library Functions

    Yared Semu

    Addis Ababa Institute of Technology

    April 2012

    Library functions1
    Library Functions

    • In C++, we make extensive use of library functions to accomplish tasks.

      • Mathematical Functions

      • IO Manipulators

    Mathematical functions
    Mathematical Functions

    The mathematical functions allow us to do mathematical operations.

    These operations include:

    • raising a number to a certain power,

    • computing the square root of a number,

    • computing the cosine of an angle, etc... .

      These functions are defined in the header file math.h (or cmath in standard C++).

    #include <cmath>

    Mathematical functions1
    Mathematical Functions …

    sqrt is the name of the function that performs the square root operation. This function takes one argument of type double and returns a result of type double.

    Multiple arguments
    Multiple Arguments

    • When a function takes multiple arguments, they are separated by commas inside the parenthesis.

    • This statement computes 3 raised to 4.

    Mathematical functions2
    Mathematical Functions …

    The function that computes the power of two numbers is :

    More examples of mathematical functions are:

    Io manipulators
    IO Manipulators

    • IO Manipulators are operators used with the insertion operator (<<) to modify or manipulate the way data is displayed.

    • Other IO manipulators, which take arguments, are defined in the iomanip.h(or iomanipin standard C++) header file.

    #include <iomanip>

    Io manipulators example
    IO Manipulators(Example)

    What is the output of this program?

    Example io manipulators
    Example- IO Manipulators

    • Write a program that displays the output show below.