Hello! I am having issues printing the correct things. My teacher asked me to create a class that describes a passenger plane's properties and allows the plane to fly, land, and board and disembark passengers. To write the correct instructions, "In this project, you will write a class named Plane that has the following properties, each represented by an instance variable. All instance variables should be private.
inflight – a boolean variable indicating whether the plane is currently in flight
passengers – an unsigned int for the number of passengers currently onboard plane
capacity – an unsigned int for the maximum number of passengers the plane can hold
description - a string with the name of the current owner / operator of the plane and its model (for example, Air Canada 747).
In addition, the class should have the following member functions. All functions should be public.
Plane(int, string). The constructor should accept the plane's passenger capacity and the description of the plane.
The default value for the capacity should be 10
The default value for the description should be "(none)"
It should also initialize the inflight variable to false and the number of passengers to 0. Make the constructor an in-line function.
Notice that because this function has two parameters which both have default values, this also serves as the default constructor.
Accessors. Appropriate "getter" functions should be supplied for each of the instance variables. For example, you should have a function named getPassengers which takes no parameters and returns the number of passengers currently onboard. Each accessor should be an in-line function.
Modifiers (or mutators). Supplier "setter" functions only for the description and capacity instance variables. For example, you should have a function named setCapacity which takes an unsigned int parameter used to set the maximum number of passengers allowed on the plane, and have a void return type. Each modifier should be an in-line function.
void fly(). This function takes no parameters and sets the value of the inflight instance variable to true. (It should print an error message if the plane is already flying.) Make this function definition external to the class definition.
void land(). This function takes no parameters and sets the value of the inflight instance variable to false. (It should print an error message if the plane is not flying.) Its definition should be external to the Plane class definition (not in-line).
void board(unsigned int). This function adds the number passed as a parameter to the number of passengers already onboard. It should not allow the number of passengers to exceed the capacity of the plane (if it does, print an error message and set the number of passengers to capacity). If the plane is inflight, then print an error message and do not update the number of passengers. Make this function external to the Plane class definition.
void disembark(unsigned int). This function subtracts the number passed as a parameter from the number of passengers already on board. If the resulting number of passengers would be a negative number, then set passengers to 0 (this is not an error). If the plane is inflight, then print an error message do not update the number of passengers. Make this function definition external to the class definition.
void show(). This function displays all properties of the plane, for example, if a Airbus 320 out of France plane has a capacity of 162 passengers, and is currently in-flight with 139 passengers"
Sorry for posting the instructions, but on my previous post someone recommended me to post them with the post. After the instructions, the teacher asked me to "Demonstrate your class by writing a main function which does the following:
Create a United 737 plane with a capacity of 285 passengers (set the values using the constructor)
Board the first 20 passengers .
Board another 100 passengers.
Board another 121 passengers.
Take off for New York.
Use the show function to print the current state of the plane.
Land the plane.
Disembark all passengers (use disembark(9999), which should empty the plane).
Show the status of the plane again."
That is the stuff I tried to do in main. I know there is some issue with the variables, but I can't seem to find the issue. Thank you, anything would help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <iostream>
using namespace std;
class Plane;
void Plane(int capacity, string description);
void land();
void fly();
void board(unsigned int);
void disembark(unsigned int);
void show();
int getPassengers();
int setCapacity();
int main()
{
getPassengers();
setCapacity();
string description = "United 737";
int passengers = 20;
passengers += 100;
passengers += 121;
fly();
show();
land();
disembark(9999);
show();
}
class Plane {
private:
bool inflight;
unsigned int passengers;
unsigned int capacity;
string description;
public:
Plane(int capacity, string description)
{
bool inflight = false;
int passengers;
}
int getPassengers();
int setCapacity();
string setDescription();
void fly();
void land();
void board(unsigned int);
void disembark(unsigned int);
void show();
};
int getPassengers()
{
int passengers;
}
int setCapacity()
{
int capacity = 10;
}
string setDescription()
{
string description = "";
}
void fly()
{
bool inflight = true;
}
void land()
{
bool inflight = false;
}
void board(unsigned int)
{
bool inflight;
int passengers;
int capacity;
if (inflight = true)
{
cout << "ERROR" << endl;
}
else if (inflight = false)
{
if (passengers > capacity)
{
cout << "ERROR" << endl;
passengers = capacity;
}
}
}
void disembark(unsigned int)
{
int passengers;
if (passengers < 0)
{
cout << "ERROR" << endl;
}
else if (passengers == 9999)
{
passengers = 0;
}
else if (passengers >=0)
{
passengers -= passengers;
}
}
void show()
{
land();
fly();
bool inflight;
string description;
int capacity;
int passengers;
if (inflight = true)
{
cout << "The status of the first plane is: " << endl;
cout << "The " << description << " (cap. " << capacity << ") is in flight with " << passengers << " passengers." << endl;
}
else if (inflight = false)
{
cout << "After landing and disembarking, the status of the plane is: " << endl;
cout << "The " << description << " (cap. " << capacity << ") is in landed with no passengers." << endl;
}
}
|