C++ Homework to be done
Review Questions and Problems
3;-.=cification: The Convert class performs conversions from miles to inches. "c need to convert 2.5 miles to inches. (Recall that there are 5,280 feet in a
12 inches in a foot.) _,-,de-cios t ream»
std;
inches;
Convert(){ miles = 0.0; inches = 0.0; } Convert(double m){miles = m; } double Miles2Inches();
jJle Convert::Miles2Inches()
inches = miles * 5208.0 * 12.0; return inches;
Convert MyMiles(2.5), MyOtherMiles; double Mylnches; Mylnches = MyOtherMiles.Miles2Inches(); cout « "\n Total inches are" « Mylnches; return 0;
Programming Problems Write complete C++ programs for the following problems.
29. Write a C++ program that sets up a class Sailboat containing private data for a boat's manufacturer and name, length, beam (width), and draught (water depth required to float the boat). The class has set functio s for all data as well as a Writelnto that reports all the Sailboat object data to the screen. It also has a > operator that compares Sailboat objects. One boat is greater than another if it is longer. If the boats are the same length, then the larger beamed boat is bigger.
Create a data file of boat data called "boats.txt" so that the first line is the total number of boats in the file; the next three lines are the manufacturer; followed by the name; and then the boat length, beam, and draught. For exam- ple, this file contains three boats:
3 McGregor 25 The Sparkly Lady 25 8 3.5
452 Chapter 7 • Classesand Objects
Vi ctori a Waylay 18 6 3 Hobie Lazy Days 18 8 1.0
Your mai n function should set up an array of five boat objects. Place boat objects in your "boats.txt" file. In main, call ReadFi 7e, which openy data file and fill your Sa i 7boat: array with the boat data from the file. It turns a true if it found the file and read s ccessfully, a false if it could noto the file. Report any file problem from main. If the array was filled, showt boat information in the order of the data file (unsorted) and then sort array by using a Bubb 1esort: function. Write the boat objects to the scree~ the order from smallest to largest boat.
30. In Chapter 5, Problem 32, (page 290) we wrote a program for the C++ Inte tional Airport parking garage. We are now going to rewrite the program.u a ParkingGarageAttendant class. (You may shorten the name of the clas PGA.) Please read the Chapter 5 problem. It describes the non-class appr to this situation. For this problem, our PGA class has two constructors, the fault constructor sets the parking rate to $2.00 per 30 minutes. The overloa constructor is passed the dollar and minute values. (For example, maybe parli: ing is more expensive on a holiday, such as $2.50 per 30 minutes.) The·PG_ class has a SetTimeIn function that is passed the integer hour and minutes parker arrived at the airport. There is also an AskTimeIn function, in wrnclj. class asks the user for the time he arrived at the airport in HR:MIN fog The Ca7cu7at:eFee function has a duall role. Before it calculates the parked and fee, it accesses the computer's system time for the hour minute-which is assumed to be the time out values. Ca 7cu7ateFee calls private Va7i dat:eTimes function that checks to see that the time in value~ reasonable (i.e., hour 0-23, minutes 0-59) and that the time in is before time out. The Va7idateTime sets a valid. flag accordingly. (The valid tlagis class member, true indicates good data, false, bad.) If the data is good, the' and fee are calculated and a true is returned. If the times are not valid;· Ca 7cu 7ateFee returns a false. The Ca 7CIl 7ateFee function uses referent return the fee, and total time parked in hours and minutes to the parker.
In main, declare one PGA object. Begin a "parking" loop that ask user to enter the arrival time at the airport in HR:MIN format. You may obtain this time in mai n and set it into the PGA object, or have the object the parker directly. Remember, write yOW' fee using a "S" and two digits of.<i. racy. Your PGA class contains cout statements only in the AskTi meIn fUrI$ti All other information including invalid times statements are written froITlnt~
31. Oh no! Not another Mortgage Calculator problem! Oh yes. Look at we've accomplished with this one problem. We wrote the entire ask, calc show results code in main in Chapter 2, Problem 46. In Chapter 4, Preble we broke the program tasks into functions. In Chapter 5, Problem 2,7; streamlined the data collection by have an Ask function that obtainecI. three inputs. Now we'll write a C++ Mortgage Calculator class.