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'