I am trying to overload some operators, using objects. The program compiles fine, but crashes when run, right after getting to the overloading part. Here is the meat of what I am using:
(in driver)
(cab_9999 + cab_flightNum); //second object is current object
(in header file/implementation file after definitions)
(in functions of implementation file)
const Cabin operator +( const Cabin& allFlight, const Cabin& thisFlight)
{
(print test message)
}
Just doing this crashes it. I eventually need to be able to get one int from thisFlight and add it to an allFlight int, but I can't get this to even work, so I am stuck. Any thoughts on either? I appreciate the help. -Scott
Thanks for the idea. I tried that, and it did not change anything. I am not sure how to post the nice version of code most people post, but here is a text version of my driver, minus some simple text to the user.
char exitWord; //Used to exit program if user chooses to
int flightNum; //flightnum variable for the user to input the flight number
int pass_count; // passenger count for the flight.
//Initialize the overall cabin, flight 9999
Cabin cab_9999(9999);
int main()
{
//User Greeting
//Begin do-while loop for adding flights
do
{
//Ask the user for the flight number
cin >> flightNum;
//Create a Cabin object, and pass it the flight number
Cabin cab_flightNum(flightNum);
// initialize the pass_count
pass_count = 1;
//Increment the number of flights(STATIC FUNCTION)
Cabin::incFlights();
//Begin Do-While for each Cabin
do
{
cab_flightNum.showCabin();
cab_flightNum.reserveSeat(pass_count);
// Increment the number of passengers for the flight
pass_count++;
//Show the cabin again
cab_flightNum.showCabin();
//Show the number of passengers on the plane
cab_flightNum.getNumPass();
//Ask the user if they are done adding passengers to the flight
cin >> exitWord;
//End do-While loop for Cabin/flight
}while (exitWord != 'n');
//Now calculate and display forward and aft cabin weight
cab_flightNum.getForAftWt();
//Now add passengers from this flight to the master flight.
(cab_9999 + cab_flightNum);
//Ask user if they are done with program
cin >> exitWord;
cout << "\n";
if ( exitWord == 'y')
{
cout << "There were " << cab_flightNum.showFlights()
<< " flights scheduled today.\n\n";
}
//End do-While loop for program
} while ( exitWord != 'y');
system("PAUSE"); // Keeps windows screen up
return 0;
}
Here is the .h/implementation file (all in one) with only the initialization, and overloaded function.
const int rnum = 12; //Represents the number of rows in the aircraft
const int snum = 3; //Represents the number of seats per row
const int max_Weight = 6120; //Maximum Aircraft weight limit for passengers
//Class construction
class Cabin
{
public:
Cabin(int); //Constructor (User adds the flight num)
Cabin(); //Default Constructor
int cabArray[rnum][snum]; //Array for the cabin(Initialized here)
string dLocation; //String for departure location
string aLocation; //String for arrival location
void fillArray(); //Fills/Initilizes array
void showCabin(); //Prints array/cabin view to screen
void reserveSeat(int); //Fill individual array block/seat
void getACWeight(); //Gets total Aircraft passenger weight
void getForAftWt(); //Finds the weight of the forward and aft
//sections of the plane for balance.
void swapSeats(); //Allow user to swap pass data from seat
//to seat
void getNumPass(); //Gets number of passengers
int numPass; //Number of passengers
//Static integer to count the number of flights (Cabins created)
static int flt_Count;
//Static function to increment the number of flights
static int incFlights(){flt_Count++;}
//Member funtion to show the number of flights
int showFlights(){return flt_Count;}
private:
int flightNumber; //Holds the flight number
};
//Fill/initilize the array (Runs the fillArray function)
fillArray();
}
//************************************************************************
void Cabin::getNumPass()
{
int fnum; //Counter for the row loop
int fsnum; //Counter for the seat loop
int numPass=0; //Number of passengers
//Loop through the array
for (int fnum = 0; fnum < rnum; fnum++)
{
for (fsnum = 0; fsnum < snum; fsnum++)
{
//Check for weight, and add a passenger
if (cabArray[fnum][fsnum] > 0)
{
numPass++;
}
}
}
//Print the number of passengers
cout << "\nThere are " << numPass << " passengers now on flight "
<< flightNumber << "\n\n";
}
File Name: Driver_7.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include "Cabin.h" // Builds Cabin class
usingnamespace std; // Namespaces
char exitWord; //Used to exit program if user chooses to
int flightNum; //flightnum variable for the user to input the flight number
int pass_count; // passenger count for the flight.
//Initialize the overall cabin, flight 9999
Cabin cab_9999(9999);
int main()
{
//User Greeting
//Begin do-while loop for adding flights
do
{
//Ask the user for the flight number
cin >> flightNum;
//Create a Cabin object, and pass it the flight number
Cabin cab_flightNum(flightNum);
// initialize the pass_count
pass_count = 1;
//Increment the number of flights(STATIC FUNCTION)
Cabin::incFlights();
//Begin Do-While for each Cabin
do
{
cab_flightNum.showCabin();
cab_flightNum.reserveSeat(pass_count);
// Increment the number of passengers for the flight
pass_count++;
//Show the cabin again
cab_flightNum.showCabin();
//Show the number of passengers on the plane
cab_flightNum.getNumPass();
//Ask the user if they are done adding passengers to the flight
cin >> exitWord;
//End do-While loop for Cabin/flight
}while (exitWord != 'n');
//Now calculate and display forward and aft cabin weight
cab_flightNum.getForAftWt();
//Now add passengers from this flight to the master flight.
(cab_9999 + cab_flightNum);
//Ask user if they are done with program
cin >> exitWord;
cout << "\n";
if ( exitWord == 'y')
{
cout << "There were " << cab_flightNum.showFlights()
<< " flights scheduled today.\n\n";
}
//End do-While loop for program
} while ( exitWord != 'y');
system("PAUSE"); // Keeps windows screen up
return 0;
}
Here is the .h/implementation file (all in one) with only the initialization, and overloaded function.
File Name: Cabin.h
#include <iostream> // Preprocessor Directives
#include <iomanip>
#include <cmath> // Math functions
usingnamespace std; // Namespaces
constint rnum = 12; //Represents the number of rows in the aircraft
constint snum = 3; //Represents the number of seats per row
constint max_Weight = 6120; //Maximum Aircraft weight limit for passengers
//Class construction
class Cabin
{
public:
Cabin(int); //Constructor (User adds the flight num)
Cabin(); //Default Constructor
int cabArray[rnum][snum]; //Array for the cabin(Initialized here)
string dLocation; //String for departure location
string aLocation; //String for arrival location
void fillArray(); //Fills/Initilizes array
void showCabin(); //Prints array/cabin view to screen
void reserveSeat(int); //Fill individual array block/seat
void getACWeight(); //Gets total Aircraft passenger weight
void getForAftWt(); //Finds the weight of the forward and aft
//sections of the plane for balance.
void swapSeats(); //Allow user to swap pass data from seat
//to seat
void getNumPass(); //Gets number of passengers
int numPass; //Number of passengers
//Static integer to count the number of flights (Cabins created)
staticint flt_Count;
//Static function to increment the number of flights
staticint incFlights(){flt_Count++;}
//Member funtion to show the number of flights
int showFlights(){return flt_Count;}
private:
int flightNumber; //Holds the flight number
};
//const Cabin operator +(const Cabin& allFlight, const Cabin& thisFlight);
Cabin operator +(const Cabin& allFlight, const Cabin& thisFlight);
Cabin::Cabin(int flightNum) //Class Constructor
{
//initialize the flightNumber variable
flightNumber = flightNum;
//Fill/initilize the array (Runs the fillArray function)
fillArray();
}
//************************************************************************
void Cabin::getNumPass()
{
int fnum; //Counter for the row loop
int fsnum; //Counter for the seat loop
int numPass=0; //Number of passengers
//Loop through the array
for (int fnum = 0; fnum < rnum; fnum++)
{
for (fsnum = 0; fsnum < snum; fsnum++)
{
//Check for weight, and add a passenger
if (cabArray[fnum][fsnum] > 0)
{
numPass++;
}
}
}
//Print the number of passengers
cout << "\nThere are " << numPass << " passengers now on flight "
<< flightNumber << "\n\n";
}
//************************************************************************
//const Cabin operator +( const Cabin& allFlight, const Cabin& thisFlight)
Cabin operator +( const Cabin& allFlight, const Cabin& thisFlight)
{
//int my_flight;
//my_flight = (allFlight.numPass + thisFlight.numPass);
//allFlight.numPass = my_flight;
cout << "Test message";
// cout << " I got " << my_flight << " total passengers...\n";
//allFlight.numPass = (allFlight.numPass + thisFlight.numPass);
}
I think that should be the relevant code. Again, sorry for the length. Thanks for the help!