Homework 4

Physics 131/231: Simulations in C++

Winter 2000

Problem Set 4

Due 2:00 pm Tuesday, February 8.

1. Write a simple class called Time which has 2 private data members: Hours and Minutes. Show that it compiles and runs with the following main program:

void main() {
   Time noon(12,0);
   cout << "noon=" << noon.getHours() << ":"
          <<noon.getMinutes() <<endl;
   Time meetings[3];
   cout << "Enter hours and minutes" << endl;
   int hours, min;
   cin >> hours >> min;
   Time mytime(hours,min);
   cout << "mytime=" << mytime.getHours() 
        << ":"<< mytime.getMinutes() <<endl;
 }

2. Write an class Array2D along the lines of the Array class from class, but for 2D arrays. Rather than try to use mat[i][j], use operator() to allow mat(i,j). Write and run a main program to test all the class's public functions.

3. Make Array2D into a class template. Test it for both Array2D < int> and Array2 < double>. In all cases, show what happens when you compile and run your program.