I am relatively new to C++ and I am in the process of creating a hypothetical 'Flight Management' application as a learning experience and to test out various new skills. Simply put, I have a FlightPlan
class which among other things contains a private member pointer to an object of type Aircraft
(well, for now).
Objects derived from Aircraft
are pre-existing at the time of FlightPlan
creation, and I want to be able to assign an Aircraft
derived object pointer to a FlightPlan
(the a/c that will carry out the flight in the plan) after a FlightPlan
is constructed and not just during construction.
(using a method such as
flight-plan-obj.AssignAircaft(cargo-aircraft-obj-pointer);
My issue is that Aircraft
is an abstract class that CargoAircraft
and PassengerAircraft
derive from, therefore it is not known what 'type' of aircaft will be assigned to the flight plan during it's construction. My code is below. (Much has been left out for brevity)
class FlightPlan {
private:
string departure;
string destination;
Aircraft* aircraft; // The a/c that will later be assigned to the flight
// Obviously this is currently wrong
public:
FlightPlan(string dep, string dest);
void AssignAircraft(Aircraft* ac);
// Other methods
}
The Aircraft
abstract class:
class Aircraft {
protected:
Aircaft(string tailId, string acType); // ctor for the sub-classes
string tailId;
string acType;
public:
virtual string Overview() const =0; // pure virtual method
}
And an example of an object derived from Aircraft
:
class CargoAircraft : public Aircraft {
private:
int cargoCapacity;
public:
CargoAircraft(string tailId, string acType, int cargoCapcity);
string Overview() const override;
}
// ctor defined in the .cpp file as:
CargoAircraft::CargoAircraft(string tailId, string acType, int cargoCapacity)
: Aircraft(tailId,acType), cargoCapacity(cargoCapacity) {};
Is what I'm trying to do possible? My understanding was that pointer polymorphism would allow me to do:
Aircraft* aircraft; or Aircaft* aircraft = nullptr;
in FlightPlan
and that would later accept an object pointer of a class derived from Aircraft
?
On compile I receive: error: ‘Aircraft’ does not name a type
Is there something fundamentally wrong with my approach here? Do I need to give up on Aircraft
being abstract and just treat it as more of a base class and give it a public default constructor and make it's methods just virtual instead of pure-virtual?
Any and all advice will be graciously received, and I will gratefully accept any other hints or tips regarding my use of C++. Thank you.