-1

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;
}
Amai
  • 141
  • 6
  • From what I can tell your `operator+` shouldn't even compile. Post a [mcve]. – melpomene Dec 04 '18 at 01:24
  • 1
    The array doesn't contain objects, it contains pointers. So what you are doing are pointer arithmetic operations, that do not relate to your overloaded operators. – 273K Dec 04 '18 at 01:26
  • @S.M.So from what I'm understanding, I'm apparently doing those operations on the address that the pointer points to and not the object itself? If so, would dereferencing fix that? – Amai Dec 04 '18 at 01:32
  • Try it yourself. – 273K Dec 04 '18 at 01:34
  • I tried adjusting it but it seems I still end up with the error. Hmm :/ – Amai Dec 04 '18 at 01:43

1 Answers1

0

The way to make this work is to use virtual functions. But we run into the problem that the base class cannot return a derived class object. So, to solve this problem, we can simply have the Derived class return the Base class instead. So here is some example code:

class Ship
{
public:
    Ship(string name, string year)
    {
        this->name = name;
        this->year = year;
    }

    virtual Ship & operator + (int n)
    {
        return *this;
    }

    virtual Ship & operator ++()
    {
        return *this;
    }

    virtual Ship & operator ++ (int i)
    {
        return *this;
    }
public:

    string name;
    string year;
};

class CruiseShip : public Ship
{
public:
    virtual Ship& operator+(int n)
    {
        maxPassengers += n;
        return *this;
    }

    virtual Ship & operator++() // prefix
    {
        ++maxPassengers;
        return *this;
    }

    virtual Ship & operator++(int) // postfix
    {
        CruiseShip temp(*this);
        operator++();
        return temp;
    }


    CruiseShip(string name, string year, int passengers) : Ship(name, year)
    {
        maxPassengers = passengers;
    }

    int maxPassengers;

};

Creating a virtual function in both the base class and the derived class allows there to be a definition no matter what type of ship it is AND still allows us to define the virtual function differently in the derived class.

Note that CruiseShip overloaded operators return Ships and not CruiseShips. This satisfies the requirement to have the same return type of the virtual function.

Then in main, the only changes you'll have to make is to dereference your pointer and put that dereferenced pointer in parentheses like this: (*Ship[1])++

cwbusacker
  • 507
  • 2
  • 12
  • `operator+` only needs one argument if it's a member function, which it appears to be. That said, [`operator+` should almost always be a non-member](https://stackoverflow.com/a/4421729/364696), so it's a design flaw to provide it as a member function (and it should be returning a new value, not a reference; the `operator+` as written is correct for `operator+=`, not `operator+`). – ShadowRanger Dec 04 '18 at 02:25
  • My ship class doesn't include it, mainly because I'm not sure how I would do so, I considered using virtual, but that would mean the function header must be the same (which wouldn't work b/c of the return type). I'm honestly not sure how to go about dereferencing my pointers, I've tried a few different ways and all of them still end of in segmentation errors. – Amai Dec 04 '18 at 02:30
  • If you are wanting to do it the way you are doing it, you'll either have to declare your CruiseShip as a CruiseShip or make the overloaded operators virtual. This is because of the type you are using. If you choose the virtual route, you're right the Ship must return the same type. The way to resolve this would be to have all the concrete classes overload the virtual function OR have Ship return itself without doing anything to it. But it must return the same type. – cwbusacker Dec 04 '18 at 02:40
  • If I must return CruiseShip for my operator++, but CruiseShip isn't in Ship's scope. I dun understand how the virtual function would work because in the ship class you wouldn't be able to write it as `virtual CruiseShip operator++(int)`. While in the CruiseShip class that is neccessary. – Amai Dec 04 '18 at 02:51
  • Ah. Then, you're right it probably shouldn't be virtual. I think the better way would be to declare 3 different pointers for the ships. That way they aren't of type Ship. I demoed it and it worked. I'll post my solution – cwbusacker Dec 04 '18 at 03:10
  • Yea...the problem is the homework assignment told us to make an array of Ship pointers Or else I would have definitely not done it this way. – Amai Dec 04 '18 at 03:24
  • @Amai I have updated the answer and confirmed that it works. – cwbusacker Dec 04 '18 at 18:17