CSCE 2004 - Laboratory Assignment 11

CSCE 2004 - Laboratory Assignment 11

The objective of this lab is to introduce you to object oriented programming. Specifically how to use objects of this class in a program and how to define and extend a simple class. This laboratory assignment has the following steps:

    What are classes:

    A class in C++ is a collection of variables and methods (functions). Any class has two main sections: (1) interface, (2) implementation. The reason behind programming using classes is to allow the programmers to use methods (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 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 withe new datatypes that they create.

  1. - Compiling with classes
  2. Before starting to work with classes, you need to know how to compile a program that is contained in multiple files. Download the three files:

    temp.h - the header file for a Temperature class
    temp.cpp - the implementation file for a Temperature class 
    usetemp.cpp - a program that uses the Temperature class
    to lab11 directory under labs. start compiling each file on its own by typing the following:
    (1) g++ -c temp.cpp  This command will create a file called temp.o. Verify using ls.
    (2) g++ -c usetemp.cpp  This command will create a file called usetemp.o. Verify using ls.
    (3) g++ temp.o usetemp.o -o lab11.exe This command will combine both files and it will create the executable file lab11.exe. Verify using ls.
    Now, run the program. Notice that nothing happens since the program itself does not do anything. Add a cout statement in usetemp.cpp to print out your name and then type g++ temp.o usetemp.o -o lab11.exe and then run the program. Did anything happen?

    Since you did not recompile the temp.o and usetemp.o after adding the cout statement, the new lab11.exe file will be identical to the old version (it is using old temp.o and usetemp.o). Now type g++ -c usetemp.cpp and then type g++ temp.o usetemp.o -o lab11.exe. Run lab11.exe did anything change? Your name should appear.

    Using makefiles

    Another way to work with multiple files is called makefile. Assuming that we have the previous files in one directory, the necessary make file will have the following:

    lab11.exe : temp.o usetemp.o
    	g++ temp.o usetemp.o -o lab11.exe
    temp.o: temp.cpp temp.h
    	g++ -c temp.cpp 
    usetemp.o: usetemp.cpp temp.h
    	g++ -c usetemp.cpp 
    	rm *.o lab11.exe 
    To understand each line in the makefile, please refer to the notes on the class where there is a link to a detailed page that describes the makefile structure. In summary, each pair of lines is of the format:
    Target: List of things it is built from (depends on)
    <TAB$gt;Compiler command (or any linux command) to execute
    When the make command is executed, it looks for a file called Makefile or makefile. It then looks at the timestamps on all Target files and compares the timestamp to the timestamps on the list of dependency files. If the Target is older than the dependencies, it executes the command on the next line.

    As you noticed before, if we have a program that is contained in multiple files we need to combine these files into one executable instance. But, if we introduce a change in one of the files, then we need to recompile that changed file before recreating the executable instance. To automatically incorporate any change we do into the executable file we build a mak file and then we run that makefile which will create the new executable file (lab11.exe in our example).

    Use the command make to create the lab11.exe file and then run the program. Type make again and notice what happens. Modify the usetemp.cpp file, and then run the make command, what happened? Now try the follwoing commands, after executing each of the commands run the make command and notice what happens:

    (1) touch temp.cpp.
    (2) touch usetemp.cpp
    (3) touch temp.h
    (4) make clean then make (N)TE: Clean has no dependencies so it always does its command.
    Explain the reason behind what you noticed in the textbox below. The touch command is a simple command that is used to update the file time stamp (the last time it has been modified in).

    Programmer Tip: It must be a tab not spaces at the beginning of the 2nd line (containing the linux command). If you use spaces instead, the makefile is invalid and make will not work.

  3. Defining and using objects
  4. Class 'Temperature'

    In order to create your own data types to solve a problem you must "describe" the data within the object and the "operations" that are allowed on this data. This process is called "defining instantiable classes".

    The following C++ code defines a class that contains "Temperature" objects. The instance variable contains a single temperature value in Celsius. The methods of this class let users "access"(read) or "mutate"(change) this value. temp.h

    //Class to implement Temperature object 
    class Temp 
    	double CelsiusTemperature;//instance variable 
    	double getCelsius(); 
    	double getFahrenheit(); 
    	void setCelsius(double Temperature); 
    	void setFahrenheit(double Temperature);

    Programmer Tip: The ; at the end of the .h file is required.

    #include <iostream>
    #include "temp.h"
    using namespace std;
      CelsiusTemperature = 0; 
    //Accessor to get Celsius temperature
    double Temp::getCelsius()
      return CelsiusTemperature; 
    //Accessor to get Fahrenheit temperature 
    double Temp::getFahrenheit()
      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 ;
    #include <iostream>
    #include "temp.h"
    using namespace std;
    int main()
    //Your code here...
    return 0;

    Try to create and use some instances of the Temperature class. In your main program (i.e. usetemp.cpp), create the first Temperature object and set the initial value 100.0 Celsius degree(oC). Similarly, create the second object with the initial temperature 32.0 Fahrenheit degree (oF). Now, convert the temperature of the first object from (oC) to (oF) and from (oF) to (oC) with the second one, print out the corresponding converted degrees in main program.

  5. Modifying Classes
  6. Adding new methods to the class Temperature: setKelvin and getKelvin

    Using the methods in Temperature as an example, extend this class definition to contain two new methods. The "setKelvin" method should take a temperature in Kelvin and save it appropriately. "getKelvin" method should return the temperature converted to Kelvin.(Remember C=K-273).

    Adding new methods to the class Temperature: Print

    In this step, take an input temperature (Celsius degree) from user, then initialize a Temperature object with this value. Write a method Print() in temp.cpp to print out the input Celsius degree and its corresponding Fahrenheit degree. You need also display messages "Lower than absolute zero;" if the input degree is less than -273; "Below freezing;" if it is less than 0 and "Above freezing;" if it is greater than 100. Then, call this method from the main program.

  7. Submit Your Work
  8. 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 full name:
    Your UAID number: