Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to overload the '+' operator to add a float to a class and return a float(C++)?

+3 votes
asked Nov 1, 2021 by Areeb Sherjil (1,960 points)
edited Nov 2, 2021 by Areeb Sherjil

I have a class called Sensors that has a variable called 'value' which is a float. I want to be able to re-write the following:

averageTemperature = averageTemperature + sensors[count].value;// sensors is an array of Sensors object

to 

averageTemperature = averageTemperature + sensors[count];

//averageTemperature is also a float.

So far I have done this:

class Sensors

{

public:

// alot of other code here

float getValue()const { return value; }

friend float operator + (float fl1,const Sensors& sensor1);// float+class=float

protected:

float value{};

};

float operator+(float fl1, const Sensors& sensor1)

{return fl1+sensor1.getValue();}

As you can see there is a problem i.e a friend function has to be used. Is there anyway around this.(I have no idea why the formatting has gone wrong!)

2 Answers

+1 vote
answered Nov 3, 2021 by Dino Vity (740 points)
selected Nov 4, 2021 by Areeb Sherjil
 
Best answer

class Sensors
{
public:
    // alot of other code here
    float getValue()const { return value; }

    // define  operator+  for    Sensors+float
    float operator+(float fl1) const { return fl1 + value; }


protected:
    float value{};
};

//  define  operator+  for  float+Sensors
float operator+(float fl1, const Sensors& sensor1) {
    return sensor1 + fl1;   // call member func
}

+1 vote
answered Nov 2, 2021 by Peter Minarik (86,180 points)
edited Nov 2, 2021 by Peter Minarik

Your code works fine. You just have to keep in mind that you defined overloading for float + Sensors, and not (yet) for Sensors + float.

#include <iostream>

class Sensors
{
protected:
    float value;
    
public:
    Sensors(float initValue) : value(initValue) { }
    float getValue() const { return value; }
    friend float operator + (float fl1, const Sensors& sensor1) { return fl1 + sensor1.getValue(); }
};

int main()
{
    Sensors sensor(2);
    std::cout << (3 + sensor) << std::endl; // Runs fine
    //std::cout << (sensor + 3) << std::endl; // Compilation error
    return 0;
}

Question: are you sure that Sensors + float should produce a float and not a Sensors?! Normally, it should be a latter with a float cast operator overwritten as well so Sensors can stand in for floats.

commented Nov 3, 2021 by Areeb Sherjil (1,960 points)
Yes. I wanted to make a float which would equal a float added to the value inside the Sensors class. so my code works like this float= float+sensors.value but will it work like this float= sensors.value+float?
commented Nov 3, 2021 by Peter Minarik (86,180 points)
As long as you have the

float operator + (float, const Sensors &)

function (operator overload), you can write expressions like

float = float + Sensors

OR

float = float + Sensors.getValue().
commented Nov 4, 2021 by Areeb Sherjil (1,960 points)
thanks man, I understood that!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...