Question: I need my code to have none initialization list #include #include #include using namespace std; class Ship { protected: string name; string yearBuilt; public: Ship

I need my code to have none initialization list #include
#include
#include
using namespace std;
class Ship {
protected:
string name;
string yearBuilt;
public:
Ship(const string& name, const string& yearBuilt)
: name(name), yearBuilt(yearBuilt){}
string getName() const { return name; }
string getYearBuilt() const { return yearBuilt; }
virtual void print() const {
cout << "Ship name: "<< name <<"
";
cout << "Year built: "<< yearBuilt <<"
";
}
};
class CruiseShip : public Ship {
private:
int maxPassengers;
public:
CruiseShip(const string& name, const string& yearBuilt, int maxPassengers)
: Ship(name, yearBuilt), maxPassengers(maxPassengers){}
int getMaxPassengers() const { return maxPassengers; }
void print() const override {
cout << "Cruise Ship name: "<< getName()<<"
";
cout << "Maximum passengers: "<< maxPassengers <<"
";
}
};
class CargoShip : public Ship {
private:
int cargoCapacity;
public:
CargoShip(const string& name, const string& yearBuilt, int cargoCapacity)
: Ship(name, yearBuilt), cargoCapacity(cargoCapacity){}
int getCargoCapacity() const { return cargoCapacity; }
void print() const override {
cout << "Cargo Ship name: "<< getName()<<"
";
cout << "Cargo capacity: "<< cargoCapacity <<" tons" <<"
";
}
};
int main(){
vector ships;
ships.push_back(new Ship("Ship A","2000"));
ships.push_back(new CruiseShip("Cruise Ship B","2010",2000));
ships.push_back(new CargoShip("Cargo Ship C","2020",5000));
for (const auto& ship : ships){
ship->print();
cout <<"
";
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!