0

So what I am trying to do is make the sum operator do the some of 2 elements of class but I have an issue when modifying the class constructor In this code

#include<iostream>
using namespace std;

class Baza {
public:
    int a;
    int b;
    int c;

    Baza(double x, double y){
        this->a=x;
        this->b=y;
    }
    
    Baza Suma(const Baza& val) const{
        return Baza(a+val.a,b+val.b);
        
    }
    Baza operator+(const Baza& val) const{
        return Suma(val);
    }

    
};


class Derivata : public Baza {
    
public:
    int o;
    int p;
    Derivata(double x, double y) : Baza(x,y){
    this->o = x ;
    this->p = y;
    }
    Derivata Suma(const Derivata& val) const{
        return Derivata(a+val.a,b+val.b);
    }
    Derivata operator+(const Derivata& val) const{
        return Suma(val);
    }

    
};



ostream& operator<<(ostream& os, const Baza& val) {
    os << val.a << ',' << val.b;
    return os;
}

int main() {
    Baza obd1(2,3);
    cout << obd1 << endl;
    Baza obd2(4,5);
    cout << obd2 << endl;
    Baza obd3 = obd1 + obd2;
    cout << obd3 << endl;
    /*
    Derivata obiect1(5,6);
    Derivata obiect2(10,3);
    Derivata obiect3 = obiect1 + obiect2;
    cout << obiect3 << endl;
    return 0;
    */
}

The algorithm works , makeing the right sum for objects ob1 and ob2 , but if I am trying to change the "Baza" constructor like this Baza(double x, double y){ this->a=x + 5; this->b=y; } The sum won't add up . Any ideas ?

VlAd TbK
  • 69
  • 1
  • 8
  • It works just as you wrote it - you have `obd1.a` equal to `7`, `obd2.a` equal to `9`, adding both together `7 + 9`, which is `16`, and you pass that to `Baza` constructor, which gets `16` and adds `5` to it, resulting in `obd3.a` equal to `21`. – Yksisarvinen Jan 20 '21 at 12:46
  • And how can I make it stop do the another + 5 from Baza ? Just to the 7 + 9 – VlAd TbK Jan 20 '21 at 12:49
  • Define constructor that doesn't perform sum, or call constructor without arguments and set values of a and b. – Lazar Đorđević Jan 20 '21 at 12:52
  • Can you give me an example how the second method would look like ? I am new to C++ can't really understand – VlAd TbK Jan 20 '21 at 12:58

1 Answers1

1

I can think of 2 ways to resolve your issue. One is good and done according to basic rules of operator overloading, second somewhat reasonable (if we assume that Baza constructor absolutely have to add 5 to a).

1. Implement your operator+ in terms of operator+=

In this method, you first implement operator += and then, you can use it by taking a copy in operator+. It could also by done by writing copy manually, which may produce less WTFs/minute.
It would be preferred to implement operator+ as a non-member, but I don't want to change your code too much.

Baza& operator+= (const Baza& val)
{
    a += val.a;
    b += val.b;
}

Baza operator+(Baza val) const {
    return val += (*this);
}

// Baza operator+(const Baza& val) const {
   // Baza copy = *this;
   // return copy += val;
// }

2. Provide another constructor, which doesn't do any shenanigans

Note, this method modifies members directly, which means you could use your original constructor as well, but it's still a good idea to have the default constructor if your only other constructor does things to data (and those things are sometimes undesireable).

Baza()
{
    a = 0;
    b = 0;
}

Baza operator+(const Baza& val) const{
    Baza ret;
    ret.a = a + val.a;
    ret.b = b + val.b;
    return ret;
}
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52