Category : Miscellaneous Language Source Code
Archive   : MTD.ZIP
Filename : VEHICLE.CPP

 
Output of file : VEHICLE.CPP contained in archive : MTD.ZIP
// vehicle.cpp :: TC++ 1.0 multiple inheritance demonstration

#include

// == Class declarations

// -- A Vehicle is an "abstract" class
class Vehicle {
float topSpeed;
float currentSpeed;
public:
void accelerate(float amount);
void decelerate(float amount);
void stop();
float getTopSpeed(void) { return topSpeed; }
void putTopSpeed(float s) { topSpeed = s; }
float getSpeed(void) { return currentSpeed; }
void putSpeed(float s) { currentSpeed = s; }
Vehicle() { topSpeed = currentSpeed = 0.0; }
Vehicle(float maxSpeed);
~Vehicle() { stop(); }
};

// -- A TwoWheeler is a Vehicle with a top speed of 35.0
class TwoWheeler : public Vehicle {
public:
TwoWheeler() { putTopSpeed(35.0); }
};

// -- A FourWheeler is a Vehicle with a top speed of 120.0
class FourWheeler : public Vehicle {
public:
FourWheeler() { putTopSpeed(120.0); }
};

// -- A Boat is a new class
class Boat {
float weight; // Exceed this and she sinks
public:
float getWeight(void) { return weight; }
void putWeight(float w) { weight = w; }
Boat() { weight = 0.0; }
};

// -- An amphibious vehicle is a boat with four wheels!
class Amphibious : public Boat, FourWheeler {
public:
Amphibious() { putTopSpeed( 45.0 ); }
};

// == Method implementations

void Vehicle::accelerate(float amount)
{
currentSpeed += amount;
if (currentSpeed > topSpeed)
putSpeed(topSpeed);
}

void Vehicle::decelerate(float amount)
{
currentSpeed -= amount;
if (currentSpeed < 0.0)
putSpeed(0.0);
}

void Vehicle::stop()
{
while (currentSpeed > 0.0)
decelerate(1.0);
}

Vehicle::Vehicle(float maxSpeed)
{
topSpeed = maxSpeed;
currentSpeed = 0.0;
}

main()
{
TwoWheeler mybike; // Define test objects for each class
FourWheeler mycar;
Boat myboat;
Amphibious mytank;

myboat.putWeight(1200); // Assign weights to myboat, mytank
mytank.putWeight(20000);

mybike.accelerate(2.0); // Call a few object methods
/* myboat.putSpeed(46.5);*/ // Incorrect--no setSpeed method
mytank.putSpeed(58.0); // Okay--inherited from Vehicle
mytank.decelerate(20.0);
mytank.stop();

printf("mybike's top speed = %f\n", mybike.getTopSpeed());
printf("mycar's top speed = %f\n", mycar.getTopSpeed());
printf("myboat's weight = %f\n", myboat.getWeight());
printf("mytank's top speed = %f\n", mytank.getTopSpeed());
printf("mytank's weight = %f\n", mytank.getWeight());

}


  3 Responses to “Category : Miscellaneous Language Source Code
Archive   : MTD.ZIP
Filename : VEHICLE.CPP

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/