EGR 2261 Unit 8 Strings and Enumeration Types

EGR 2261 Unit 8 Strings and Enumeration Types

EGR 2261 Unit 8 Strings and Enumeration Types Read Malik, Chapter 7 (but you can skip pages 485-490 on namespaces). Homework #8 and Lab #8 due next week. Quiz next week.

Whats in Chapter 7 Chapter 7 covers three separate topics: 1. 2. 3. Enumeration types, which are the simplest kind of programmer-defined data type (as opposed to built-in data types such as int, double, char, and bool).

Namespaces, which weve been using since Week 1 and which also have more advanced uses. Well skip this topic. The string data type, which weve been using since Week 2. Well do this topic first, and then do enumeration types. 2 Strings

A string is a sequence of characters. String constants in C++ are enclosed in "". Examples of string constants: (a 7-character string) "TVC15" (a 5-character string) "A very nice string!" (a 19-character string) "porkpie" Recall that we must use the getline

function to read in a string containing spaces from the keyboard. 3 Two Kinds of Strings in C++ In C++, the term string can mean: The string data type, which is new to C+ +. (It did not exist in C.) To use it, you must include the

header file. Weve been using this data type since Week 2. Discussed in Chapter 7. A null-terminated character array. This was the only kind of string in C. The book refers to these as C-strings. Much less convenient than C++s string data type, but some people still use it. Discussed in Chapter 8, but well skip it. 4

Using [] with the string Type You can use the operator [] to access an individual character in a string. (As well learn next week, this operator is called the array subscript operator.) Example: string myStr1 = "porkpie"; cout << myStr1[1]; Display the Declare and initialize a

string variable. character in position 1 of the string. Caution: A strings first character is in position 0, and its second character is in position 1, and so on. C++ Programming: From Problem Analysis to Program Design, Seventh Edition 5 Using + with the string Type The binary operator + concatenates (combines) strings to create a longer string.

Example: string myStr1, myStr2, myStr3; myStr1 = "porkpie"; myStr2 = "hat"; myStr3 = "my " + myStr1 + " " + myStr2; cout << myStr3; C++ Programming: From Problem Analysis to Program Design, Seventh Edition 6 Lots of string Functions

The header file contains many useful and powerful functions for working with strings. Some of these are listed in Table 7-1 on pages 494-495. Some others are listed on pages 1334-1335 of Appendix F. See next slides. Note that some of these functions (which I've marked with *) change the string on which they operate. Also, some functions (which I've marked with red arrows) are equivalent to each other.

7 * * * * * 8 This special value is equal to -1.

9 * * * * 10 Lots of Examples

See the book's Examples 7-14 through 7-18. See also Chapter 7's Programming Example: Pig Latin Strings. 11 Review: Partial Hierarchy of Data Types Data Types Integral

(int, bool, char, ) Simple Data Types Structured Data Types Floating-Point (double, ) Enumeration

Pointers Recall that a data type is a set of values with a set of operations on them. Enumeration Type An enumeration type is a simple data type created by the programmer. To define an enumeration type, you must provide: A name for the data type A set of values for the data type C++ Programming: From Problem Analysis to Program Design, Seventh Edition

13 Enumeration Type (contd.) Syntax to create an enumeration type: value1, value2, are identifiers called enumerators. Its conventional to use all uppercase letters for the enumerators, but this is not required. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

14 Enumerators Must be Valid Identifiers Enumerators must be valid identifiers. They cannot be numeric values such as 5 or 3.14, nor can they be char values such as 'A'. They must obey the rules for valid identifiers that we learned in Unit 2. C++ Programming: From Problem Analysis to Program Design, Seventh Edition 15 Declaring Variables of an

Enumeration Type After youve created an enumeration type, youll want to declare variables of that type, using the syntax : This is the same syntax we use for declaring variables of built-in types such as int or char. C++ Programming: From Problem Analysis to Program Design, Seventh Edition 16 Example: Declaring Variables of an Enumeration Type Example: Suppose weve created an enumeration type

named sports: Then to declare two variables of the sports type, we can write: Its similar to the declaration int num1, num2; C++ Programming: From Problem Analysis to Program Design, Seventh Edition 17 Assignment You can assign a value directly to an enumeration-type variable. Example: popularSport = FOOTBALL;

Here were assigning a value (FOOTBALL) to a variable (popularSport). Its similar to the statement num1 = 5; C++ Programming: From Problem Analysis to Program Design, Seventh Edition 18 Assignment (contd.) You can also assign the value of one enumeration-type variable to another variable of that type. Example: What is the value of

mySport after this code runs? C++ Programming: From Problem Analysis to Program Design, Seventh Edition 19 Relational Operators An enumeration type is an ordered set of values. Therefore we can use the relational operators such as < and >= on them. Example: Given the enumeration type defined by then

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 20 Relational Operators: Example 21 Enumeration Types as Integral Types

Enumeration types are really just integral types that let you use words as aliases for integer values. Example: Given the enumeration type defined below, BASKETBALL is really just an alias for 0, and FOOTBALL is an alias for 1, and HOCKEY is an alias for 2, and so on. This is useful when its easier for the programmer to think in terms of words rather than in terms of special numbers that stand for those words.

Output of Enumeration Types If you use cout to display the value of an enumerationtype variable, whats actually displayed is the integer value. Example: This code displays 2. C++ Programming: From Problem Analysis to Program Design, Seventh Edition 23

Output of Enumeration Types (contd.) Heres a way to display the enumerator instead of the integer value: C++ Programming: From Problem Analysis to Program Design, Seventh Edition 24 Arithmetic Operations on Enumeration Types? If enumeration types are really integral types, can you perform arithmetic operations on them? Yes and no. The following statements

are not legal: But the following is legal: C++ Programming: From Problem Analysis to Program Design, Seventh Edition 25 Enumeration Types and Loops Since an enumeration type is an integral data type, you can use a loop to walk through each of the types values: C++ Programming: From Problem Analysis to Program Design, Seventh Edition

26 Input of Enumeration Types? You cannot use cin to read a value directly into an enumeration-type variable. C++ Programming: From Problem Analysis to Program Design, Seventh Edition 27 Enumeration Types as Function Parameters Enumeration types can be passed as

parameters to functions either by value or by reference. See example on next slide. C++ Programming: From Problem Analysis to Program Design, Seventh Edition 28 Enumeration Types as Function Parameters: Example 29 Enumeration Types as Function

Return Values A function can also return a value of an enumeration type. You will do this in one of the Lab 8 programs. See example on next slide. C++ Programming: From Problem Analysis to Program Design, Seventh Edition 30 Enumeration Types as Function Return Value: Example 31

Final Points This largely concludes the topic of enumeration types. The next few slides discuss two or three minor related points--in particular: Anonymous types The typedef statement Declaring Variables When Defining the Enumeration Type

You can declare variables of an enumeration type in the same statement that defines the type. Example: To define a type named grades and also declare a variable of that type named courseGrade, we can write: enum grades {A, B, C, D, F} courseGrade; Its equivalent to these two statements: enum grades {A, B, C, D, F}; grades courseGrade; Probably clearer to use two statements rather than one. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

33 Anonymous Data Types As a variation on the previous slides example, you can declare variables of an anonymous type (a type that has no name). Example: Were creating a new data type, but were not giving this data type a name. Here mySport is a variable of an anonymous type whose possible values are BASKETBALL, FOOTBALL, BASEBALL, and HOCKEY. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

34 Anonymous Data Types (contd.) Drawbacks to using anonymous data types: Tends to be confusing. You cannot pass/return an anonymous type to/from a function. Best practice: Dont use anonymous data types. And use separate statements to define your enumeration type and declare variables of that type. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

35 typedef Statement This is a new topic, separate from enumerated types but similar because it deals with programmer-defined data types. The typedef statement creates a synonym to an existing data type. Despite its name, typedef does not define a new data type. It merely lets you use a new name for an existing data type. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

36 typedef Statement (contd.) Syntax: Example: typedef int integer; After this statement, you can use the word integer in place of the word int, such as in this declaration: integer num1, num2 = 5; Mainly useful as a convenience to reduce the amount of typing for advanced data types that have long, complicated names. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

37

Recently Viewed Presentations

  • Code of Professional Practice for IT Practitioners

    Code of Professional Practice for IT Practitioners

    Senior professionals have an obligation to mentor junior professionals. Although professionals are highly educated, junior members of a profession require additional learning and experience to develop professional judgment. This learning is best accomplished under the tutelage of a senior professional.
  • Stereotypes - MYLAW

    Stereotypes - MYLAW

    Stereotypes. Can spread positive or negative messages. There is often a focus on race and gender when discussing stereotypes. But, "groups" can be stereotyped. For example: Men are good at math. Or, American tourists are rude.
  • Video Conference Training - University of British Columbia

    Video Conference Training - University of British Columbia

    Please create your own VC checklist, consist of Conference ID, Session Name, Date and Time. ... If the screen is blank, please press screen firmly. ... Video Conference Training Last modified by:
  • Radical radicles: Flowers, fruits, & seeds Flower development

    Radical radicles: Flowers, fruits, & seeds Flower development

    Beans, peas, lentils, carob, peanut, tamarind, alfalfa Conifers & Cones Cones develop from female flower Seeds borne under bracts Have single wing Some cones are "serotinous" and open only after fire Forgotten seed cache Environmental triggers Temperature, moisture, sunlight Scarification:...
  • Northeast Region Consultant Services Overview

    Northeast Region Consultant Services Overview

    WisDOT's first steps are to determine the scope of work needed; estimate time and costs for the services; and, determine the schedule of when we need the services completed. Once this is known, we start planning what it will take...
  • Year 7 Order of Operations Dr J Frost

    Year 7 Order of Operations Dr J Frost

    Cube Root. You should be familiar with √ meaning 'square root', i.e. what number squared gives this value? What do you think 38 is? Can you verify it using your calculator? ? (the cube root of 8 is 2 because...
  • Honors Chemistry - tumwater.k12.wa.us

    Honors Chemistry - tumwater.k12.wa.us

    School wide non-negotiables. Cell Phones: We provide all the tech you need, put yours away.. In Class and On Time. Respect: Represent yourself in a positive way by using appropriate language, listening to others and treating others as you would...
  • MMI HLDA - University of Colorado Boulder

    MMI HLDA - University of Colorado Boulder

    And, a few annotation Errors Some Interesting Problems Addressed Word sense annotation transferred from old Treebank to new Treebank Coreference annotation transferred to new Treebank Treebank/PropBank with or without NMLs reside in harmony Various levels of data quality identified in...