Download C++ Programming: Enumeration Types and Strings and more Slides Computer Science in PDF only on Docsity!
Lecture
Enum, Arrays, Strings, Struct and more
Dr. Mageda Sharafeddin
Objectives In this chapter, you will:
- Learn how to create and manipulate your own simple data type—called the enumeration type
- Become aware of the typedef statement
- Explore the string data type, and learn how to use the various string functions to manipulate strings
- Learn about records (structs)
- Examine various operations on a struct
- Manipulate data using a struct
- Learn about the relationship between a struct and functions
- Examine the difference between arrays and structs C++ Programming: From Problem Analysis to Program Design, Seventh Edition 2
C++ Programming: Program Design Including Data Structures, Second Edition 4 Enumeration Type
- Data type - a set of values together with a set of operations on those values
- To define a new simple data type, called enumeration type, we need three things: - A name for the data type - A set of values for the data type - A set of operations on the values
C++ Programming: Program Design Including Data Structures, Second Edition 5 Enumeration Type (continued)
- A new simple data type can be defined by specifying its name and the values, but not the operations
- The values must be identifiers
C++ Programming: Program Design Including Data Structures, Second Edition 7 Examples
- The following are illegal enumeration types because none of the values is an identifier: enum grades{'A', 'B', 'C', 'D', 'F'}; enum places{1st, 2nd, 3rd, 4th};
- The following are legal enumeration types: enum grades{A, B, C, D, F}; enum places{first, second, third, fourth};
C++ Programming: Program Design Including Data Structures, Second Edition 8 Declaring Variables
- The syntax for declaring variables is: dataType identifier, identifier,...;
- The following statement defines an enumeration type sports enum sports{basketball, football, hockey, baseball, soccer, volleyball};
- The following statement declares variables of the type sports. sports popularSport, mySport;
10 Operations
- No arithmetic operation is allowed on enumeration types
- The following statements are illegal; mySport = popularSport + 2; //illegal popularSport = football + soccer; //illegal popularSport = popularSport * 2; // illegal
C++ Programming: Program Design Including Data Structures, Second Edition 11 Operations (continued)
- The increment and decrement operations are not allowed on enumeration types
- The following statements are illegal; popularSport++; //illegal popularSport--; //illegal
Examples of legal enum operations enum sports {BASKETBALL, FOOTBALL, HOCKEY, BASEBALL, SOCCER, VOLLEYBALL}; sports popularSport, mySport; popularSport = FOOTBALL; popularSport = static_cast(popularSport + 1); What is the value of popularSport after the last statement is executed? C++ Programming: Program Design Including Data Structures, Second Edition 13
Examples of legal enum operations enum sports {BASKETBALL, FOOTBALL, HOCKEY, BASEBALL, SOCCER, VOLLEYBALL}; FOOTBALL <= SOCCER HOCKEY > BASKETBALL BASEBALL < FOOTBALL for (mySport = BASKETBALL; mySport <= SOCCER; mySport = static_cast(mySport + 1))
-. -. -. C++ Programming: Program Design Including Data Structures, Second Edition 14
C++ Programming: Program Design Including Data Structures, Second Edition 16 Functions and Enumeration Types
- Enumeration type can be passed as parameters to functions either by value or by reference
- A function can return a value of the enumeration type
Enumeration as input or output
- enum courses {ALGEBRA, BASIC, PASCAL, CPP, PHILOSOPHY, ANALYSIS, CHEMISTRY, HISTORY};
- courses registered;
- char ch1, ch2;
- cin >> ch1 >> ch2; //Read two characters C++ Programming: Program Design Including Data Structures, Second Edition 17
C++ Programming: Program Design Including Data Structures, Second Edition 19 typedef Statement
- You can create synonyms or aliases to a previously defined data type by using the typedef statement
- The syntax of the typedef statement is: typedef existingTypeName newTypeName Ex: typedef int integer; integer i;
20 ANSI/ISO Standard C++, Namespaces namespace namespace_name { members } namespace globalType { const int N = 10; const double RATE = 7.50; int count = 0; void printResult(); } globalType::RATE; globalType::printResult();