



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS 220 -- Lab 7 Intro to OOP – Another Method of Defining Your Own Abstract Data Type Before beginning today’s lab, copy the files needed using the command cp -R ~ernie/220/lab. As usual, include links to the sources, scripts, and answers to questions. We have seen that structs allow us to create new data types that allow us to more closely model real-world items (like divers, students, etc.). The concept of a struct is closely related to that of a class in object-oriented programming. In today’s lab, you will explore the use of objects and classes for creating a C++ program. Object-Oriented Programming Object-oriented programming is a method for organizing the design of a computer program. Up to this point, we have been using a design technique called functional programming (sometimes called procedural programming ). In a program using functional program design, the program is logically divided into a series of functions. Each function accomplishes part of the overall goal of the program. Object-oriented program design divides a program into a series of objects****. Each object has associated data and operations. By using the objects, we accomplish the overall goal of the program. Object-oriented design is very popular because it allows the computer programmer to model real-world objects more closely (by specifying all of the data associated with a particular “thing” as well as the behavior of the “thing”). By allowing us to group behavior for a particular “thing” along with data of the “thing”, we get a complete picture. There are two groups of files in the lab7 directory. One that finds scores for a diving team ( diver.h, diver.cpp, mainpgm.cpp ) and one that uses an object-oriented model of a time ( time.h, time.cpp, testtime.cpp ). Let’s look at the diving program first. This program is similar in functionality to the programs written in previous labs and allows you to enter information about members of a diving team, calculate their average scores and print the results. Open the file diver.h. This file contains part of an object-oriented implementation of a diver. When we think about an actual diver in the real world, we associate pieces of data with the diver. Some things that we might associate with a particular diver are the diver’s name, the scores the diver earned in their last competition, etc. We have seen that we can create a struct to group this related data together in a program. However a struct only allows us to model attributes (data) associated with the diver. We cannot use a struct to logically associate behavior (functions) with a diver. Thus by using object-oriented programming techniques, we can more completely model real-world objects by associating the appropriate characteristics and behavior with an object in the program. This type of modeling was not possible with functional programming design. In object-oriented programming, the programming mechanism that provides the specification for the data and functions associated with a particular object (thing) is called a class. The file that we are currently examining gives the class definition for a new type called DiverType. Notice that the class definition is very similar to a struct definition. The class definition specifies the data that should be logically associated with a class (just as a struct would). The main difference between a struct definition and a class definition is that functions can be included in a class definition, but are not included in a struct definition. Notice the function
prototypes included in the diver.h file. These prototypes show the functions that should be logically grouped with a diver. In object-oriented programming, the data items associated with a class are called member data and the functions are called member functions. The word “member” emphasizes that these things are key concepts that are PART of the class (new type). Parts of the program probably look odd to you at this point because you are not used to using object-oriented design. In spite of your unfamiliarity, you should be able to identify the key components of the DiverType class. Question 1: What member data is associated with the DiverType class? Question 2: What member functions are associated with the DiverType class? Remember that when we utilize functions, we are required to implement functions in addition to providing prototypes. Notice that in the diver.h file, the class DiverType contains no implementations. Object-oriented design still requires the use of function implementations, however the implementations usually appear in a separate file called the implementation file. Open the file diver.cpp to view the implementation file for the DiverType class_._ As you might guess from the name, the implementation file contains the implementations for the functions associated with the class. [The diver.h file is called the specification file or header file. The header file contains all the information about the data and the functions associated with a class, but does not provide any implementation.] The functions associated with the class will operate just like non-object-oriented functions that we’ve seen earlier in the semester. You can probably tell what each one does just by looking at it. The main difference between calling a non-object-oriented function and an object-oriented function is that when you call a member function, you need to specify what object you would like to call the function with. Notice that in each method, at least one of the data member is accessed. This is because member functions provide a communication mechanism with the class for things outside the class. The main program’s only method of communication with the private data members is through the public member functions. You may have noticed that there is no main() function anywhere in the diver.h file or the diver.cpp file. We know that a C++ program HAS to have a function called main(), but where is it? It’s in yet another file (we’ll talk about why later). Open the file mainpgm.cpp. This file declares variables of type DiverType and uses the functions in the DiverType class. First let’s explore how to declare variables of a type that is defined in a class. You’ll probably notice that declaring something that has a type defined by a class is not any different than declaring a variable of any other type. When we declare a variable to be of a type that is a class, we say that the variable is an instance or object of the class. (This is where the term “object-oriented programming” comes from. It is programming that allows us to create objects of new types.) Each time that an instance of a class is declared, space is allocated for each of the variables associated with this instance of the class. The terms class and object are often used incorrectly because people mistakenly believe that they mean the same thing. Some people find it helpful to think the difference using the following analogy. Think of a class as a rubber stamp. The information stored on the stamp (design of the stamp) determines what the imprint will look like if we place ink on the stamp and press it on paper. In the same way, the class file
DiverType. To create a second diver, we only need to add another variable declaration to the main() function. Add the statements to the main program to declare a second variable of type DiverType. Then add the statements necessary to enter the data for the second diver. Question 3: Draw a picture/diagram of the two variables declared to be of type DiverType. Show the data values stored in each instance. Now add the statements to find the average score for the second diver and print the resulting average for that diver. Another OOP Program The lab directory also contains several files related to the time example seen in lecture. You should see a file called time.h. This is the specification file for the TimeType class. There is also a file called time.cpp ; this is the implementation file for our class. There is one final file called testtime.cpp that will allow us to test the functionality of our class by declaring variables of type TimeType. Open the file time.h. Add header comments to this file and comment each function and data member. Question 4: What data members are associated with the TimeType class? Notice that there is a reserved word private associated with these data values. This reserved word indicates that others will not have access to the variables from outside the class. This helps control access to your data and is a very important concept (called information hiding ) in OOP. Question 5: What member functions are associated with the TimeType class? Close the specification file and open the implementation file. The implementation file contains the function code for the member functions of the TimeType class. Notice that each function heading looks slightly different from other function headings that we’ve seen this semester. The name of the function is preceded by the class name ( TypeType ) and two colons “::”. This notation tells the compiler that the functions are defined as part of the class called TimeType. This format will be used for all functions associated with a class. Again the style for the implementation file is not correct; add the required comments to each function within the file. We’ve now looked at all the major aspects of the TimeType class. Remember that a class is just a way of defining a new type (including data and operations defined on the data). Close the file time.cpp and open the third file testtime.cpp. This file declares variables of type TimeType and uses them in a program. Notice that the testtime.cpp file includes the “ time.h ” file. This allows us to have access to the specification of the TimeType class from within the testtime.cpp file. Because the specification file for TimeType included the implementation file for the TimeType class, the testtime.cpp file is given a full picture (both specification and implementation) of the TimeType class. The first two lines in main declare two instances of the class TimeType. Remember that an instance is a variable that has a class as a type. After the instances are declared, the set() function is called.
Question 6: Draw a picture/diagram of the 2 instances of the class TimeType declared in main(). Show the data that is stored in each instance after the first 2 calls to set(). On the next line of code, you should see firstTime.Write( ); This is a function call to a member function with the instance firstTime of type TimeType. Remember that we said function calls to member functions would look a little bit different than function calls to non-object-oriented functions. When you call a member function of a class, you need to specify WHICH instance to make the function call with. This ensures that the proper data will be used in the function operations. To call a member function, the format InstanceName.MemberFunctionName( parameter list ); is always used. Notice that this format is similar to the format that we used with structs to access individual data fields of a variable declared to be of a type that is a struct. The format is called “dot” notation. Scan through the rest of the function calls. Note that return values and parameter lists work just like they did with non-OOP. Adding to the code Add code to the main program that declares a third instance of the TimeType class and assigns a value to that time. (You can pick any name for your instance and any values). Write the appropriate code to determine whether your new instance has a time less than the time store in firstTime. (HINT: look at the functions associated with the class.) Print out a message in main saying whether the new time is less than the firstTime or not. Compile and test your program. When you are sure that your code is working correctly, add a new member function to the TimeType class called FallBack(). This function should be called the day that daylight savings time ends and will decrease the current time by 1 hour. While writing your function, think about the following issues: Which files need to be changed in order to add the new function? Does the new function require any data to be passed in as parameters (or is all of the required data associated with the class itself)? How would you call the new function? After you have written your FallBack() function, alter the main() program to call the function to test it. Remember that the testtime.cpp file doesn’t accomplish anything logical