1 / 11

Lecture 06 Reference Variables

Lecture 06 Reference Variables. Learn about: - Reference arguments - Pass Arguments and Structures by reference - const function arguments. Reference Variables (page 137-139). A reference is an alias (a different name) to a variable. How to declare a reference variable?

howell
Télécharger la présentation

Lecture 06 Reference Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 06Reference Variables Learn about:- Reference arguments- Pass Arguments and Structures by reference- const function arguments

  2. Reference Variables (page 137-139) • A reference is an alias (a different name) to a variable. • How to declare a reference variable? i) By placing a type and & in front of a variable. ii) Assign another variable of the same type to the reference variable. For example: double someMoney; double &cash = someMoney

  3. Reference Variables (page 137-139) double someMoney; double &cash = someMOney; cash = 6.78; cout<<cash<<endl; // displays 6.78 cout<<someMoney; // displays 6.78 someMOney = 111.333; cout<<cash<<endl; // displays 111.333 cout<<someMoney; // displays 111.333

  4. Reference Arguments • Passing arguments by reference allows a function to access variables in the calling program, as well as returning more than one value. • Reference arguments are indicated by the & following the data type. (float n, float &intp, float &fracp)

  5. Reference Arguments Example 1 #include <iostream.h> void intfrac(float n, float& intp, float& fracp) { long temp = static_cast<long>(n); // temp = 10 intp = static_cast<float>(temp); // intp = 10 fracp = n - intp; // fracp = 0.25 } void main() { float number, intpart, fracpart; do { cout << "\nEnter a real number: "; cin>>number; // input: 10.25 intfrac(number, intpart, fracpart); cout << "Integer part is " << intpart << ", fraction part is " << fracpart << endl; } while( number != 0.0 ); }

  6. Reference Arguments Example 1 (cont.) • The & indicates that intp is an alias for whatever variable is passed as an argument. Similarly for fracp. • Note that the & is not used in the function call. • Do not confuse with the address of operator (same symbol).

  7. Pass By Reference: Example 2 #include <iostream.h> void order(int& numb1, int& numb2) { if(numb1 > numb2) { int temp = numb1; numb1 = numb2; numb2 = temp; } } void main() { int n1=99, n2=11; // not ordered int n3=22, n4=88; // ordered order(n1, n2); order(n3, n4); cout << "n1=" << n1 << endl; cout << "n2=" << n2 << endl; cout << "n3=" << n3 << endl; cout << "n4=" << n4 << endl; }

  8. Passing Structures By Reference Example #include <iostream.h> struct Distance { // English distance int feet; float inches; }; void engldisp( Distance dd ) { cout << dd.feet << "\'-" << dd.inches << "\""; } void scale( Distance& dd, float factor) { float inches = (dd.feet*12 + dd.inches) * factor; dd.feet = static_cast<int>(inches / 12); dd.inches = inches - dd.feet * 12; }

  9. Passing Structures By Reference Example (cont.) int main() { Distance d1 = { 12, 6.5 }; Distance d2 = { 10, 5.5 }; cout << "\nd1 = "; engldisp(d1); cout << "\nd2 = "; engldisp(d2); scale(d1, 0.5); scale(d2, 0.25); cout << "\nd1 = "; engldisp(d1); cout << "\nd2 = "; engldisp(d2); cout << endl; return 0; } • By default, structures are passed by value (i.e. copy each member). • Passing structure by reference is much more efficient.

  10. const Function Arguments • Passing arguments by reference is more efficient and also allows the function to modify them directly. • Can you pass an argument by reference for efficiency, but with a guarantee that the function cannot modify it? • You can apply the const modifier.

  11. const Function Arguments Example #include <iostream.h> void main() { int alpha = 7; int beta = 11; aFunc(alpha, beta); } void aFunc(int& a, const int& b) { a = 107; // OK b = 111; // error: can't modify const }

More Related