Friday, October 25, 2013

Introducing struct

Struct - structures - are similar to classes. Not sure of the difference. This also is the first time to use functions passing by address rather than by value - note the & syntax and that the function declaration and its definition both use the &. Note that using the const parameter can help reduce the risk of a variable being incorrectly modified when passed by reference.
#include <iostream>

using namespace std;

//Time has to be declared before its use in a function definition
 struct Time {
        int hour, minute;
        double second;
    };

//you don't put the parameter in the initial function declaration
// this function passes by address, not by value
void print_time(Time&);



int main()
{

  Time time = { 11, 59, 3.14159}; //you can assign numbers in order
  print_time(time);
    return 0;
}

No comments:

Post a Comment