-
November 17th, 2012, 10:34 PM
#1
[RESOLVED] Inheritance Problems
How come I can't get this program to run even though derived class Extended_queue inherited from the base class Queue?
header: Queue with Extended_queue
Code:
const int maxqueue = 10; //small value for testing
enum Error_code
{
success, fail, range_error, underflow, overflow,
fatal, not_present, duplicate_error,
entry_inserted, entry_found, internal_error
};
typedef char Queue_entry;
class Queue
{
public:
Queue();
bool empty() const;
Error_code serve();
Error_code append(const Queue_entry &item);
Error_code retrieve(Queue_entry &item) const;
protected: //use with extended classes
int count;
int front, rear;
Queue_entry entry[maxqueue];
};
Queue::Queue()
{
//Post: The Queue is initialized to be empty.
count = 0;
rear = maxqueue - 1;
front = 0;
}
bool Queue::empty() const
{
//Post: Return true if the Queue is empty, otherwise return false.
return count == 0;
}
Error_code Queue::append(const Queue_entry &item)
{
//Post: item is added to the rear of the Queue. If the Queue is full return an
// Error code of overflow and leave the Queue unchanged.
if (count >= maxqueue) return overflow;
count++;
rear = ((rear + 1) == maxqueue) ? 0: (rear + 1);
entry[rear] = item;
return success;
}
Error_code Queue::serve()
//Post: The front of the Queue is removed. If the Queue is empty return an Error_code of underflow.
{
if (count <= 0)
return underflow;
count--;
front = ((front + 1) == maxqueue) ? 0 : (front + 1);
return success;
}
Error_code Queue::retrieve(Queue_entry &item) const
//Post: The front of the Queue retrieved to the output parameter item. If the Queue
// is empty return an Error_code of underflow.
{
if (count <= 0) return underflow;
item = entry[front];
return success;
}
class Extended_queue : public Queue //Public to allow visibility between original and extended classes.
{
public:
bool full() const;
int size() const;
void clear();
Error_code serve_and_retrieve(Queue_entry &item);
};
int Extended_queue::size() const
//Post: Return the number of entries in the Extended_queue.
{
return count;
}
header: Runway
Code:
#include "Queue.h"
#include "Plane.h"
enum Runway_activity {idle, land, takeoff};
class Runway
{
public:
Runway(int limit);
Error_code can_land (const Plane ¤t);
Error_code can_depart (const Plane ¤t);
Runway_activity activity (int time, Plane &moving);
void shut_down(int time) const;
private:
Extended_queue landing; //uses Extended_queue class
Extended_queue takeoff; //uses Extended_queue class
int queue_limit;
int num_land_requests; // number of planes asking to land
int num_takeoff_requests; // nuumber of planes asking to take off
int num_landings; // num. of planes that have taken off
int num_takeoffs; // num. of planes that have taken off
int num_land_accepted; // num. of planes queued to land
int num_takeoff_accepted; // number of planes queued to take off
int num_land_refused; // number of landing planes refused
int num_takeoff_refused; // number of departing planes refused
int land_wait; // total time of planes waiting to land
int takeoff_wait; // total time of planes waiting to take off
int idle_time; // total time runway is idle
};
Runway::Runway(int limit)
{
//Post: The Runway data members are initialized to record no prior Runway use
// and to record the limit on queue sizes.
queue_limit = limit;
num_land_requests = num_takeoff_requests = 0;
num_landings = num_takeoffs = 0;
num_land_refused = num_takeoff_refused = 0;
num_land_accepted = num_takeoff_accepted = 0;
land_wait = takeoff_wait = idle_time = 0;
};
Error_code Runway::can_land(const Plane ¤t)
/* Post: If possible, the Plane current is added to the landing Queue; otherwise,
an Error_code of overflow is returned. The Runway statistics are updated.
Uses: class Extended_queue */
{
Error_code result;
if(takeoff.size() < queue_limit)
result = takeoff.append(current);
else
result = fail;
num_takeoff_requests++;
if (result != success)
num_land_refused++;
else
num_land_accepted++;
return result;
}
Error_code Runway::can_depart(const Plane ¤t)
/* Post: If possible, the Plane current is added to the takeoff Queue; otherwise, an Error_code of overflow
is returned. The Runway statistics are updated.
Uses: class Extended_queue */
{
Error_code result;
if(takeoff.size() < queue_limit)
result = takeoff.append(current);
else
result = fail;
num_takeoff_requests++;
if (result != success)
num_takeoff_refused++;
else
num_takeoff_accepted++;
return result;
}
Runway_activity Runway::activity(int time, Plane &moving)
/* Post: If the landing Queue has entries, its front Plane is copied to the parameter
moving and a result land is returned. Otherwise, if the takeoff Queue has
entries, its front Plane is copied to the parameter moving and a result
takeoff is returned. Otherwise, idle is returned. Runway statistics are
updated.
Uses: class Extended_queue. */
{
Runway_activity in_progress;
if(!landing.empty())
{
landing.retrieve(moving);
land_wait += time - moving.started();
num_landings++;
in_progress = land;
landing.serve();
}
else if (!takeoff.empty())
{
takeoff.retrieve(moving);
takeoff_wait += time - moving.started();
num_takeoffs++;
in_progress = takeoff;
takeoff.serve();
}
else
{
idle_time++;
in_progress = idle;
}
return in_progress;
}
void Runway::shut_down(int time) const
//Post: Runway usage statistics are summarized and printed.
{
cout << "Simulation has concluded after " << time << " time units." << endl
<< "Total number of planes processed "
<< (num_land_requests + num_takeoff_requests) << endl
<< "Total number of planes asking to land "
<< num_land_requests << endl
<< "Total number of planes asking to take off "
<< num_takeoff_requests << endl
<< "Total number of planes accepted for landing "
<< num_land_accepted << endl
<< "Total number of planes accepted for takeoff "
<< num_takeoff_accepted << endl
<< "Total number of planes refused for landing "
<< num_land_refused << endl
<< "Total number of planes refused for takeoff "
<< num_takeoff_refused << endl
<< "Total number of planes that landed "
<< num_landings << endl
<< "Total number of planes that took off "
<< num_takeoffs << endl
<< "Total number of planes left in landing queue "
<< landing.size( ) << endl
<< "Total number of planes left in takeoff queue "
<< takeoff.size( ) << endl;
cout << "Percentage of time runway idle "
<< 100.0 * ((float) idle_time)/((float) time) << "%" << endl;
cout << "Average wait in landing queue "
<< ((float) land_wait)/((float) num_landings) << " time units";
cout << endl << "Average wait in takeoff queue "
<< ((float) takeoff_wait)/((float) num_takeoffs)
<< " time units" << endl;
cout << "Average observed rate of planes wanting to land "
<< ((float) num_land_requests)/((float) time)
<< " per time unit" << endl;
cout << "Average observed rate of planes wanting to take off "
<< ((float) num_takeoff_requests)/((float) time)
<< " per time unit" << endl;
}
header: Plane
Code:
using namespace std;
enum Plane_status {null, arriving, departing};
class Plane
{
public:
Plane();
Plane (int flt, int time, Plane_status status);
void refuse() const;
void land(int time) const;
void fly(int time) const;
int started() const;
private:
int flt_num;
int clock_start;
Plane_status state;
};
Plane::Plane(int flt, int time, Plane_status status)
/* Post: The Plane data members flt_num, clock_start, and state are set to the
values of the parameters flt, time and status, respectively.
*/
{
flt_num = flt;
clock_start = time;
state = status;
cout << "Plane number " << flt << " ready to ";
if (status == arriving)
cout << "land." << endl;
else
cout << "take off." << endl;
}
Plane::Plane()
/* Post: The Plane data members flt_num, clock_start, state are set to illegal
default values.
*/
{
flt_num = -1;
clock_start = -1;
state = null;
}
void Plane:: refuse() const
//Post: Processes a Plane wanting to use Runway, when the Queue is full.
{
cout << "Plane number " << flt_num;
if (state == arriving)
cout << " directed to another airport" << endl;
else
cout << " told to try to takeoff again later" << endl;
}
void Plane::land(int time) const
//Post: Processes a Plane that is landing at the specified time.
{
int wait = time - clock_start;
cout << time << ": Plane number " << flt_num << " landed after "
<< wait << " time unit" << ((wait == 1) ? "" : "s")
<< " in the takeoff queue." << endl;
}
void Plane::fly(int time) const
{
int wait = time - clock_start;
cout << time << ": Plane number " << flt_num << " took off after "
<< wait << " time unit" << ((wait == 1) ? "" : "s")
<< " in the takeoff queue." << endl;
}
int Plane::started() const
//Post: Return the time that the Plane entered the airport system.
{
return clock_start;
}
header: random
Code:
/* Program extracts from Appendix B of
"Data Structures and Program Design in C++"
by Robert L. Kruse and Alexander J. Ryba
Copyright (C) 1999 by Prentice-Hall, Inc. All rights reserved.
Extracts from this file may be used in the construction of other programs,
but this code will not compile or execute as given here. */
#include <time.h>
#include <limits.h>
#include <math.h>
// Section B.2:
class Random {
public:
Random(bool pseudo = true);
// Declare random-number generation methods here.
double random_real();
int random_integer(int low, int high);
int poisson(double mean);
private:
int reseed(); // Re-randomize the seed.
int seed,
multiplier, add_on; // constants for use in arithmetic operations
};
// Section B.3:
int Random::reseed()
/*
Post: The seed is replaced by a pseudorandom successor.
*/
{
seed = seed * multiplier + add_on;
return seed;
}
Random::Random(bool pseudo)
/*
Post: The values of seed, add_on, and multiplier are
initialized. The seed is initialized randomly only if pseudo == false.
*/
{
if (pseudo) seed = 1;
else seed = time(NULL) % INT_MAX; //max_int
multiplier = 2743;
add_on = 5923;
}
double Random::random_real()
/*
Post: A random real number between 0 and 1 is returned.
*/
{
double max = INT_MAX + 1.0; //max_int
double temp = reseed();
if (temp < 0) temp = temp + max;
return temp / max;
}
int Random::random_integer(int low, int high)
/*
Post: A random integer between low and high (inclusive) is returned.
*/
{
if (low > high) return random_integer(high, low);
else return ((int) ((high - low + 1) * random_real())) + low;
}
int Random::poisson(double mean)
/*
Post: A random integer, reflecting a Poisson distribution
with parameter mean, is returned.
*/
{
double limit = exp(-mean);
double product = random_real();
int count = 0;
while (product > limit) {
count++;
product *= random_real();
}
return count;
}
/*************************************************************************/
the program:
Code:
#include <iostream>
#include "Runway.h"
#include "Random.h"
using namespace std;
void initialize(int &end_time, int &queue_limit, double &arrival_rate, double &departure_rate)
/* Pre: The user specifies the number of times unit in the simulation
the maximal queue sizes permitted, and the expected arrival and departure rates for the airport.
Post: The program prints instructions and initializes the parameters end_time, queue limit, arrival_rate,
and departure_rate to the specified values.
Uses: utility function user_says_yes
*/
{
cout << "This program simulates an airport with only one runway." << endl
<< "One plane can land or depart in each unit of time." << endl;
cout << "Up to what number of planes can be waiting to land "
<< "or take off at any time? " << flush;
cin >> queue_limit;
cout << "How many units of time will the simulation run?" << flush;
cin >> end_time;
bool acceptable;
do
{
cout << "Expected number of arrivals per unit time?" << flush;
cin >> arrival_rate;
cout << "Expected number of departures per unit time?" << flush;
cout << "Expected number of departures per unit time?" << flush;
cin >> departure_rate;
if (arrival_rate < 0.0 || departure_rate < 0.0)
cout << "These rates must be nonnegative." << endl;
else
acceptable = true;
if(acceptable && arrival_rate + departure_rate > 1.0)
cout << "Safety Warning: This airport will become saturated." << endl;
}while(!acceptable);
}
void run_idle(int time)
//Post: The specified time is printed with a message that the runway is idle.
{
cout << time << ": Runway is idle." << endl;
}
int main( ) // Airport simulation program
/* Pre: The user must supply the number of time intervals the simulation is to run,
the expected number of planes arriving, the expected number of planes
departing per time interval, and the maximum allowed size for runway
queues.
Post: The program performs a random simulation of the airport, showing the
status of the runway at each time interval, and prints out a summary of
airport operation at the conclusion.
Uses: Classes Runway, Plane, Random and functions run_idle, initialize. */
{
int end_time; //time to run simulation
int queue_limit; //size of Runway queues
int flight_number = 0;
double arrival_rate, departure_rate;
initialize(end_time, queue_limit, arrival_rate, departure_rate);
Random variable;
Runway small_airport(queue_limit);
for (int current_time = 0; current_time < end_time; current_time++)
{ //loop over time intervals
int number_arrivals = 1;//variable.poisson(arrival_rate); //current arrival requests
for (int i = 0; i < number_arrivals; i++)
{
Plane current_plane(flight_number++, current_time, arriving);
if (small_airport.can_land (current_plane) != success)
current_plane.refuse();
}
int number_departures = variable.poisson(departure_rate); //current dep. req.
for (int j = 0; j < number_departures; j++)
{
Plane current_plane (flight_number++, current_time, departing);
if(small_airport.can_depart(current_plane) != success)
current_plane.refuse();
}
Plane moving_plane;
switch (small_airport.activity(current_time, moving_plane))
{ //Let at most one Plane onto the Runaway at current_time.
case land:
moving_plane.land(current_time);
break;
case takeoff:
moving_plane.fly(current_time);
break;
case idle:
run_idle(current_time);
}
}
small_airport.shut_down(end_time);
}
Produced Errors:
PHP Code:
Runway.h||In member function 'Error_code Runway::can_land(const Plane&)':|
Runway.h|51|error: no matching function for call to 'Extended_queue::append(const Plane&)'|
Queue.h|42|note: candidates are: Error_code Queue::append(const Queue_entry&)|
Runway.h||In member function 'Error_code Runway::can_depart(const Plane&)':|
Runway.h|71|error: no matching function for call to 'Extended_queue::append(const Plane&)'|
Queue.h|42|note: candidates are: Error_code Queue::append(const Queue_entry&)|
Runway.h||In member function 'Runway_activity Runway::activity(int, Plane&)':|
Runway.h|94|error: no matching function for call to 'Extended_queue::retrieve(Plane&)'|
Queue.h|64|note: candidates are: Error_code Queue::retrieve(Queue_entry&) const|
Runway.h|102|error: no matching function for call to 'Extended_queue::retrieve(Plane&)'|
Queue.h|64|note: candidates are: Error_code Queue::retrieve(Queue_entry&) const|
Runway.h|105|error: cannot convert 'Extended_queue' to 'Runway_activity' in assignment|
||=== Build finished: 5 errors, 0 warnings ===|
-
November 18th, 2012, 01:52 AM
#2
Re: Inheritance Problems
In Runway::can_land(const Plane ¤t), you have the following:
Code:
result = takeoff.append(current);
takeoff is an object of type Extended_queue, which inherits append() from Queue. Looking at the declaration of Queue::append():
Code:
typedef char Queue_entry;
Error_code append(const Queue_entry &item);
append() is expecting a const char& and you are calling it with a const Plane&.
You should declare your classes in the header file and define them in the CPP source file. The only exception to this are template classes/functions or any member functions that you would prefer the compiler to inline.
-
November 18th, 2012, 04:28 AM
#3
Re: Inheritance Problems
It seems like on the main program on the part:
Code:
Plane current_plane(flight_number++, current_time, arriving);
if (small_airport.can_land (current_plane) != success)
where current_plane is the object being passed into a Runway function that takes Plane references. The reference is passed into the Queue function append() that takes a constant char reference.
Code:
Error_code Queue::append(const Queue_entry &item)
{
//Post: item is added to the rear of the Queue. If the Queue is full return an
// Error code of overflow and leave the Queue unchanged.
if (count >= maxqueue) return overflow;
count++;
rear = ((rear + 1) == maxqueue) ? 0: (rear + 1);
entry[rear] = item;
return success;
}
That reference item, which supposedly a char, is stored into the next index of array entry. When it does, it will return the enum value of success, which I think is 1...
Since the whole structure of the program is so complex, is there a way where I can convert the current_plane to a char type or somehow write on the main.cpp that can satisfy the if statement mentioned above?
-
November 18th, 2012, 06:28 PM
#4
Re: Inheritance Problems
I have solved my own problems (once again). My options were to overload the append() so that it takes Plane object ref. but then another solution came to my head and I'm happy about it. I have made a personal update log for this little hugely complicated program to show what I have learned and solved.
Also special thanks to @ttrz for pointing out the problem and the coding advice.
Code:
Program: Airport Simulation
Files:
-Airport Sim.cpp
-Runway.h
-Plane.h
-QueuePlane.h
-Random.h
Changes:
% 11/18/2012 %
-Source code
-Airport Sim.cpp
88: enum Runway_activity takeoff has been confused with Extended_queue object takeoff. The enum type has been changed to RWA_takeoff.
-Runway.h
4: The change was made to reflect the change in 88: in Airport Sim.cpp
-Plane.h
1-2, 82: Inclusion guards have been added to prevent anymore compiling errors.
-QueuePlane.h
- This file was copied from the original Queue.h
10: typedef char Node_entry has been removed because it doesn't match when passing in a Plane object as parameter
for Extended_queue functions.
- File-wide changes -- All Queue functions now take parameters of Plane object references.
-Random.h
- The top of this file has included headers: time, limits, and math
- Class member declarations were made
47, 58: max_int has been changed to a provided macro of limits.h, INT_MAX
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|