Cpp - Expressions With Reference Type

Introduction

The following code shows how to use reference types in expressions.

The code converts strings to upper case.

Demo

#include <iostream> 
#include <string> 
#include <cctype>                // For toupper() 

using namespace std; 
// Converts the content of str to uppercase. 
void strToUpper( string& );              

int main() /*from   w w  w.  j  a  va  2 s . c o  m*/
{ 
   string text("Test with assignments \n"); 

   strToUpper(text); 
   cout << text << endl; 

   strToUpper( text = "Flowers"); 
   cout << text << endl; 

   strToUpper( text += " cheer you up!\n"); 
   cout << text << endl; 
   return 0; 
} 

void strToUpper( string& str)   
{                              
     int len = str.length(); 
     for( int i=0; i < len; ++i) 
       str[i] = toupper( str[i]); 
}

Result

Related Topic