fork download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Ship
  7. {
  8. protected:
  9. string shipName;
  10. string year;
  11. public:
  12. Ship();
  13. Ship(string, string);
  14. virtual void print();
  15. };
  16.  
  17. Ship::Ship()
  18. {
  19. shipName = " ";
  20. year = " ";
  21. }
  22.  
  23. Ship::Ship(string myName, string myYear) // Constructor
  24. {
  25. //strcpy(shipName, myName);
  26. //strcpy(year, myYear);
  27. shipName = myName;
  28. year = myYear;
  29. }
  30.  
  31. void Ship::print()
  32. {
  33. cout << shipName << endl;
  34. cout << year << endl;
  35. }
  36.  
  37. class CargoShip : public Ship
  38. {
  39. protected:
  40. int load;
  41. public:
  42. CargoShip();
  43. CargoShip(string, string, int);
  44. virtual void print();
  45.  
  46. };
  47.  
  48. CargoShip::CargoShip() : Ship()
  49. {
  50. load = 0;
  51. }
  52.  
  53. CargoShip::CargoShip(string myName, string myYear, int myLoad) :
  54. Ship(myName, myYear)
  55. {
  56. load = myLoad;
  57. }
  58.  
  59. void CargoShip::print()
  60. {
  61. cout << shipName << endl;
  62. cout << load << endl;
  63. }
  64.  
  65. class CruiseShip : public Ship
  66. {
  67. protected:
  68. int passNum;
  69.  
  70. public:
  71. CruiseShip();
  72. CruiseShip(string, string, int);
  73. virtual void print();
  74.  
  75. };
  76.  
  77. CruiseShip::CruiseShip() : Ship()
  78. {
  79. passNum = 0;
  80. }
  81.  
  82. CruiseShip::CruiseShip(string myName, string myYear, int myNum) :
  83. Ship(myName, myYear)
  84. {
  85. passNum = myNum;
  86. }
  87.  
  88. void CruiseShip::print()
  89. {
  90. cout << shipName << endl;
  91. cout << passNum << endl;
  92. }
  93.  
  94.  
  95. #include <iostream>
  96. #include <string>
  97. using namespace std;
  98.  
  99. int main()
  100. {
  101.  
  102. Ship* shipArray[3];
  103.  
  104. shipArray[0] = new Ship("S.S. Shippy", "1997");
  105. shipArray[1] = new CruiseShip("S.S. La Cruise", "2017", 20);
  106. shipArray[2] = new CargoShip("S.S El Cargo", "1845", 300);
  107.  
  108. shipArray[0]->print();
  109. shipArray[1]->print();
  110. shipArray[2]->print();
  111.  
  112. delete shipArray[0];
  113. delete shipArray[1];
  114. delete shipArray[2];
  115.  
  116. return 0;
  117. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
S.S. Shippy
1997
S.S. La Cruise
20
S.S El Cargo
300