C++ Function Parameter passing structure by reference

Description

C++ Function Parameter passing structure by reference

#include <iostream>
using namespace std;
struct Measure/*from  w ww  .j a v  a 2s .  c  o  m*/
{
   int feet;
   float inches;
};
void scale( Measure&, float );       //function
void engldisp( Measure );            //declarations
int main()
{
   Measure d1 = { 12, 6.5 };         //initialize d1 and d2
   Measure d2 = { 10, 5.5 };
   cout << "d1 = "; engldisp(d1);     //display old d1 and d2
   cout << "\nd2 = "; engldisp(d2);
   scale(d1, 0.5);                    //scale d1 and d2
   scale(d2, 0.25);
   cout << "\nd1 = "; engldisp(d1);   //display new d1 and d2
   cout << "\nd2 = "; engldisp(d2);
   cout << endl;
   return 0;
}
// scales value of type Measure by factor
void scale( Measure& dd, float factor)
{
   float inches = (dd.feet*12 + dd.inches) * factor;
   dd.feet = static_cast<int>(inches / 12);
   dd.inches = inches - dd.feet * 12;
}
// display structure of type Measure in feet and inches
void engldisp( Measure dd )   //parameter dd of type Measure
{
   cout << dd.feet << "\'-" << dd.inches << "\"";
}



PreviousNext

Related