0
#include <iostream>
#include <string>

class TOperator {
private:
  string name;

public:
  TOperator(string name) { this->name = name; }
  TOperator();
  void setName(string name) { this->name = name; }
  string getName { return name; }
};

class TComanda {
private:
  Item *I = new Item[1];
  int count;
  string status;

public:
  TComanda(string status) { this->status = status; }
  TComanda();
  void setSatus(string status) { this->status = status; }
  string getStatus { return status; }
  TComanda operator+=(string, const TOpeator &op) {
    TComanda result;

    result.name = name += op.name;

    result.status = status += op.status;
  }
};

class Item {
private:
  string object;
  int price;

public:
  Item(string object, int price) {
    this->object = object;
    this->price = price;
  }
  Item();
  void setObject(string object) { this->object = object; }
  string getObject { return objectt; }
  oid setPrice(int price) { this->price = price; }
  string getPrice { return price; }
};

int main() {
  TOperator oper("Alex");
  TComanda command("online", oper);
  command += Item("usb stick", 220);
  command += Item("keyboard", 175);
  oper.displayCommanda();
  return 0;
}

So i have the main and then I have to create all the classes and functions for it to work, I'm not really sure how to I overload the += for it to work, I'm kinda new in C++ and this comes very heavy for me, can someone please explain how do I overload the += so that in the display I have "Alex online, usb stick, 220" for example

Also this sequence is not done by me and I'm not sure it does what I want, I'm pretty sure it's wrong but I don't know why

TComanda operator+=(string, const TOpeator &op) {
  TComanda result;

  result.name = name += op.name;

  result.status = status += op.status;
}
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • 2
    There is a "canonical" answer to operator overloading: [SO: What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/q/4421706/7478597) – Scheff's Cat Oct 06 '21 at 06:39
  • 1
    Every assignment operator should `return *this;` to be idiomatic, and the return type should be a (non-const) reference of the class it belongs to. – Scheff's Cat Oct 06 '21 at 06:40
  • When asking questions please make sure that the code you post only contains the syntax error for which you don't know how to fix it. `string getName { return name; }`, `oid setPrice(int price)`. … all result in syntax errors, furthermore the order of the class definitions is a problem and would prevent the code from compile. Due to all those problems, it is hard to judge if this is your actual code and if there are more discrepancies between your actual code and the one you show here. – t.niese Oct 06 '21 at 06:56

0 Answers0