Docsity

C++ Numeric Types, Expressions, and Output, Slides of Advanced Computer Programming

Partial preview of the text

Download C++ Numeric Types, Expressions, and Output and more Slides Advanced Computer Programming in PDF only on Docsity!

Chapter 3

Numeric Types,

Expressions, and

Output

Chapter 3 Topics

  • Constants of Type int and float
  • Evaluating Arithmetic Expressions
  • Implicit Type Coercion and Explicit Type

Conversion

  • Calling a Value-Returning Function
  • Using Function Arguments
  • Using C++ Library Functions in

Expressions

  • Calling a Void Function
  • C++ Manipulators to Format Output
  • String Operations length,find,and substr

4

C++ Simple Data Types

simple types

integral floating

char short int long bool enum float double long double

unsigned

Standard Data Types in C++

  • Integral Types
    • represent whole numbers and their negatives
    • declared as int, short, or long
  • Floating Types
    • represent real numbers with a decimal point
    • declared as float or double
  • Character Type
    • represents single characters
    • declared as char (^) 5

Scientific Notation

7

2.7E4 means 2.7 x 10

4

2.7E-4 means 2.7 x 10

  • 4

More About Floating Point Values

  • Floating point numbers have an integer part and a fractional part, with a decimal point in between. Either the integer part or the fractional part, but not both, may be missing

Examples 18.4 500. .8 -127.

  • Alternatively, floating point values can have an exponent, as in scientific notation--the number preceding the letter E doesn’t need to include a decimal point

Examples 1.84E1 5E2 8E-1 -.127358E

10

Main returns an int value to the operating

system

//******************************************************* // FreezeBoil program // This program computes the midpoint between // the freezing and boiling points of water //******************************************************* #include < iostream > using namespace std; const float FREEZE_PT = 32.0; // Freezing point of water const float BOIL_PT = 212.0; // Boiling point of water int main() { float avgTemp; // Holds the result of averaging // FREEZE_PT and BOIL_PT

11

Function main Continued

cout << “Water freezes at “ << FREEZE_PT << endl; cout << “ and boils at “ << BOIL_PT << “ degrees.” << endl;

avgTemp = FREEZE_PT + BOIL_PT; avgTemp = avgTemp / 2.0;

cout << “Halfway between is “; cout << avgTemp << “ degrees.” << endl;

return 0; }

13

More C++ Operators

int age;

age = 8;

age = age + 1;

age

age

14

Prefix Form

Increment Operator

int age;

age = 8;

++age;

age

age

16

Decrement Operator

int dogs;

dogs = 100;

dogs--;

dogs

dogs

Which Form to Use

  • When the increment(or decrement) operator

is used in a “ stand alone ” statement solely to

add one(or subtract one) from a variable’s

value, it can be used in either prefix or

postfix form

17

dogs--; --dogs;

USE EITHER

What is an Expression in C++?

  • An expression is a valid arrangement of

variables, constants, and operators

  • In C++ each expression can be evaluated to

compute a value of a given type

  • The value of the expression

9.3 * 4.5 is 41.

Operators can be

binary involving 2 operands 2 + 3

unary involving 1 operand - 3

ternary involving 3 operands later