// you’re reading...

C++ Glossary

Passing and Returning Structures

Passing structures by value makes the most sense when the structure is relatively compact

  • So let’s look at a couple examples along those lines



The first example deals with travel time (not to be confused with time travel)

  • Some maps will tell you that
    • It is 2 hours, 56 minutes, from Zero One City to Binary Falls
    • And 1 hour, 16 minutes, from Binary Falls to Hexadecimal Valley



You can use a structure to represent such times

  • Using one member for the hour value
  • And a second member for the minute value



Adding two times is a little tricky

  • Because you might have to transfer some of the minutes to the hours part
    • For example, the two preceding times sum to 3 hours, 72 minutes, which should be converted to 4 hours, 12 minutes



Let’s develop a structure to represent a time value

  • And then a function that takes two such structures as arguments
    • And returns a structure that represents their sum



Defining the structure is simple (A-1):


Example of (A-1)

struct travel_time
{
	int hours;
	int mins;
};



Next, consider the prototype for a sum() function that returns the sum of two such structures

  • The return value should be type travel_time, and so should the two arguments
    • Thus, the prototype should look like this (A-2):



Example of (A-2)

travel_time sum(travel_time t1, travel_time t2);



To add two times, you first add the minute members

  • Integer division by 60 yields the number of hours to carry over
    • And the modulus operation (%) yields the number of minutes left



(A-3) incorporates the above approach into the sum() function

  • And adds a show_time() function
    • To display the contents of a travel_time structure

 


Example of (A-3)

// travel.cpp -- using structures with functions
#include <iostream>
struct travel_time
{
	int hours;
	int mins;
};
const int Mins_per_hr = 60;
 
travel_time sum(travel_time t1, travel_time t2);
void show_time(travel_time t);
 
int main()
{
	using namespace std;
	travel_time day1 = {2, 56}; // 2 hrs, 56 min
	travel_time day2 = {1, 16}; // 1 hrs, 16 min
 
	travel_time trip = sum(day1, day2);
	cout << "Two-day total: ";
	show_time(trip);
 
	travel_time day3= {5, 12};
	cout << "Three-day total: ";
	show_time(sum(trip, day3));
 
	return 0;
}
 
travel_time sum(travel_time t1, travel_time t2)
{
	travel_time total;
 
	total.mins = (t1.mins + t2.mins) % Mins_per_hr;
	total.hours = t1.hours + t2.hours +
		    (t1.mins + t2.mins) / Mins_per_hr;
	return total;
}
 
void show_time(travel_time t)
{
	using namespace std;
	cout << t.hours << " hours, "
	     << t.mins << " minutes\n";
}



Here travel_time acts just like a standard type name; you can use it to

  • Declare variables
  • Function return types
  • Function argument types



Because variables such as total and t1 are travel_time structures

  • You can apply the dot membership operator to them



Note that because the sum() function returns a travel_time structure

  • You can use it as an argument for the show_time() function



Because C++ functions, by default, pass arguments by value

  • The show_time(sum(trip, day3)) function call
    • First evaluates the sum(trip, day3) function call in order to find its return value
  • The show_time() call
    • Then passes sum()’s return value, not the function itself to show_time()



Here’s the output of the program in (A-3):

Output


Discussion

No comments for “Passing and Returning Structures”

Post a comment