EECS168 09:Lab6
| Navigation | 
|---|
| Home | 
| Information | 
| Classwork | 
Objectives
- Understand passing variable by Reference
 - Understand Function Overloading
 - Understand Automatic Type Casting
 
In this lab you will extend the concepts of passing by reference and function overloading that you have begun to learn in previous assignments, and add to that automatic type casting.
A Simple Function
Writing Functions in C++
In previous labs you began writing your own functions (programmer-defined functions) and making function calls to them. This requires that you declare the function first. It is common to use a function prototype above your main() function. This prototype is not the entire function, it is just the function header, which includes the name of the function, its return type, and the types of its arguments. Here is a sample function header:
double monthly_tax(double salary);
Later in your code, after main(), you can write the function definition, which tells the compiler how the function actually works. Here's a function definition that works with the declaration shown above:
double monthly_tax(double salary)
{
    const double TAX_RATE = 0.07;
    return (TAX_RATE*salary);
}
Functional Programming
When using functions, main() becomes the driver of the program. This means that it it usually contains very little code; instead, it primarily makes calls to functions which handle the work. The scenario for this lab is: you want to buy a pizza and the pizzeria offers the same price for a round pizza, a rectangular pizza, and a sheet pizza (similar to a sheet cake). The sheet pizza has fixed the dimensions of a 18x24 rectangle, but is available in three portions, Full, Half, or Quarter. You want to calculate the unit price of each shape pizza to see which one has a lower unit price. You will write a program which asks the user for radius of a circle, the length and width of a rectangle, and the portion of a sheet being offered. It will then determine which one has a lowest unit price.
Passing Variables by Reference
Sometimes retuning a single value from a function is not sufficient. One way to have a function produce multiple values is to use a method called pass by reference. This passes the function a pointer to a variable created outside the scope of that function, instead of passing in a copy of the current value of a variable.
For this program you will need to write a function to get multiple input values from the user, and assign those values to variable passed to the function by reference.
- Function name: get_input
- Parameters: radius, width, height, portion, and price
 - Return value: void
 
 
Creating Overloaded Functions
You will be writing the following overloaded functions:
- Function name: area
- Parameter: radius of a circle
 - Return value: area of the circle
 
 - Function name: area
- Parameters: the width and height of a rectangle
 - Return value: area of the rectangle
 
 - Function name: area
- Parameter: character indicating whole, half, or quarter sheet
 - Return value: area of sheet pizza selected
 
 
Please note that function overloading allows us to use the same name for the three functions that calculate the areas.
- Function name: unit_price
- Parameters: price of a pizza, area of the pizza
 - Return value: the unit price of the pizza, i.e. the price divided by the area
 
 
Copy the following code to a file named cheaperPizza.cpp, and follow the instructions given as comments to complete the program. You can use the area functions used in Lab 5 as examples for defining the new functions in this code. The function prototypes have been provided for you in the code.
Sample Program Operation
./cheaperPizza 
What is the radius of the round pizza? 9
What is the width of the rectangular pizza? 6
What is the height of the rectangular pizza? 6
What is the portion of the sheet pizza? (Enter F for full, H for half, or Q for quarter) H
What is the price of the pizza? 10
The pizza unit prices are:
Round - $0.04
Rectangle - $0.28
Sheet - $0.05
The round pizza is the cheapest!
Program Code
The function prototypes (declarations) for each of the function you need to create are already in the code and may not be changed in any way.
/* -----------------------------------------------------------------------------
 *
 * Author: Ima Student
 * Date: February 23, 2009
 * EECS-168 Lab 6 assignment.  This program will tell the user which of two 
 * pizza's is cheaper, based on their dimensions and price.
 *
 ---------------------------------------------------------------------------- */
#include <iostream>
using namespace std;
const double PI = 3.14159;
// FUNCTION DECLARATION for getting input from the user
void get_input(double& radius, double& width, double& height, char& portion, double& price);
// FUNCTION DECLARATION for calculating area of a circle
double area(double radius);
// FUNCTION DECLARATION for calculating the area of a rectangle
double area(double width, double height);
// FUNCTION DECLARATION for calculating the area of a sheet
double area(char portion);
// FUNCTION DECLARATION for calculating the unit price
double unit_price(double price, double area);
int main()
{
    //variable declarations
    //radius of a circle;
    double radius;
    //dimensions of a rectangle
    double width, height;
	//portion of a sheet pizza
	char portion;
    //price of a pizza
    double price; 
    //Function Calls
    
    // Make the required function calls to get user input
    // Make function calls to calculate areas and unit prices
    // Print out the unit prices for the user
    // The program should calculate and tell the user which pizza is cheaper to buy
    // Uncomment the following lines when you get to the section titled "Automatic Type Casting"
    //int favorite_radius = 12;
    //double favorite_area = area(favorite_radius);
    
    return 0;
}
// FUNCTION DEFINITION for user input
// FUNCTION DEFINITION for circle area 
// FUNCTION DEFINITION for rectangle area
// FUNCTION DEFINITION for sheet area
// FUNCTION DEFINITION for unit price
Automatic Type Casting
C++ (and some other languages) can automatically cast a variable as a different type as needed for a function call, however there are some cases where this will not work. Uncomment the two lines near the end of the main function, then recompile. This will result in a compiler error. Create a file called auto_cast.txt and explain why you get an error and how to fix it (you may not change the declaration of favorite_radius as an int, and you must pass favorite_radius to the area function). Also remember fix the code so that it compiles and runs without errors before submitting.
Lab Submission
- cheaperPizza.cpp
 - auto_cast.txt
 
The submission procedure is the same as was described in previous labs, except that:
- Your tarball and the directory inside it should be named like Smith-1234567-Lab-06
 - The email subject should be "[EECS 168] Lab 06" (without the quotes).
 
Remember to include your full name, student number, and disk usage in the email.