Presentation is loading. Please wait.

Presentation is loading. Please wait.

Auburn University http://www.eng.auburn.edu/~xqin COMP 3000 Object-Oriented Programming for Engineers and Scientists Programmer-Defined Functions Dr.

Similar presentations


Presentation on theme: "Auburn University http://www.eng.auburn.edu/~xqin COMP 3000 Object-Oriented Programming for Engineers and Scientists Programmer-Defined Functions Dr."— Presentation transcript:

1 Auburn University http://www.eng.auburn.edu/~xqin
COMP 3000 Object-Oriented Programming for Engineers and Scientists Programmer-Defined Functions Dr. Xiao Qin Auburn University

2 Chapters Ch 1, Ch 2, Ch 3.1 and 3.2 Please read Ch 1.1 on page 3 1-2 2

3 C++ Variables C++ Identifiers Variables
Keywords/reserved words vs. Identifiers Must start with a letter or the underscore symbol The rest must be letters, digits, or the underscore symbol Case-sensitivity and validity of identifiers Meaningful names! (see also Page 13) Variables A memory location to store data for a program Must declare all data before use in program 1-3 3

4 Keywords Reserved Words Predefined meaning in C++ Example:
short, long, char, true, false, if, else, for, while, class, private, public, typedef, static, sizeof, new, delete, See Appendix 1 for a complete list of keywords. Yes. C++ is case-sensitive 1-4 4

5 Declare a Variable A variable must be declared before it is used!
Tell the compiler what kind of data (i.e., data type) will be stored in the variable See what will happen when you compile a C++ program where a variable is not declared. Give an example. 1-5 5

6 Data Types: Display 1.2 Simple Types (1 of 2)
Page 9: Short to = = 2^ > bits for 2 Bytes -> 8 Bits per Byte double: commonly used floating-pint number Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-6 6

7 Data Types: Display 1.2 Simple Types (2 of 2)
Char may be treated as an integer type to save space. Unsigned Version: unsigned int, unsigned long Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-7 7

8 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Assigning Data Initializing data in declaration statement Results "undefined" if you don’t! int myValue = 0; int count = 0, distance = 15, factor = 1; Assigning data during execution Lvalues (left-side) & Rvalues (right-side) Lvalues must be variables Rvalues can be any expression Example: distance = rate * time; Lvalue: "distance" Rvalue: "rate * time" Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-8 8

9 Display 1.3 Some Escape Sequences (1 of 2)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-9 9

10 Constants Naming your constants Use named constants instead
Literal constants are "OK", but provide little meaning e.g., seeing 24 in a pgm, tells nothing about what it represents Use named constants instead Meaningful name to represent data const int NUMBER_OF_STUDENTS = 24; const int BRANCH_COUNT = 10, WINDOW_COUNT = 10; //not recommended, a separate line is clearer. Called a "declared constant" or "named constant" Now use it’s name wherever needed in program Added benefit: changes to value result in one fix 1-10 10

11 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Type Casting Two types Implicit—also called "Automatic" Done FOR you, automatically 17 / 5.5 This expression causes an "implicit type cast" to take place, casting the 17  17.0 Explicit type conversion Programmer specifies conversion with cast operator (double)17 / 5.5 Same expression as above, using explicit cast (double)myInt / myDouble More typical use; cast operator on variable Precision of Calculations VERY important consideration! Expressions in C++ might not evaluate as you’d "expect"! "Highest-order operand" determines type of arithmetic "precision" performed Common pitfall! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-11 11

12 Shorthand Operators: Two Options
Post-Increment intVar++ Uses current value of variable, THEN increments it Pre-Increment ++intVar Increments variable first, THEN uses new value "Use" is defined as whatever "context" variable is currently in No difference if "alone" in statement: intVar++; and ++intVar;  identical result Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-12 12

13 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Console Input/Output I/O objects cin, cout, cerr Defined in the C++ library called <iostream> Must have these lines (called pre- processor directives) near start of file: #include <iostream> using namespace std; Tells C++ to use appropriate library so we can use the I/O objects cin, cout, cerr Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-13 13

14 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Formatting Numbers "Magic Formula" to force decimal sizes: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); These stmts force all future cout’ed values: To have exactly two digits after the decimal place Example: cout << "The price is $" << price << endl; Now results in the following: The price is $78.50 Can modify precision "as you go" as well! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-14 14

15 Boolean Expressions: Display 2.1 Comparison Operators
Logical Operators Logical AND (&&) Logical OR (||) Equality and Relational Operators Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2-15 15

16 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Branching Mechanisms if-else statements Choice of two alternate statements based on condition expression Example: if (hrs > 40) grossPay = rate* *rate*(hrs-40); else grossPay = rate*hrs; Like if-else statements in Java Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2-16 16

17 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
The switch Statement A new stmt for controlling multiple branches Uses controlling expression which returns bool data type (true or false) Syntax: Display page 62 next slide Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2-17 17

18 The break and continue Statements
Flow of Control Recall how loops provide "graceful" and clear flow of control in and out In RARE instances, can alter natural flow break; Forces loop to exit immediately. continue; Skips rest of loop body These statements violate natural flow Only used when absolutely necessary! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2-18 18

19 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Loops 3 Types of loops in C++ while Most flexible No "restrictions" do-while Least flexible Always executes loop body at least once for Natural "counting" loop Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2-19 19

20 Introduction to Functions
Building Blocks of Programs Other terminology in other languages: Procedures, subprograms, methods In C++: functions I-P-O Input – Process – Output Basic subparts to any program Use functions for these "pieces" Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-20 20

21 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Predefined Functions Libraries full of functions for our use! Two types: Those that return a value Those that do not (void) Must "#include" appropriate library e.g., <cmath>, <cstdlib> (Original "C" libraries) <iostream> (for cout, cin) Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-21 21

22 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Even More Math Functions: Display 3.2 Some Predefined Functions (1 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-22 22

23 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Even More Math Functions: Display 3.2 Some Predefined Functions (2 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-23 23

24 Random Number Generator
Return "randomly chosen" number Used for simulations, games rand() Takes no arguments Returns value between 0 & RAND_MAX Scaling Squeezes random number into smaller range rand() % 6 Returns random value between 0 & 5 Shifting rand() % 6 + 1 Shifts range between 1 & 6 (e.g., die roll) See Example: rand.cpp Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-24 24

25 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Random Number Seed Pseudorandom numbers Calls to rand() produce given "sequence" of random numbers Use "seed" to alter sequence srand(seed_value); void function Receives one argument, the "seed" Can use any seed value, including system time: srand(time(0)); time() returns system time as numeric value Library <time> contains time() functions Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-25 25

26 Components of Function Use
3 Pieces to using functions: Function Declaration/prototype Information for compiler To properly interpret calls Function Definition Actual implementation/code for what function does Function Call Transfer control to function Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-26 26

27 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
main(): "Special" Recall: main() IS a function "Special" in that: One and only one function called main() will exist in a program Who calls main()? Operating system Tradition holds it should have return statement Value returned to "caller"  Here: operating system Should return "int" or "void" Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-27 27

28 Programming Exercise 1 Write a C++ function that display the calculator menu. The program will prompt the user to choose the operation choice (from 1 to 5). Then it asks the user to input two integer vales for the calculation. See the sample below. Write a main function to call this menu function: MENU 1. Add 2. Subtract 3. Multiply 4. Divide 5. Modulus Enter your choice: 1 3-28 28

29 Programming Exercise 1 int displaymenu(){ int choice;
cout<<" MENU ” << "\n"; cout<<" 1.Add"<<"\n"; cout<<" 2.Subtract"<<"\n"; cout<<" 3.Multiply"<<"\n"; cout<<" 4.Divide"<<"\n"; cout<<" 5.Modulus"<<"\n"; cin >> choice return choice; } 3-29 29

30 Programming Exercise 2 Write a C++ function that computer the standard deviation of four scores. Write a main function to call this menu function: 3-30 30


Download ppt "Auburn University http://www.eng.auburn.edu/~xqin COMP 3000 Object-Oriented Programming for Engineers and Scientists Programmer-Defined Functions Dr."

Similar presentations


Ads by Google