Defining New Data Types in C++
Defining New Data Types in C++
Enumerated types
An enumeration is a data type in which labels for all possible values of the type can be listed The type declaration consists of the keyword enum followed by the name of the new type and a block of code in which labels for all values of the type are listed Syntax:
enum NewTypeName {value1, value2, , valueN};
Enumeration constants
The value labels listed in an enumeration are called enumeration constants Enumeration constants must be valid C++ identifiers; they are not string or char literals Enumeration constants are stored in memory as integers; by default, the first is assigned the value 0, the second 1, etc. Thus the order of the listing determines the relative magnitude of enumeration constant values; the first is less than the second, which is less than the third, and so forth
Example 1 (enum)
The following enumeration uses explicit assignment to specify values for the symbols used in the Roman numeral system:
enum RomanNum { I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000};
Example 2 (enum)
The following enumeration type creates constants that stand for the months of the year:
enum MonthType {JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
Only the first constants value is specified; since it is 1, the second is 2, the third is 3, and so on until the last (DEC) with the value 12
Operations on enumerations
Since enumerations are not built-in data types, only some of the most common operations can be performed using variables of these types The allowable operations include:
logical comparison using the relational operators (<, >, <=, >=, ==, !=) simple arithmetic (but not arithmetic/assignment operations like ++ or --) enumerations can be parameters to, and/or return values from, functions enumerations can be used as switch expressions and/or case labels in switches example on next slide
MonthType thisMonth; switch ( thisMonth ) // using enum type switch expression { case JAN : case FEB : case MAR : cout << Winter quarter ; break ; case APR : case MAY : case JUN : cout << Spring quarter ; break ; case JUL : case AUG : case SEP : cout << Summer quarter ; break ; case OCT : case NOV : case DEC : cout << Fall quarter ; }
. . .
}
Example
#include <iostream.h> #include <stdlib.h> enum RomanNum {I=1, V=5, X=10, L=50, C=100, M=1000}; int main() { cout << "Welcome to the world of Roman numerals!" << endl; int num = (int)(M + C + L + X + V + I); cout << "MCLXVI=" << num << endl; num = (int)((L-X) + (V-I)); cout << "XLIV=" << num << endl; system("PAUSE"); return 0; }
Databases
A database is a collection of information organized for ease of storage and retrieval A library catalog is a good example of a database; such a catalog contains information about each book in the librarys collection Although each book is unique, books share common characteristics, such as author, title, publisher, etc. We store information in the database based on these common characteristics
Database components
At the macroscopic level, a database consists of one or more files containing information The information in the files is stored in the form of records; in our example, each record would contain all of the information concerning a specific book Within the records, individual data items are stored in fields; for library books, each record would have separate fields for author, title, publisher, etc. All the records contain the same set of fields; they differ in the actual data stored in the fields
Record structures
If we wanted to create our own library database, we would want to consider how people use books, and what information they might want to look up We could come up with a preliminary list of fields to include, such as:
Author Title Subject(s) Call number (the Dewey Decimal or Library of Congress classification that determines where the book is shelved) ISBN (for those who delight in reading bar codes and many more, but this is enough to start with
Example
struct BookRecord { string title; string author; string subject1, subject2, subject3; string callNo; string ISBN; };
This statement allocates memory for two BookType records; the next step is to assign values to these variables
Example
myBook.title = Seabiscuit; myBook.author = Hillenbrand, Laura; myBook.subject1 = Horse racing; myBook.subject2 = Great Depression 1929-1939, U.S.; myBook.subject3 = Biscuits, Aquatic; myBook.ISBN = 0-345-46508-3; myBook.callNo = 939.41 H42; yourBook.title = Geeks; yourBook.author = Katz, Jon; yourBook.subject1 = Computer technicians U.S. case studies; yourBook.subject2 = Electronic data processing personnel U.S. case studies; yourBook.ISBN = 0-375-50298-X; yourBook.callNo = TK7885.54 K38;
Aggregate operations
An aggregate operation is an operation performed on an entire struct variable (as opposed to one of its member variables) The following are allowable aggregate operations on struct variables:
assignment to another struct variable of the same type (example: yourBook = myBook;) pass to a function as an argument (either by value or by reference) use as return value from a function
On the other hand, the following aggregate operations are NOT allowed: I/O, arithmetic, logical comparison
It is therefore much more common to pass struct variables by reference, because then only a single copy of the large object is held in memory
Hierarchical structures
A struct can contain an enum type as one of its member variables It is also possible for a struct to contain a member variable of a struct type Such an arrangement makes possible a greater level of detail in each record
32
.02
.failrate
25 1999
.lastServiced
.purchaseDate
.cost