Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators...

27
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements Introduction to Library Functions

description

Summary of Operators

Transcript of Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators...

Page 1: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Last Time….

OperatorsArithmetic OperatorsAssignment OperatorsIncrement/Decrement OperatorsRelational OperatorsLogical Operators

ExpressionStatementsIntroduction to Library Functions

Page 2: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Precedence of OperatorsEvaluation the following Expressions:

a) 4 + 7 * 6 / 3 – 5b) ++ 23 / - 3 + 3 --

Page 3: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Summary of Operators

Page 4: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Defining constants

const Qualifier

#define directive

Page 5: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

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

Page 6: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Static Castsint result;float a = 4.0;result = a * 3.56;

<<Compiler Signals a WARNING!>>

How do you cast to another type?>>Up-Cast>>Down-Cast

Page 7: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

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.

Page 8: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

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.

Page 9: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Explicit Type Conversion

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

Page 10: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Syntax

static_cast <DataType>(Value)

A variable or Literal value

Data Type you wish to convert it to

Page 11: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

int main(){

int books, months;double booksPerMonth;

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

cout<<“How many month will it take you to read them?”cin>>months;

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

system(“pause”);return 0;}

How would you prevent the INTEGER DIVISION?

Page 12: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Quiz _1 : Implement an Pseudocode

Write a program that implements the following algorithm.

Start 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 Stop

Page 13: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

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]

Page 14: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

[Solutions to the Quiz Questions]

Page 15: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Library Functions

Yared SemuAddis Ababa Institute of Technology

April 2012

Page 16: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Library Functions

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

–Mathematical Functions–IO Manipulators

Page 17: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Mathematical FunctionsThe 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>

Page 18: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

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.

Page 19: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Multiple Arguments• When a function takes multiple

arguments, they are separated by commas inside the parenthesis.

• This statement computes 3 raised to 4.

Page 20: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Mathematical Functions … The function that computes the power of two numbers is :

More examples of mathematical functions are:

Page 21: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

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 iomanip in standard C++) header file.

#include <iomanip>

Page 22: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Commonly Used Manipulators

General Output

Manipulator Range Description

endl now Write a newline ('\n') and flush buffer.

setw(n) next •Sets minimum field width on output. (Note: A field is an area on a computer screen, where information such as characters or numbers can be entered and manipulated)•Use left and right to justify the data appropriately in the field. •Output is right justified by default. e.g. cout<<setw(7)<<n<<endl

width(n) next Same as setw(n)

Page 23: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

IO ManipulatorsGeneral Output

Manipulator Range Description

left now •Left justifies output in field width.•Only useful after setw(n).

right next •Right justifies output in field width. •Since this is the default, it is only used to override the effects of left. •Only useful after setw(n).

setfill(ch) all •Only useful after setw.• If a value does not entirely fill a field, the character ch will be used to fill in the other characters. •Default value is blank.•E.g. cout << setw(4) << setfill('0') << n << endl;

Page 24: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

IO ManipulatorsFloating point output

Manipulator Range Description

setprecision(n) all •Sets the number of digits printed to the right of the decimal point. •This applies to all subsequent floating point numbers written to that output stream. •However, this won't make floating-point "integers" print with a decimal point. •It's necessary to use fixed for that effect

fixed all •Used fixed point notation for floating-point numbers.• Opposite of scientific. •If no precision has already been specified, it will set the precision to 6

scientific all •Formats floating-point numbers in scientific notation.• Opposite of fixed.

Page 25: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

IO Manipulators(Example)What is the output of this program?

Page 26: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Example- IO Manipulators• Write a program that displays the

output show below.

Page 27: Last Time. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.

Exercises