Pass structure reference to a function - C++ Data Type

C++ examples for Data Type:struct

Description

Pass structure reference to a function

Demo Code

#include <iostream>
#include <iomanip>
using namespace std;
struct Employee  // declare a global data type
{
   int id;/*from  w  w  w .jav a 2 s. c  o  m*/
   double payRate;
   double hours;
};
double calcNet(Employee&);       // function prototype
int main()
{
   Employee emp = {6782, 8.93, 40.5};
   double netPay;
   netPay = calcNet(emp);        // pass a reference
   // Set output formats
   cout << setw(10) << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setprecision(2);
   cout << "The net pay for employee " << emp.id << " is $" << netPay << endl;
   return 0;
}
double calcNet(Employee& temp)   // temp is a reference variable
{
   return temp.payRate * temp.hours;
}

Result


Related Tutorials