CSCE 2004 - Laboratory Assignment 11

    The objective of this lab is to introduce students to object oriented programming. We will start by using the string class, which is included in almost all C++ compilers. Then we will learn how to define and use our own classes using a simple temperature class. This lab has the following steps.

  1. Object Oriented Programming
  2. There are two widely used programming methods today: procedural programming and object oriented programming. In a procedural program, you typically have data stored in variables and arrays, and a collection of functions that perform operations on this data. The data and functions are separate, and we can mix and match them any way we wish. This is what we have been doing in class all semester.

    In object oriented programming, the goal is to combine data and operations that belong together to create "objects". By doing this, we can use the syntax of the programming language to tie the data and operations to each other, and to "hide" the representation of the data and the implementation of the operations from users of these objects. This reduces confusion caused by too much mixing and matching, and makes it easier for programmers to write larger and more complex programs.

    In C++, we use "classes" to define the data and operations go together in objects. The data contained in an object are known as the object's "attributes", and the functions that implement operations are known as "methods" or "member functions". Any class has two main sections: (1) interface, (2) implementation. All of the information we need to use a class is contained in the interface, and all of the code needed to define the operations is in the implementation. This separation allows programmers to use methods (member functions) without worrying about the implementation of these methods.

    For example, you can create a class called "Currency" that, given the amount of currency and the type of that currency will convert it to any other currency by only using the "Convert()" method. Programmers who use this method do not need to worry about the implementation of that method, all they need to know is how to call it. Classes are ways to allow individual programmers the ability to extend the programming language with new data types that they create.

  3. Using String Objects
  4. The string class is part of most C++ implementions. We can use this class by adding "#include " at the top of our program. Consider the following program:

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
       string phrase = "The quick brown fox jumps over the lazy dog.";
       string copy(phrase);
       string part(phrase, 10, 9); 
       cout << "phrase = " << phrase << endl;
       cout << "length = " << phrase.length() << endl;

    The first three lines of code in main declare and initialize three string objects called "phrase", "copy" and "part". The next two lines print phrase and its length using the length() member function.

    Copy the code above into a file called "string.cpp" and add some lines of code to print out "copy" and "part". Then compile the program using "g++ -o string.exe string.cpp". Now run the program. Can you deduce what the parameters mean in the definition of the string "part"?

    Copy and paste your program output below.

  5. Using String Methods
  6. There are dozens of methods (member functions) in the string class. In this section, we will be using the following methods to manipulate strings. For the full list, see table 10-7 on p582 in your textbook.

    Step 1: Add the following lines of code to your program from above. Then add some code to print out the variables part and copy again. Before you run the program, read the descriptions of methods above to see if you can figure out what the code does. Now run the program. Does it do what you expected?

    int pos = copy.find(part, 0);
    copy.erase(pos, part.length() + 1);
    copy.insert(pos, "sleeping ");

    Step 2: Extend your program to use the string methods above to replace the word "quick" with the word "brave" in the string phrase. Then add code to print out the phrase variable. Now run the program. Does it work?

    Step 3: Modify your program to use the "append" method to append the copy string to the end of the phrase string. Then add code to print out the new value of phrase. When you compile and run the program you will see that there is no space between the two sentences. Can you figure out how to fix this using the string methods above? If so, add the code to your program and recompile.

    Step 4: Finally, add the following lines of code to the bottom of your program. Before you compile and run the program, read the descriptions of methods above to see if you can figure out what the code does. Now run the program. Does it do what you expected?

    for (int i = 0; i < copy.length(); i++)
       if ( == ' ')
       cout << "copy = " << copy << endl;
       cout << "length = " << copy.length() << endl;

    Copy and paste your final program below.

    Copy and paste your program output below.

  7. Defining and Using the Temp Class
  8. In this section, we will be creating and using our own class called "Temp". To do this, we need to create separate files for the interface and implementation, and a small main program that uses this class.

    Step 1: Copy and paste the class interface below into a file called "temp.h". By convention, all C++ class interface files are stored in files with names ending with a ".h". Notice that this class has four public methods (getCelsius, getFahrenheit, setCelsius, setFahrenheit) and one private attribute (CelsiusTemperature).

    // Interface for temperature class 
    class Temp 
       Temp(const Temp & temp);
       double getCelsius() const; 
       double getFahrenheit() const; 
       void setCelsius(double Temperature); 
       void setFahrenheit(double Temperature);
       double CelsiusTemperature;

    Step 2: Copy and paste the class implementation below into a file called "temp.cpp". By convention, all C++ class implementation files are stored in files with names ending with a ".cpp". Notice that each of these functions have a "Temp::" before the method name. This is to tell the C++ compiler which class they are part of.

    // Implementation of temperature class 
    #include "temp.h"
    using namespace std;
       CelsiusTemperature = 0; 
    Temp::Temp(const Temp & temp)
       CelsiusTemperature = temp.CelsiusTemperature; 
    // Accessor to get Celsius temperature
    double Temp::getCelsius() const
       return CelsiusTemperature; 
    // Accessor to get Fahrenheit temperature 
    double Temp::getFahrenheit() const
       return 9.0 * CelsiusTemperature / 5.0 + 32.0;
    // Mutator to set Celsius temperature 
    void Temp::setCelsius(double Temperature)
       CelsiusTemperature = Temperature;
    // Mutator to set Fahrenheit temperature 
    void Temp::setFahrenheit(double Temperature)
       CelsiusTemperature = (Temperature - 32.0) * 5.0 / 9.0 ;

    Step 3: Copy and paste the code below into a file called "main.cpp". Notice that this file is including "temp.h". This means that we can use the public methods of the temperature class Temp in our main program. We are NOT allowed to access the private attribute CelsiusTemperature, only the methods of the Temp class are allowed to change this variable.

    #include <iostream>
    #include "temp.h"
    using namespace std;
    int main()
       Temp one;
       Temp two;
       cout << one.getFahrenheit() << endl;
       cout << two.getFahrenheit() << endl;
       return 0;

    Step 4: To compile this program, type in "g++ temp.cpp main.cpp -o main.exe". This will combine the implementation of the temperature class with your main program to create main.exe. Now run the program to see what it does.

    Step 5: Now extend the main.cpp above to define three new objects called "hot", "cold" and "just_right" and set these to appropriate values using the "setFahrenheit" method, and then print out both their Celsius and Fahrenheit values using the corresponding get methods. Hopefully you will find that using the temperature class is almost as easy as using the string class.

    Copy and paste your new main.cpp program below.

  9. Extending the Temp Class
  10. In general, it is helpful to have a print method in every class so users can more easily output the values stored in the object. To do this we must make changes to both the interface and implementation of the class.

    Step 1: Edit the temp.h file to add a new public method called "print" with no parameters and no return value.

    Step 2: Edit the temp.cpp file to add the implementation of the "print" method. It would be nice for your method to print out the temperature in both Celsius and Fahrenheit. To get these values, you can call the getCelsius and getFahrenheit methods. Since you are inside the class definition, you do not need to use "value = object.method_name()", you can simply use "value = method_name()".

    Step 3: Your final step is to call the new "print" method from within your main.cpp program. Use your new method to print out the values of "one", "two", "hot", "cold" and "just_right". Now compile your program using "g++ temp.cpp main.cpp" and run your program again. With any luck, you should see the F and C values for all five of these objects.

    Copy and paste your modified temp.cpp below.

    Copy and paste your modified main.cpp program below.

    Finally, copy and paste your program output below.

  11. Submit Work
  12. This lab assignment will be submitted electronically to the TAs once you fill in the fields below and click on the "submit" button. You do NOT need to print a copy of this document to hand in.

    Your UAID number:
    Your website PASSWORD: