#include <iostream>
#include <string>
using namespace std;
class Ship
{
string shipName;
string shipYear;
public:
Ship(string n = "Titanic", string y = "1980")
{
shipName = n;
shipYear = y;
}
void setShipName(string n) { shipName = n; }
void setShipYear(string y) { shipYear = y; }
string getShipName() const { return shipName; }
string getShipYear() const { return shipYear; }
virtual void print() const {}
};
class Cruiseship : public Ship
{
int numPass;
public:
Cruiseship(int numPass) : Ship("Hawaii-ish", "1948")
{ this->numPass = numPass; }
void setNumPass(int num) { numPass = num; }
int getNumPass() const { return numPass; }
void print() const
{ cout << this->getShipName() << this->getNumPass(); }
};
class CargoShip : public Ship
{
int cargoCapacity;
public:
CargoShip(int cargoCapacity) : Ship("USBEB", "2050")
{ this->cargoCapacity = cargoCapacity; }
void setCargoCapacity(int cc) { cargoCapacity = cc; }
int getCargoCapacity() const { return cargoCapacity; }
void print() const
{ cout << this->getShipName() << this->getCargoCapacity(); }
};
int main()
{
const int NUM_SHIPS = 3;
Ship *ships[NUM_SHIPS] =
{
new Ship,
new Cruiseship(2000),
new CargoShip(900)
};
for (int i=0; i<3; i++)
cout << ships[i]->print();
for (int i=0; i<3; i++)
delete ships[i];
}
Design a Ship class that has the following members:
--A member variable for the name of the ship (a string)
--A member variable for the year that the ship was built (a string)
--A constructor and appropriate accessors and mutators
--A virtual print function that displays the ship's name and the year it was built.Design a CruiseShip class that is derived from the Ship class.
The CruiseShip class should have the following members:
--A member variable for the maximum number of passengers (an int)
--A constructor and appropriate accessors and mutators
--A print function that overrides the print function in the base class. The CruiseShip class's print function should display only the ship's name and the maximum number of passengers.Design a CargoShip class that is derived from the Ship class.
The CargoShip class should have the following members:
--A member variable for the cargo capacity in tonnage (an int).
--A constructor and appropriate accessors and mutators.
--A print function that overrides the print function in the base class. The CargoShip class's print function should display only the ship's name and the ship's cargo capacity.Demonstrate the classes in a program that has an array of Ship pointers. The array elements should be initialized with the addresses of dynamically allocated Ship, CruiseShip, and CargoShip objects.
이 내가 지금까지 무엇을하지 않습니다. 임지고 오류 :(
어떤 오류가 있습니까? 어떻게 컴파일합니까? – Deduplicator
@Deduplicator 메인에 cout << ships[i]-> 인쇄(); – user3453084
'cout << ships[i]-> print()'이게 뭐가 잘못 됐는지 알아낼 수있을거야. 선언문과 본문을보세요. – Radiodef