0

This is the summary of the code, and it is supposed to be like that: There are 4 classes. The first one is an abstract class. It has two virtual functions(get & set method) that are redefined by the next two classes - both inherit the first one. The fourth class has an integer for dimension, and a matrix(size nxn) whose type is the first class. It also contains a destructor, get method and a destructor.

  class Polje{
protected:
    int value;
public:
    virtual int GetValue() = 0;
    virtual void SetValue(int n) = 0;
};

class Voda : public Polje{
    int GetValue(){return value*2;}
    void SetValue(int n){value = n;}
};

class Zemlja : public Polje{
    int GetValue(){return value*(-3);}
    void SetValue(int n){value = n+1;}
};

class Tabla{
public:
    int dim;
    Polje ***mat;
public:
    int GetDim(){return dim;}

    Tabla(int d){
        dim = d;
        mat = new Polje**[dim];
        for(int i = 0; i < dim; i++)
            mat[i] = new Polje*[dim];
    }

    Polje* GetPolje(int x, int y){
        return mat[x][y];
    }


    ~Tabla(){
        for(int i=0; i<dim; i++)
            delete [] mat[i];
        delete [] mat;
    }
};

I need to overload the += operator(argument Polje *p) in a way that it sets a new value to the element of the matrix, in the first free place. Also, is there a way to later determine what type of Polje(Voda or Zemlja) is the element of the matrix?

A_Mel
  • 21
  • 2
  • 7
  • 2
    I wouldn't. This is not the behaviour I'd expect from the `+=` operator. Is this a hard requirement of the project, common sense be damned? – user4581301 Sep 09 '21 at 23:43
  • 1
    Tactical note: `Polje ***mat;` is one of the worst ways to make a 2D matrix. It is slow, error-prone, and [requires support functions](https://en.cppreference.com/w/cpp/language/rule_of_three) that you have yet to declare. Consider using [something like this](https://stackoverflow.com/a/2076668/4581301) instead. – user4581301 Sep 09 '21 at 23:49
  • 1
    Tactical note: You should not need to know the exact `Polje`-derived class you are dealing with. Prefer to expand the `Polje` interface to abstract away all knowledge of the exact type. If you cannot, it is often a sign that inheritance is not the best solution. We need to know more about your use case to be able to help you though this problem. – user4581301 Sep 09 '21 at 23:55
  • You would also need a method for a `Polje` to know whether its setter has ever been called. – jxh Sep 10 '21 at 00:07
  • 3
    *"I need to overload the += operator(argument Polje *p) in a way that it sets a new value to the element of the matrix, in the first free place."* -- which part of this is causing you trouble? Overloading `operator+=`? Setting a new value to the element of the matrix? Finding the first free place? Determining if a place is free? Divide-and-conquer so you can deal with simpler problems. *(If you can tell if a place is free, write a function for it. If you can find the first free place, write a function for it. Build up from the known to the unknown.)* – JaMiT Sep 10 '21 at 01:03
  • @user4581301 This is one of the crazy requirements of the project. I could swear that these people are just making it up randomly as they go. More about the use... I'm supposed to set the value of every matrix element in the main function by first determining the type of it(Voda or Zemlja) and then setting the value. – A_Mel Sep 10 '21 at 05:51
  • @JaMiT It's the complete thing. I can't really get my head around the each element being of type Polje, and each having this value integer. I had an idea to preset every element to some value, and then be able to set the wanted value to the first element which has this preset value, but I couldn't make it work. – A_Mel Sep 10 '21 at 05:55
  • 1
    @A_Mel "the complete thing" is too broad, as it has several parts. Pick one aspect to work on first, and temporarily ignore the rest. Divide-and-conquer so you can deal with simpler problems. – JaMiT Sep 10 '21 at 05:58
  • 3
    Agree with JaMiT. When you get stuck on a problem, find a smaller problem. There's no such thing as a big problem, just little problems ganging up on you. On the requirement. Sometimes requirements are just plain wrong. At a good company you'll be able to go to the next person up the chain and say something like, "Boss, this is whack. Here's my reasoning and my counter-proposal." Odds are you do need this behaviour. It just shouldn't be bundled into `operator +=`. It's like having a function named `getX` that sets X instead. It causes confusion, and confusion causes bugs. – user4581301 Sep 10 '21 at 06:16
  • 1
    Of course this could be for a course, and learning NOT to do this in real life could be one of the educational goals. Or the teacher just might have never written any production-grade, complicated programs written and maintained by multiple people for a couple dozen years and doesn't know why this is avoided. Met more than my fair share of those bouncing back and forth between industry and education over the past few decades. – user4581301 Sep 10 '21 at 06:16
  • @JaMiT True that. Just figured out a simpler, more reasonable, version of this problem by figuring out how exactly it would be used in the main function. – A_Mel Sep 10 '21 at 12:15
  • @user4581301 Very true indeed. Guess you just have to run into stuff like this from time to time. But, it's always nice to get some experience. Notes taken, lessons learned. Thanks to both of you for the tips. – A_Mel Sep 10 '21 at 12:17

0 Answers0