C++ Reference Change an object in a function

Description

C++ Reference Change an object in a function

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class Employee{/*ww  w.ja  va  2 s .  c  om*/
  public:
    int  workHour;
    double salary;
};

void myFunction(Employee& refS){
    refS.workHour = 10;
    refS.salary      = 3.0;
    cout << "The value of copyS.salary = " <<refS.salary<< endl;
}

int main(int nNumberofArgs, char* pszArgs[])
{
    Employee s;
    s.salary = 0.0;

    cout << "The value of s.salary = " << s.salary  << endl;

    cout << "Calling myFunction(Employee*)" << endl;
    myFunction(s);
    cout << "Returned from myFunction(Employee&)" << endl;

    cout << "The value of s.salary = " << s.salary << endl;

    return 0;
}



PreviousNext

Related