1 / 29

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++. C++ Tokens. The smallest individual unit when writing program (similar to the alphabet and vocabulary of English) Three types Special symbols Word symbols Identifiers. //basics.cpp //This is to show some basic elements of C++

karis
Download Presentation

Chapter 2 Basic Elements of C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 Basic Elements of C++

  2. C++ Tokens • The smallest individual unit when writing program (similar to the alphabet and vocabulary of English) • Three types • Special symbols • Word symbols • Identifiers Chapter 2 Basics of C++

  3. //basics.cpp //This is to show some basic elements of C++ //The following two lines are preprocessor directives #include <iostream> using namespace std; /*Here begins the main() function Every C++ program has a main() function*/ int main() { cout << "Hello world!\n\n"; //Every C++ statement ends //with a semicolon return 0; } //The main() finishes here with this right curly bracket Chapter 2 Basics of C++

  4. C++ Tokens in basics.cpp Special Symbols Chapter 2 Basics of C++

  5. C++ Tokens in basics.cpp Word Symbols (Reserved Words, Keywords) Chapter 2 Basics of C++

  6. C++ Tokens in basics.cpp Identifiers These are all predefined identifiers from the standard library. But you can define your own identifiers. Chapter 2 Basics of C++

  7. C++ Tokens • If a token is made of more than two characters, no spaces are allowed among these characters. (editor can help) • You are not allowed to use keywords for other purposes, e.g. naming your own variable int • A C++ identifiers can have letters, digits, and underscore (_) and cannot begin with a digit. • C++ is case-sensitive!!!!!!!!!!!!! • Use meaningful identifiers • I will follow certain naming convention for identifiers: myIdentifierName Chapter 2 Basics of C++

  8. C++ Data Types • Computer uses different # of digits to store different types of data Chapter 2 Basics of C++

  9. C++ Data Types (Integral) Exercises: You can run the program in Appendix G to find out the memory size of your system. You can also run the program on page 1049 to view the range of values of different data types. Chapter 2 Basics of C++

  10. C++ Data Types (Floating point) Chapter 2 Basics of C++

  11. Arithmetic Operators • Five arithmetic operators + addition - subtraction * multiplication / division % remainder (modulus) • Terms • Operands • Binary vs. unary operators Chapter 2 Basics of C++

  12. Order of Precedence • *, /, And % have higher level of precedence than + and – • No precedence within the same level • Parentheses have highest precedence Chapter 2 Basics of C++

  13. Expressions • Integral expression yields integral value • Decimal expression yields decimal value • Mixed expression yields the highest precision of the values of all the operands Chapter 2 Basics of C++

  14. Type Conversion (Casting) staic_cast<dataTypeName>(expression) Cast operation has higher precedence than arithmetic operation Example 2-9 This is optional. You don’t have to understand this at this stage. Chapter 2 Basics of C++

  15. Storing Data in Memory • Two steps • Allocate memory • Names to use for each memory location • Types of data to store in each memory location • Whether the value of the data will change (variable vs. constant) • Assign data to the allocated memory Chapter 2 Basics of C++

  16. Storing Data in Memory: Declaration • Variables dataType identifier, identifier, … ; • Constants const dataType identifier = value; Chapter 2 Basics of C++

  17. Storing Data in Memory: Assignment • Use assignment operator = variableIdentifier = expression; • Each assignment operation erase the previous stored dataHow to swap the contents of two variables? Chapter 2 Basics of C++

  18. Storing Data in Memory: Initialization • What is initialization Assign a value to a memory location for the first time • Constant must be initialized when it is declared • Variables contains garbage if it is not initialized • Declaration and initialization can be put into one statement Chapter 2 Basics of C++

  19. Input (cin) Statement • Put data into variables from the standard input devices (keyboard in most cases) • Syntax cin >> variableOne >> variableTwo …; • Variable must be declared first • #include <iostream> Don’t confuse with insertion operator << Stream extraction operator Chapter 2 Basics of C++

  20. Input (cin) Statement • Example 2-15 • Works with string without spaces • #include <string> • Reads string with spacesgetline(cin, stringName); Chapter 2 Basics of C++

  21. Output • Syntaxcout << expression or manipulator << …; • Expression will be evaluated before printing • Manipulator is used to format the output • Escape sequences (try \7) Chapter 2 Basics of C++

  22. Increment and Decrement Operators (Optional) • ++ -- • These three statements are equivalent • count = count + 1; • count++; //post-increment • ++count; //pre-increment • These two are not (suppose x = 5) • y = x++; //y = 5 after execution • y = ++x; //y = 6 after execution • Try to use them only in stand-alone format (or not to use them at all) Chapter 2 Basics of C++

  23. Compound Assignment operators (Optional) • += -= *= /= %= • These two statements are equivalent • total = total + 10; • total += 10; • Avoid complicated compound assignments (or not to use them at all) x /= y + 5; //what does this mean? Chapter 2 Basics of C++

  24. Program Style and Format • Source code: .cpp • Functionint main() { …; return 0; } • Syntax rules must be strictly followed • Blanks and indentations • Optional • Increase readability • Comments and prompts Chapter 2 Basics of C++

  25. Recommended exercises • Exercises2, 4, 6, 7, 10, 15, 18, 23 • Programming exercises8, 10 Chapter 2 Basics of C++

  26. Programming Example: Make Change • Problem • Input any change expressed in cents • Computes the # of half-dollars, quarters, dimes, nickels and pennies to be returned • For example 646 cents should be returned as 12 half-dollars, 1 quarter, 2 dimes, and 1 penny. Chapter 2 Basics of C++

  27. Programming Example: Make Change • Problem analysis Suppose the change is 646 cents. 1. Change = 646 2. # of Half-dollars = 646/50 = 12 3. Remaining Change = 646 % 50 = 46 4. # of Quarters = 46 / 25 = 1 5. Remaining Change = 46 % 25 = 21 6. # of Dimes = 21 / 10 = 2 7. Remaining Change = 21 % 10 = 1 8. # of Nickels = 1 / 5 = 0 9. Remaining Change = 1 % 5 = 1 • Algorithm 1. Get the change in cents. 2. Find the number of half-dollars. 3. Calculate the remaining change. 4. Find the number of quarters. 5. Calculate the remaining change. 6. Find the number of dimes. 7. Calculate the remaining change. 8. Find the number of nickels. 9. Calculate the remaining change. 10. The remaining change is the number of pennies Chapter 2 Basics of C++

  28. Programming Example: Make Change Variables • From the above steps it appears that we will need variables to hold half-dollars, quarters and so on • Since we are not going to use the values of # of half-dollars, quarters and so on in any calculation, we can simply output them. The only thing that keeps changing is the change int change; Named Constants const int Halfdollar = 50; const int Quarter = 25; const int Dime = 10; const int Nickel = 5; Chapter 2 Basics of C++

  29. Programming Example: Make Change Main Algorithm 1. Prompt the user for input 2. Get input 3. Echo the input by displaying the entered change on the screen 4. Compute and print the number of half-dollars 5. Calculate the remaining change 6. Compute and print the number of quarters 7. Calculate the remaining change 8. Compute and print the number of dimes 9. Calculate the remaining change 10. Compute and print the number of nickels 11. Calculate the remaining change 12. Print the remaining change Chapter 2 Basics of C++

More Related