Don't like ads? PRO users don't see any ads ;-)
Guest

CS2370 Prog 4

By: mattfong on May 2nd, 2011  |  syntax: None  |  size: 2.20 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Matthew Fong
  2. //Program 4: Inheritance/polymorphism
  3. //Description: Using inheritance and polymorphism to show an array of ships
  4.  
  5. #ifndef SHIP_H_INCLUDED
  6. #define SHIP_H_INCLUDED
  7. #include <iostream>
  8. #include <string>
  9. using namespace std;
  10.  
  11. class Ship
  12. {
  13.     protected:
  14.         string name;
  15.         string year;
  16.     public:
  17.         Ship(string n, string y)
  18.         {
  19.             name = n;
  20.             year = y;
  21.         }
  22.  
  23.         virtual void print()
  24.         {
  25.             cout << "Ship's name: " << name << endl;
  26.             cout << "Year built: " << year << endl;
  27.             cout << endl;
  28.         }
  29. };
  30.  
  31. class CruiseShip : public Ship
  32. {
  33.     private:
  34.         int maxPassenger;
  35.     public:
  36.         CruiseShip(int m, string n, string y) : Ship(n, y)
  37.         {
  38.             maxPassenger = m;
  39.         }
  40.  
  41.         void print()
  42.         {
  43.             cout << "Ship's name: " << name << endl;
  44.             cout << "Year built: " << year << endl;
  45.             cout<< "Maximum Passengers: " << maxPassenger << endl;
  46.             cout << endl;
  47.         }
  48. };
  49.  
  50. class CargoShip : public Ship
  51. {
  52.     private:
  53.         int tons;
  54.     public:
  55.         CargoShip(int t, string n, string y) : Ship(n, y)
  56.         {
  57.             tons = t;
  58.         }
  59.  
  60.         void print()
  61.         {
  62.             cout << "Ship's name: " << name << endl;
  63.             cout << "Year built: " << year << endl;
  64.             cout << "Maximum capacity: " << tons << " tons" << endl;
  65.             cout << endl;
  66.         }
  67. };
  68. #endif // SHIP_H_INCLUDED
  69.  
  70.  
  71. #include "ship.h"
  72. #include <iostream>
  73. #include <string>
  74. using namespace std;
  75.  
  76. int main()
  77. {
  78.     Ship *array[3]; //creates an array of pointers
  79.     Ship *fishing = new Ship ("Northwestern", "1977"); //Creates a pointer to the Ship
  80.     Ship *cruise = new CruiseShip(1500, "Carnival Dream", "2009"); //Creates a pointer to the CruiseShip
  81.     Ship *cargo = new CargoShip(250000, "APM-Maersk", "2003"); //Creates a pinter to the CargoShip
  82.  
  83.     array[0] = fishing; //Puts in the pointer to Ship
  84.     array[1] = cruise;  //Puts in the pointer to CruiseShip
  85.     array[2] = cargo;   //Puts in the pointer to CargoShip
  86.  
  87.     for(int i = 0; i < 3; i++)
  88.     {
  89.        array[i]->print();
  90.     }
  91. }