Operator Overloading -

Hi guys, now I try to learn about operator overloading, I understand something but not it all. I want to decrease the salary by 100, I don't know if my function is correct, I tried an example, but I have an error. My question is how can I call this function in my main?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include<iostream>
using namespace std;
class Employee{
private:
    string name="";
    int salary=0;
public:
    Employee(string nam="",int sal=0)
    {
        name=nam;
        salary=sal;
    }
    Employee operator-(const Employee& c)
    {
        Employee newSalary;
        newSalary.salary=salary-100;
        return newSalary;
    }
    void showThis()
    {
        cout<<salary<<endl;
    }
};
int main()
{
    Employee d1 ("Dam",2000);
    -d1;
    d1.showThis();
    return 0;

}
Since you want to subtract an int from an object, you need to have the minus operator take an int.

In the actual function, you should make a new Employee object equal to the original (the *this object) , then subtract the int value from its salary. Finally, return the new Employee object.

1
2
3
4
5
6
7
    Employee operator-(int val)
    {
        Employee newEmp = *this;            // make new Employee object, copy of the old one (*this)
        newEmp.salary = newEmp.salary - val; // subtract val from the salary of the new Employee
        
        return newEmp; // return new Employee object
    }


Your main will then look like this:
1
2
3
4
5
6
7
8
int main()
{
    Employee d1 ("Dam",2000);
    d1 = d1 - 100;
    d1.showThis();
    return 0;

}
Last edited on
And if I write only Employee newEmp;
Doesn't work because I didn't make a new Employee object?
No, you still made a new Employee object, it just doesn't have the same name as the original. In fact, it has no name. You are only subtracting salary, so name should go unchanged.

Thus the name of the new and old objects should be the same, while the salaries should be different.

The code below is a different way of doing the exact same thing as in my previous post.

1
2
3
4
5
6
7
8
9
Employee operator-(int val)
{
     Employee newEmp; // make new object
     
     newEmp.name = name; // assign old objects name to new objects name            
     newEmp.salary = salary - val; // new objects salary is old objects salary minues val
        
     return newEmp; // return new Employee object
}


In the first, I make a new object which is an exact copy of the original (same name, same salary). Then I simply subtract val from the salary of the new object.

In this code, I make a new object using default constructor (no name, salary = 0). Then I assign the original objects name to new object's name, and original objects 'salary minus val' to new object's salary.

The result is the same in both cases: same name, but salaries differ by 'val'
Last edited on
Thank you so much, now I understand, . Thank you again. :D You explained so well, for a noob like me :)
Topic archived. No new replies allowed.