User Defined Simple Data Types Objectives In this
User Defined Simple Data Types
Objectives In this chapter you will: Learn how to create and manipulate your own simple data type—called the enumeration type Become familiar with the typedef statement
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
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
Enumeration Type (continued) The syntax for enumeration type is: value 1, value 2, … are identifiers called enumerators value 1 < value 2 < value 3 <. . .
Enumeration Type (continued) Enumeration type is an ordered set of values If a value has been used in one enumeration type It cannot be used by another in the same block The same rules apply to enumeration types declared outside of any blocks
Assignment The statement: popular. Sport = FOOTBALL; stores the word FOOTBALL into popular. Sport The statement: my. Sport = popular. Sport; copies the contents of the variable popular. Sport into my. Sport
Operations No arithmetic operation is allowed on enumeration types The following statements are illegal:
Operations (continued) The increment and decrement operations are not allowed on enumeration types The following statements are illegal:
Operations (continued)
Operations and Input/Output Because an enumeration is an ordered set of values We can use relational operators with them The cast operator can be used to increment, decrement, and compare values Values can be used in loops Input and output are defined only for built-in data types such as int, char, double The enumeration type can be neither input nor output (directly)
Functions and Enumeration Types Enumeration types can be passed as parameters to functions either by value or by reference A function can return a value of the enumeration type
#include <iostream> using namespace std; int main(){ enum e_acompany {Audi=4, BMW=5, Cadillac=11, Ford=44, Jaguar=45, Lexus, Maybach=55, Rolls. Royce=65, Saab=111}; e_acompany my_car_brand; my_car_brand = Rolls. Royce; if (my_car_brand == Ford) cout << "Hello, Ford-car owner!" << endl; else if (my_car_brand == Rolls. Royce) cout << "Damm, I want one!" << endl; return 0; }
#include <iostream> using namespace std; int main(){ enum e_acompany {Audi=4, BMW=5, Cadillac=11, Ford=44, Jaguar=45, Lexus, Maybach=55, Rolls. Royce=65, Saab=111}; e_acompany my_car_brand; my_car_brand = Rolls. Royce; if (my_car_brand == Ford) cout << "Hello, Ford-car owner!" << endl; else if (my_car_brand == Rolls. Royce) cout << "Damm, I want one!" << endl; return 0; }
#include <iostream> using namespace std; int main(){ enum Days{Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; Days The. Day; int j; cout<<"Please enter the day of the week "; cin>>j; The. Day = Days(j); if(The. Day == Sunday || The. Day == Saturday) cout<<"Hurray it is the weekend"<<endl; else cout<<"Curses still at work"<<endl; return 0; }
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 does not create any new data types typedef creates an alias to an existing data type
Typedef example typedef int fruit; fruit apple = 0; fruit orange = 1; fruit banana = 2; typedef float price; price cost[3]; cost[apple] = 2. 25 cost[orange] = 0. 85; cost[banana] = 1. 15;
- Slides: 20