Created
October 12, 2012 22:42
-
-
Save DerekV/3882060 to your computer and use it in GitHub Desktop.
Pass by refrence, example with alternatives
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <utility> | |
using namespace std; | |
void getHoursRate(double &hours, double &rate) | |
{ | |
cout << "enter hours:" << endl; | |
cin >> hours; | |
cout << "enter rate:" << endl; | |
cin >> rate; | |
} | |
void getHoursRate2(double *hours, double *rate) | |
{ | |
cout << "enter hours:" << endl; | |
cin >> *hours; | |
cout << "enter rate:" << endl; | |
cin >> *rate; | |
} | |
struct HoursAndRate | |
{ | |
double hours; | |
double rate; | |
}; | |
HoursAndRate getHoursRate3() | |
{ | |
HoursAndRate input; | |
cout << "enter hours:" << endl; | |
cin >> input.hours; | |
cout << "enter rate:" << endl; | |
cin >> input.rate; | |
return input; | |
} | |
pair<double,double> getHoursRate4() | |
{ | |
pair<double,double> userInput; | |
cout << "enter hours:" << endl; | |
cin >> userInput.first; | |
cout << "enter rate:" << endl; | |
cin >> userInput.second; | |
return userInput; | |
} | |
int main() | |
{ | |
double hours; | |
double rate; | |
// method 1 | |
getHoursRate(hours,rate); | |
cout << "hours is " << hours << " and rate is " << rate << endl; | |
// method 2 | |
getHoursRate(hours,rate); | |
cout << "hours is " << hours << " and rate is " << rate << endl; | |
// method 3 | |
HoursAndRate userInput = getHoursRate3(); | |
cout << "hours is " << userInput.hours << " and rate is " << userInput.rate << endl; | |
// method 3 | |
pair<double, double> pair = getHoursRate4(); | |
cout << "hours is " << pair.first << " and rate is " << pair.second << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment