I've looked through a few articles that seem similar to my problems that I'm having on my homework for intro to C++, but am still unable to find a solution.
I'm attempting to overload the operator+ to increase the passenger capacity by a int n. Also, overloading the operator++ to increase the passenger capacity by 1.
Note that polymorphism is happening in my class, I have Ship (base), CruiseShip (derived).
CruiseShip constructor:
CruiseShip::CruiseShip(string name, string year, int passengers) : Ship(name, year)
{
maxPassengers = passengers;
}
Operator overloads:
CruiseShip& CruiseShip::operator+(int n) const
{
maxPassengers += n;
return *this;
}
CruiseShip& CruiseShip::operator++() // prefix
{
++maxPassengers;
return *this;
}
CruiseShip CruiseShip::operator++(int) // postfix
{
CruiseShip temp(*this);
operator++();
return temp;
}
Main:
int main()
{
//Create objects, pointers
Ship *ships[3] = {new Ship("Titania", "2020"), new CruiseShip("Lusia", "2029", 200), new CargoShip("Luvinia", "2025", 500)};
//Print out ships
for(Ship *s : ships)
{
s -> print();
cout << endl;
}
//Reset a ships passenger, capacity
//I've tried testing each individually and all 3 still end up with segmentation errors
ships[1] = ships[1] + 5; //segmentation error related to this
ships[1]++; // segmentation error related to this
++ships[1]; // segmentation error related to this
//Print out ships
for(Ship *s : ships)
{
s -> print();
cout << endl;
}
//deallocate
return 0;
}