home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / REFPARM.CP$ / REFPARM
Encoding:
Text File  |  1991-12-12  |  1.2 KB  |  47 lines

  1. // REFPARM.CPP
  2.  
  3. // This is an example program from Chapter 3 of the C++ Tutorial. This
  4. //     program uses a function with reference parameters for reducing
  5. //     overhead and eliminating pointer notation.
  6.  
  7. #include <iostream.h>
  8.  
  9. // ---------- A big structure
  10. struct bigone
  11. {
  12.    int serno;
  13.    char text[1000];   // A lot of chars
  14. } bo = { 123, "This is a BIG structure" };
  15.  
  16. // -- Three functions that have the structure as a parameter
  17. void valfunc( bigone v1 );          // Call by value
  18. void ptrfunc( const bigone *p1 );   // Call by pointer
  19. void reffunc( const bigone &r1 );   // Call by reference
  20.  
  21. void main()
  22. {
  23.    valfunc( bo );   // Passing the variable itself
  24.    ptrfunc( &bo );  // Passing the address of the variable
  25.    reffunc( bo );   // Passing a reference to the variable
  26. }
  27.  
  28. // ---- Pass by value
  29. void valfunc( bigone v1 )
  30. {
  31.    cout << '\n' << v1.serno;
  32.    cout << '\n' << v1.text;
  33. }
  34. // ---- Pass by pointer
  35. void ptrfunc( const bigone *p1 )
  36. {
  37.    cout << '\n' << p1->serno;     // Pointer notation
  38.    cout << '\n' << p1->text;
  39. }
  40.  
  41. // ---- Pass by reference
  42. void reffunc( const bigone &r1 )
  43. {
  44.    cout << '\n' << r1.serno;       // Reference notation
  45.    cout << '\n' << r1.text;
  46. }
  47.