Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,"— Presentation transcript:

1 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.

2 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

3 Operator Precedence Fourth stream insertions (‘ >’) 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. (‘=‘)

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

5 If/else Selection Structure Example Programs

6 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.

7 Switch Statements Switch statements are able to test int and char variables to handle multiple decisions. Code example.

8 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

9 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.

10 Function Definitions C++ Functions are defined using the following format: Return-value-type function-name (parameter-list) { declarations and statements return XXX; }

11 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.

12 Function Definitions –Functions can not be defined inside of other functions. Each function is an individual.

13 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.

14 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.

15 Header Files C++ provides many predefined classes. To use these classes in your program you need to include them using: #include ; –Page 169 – 170 Figure 3.6 lists many of the common Standard Library header files.

16 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.

17 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.

18 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};

19 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.

20 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.

21 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.

22 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.

23 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.

24 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.

25 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.

26 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

27 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).

28 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)

29 Recursion Write a recursive function to calculate X N where X and N are both positive integers.

30 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.

31 Inline Functions –Inline functions should only be used for very simple, frequently used functions. inline return-type functionName (parameter list) { statement;}

32 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 (&).

33 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;

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

35 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.

36 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.

37 Function Overloading int square( int x) { return x * x; } double square (double x) {return x * x;}

38 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.

39 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.

40 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.

41 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.

42 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.

43 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.

44 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.


Download ppt "Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,"

Similar presentations


Ads by Google