+ All Categories
Home > Documents > Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost...

Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost...

Date post: 28-Dec-2015
Category:
Upload: kelly-williamson
View: 215 times
Download: 0 times
Share this document with a friend
44
Operator Precedence • First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. • Second all multiplications, divisions, and modulus operations are calculated from left to right.
Transcript
Page 1: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Operator Precedence

• First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis.

• Second all multiplications, divisions, and modulus operations are calculated from left to right.

Page 2: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Operator Precedence

• Third all additions and subtractions are evaluated from left to right.

– X = (1 + (( 2 + 4) * (5 + 2)))• x = (1 + (6 * 7)) = (1 + 48) = 49

Page 3: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Operator Precedence

• Fourth stream insertions (‘<<‘) and extractions (‘>>’) are evaluated, left to right.

• Next all relational operators are evaluated left to right. (‘<‘, ‘<=‘, ‘>’, ‘>=‘)

• Equality is evaluated sixth from left to right. (‘==‘, ‘!=‘)

• Finally, assignment is evaluated. (‘=‘)

Page 4: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

C++ Keywords

• There are many keywords in C++ that are not used in other languages.– Page 63, Figure 2.2

Page 5: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

If/else Selection Structure

• Example Programs

Page 6: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

If/else Selection Structures

• Multi-line statements in an if body require braces (‘{‘ and ‘}’).– Otherwise only the first statement in the

body is executed as part of the if body.

Page 7: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Switch Statements

• Switch statements are able to test int and char variables to handle multiple decisions.

• Code example.

Page 8: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Looping Control Structures

• C++ provides three types of loop control structures.– While loop– Do/While loop

• The body of the loop is executed at least once.

– For loop

– Example code

Page 9: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Math Functions

• C++ provides predefined math functions in the Math Library.– You need to simply refer to the name of the

function with the proper parameters.– The most common functions are listed on

Page 161 in Figure 3.2.

Page 10: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Function Definitions

• C++ Functions are defined using the following format:

Return-value-type function-name (parameter-list)

{

declarations and statements

return XXX;

}

Page 11: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Function Definitions

– Parameters to functions are considered local variables to the function.

• If there are no parameters the list is empty.

– The return type of a function must be defined.

• The return type for a function that does not return a data type is void.

• The Coding Standard requires that there be only one return statement per function at the end of the function.

Page 12: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Function Definitions

– Functions can not be defined inside of other functions.

• Each function is an individual.

Page 13: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Function Prototypes

• C++ requires a function prototype for each function a class defines.– The function prototype includes the

function name, the parameter names and data types, and the order of the parameters.

Page 14: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Header Files

• C++ requires each class have both a *.C file as well has a header file *.h.– The header file contains the function

prototypes for all class functions as well as the definitions of the various data types and constants for the class.

Page 15: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Header Files

• C++ provides many predefined classes. To use these classes in your program you need to include them using:

#include <nameOfHeaderFile>;

– Page 169 – 170 Figure 3.6 lists many of the common Standard Library header files.

Page 16: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Header Files

• To include a header file for a class you define use:

#include “nameOfTheHeaderFile.h”

– For now we will only use class definitions from the Standard Library.

Page 17: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Data Types

• C++ includes the standard data types as well as data types that are “unsigned”.– Unsigned data types include only positive

numbers.– The range of int includes both positive and

negative integers.– The range of unsigned int is as wide as int

but includes only positive numbers.

Page 18: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Enumeration• Enumeration is a data type that creates a set of

integer constant identifiers.– The first identifier is set to zero (0), the second is

assigned to one (1), etc.• The identifiers can also be assigned a particular integer

value.

– Enumeration identifier names are all capital letters.– Enumeration identifiers make a program easier to

understand.

enum Status {CONTINUE, WON, LOST};

Page 19: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Storage Classes

• Storage classes are used to determine the period during which an identifier exists in memory.– The storage class specifies are: auto,

register, extern, mutable, and static.

Page 20: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Storage Classes

• An identifier’s storage class specifier determines the storage class, scope, and linkage of the identifier.– Scope refers to where the identifier can be

referenced in the program.– Linkage determines for a multiple-source-file

program whether an identifier is known only in the current source file or in any source file with proper declarations.

Page 21: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Storage Classes

• Automatic storage class creates variables when the block in which they are declared is entered, they exist while the block is active, and they are destroyed when the block is exited.– Only variables can have automatic storage.– There are two types: auto and register.– By default, local variables in functions are

auto.

Page 22: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Storage Classes

– The register keyword can be used with local variables.

• Use register when the programmer wants to suggest that the compiler place the variable in one of the computer’s high-speed hardware registers rather than memory.

• The compiler might ignore this definition.

– Only one storage class specifier can be applied to an identifier.

Page 23: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Storage Classes• The extern and static keywords declare

identifiers for variables and functions that are in the static storage class.– Variables of this type exist from the time the

program begins execution.• Memory is allocated and initialized when the

program begins execution.

– Functions of this type exist from the point where the program begins execution.

Page 24: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Storage Classes

• The extern class is associated with global variables and functions.– NOTE: The RIT CS C++ Coding Standard

does not permit the use of global variables and functions.

• The only exception is the main function.

Page 25: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Storage Classes

• The static keyword is used for local variables and functions.– Static variables maintain their values when

a function is exited so that the next time the function is executed the values are still available.

Page 26: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Scope Rules

• Scope refers to the portion of a program where identifiers have meaning.– Not all variables will be available to each

function.

– Scope Example

Page 27: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Recursion

• A recursive function is a function that calls itself, either directly, or indirectly.– Recursive functions must include the

simplest case(s) called a base case(s).

Page 28: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Recursion

• A classic recursive example is the calculation of Fibonacci numbers.– fibonacci(0) = 0– fibonacci(1) = 1– fibonacci(n) = fibonacci(n – 1) + fibonacci(n –2)

Page 29: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Recursion

• Write a recursive function to calculate XN where X and N are both positive integers.

Page 30: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Inline Functions• C++ allows the definition of inline functions.

– Inline functions tell the compiler to generate a copy of the function’s code in place to avoid a function call.

• Multiple copies of the function code are inserted into the program.

– Such functions must be preceded by the keyword inline.

– The compiler can choose to ignore such definitions.

Page 31: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Inline Functions

– Inline functions should only be used for very simple, frequently used functions.

inline return-type functionName (parameter list) { statement;}

Page 32: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Call-By-Reference• C++ allows functions calls to use Call-By-

Reference.– The calling function gives the called function the ability

to directly access and modify the caller’s data.– Call-By-Reference eliminates the overhead of copying

large amounts of data.• This method does weaken security.

– To use this passing method, the reference parameter must be preceded by an ampersand (&).

Page 33: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

References

• Variables can be defined as a reference to another variable.– Once a variable is defined as a reference,

it can not be redefined to reference a different variable.

int count = 1;

int &countRef = count;

++countRef;

Page 34: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

References

• C++ does permit functions to return references, but you should not do this. – The program quickly becomes very

confusing.

Page 35: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Function Overloading

• Function Overloading permits multiple functions with the same name to be defined as long as each function has a different set of parameters.– The compiler will chose the proper version of

the function based upon the parameter list.– By definition, an overloaded function should

execute the same task using different data types.

Page 36: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Function Overloading

– The overloaded functions do not have to have the same number of parameters.

– The return type of the functions can be different.

• Simply changing the return type, without changing the parameter list does not create an overloaded function.

Page 37: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Function Overloading

int square( int x) { return x * x; }

double square (double x) {return x * x;}

Page 38: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Function Templates

• Function Templates can be used to create overloaded functions that have an identical function body.– Function templates will define a “family” of

functions.– The compiler will execute the proper version

of the function based upon the passed data types.

– Template Example.

Page 39: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Arrays

• Arrays in C++ are very similar to arrays in other languages. – One main difference is that the range of

values in an array (or the subscripts) begin with zero (0) and go to the length of the array – 1 (n – 1).

– Accessing an element at position n is a run time error.

Page 40: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Passing Arrays• C++ passes entire arrays to functions using

call-by-reference.– This means that any changes to the array in

the function will affect the original array data.

• C++ passes individual array elements to functions using call-by-value.– This means that any changes to the value in

the function do not affect the original array element data.

Page 41: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Array Length

• Java users should note that there is no length parameter for arrays in C++. – Your program should pass the length of the

array to each function.

Page 42: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Sorting Arrays

• Sorting arrays is a common task. – We will look specifically at the bubble sort.

• The bubble sort moves smaller values to the top of the array and larger values to the bottom of the array.

• The bubble sort is fairly slow and should only be used for small arrays.

Page 43: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Searching Arrays

• To find a particular element in an array a program will search an array.– A linear search will compare the search key to

every element in the array.• Use for small unsorted arrays.

– A binary search compares the search key to the middle element in the array and then searches one-half of the remaining elements based upon the comparison.

• Much faster search but requires a sorted array.

Page 44: Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,

Multi-dimensional Arrays

• C++ permits the development of m by n arrays.– m rows and n columns.– We will only work with two dimensional

arrays.– 2-D array example.


Recommended