Source of test_amphibian.cpp


  1: /** @file test_amphibian.cpp
  2: Based on an example from N. Josuttis, Object-Oriented Programming in C++.
  3: */

  5: #include <iostream>
  6: using namespace std;

  8: #include "amphibian.h"
  9: using Josuttis::Car;
 10: using Josuttis::Boat;
 11: using Josuttis::Amphibian;

 13: int main()
 14: {
 15:     Car car(10);
 16:     car.display();
 17:     car.travel(5);
 18:     car.display();
 19:     Boat boat(30);
 20:     boat.display();
 21:     boat.travel(6);
 22:     boat.display();
 23:     Amphibian amphibian(7, 42); //7 kilometers and 42 sea miles.
 24:     amphibian.display();        //Output distance traveled

 26:     //The following code segment will not compile. Why not?
 27:     //amphibian.travel(50);
 28:     //amphibian.display();
 29:     //We can, however, do the following:
 30:     amphibian.Car::travel(50);
 31:     amphibian.display();
 32:     amphibian.Boat::travel(25);
 33:     amphibian.display();

 35:     //We can also create a new car or a new boat from an amphibian.
 36:     Car newCar(amphibian);
 37:     newCar.travel(22);
 38:     newCar.display();
 39:     amphibian.display();
 40:     Boat newBoat(amphibian);
 41:     newBoat.travel(22);
 42:     newBoat.display();
 43:     amphibian.display();

 45:     Car& newCar2(amphibian); //(or Car& newCar2 = amphibian;)
 46:     newCar2.travel(22);
 47:     newCar2.display();
 48:     amphibian.display();
 49:     Boat& newBoat2(amphibian); //(or Boat& newBoat2 = amphibian;)
 50:     newBoat2.travel(12);
 51:     newBoat2.display();
 52:     amphibian.display();

 54:     Amphibian* amphibianPtr = &amphibian;
 55:     Car* carPtr = &amphibian;
 56:     carPtr->travel(33);
 57:     carPtr->display();
 58:     amphibianPtr->display();
 59:     Boat* boatPtr = &amphibian;
 60:     boatPtr->travel(33);
 61:     boatPtr->display();
 62:     amphibianPtr->display();
 63: }