1

When I run this code it crashes with out any errors. I have tried everything i know and done searches but i can figure this one out. It builds and runs fine and goes all the way to the cout in FleetCapacity and something about that line is making the code crash . When i commented out that line the code ran fine so im not sure as to why that line of code is causing my program to crash and burn.

#include <iostream>
#include <string>
using namespace std;

class Ship{
private:
    string shipName;
    string shipYear;
public:
    Ship(string sN,string sY){shipName=sN; shipYear=sY;}
    virtual void printInfo();
    void setShipName(string);
    virtual string getShipName();
    void setShipYear(string);
    string getShipYear();
};

class CruiseShip:public Ship{
private:
    int maxPass;
    int maxCrew;
public:
    CruiseShip(string sN,string sY, int mP,int mC):Ship(sN,sY){setShipName(sN);maxPass=mP; maxCrew=mC; }
    void printInfo();
    void setMaxPass(int);
    int getMaxPass();
    void setMaxCrew(int);
    int getMaxCrew();
};

class CargoShip:public Ship{
private:
    int cargoCap;
public:
    CargoShip(string sN,string sY,int cC):Ship(sN,sY){setShipName(sN);cargoCap=cC;}
    void printInfo();
    void setCargoCap(int);
    int getCargoCap();

};


void Ship::setShipName(string sN){shipName=sN;}
string Ship::getShipName(){return shipName;}
void Ship::setShipYear(string sN){shipYear=sN;}
string Ship::getShipYear(){return shipYear;}
void Ship::printInfo(){
cout<<"The ships name is "<<shipName<<endl;
cout<<"The ships year is "<<shipYear<<endl;
}

void CruiseShip::printInfo(){
cout<<"The ships name is "<<getShipName()<<endl;
cout<<"The ships maximum passangers is "<<maxPass<<endl;
}
void CruiseShip::setMaxPass(int mP){maxPass=mP;}
int CruiseShip::getMaxPass(){return maxPass;}
void CruiseShip::setMaxCrew(int mC){maxCrew=mC;}
int CruiseShip::getMaxCrew(){return maxCrew;}

void CargoShip::printInfo(){
cout<<"The ships name is "<<getShipName()<<endl;
cout<<"The ships cargo capacity is "<<cargoCap<<endl;
}
int CargoShip::getCargoCap(){return cargoCap;}
void CargoShip::setCargoCap(int cC){cargoCap=cC;}




void fleetCapacity(Ship** s ,int e){

    cout << "Name of ship: " << s[e]->getShipName() << endl;


}

int main()
{
    const int NUMSHIPS = 3;
    //int aSize = NUMSHIPS;
    // array of ship pointers initialized with addresses of dynamically allocated class objects.
    Ship *ships[NUMSHIPS] = {
                                         new Ship("The Dinghy Berry", "1982"),
                                         new CruiseShip("Disney Adventure Tours"," ",500,100),
                                         new CargoShip("The Sea Trucker"," ", 50)

                                       };

    for (int i = 0; i < NUMSHIPS; i++ )
    {
        ships[i]->printInfo();
    }

   cout << "The entire fleet capacity is: ";
   fleetCapacity(ships, NUMSHIPS);
    cout << " tons." << endl;
    return 0;
}
1
  • 1
    What does the debugger spit out on? Programs usually don't crash silently unless you specific did something to. Commented Nov 9, 2015 at 1:50

1 Answer 1

1

you are calling fleetCapacity(ships, NUMSHIPS); which is then accessing s[e] (ships[NUMSHIPS]) in the function. The valid indices are 0 through NUMSHIPS-1.

Sign up to request clarification or add additional context in comments.

3 Comments

I might be the worst programmer ever.
Alright so could i use dynamic casting to determine if e[0] is a Ship, CruiseShip or CargoShip ? Or is there a better way to find that out?
I don't know what exactly you want the function to do, but you should use virtual functions to give each ship a different behavior depending on its class instead of using dynamic casts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.