One of the neat things that computer programs can be used for is simulations of real-world things like you're doing here. Instead of creating an expensive and/or dangerous prototype of something new, you can first create it on the computer to model the behavior of the real thing. You can then decide what changes should be made before actually building it.
#ifndef some variable you make up goes here #define same variable as above class car { public: // function prototypes go here private: // variables related to car go here }; // don't forget the semicolon! #endifThe header file contains all the function prototypes for your class (in the public section) and all the variables that pertain to your car (in the private section). Be sure to understand what the #ifndef, #define, and #endif commands are for (review p. 486 in your text).
The private variables you need to define are:
Now for the functions. Their prototypes will go in the public section while their definitions will go in the second file: the class itself. We'll talk about them next.
car::car() { // default constructor. Remember, the constructor's job // is simply to initialize the private variables. The // six variables in the car class should be // initialized as follows: // - the owner will be the generic name "default" // - put the gear in park // - distance traveled will be zero // - the car will get 20 miles per gallon // - the gas tank will hold 10 gallons max // - the car will begin with a full tank of gas } car::car(int MPG, int capacity, string newOwner) { // non-default constructor. The given parameters // will be used to initialize the miles-per-gallon // variable, the capacity of the gas tank, and // the owner's name, respectively. The other // private variables should be initialized in the // same way that the default constructor dictates } void car::go(int miles) { // this moves the car the number of miles specified // in the parameter. It moves the car forward or // backward depending upon what gear the car is in. // A car will not move if its gear is in park. // Remember, the total forward distance traveled will // decrease if the car is moving backwards. // Here is the algorithm for what this function does: // // if the car is in park // car can't move; display an error message // else // { // compute amount of gas used // if car has enough gas to proceed // { // Move car forward or backward the appropriate amount and // decrease the amount of gas by the appropriate amount. // Note that if a car goes backward beyond its starting // point, the total distance traveled will become negative. // } // else car does not have enough gas to proceed // { // compute distance that car can move with the gas remaining // move car forward or backward by that amount and // set gas amount to zero // } // } // // In all cases, the user should be informed as to how far // the car moved, in what direction, and the amount of gas used } void car::fillTank() { // fills gas tank up to capacity } void car::putinGas(int fuelAmount) { // Fills gas tank with given amt of gas. // If the tank goes over capacity, the // current amount of gas will be equal // to the tank capacity (don't overflow) } double car::getGas() const { // returns the current amt of gas car has } int car::getCapacity() const { // returns fuel tank capacity } int car::getDistance() const { // returns forward distance car has traveled } int car::getMPG() const { // returns miles per gallon rating on car } string car::getOwner() const { // returns owner of car } string car::getGear() const { // returns current gear as a string // i.e. "forward", "reverse", or "park" } void car::setGear(direction newGear) { // sets gear to park, forward, or reverse } void car::showDashboard() const { // print all stats for the car to cout. // Displays the owner, gas tank capacity, // amount of gas remaining, forward distance // traveled, miles per gallon, and current gear // the car is in }
Remember the steps you take in Visual C++ when compiling a project with multiple source code files:
Classes are powerful constructs in computer programming, but they can take some time to get used to. Start this project early. If you wait until the last minute, you may not get it all done.
This is an individual project. I encourage you to talk to others about the general nature of the project and ideas about how to pursue it. However, the technical work, the writing, and the inspiration behind these must be substantially your own. If any person besides you contributes in any way to the project, you must credit their work in your report. Similarly, if you include information that you have gleaned from other published sources, you must cite them as references. Looking at, and/or copying, other people's programs or written work is inappropriate, and will be considered cheating.