|
|
In this laboratory assignment we will consider the following:
A. Review Enumerated Types and Typedef
B. Using C++ Structures
C. Array of Structs
The enum (short for enumerated type) keyword can be used to define new data types. In programming where advanced data structures will be required, the enum statement is essential. The enum type should be used to provide good internal documentation. This makes the program more readable and easier to understand (more self-documenting). Also, another purpose for the enum statement is to help reduce logical errors in a program.
The enum statement provides a way to name integer constants and allows the programmer to invent a "new" type and to specify what values this new type can support. The syntax of the enum appears as:
enum user-defined_type {listing of data values};
or
enum {listing of integer constants};
Note the following examples:
Now we have two new data types: Day and Video. Note that these are types, not variables! Therefore, declarations similar to the following would be appropriate.enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY}; enum Video {COMEDY, ADVENTURE, HORROR, MYSTERY, MUSICAL}; enum {FALSE, TRUE};
The third enum statement listed above is equivalent to the following:Day returnDay; Video oneVideo;
const int FALSE = 0; const int TRUE = 1;
Enumerated variables are treated as "integers" by the compiler which means that these values are ordinal (ordered). In the Day list of data items, MONDAY would be stored as 0 and THURSDAY would be stored as 3, for example.
There are disadvantages of using enumerated data types, however.
One major disadvantage is the fact that variables of this type cannot be
input or output directly (using the input and output operators).
We will learn how to get around this later!
Another tool that is often used to aid in self documentation is the typedef statement. The typedef declaration is used to create an alias for an existing data type and is invaluable in making a program easier to modify. This will be extremely important to us later in the semester.
The syntax for the typedef follows:
For example,typedef ExistingDataType NewDataType;
aliases the type char as CharacterType. Now we have a "new" type. It is called CharacterType and we could declare variables as follows:typedef char CharacterType;
CharacterType letter; CharacterType name[20];
In reviewing our previous knowledge of simple data types (aka scalar types), we remember that variables of these types have a major restriction: these variables may only store one data item. For example, if the following declaration is made:
oneNumber may only store one integer value. To store multiple data items of the same type, we have the array structured data type. For example,int oneNumber;
const unsigned int MAX_VALUES = 50; int manyNumbers[MAX_VALUES];
will set up an array called manyNumbers that will have the
ability to store as many as 50 integers. One major disadvantage of
the simple data type and the array structure is the fact that each type
only stores one data type. For example, the manyNumbers array
can store only integers and the oneNumber variable may store only
an integer. There are many applications where it would be beneficial
to have one variable that could store several types of information at the
same time.
For example, in a program to manage inventory and customers for a video store,
it would be nice to have:
C++ provides the structure data type to satisfy this need. The C++ struct is a group of data items that do not necessarily have the same data type. The struct should be used whenever the programmer wishes to store related information into one variable name.
- a variable to store information related to a movie owned by a video store that will store the name of the movie, the number of copies owned by the store, and the type of movie
- a variable to store information concerning a customer of the video store including the name of a person renting a movie, the person's id number, name of a movie rented, and the return date of the movie rental.
The syntax for the C++ struct type follows:
struct StructName
{
list of member types and member names;
};
In examining our cla15.dat file, we realize that we need a struct that will store the following information for each video: name of the video, the number of copies of the video owned by the video store and the type of video. The variable, videoInfo, to declare a video struct of this type follows:
NOTE that Video is a TYPE not a variable!!! Thus, we must declare a variable to be of type video for memory locations to be associated with the member elements. Also, note that videoInfo is a variable that has been declared to be of type Video!!struct Video { string movieTitle; //name of movie unsigned int numberCopies; //number of copies string videoType; //type of video }; Video videoInfo;
The items included in the C++ struct are called members of the struct and we will learn later that structs can have data members (as shown above) and/or function members.
As stated earlier, we need to add code to the
cla15.cpp program so that it will also evaluate customers and print
out a list of video customer information. Let's now examine the cla15b.dat
file. This file contains customer information for the video store.
All data in this file is in the following form for each customer:
name of customer | (a string) |
id number of customer | (integer) |
name of video rented by the customer | (a string) |
return date of the video | (a string) |
The dot operator, "." , is used when accessing structs. It is preceded by the struct variable name and is followed by the name of a member of that struct. Hence, the "." operator is often called a membership operator. The following code will read from the stream videoIn and print values for the variable videoInfo as declared above.
//get the video information getline(videoIn, videoInfo.movieTitle); videoIn >> videoInfo.numberCopies; videoIn.get(endOfLine); getline(videoIn, videoInfo.videoType); //print out video information cout << "\n\nName of video:\t\t" << videoInfo.movieTitle << endl; cout << "Number of copies:\t" << videoInfo.numberCopies << endl; cout << "Video type:\t\t" << videoInfo.videoType << endl;
This question might best be answered by showing the complete program to process one video.
#include <fstream> using namespace std; //set up global struct type struct Video { string movieTitle; //name of movie unsigned int numberCopies; //number copies owned string videoType; //type of video }; //function prototyes void getVideo(Video&, ifstream&); //get one video int main() { //variable declarations Video videoInfo; //one video //declare and open the file containing //video information ifstream videoIn; videoIn.open("cla15.dat"); //read from the file until eof is reached getVideo(videoInfo, videoIn); while (videoIn) getVideo(videoInfo, videoIn); return 0; } //end of main //Function to get one movie's information void getVideo (Video& oneVideo, ifstream & videoIn) { char endOfLine; //get the video information getline(videoIn, oneVideo.movieTitle); videoIn >> oneVideo.numberCopies; videoIn.get(endOfLine); getline(videoIn, oneVideo.videoType); }
YES!!! Note the following function that swaps two structs of type Video.
void swap (Video& one, Video& two) { Video temp; temp = one; one = two; two = temp; }
A more common application of structs occurs when multiple structs of the same type are needed in a program. Suppose that we need to write a program that performs manipulations on as many as 100 videos. The data structure that would be needed for this application is an array of structs.
Remember that when working with structs, one of the first things that is accomplished is the creation of a struct type. This type is used to declare an array of this new type. For example, the following code defines a variable called allVideos that will store information for as many as 100 videos.
const unsigned int MAX_VIDEOS = 100; Video allVideos[MAX_VIDEOS];
Data included in an array of structs is accessed by denoting the following in the order specified:
For example, the following:
cout << allVideos[0].movieTitle << endl;
Suppose that we need a function called findAndPrint that will search the allVideos array for a particular type of video and will print out all videos included in the list that have this type. The function should receive the allVideos array so that it can be searched and secondly, the function should receive the type of videos that it is searching for. The function header definition will appear as:
and we can call this function using the following statement:void findAndPrint (Video shows[], string showType)
findAndPrint(allVideos, "comedy");
You are to submit the source program listing of the modified code,
compilation results, and a sample run of the program.
Something like the following UNIX commands will let you create what is required:
(Be sure to properly exit the script session!)
$ script lab15ex9.log
$ pr -n -t -e4 cla15.cpp
$ c++ cla15.cpp -o cla15
$ cla15
$ exit
$ handin lab15log lab15ex9.log