CS 46A Lab 7, Linking

Problems

Name:
CS46A Instructor:

Problem

Suppose we are given the following description of an enterprise:

An airline has a fleet of planes. There are two types of planes: commuter jets with one engine per wing, and jumbo jets with two engines per wing.

In addition, the following technical information is provided:

It is important to know the altitude and air speed of an airplane. We must also know the angle of a wing's flaps and the rotational speed of an engine. An airplane must be able to take off, fly, and land. Of course these procedures are different for jumbo jets and commuter jets. An engine must be able to start, throttle, and stop. We must be able to raise and lower a wing's flaps.

Let's build a model of this enterprise using C++ class declarations. For example, a fleet of planes might be represented by an array, vector, or list of pointers to airplanes:

class Fleet: std::list<Airplane*>
{
public:
   void addPlane(Airplane* p) { push_back(p); }
   void remPlane(Airplane* p) { remove(p); }
};

An airplane is an instance of the Airplane class. Each instance will contain pointers to objects representing the left and right wings:

class Airplane
{
public:
   Airplane();
   double getAirSpeed() const;
   double getAltitude() const;
   Wing* getLeftWing() const;
   Wing* getRightWing() const;
protected:
   Wing *leftWing, *rightWing;
   double altitude;
   double airSpeed;
};

A wing contains a pointer to the airplane it is attached to, and two engine pointers. (Commuter jets will set the second engine pointer to 0 to indicate that there is only a single engine per wing.):

class Wing
{
public:
   Wing(Engine* eng, Engine* eng2 = 0);
   void flaps(double amt);
   Engine* getEngine() const;
   Engine* getEngine2() const;
   double getFlapAngle() const;
   int getNumEngines() const;
   Airplane* getAirplane() const;
   void setAirplane(Airplane* plane) { airplane = plane; }
private:
   int numEngines;
   Engine *engine, *engine2;
   double flapAngle;
   Airplane* airplane;
};

An engine maintains a pointer to the wing it is attached to:

class Engine
{
public:
   Engine(Wing* w = 0);
   void start();
   void throttle(double amt);
   void stop();
   double getEngineSpeed() const;
   Wing* getWing() const;
   void setWing(Wing* w);
private:
   double engineSpeed; // in rotations/minute
   Wing* wing;
};

Jumbo jets and commuter jets are special types of airplanes, so they are declared as derived classes:

class JumboJet: public Airplane
{
public:
   JumboJet(Wing *left, Wing *right);
   void land();
   void fly();
   void takeoff();
};

class CommuterJet: public Airplane
{
public:
   CommuterJet(Wing *left, Wing *right);
   void land();
   void fly();
   void takeoff();
};

Problem

Create a project called "fleet". Place each class declaration in its own header file. For example, the JumboJet class declaration should be placed in a header file called jumbo.h.

Create implementation files for each header file. For example, jumbo.cpp should contain implementations of the member functions declared in JumboJet. Constructors and "getters" should be correctly implemented, but for now, implementations of aeronautical functions can simply print out a message indicating which functions is being called. For example:

void JumboJet::fly() { cout << "flying a jumbo jet\n"; }

Create a file called main.cpp that contains the following test harness:

int main()
{
   CommuterJet
      jet1(new Wing(new Engine()), new Wing(new Engine()));
   JumboJet
      jet2(new Wing(new Engine(), new Engine()),
          new Wing(new Engine(), new Engine()));
   Fleet fleet;
   fleet.addPlane(&jet1);
   fleet.addPlane(&jet2);

   jet1.takeoff();
   jet1.fly();
   jet1.land();

   jet2.takeoff();
   jet2.fly();
   jet2.land();

   return 0;
}

Build and Test

You will also need to carefully place #includes in the files you created. When you're done, build and test your program. Submit print outs of each file, plus a printout of the program's output.

Problem: Dependency Graphs

A dependency graph represents a program's files as drums labeled by file names and connected by dashed arrows indicating that the origin file depends on or includes the destination file. For example:

Draw a dependency graph showing the relationships between the following files (which should all be in your fleet project):

plane.h, plane.cpp, plane.obj
wing.h, wing.cpp, wing.obj
engine.h, engine.cpp, engine.obj
fleet.cpp, fleet.h, fleet.obj
jumbo.cpp, jumbo.h, jumbo.obj
commuter.cpp, commuter.h, commuter.obj
main.cpp, main.obj, fleet.exe

Problem

Use Windows NT Explorer to answer these questions. What object files did your project generate and where are they? Where is the standard library on your computer? Where are the standard headers?