Thread: Overloading operators

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    36

    Overloading operators

    Hello folks, why in the function 'addTwo' in both cases variable salary equals to 400, independent of the variable passed to the constructor in the main function.



    Code:
    #include <iostream>
    using namespace std;
    
    
    class Employee{
    	private:
    		int idNum;
    		double salary;
    	public:
    		Employee(int, double);
    		double addTwo(Employee);
    };
    Employee::Employee(int id, double sal)
    {
    	idNum = id;
    	salary = sal;
    }
    double Employee::addTwo(Employee emp)
    {
    	double total;
    	cout << emp.salary << endl;
    	total = salary + emp.salary;
    	cout << salary << endl;
    	return total;
    }
    int main(void)
    {
    	Employee clerk(1234, 400.);
    	Employee driver(3456, 650.);
    	double sum;
    	sum = clerk.addTwo(driver);
    	sum = clerk.addTwo(clerk);
    	return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Be more specific please. What did you expect to happen and didn't?
    Devoted my life to programming...

  3. #3
    Guest
    Guest
    I can only assume that you intended to write lines 31 and 32 differently. Otherwise, specify how the output and your expectations differ, as GReaper suggested. In my experience, trying to explain the issue to others will lead you to the solution all by yourself in many cases. I've abandoned countless questions before posting them, because I figured it out while writing.

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    1. Please initialize variables like total and sum upon declaration, otherwise you have no idea what value's in them before you start using them;

    2. In addTwo() the second cout<< should be total, not salary, to give you what you're looking for;

    3. The 2 sums you run in main() add (a) salary of driver to that to clerk and (b) salary of clerk to that of itself - are you sure that's what you want or should it be:
    Code:
        sum = clerk.addTwo(driver);
        sum = driver.addTwo(clerk);
    


    (4) Also recommend declaring addTwo() const and using member-initialization in the ctor

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by sean_cantab View Post
    1. Please initialize variables like total and sum upon declaration, otherwise you have no idea what value's in them before you start using them;

    2. In addTwo() the second cout<< should be total, not salary, to give you what you're looking for;

    3. The 2 sums you run in main() add (a) salary of driver to that to clerk and (b) salary of clerk to that of itself - are you sure that's what you want or should it be:
    Code:
        sum = clerk.addTwo(driver);
        sum = driver.addTwo(clerk);
    


    (4) Also recommend declaring addTwo() const and using member-initialization in the ctor
    That is an example from a book, but thank you.

    I wanted to know at line 31 when I call the function add two and pass the object to the function, in the function add Two, object Employee emp has the same values as the passed object and therefore emp.salary equals 650?
    What about the 'salary' variable, what determines which value it has since we are adding them at the line 22.

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    The salary variable corresponds to the object calling the method, so for e.g in

    Code:
     sum = clerk.addTwo(driver)
    ;

    object clerk calls the method addTwo() and so salary correspond's to clerk's salary and object driver plays the role of employee emp in this code segment

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by sean_cantab View Post
    The salary variable corresponds to the object calling the method, so for e.g in

    Code:
     sum = clerk.addTwo(driver)
    ;

    object clerk calls the method addTwo() and so salary correspond's to clerk's salary and object driver plays the role of employee emp in this code segment
    That makes sense now, I wasn't paying attention that actually clerk object calls the function addTwo().
    If I overloaded + operator, and for example I had situation variable = objectA + objectB , which of there 2 objects would call the method, A or B?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    variable = objectA + objectB is equivalent to

    variable = operator+(objectA, objectB)

    where operator+ is actually the name of a function.
    In case there's no global operator+, the function is always invoked on left-hand-side:

    variable = objectA.operator+(objectB)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by Elysia View Post
    variable = objectA + objectB is equivalent to

    variable = operator+(objectA, objectB)

    where operator+ is actually the name of a function.
    In case there's no global operator+, the function is always invoked on left-hand-side:

    variable = objectA.operator+(objectB)
    Okay, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-01-2014, 04:45 AM
  2. overloading operators
    By yahn in forum C++ Programming
    Replies: 12
    Last Post: 08-13-2007, 12:30 PM
  3. Overloading Operators
    By Zoalord in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2003, 09:08 AM
  4. Overloading Operators
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2002, 02:23 PM
  5. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM

Tags for this Thread