| Version 1.9 Build 1556
|
|
Next: The expanded include modules
Up: Introduction to Programming for AIPS++
Previous: Sample Application
The previous sections of this example demonstrated the use of
templates. The next two, briefer, sections will demonstrate
polymorphism, namely the ability to define a function with two or more
sets of different argument types. In particular, we will add to the definition
of ReadAsciiFile a module to read Vectors instead of Matrices.
The following is the program which calls the new functions
(The source code
for this program is found in
$AIPSROOT/code/trial/implement/test/tExample4.cc).
1 #include <aips/aips.h>
2 #include <aips/IO/AipsIO.h>
3 #include <aips/Arrays/ArrayIO.h>
4 #include <aips/Inputs/Input.h>
5 #include <aips/Mathematics/Math.h>
6 #include <aips/Arrays/Matrix.h>
7
8 main(int argc, char **argv)
9 {
10 Input inputs(1);
11 inputs.Version("$@w{Revision}$");
12 inputs.Create("infile1","Input1","Float Input file name?","Infile");
13 inputs.Create("infile2","Input2","Vector Input file name?","Infile");
14 inputs.Create("outfile","Output","Vector Output file name?","Outfile");
15 inputs.ReadArguments(argc, argv);
16
17 const char *filein1 = inputs.GetString("infile1");
18 const char *filein2 = inputs.GetString("infile2");
19 const char *fileout = inputs.GetString("outfile");
20
21 Int rows, cols;
22 try {
23 Matrix <Float> mat1;
24 Vector <Float> vect1;
25
26 readAsciiFile(filein1, mat1); mat1.shape(rows, cols);
27 cout << "The Matrix is " << rows << " by " << cols << endl;
28 readAsciiFile(filein2, vect1); vect1.shape(rows);
29 cout << "The Vector is " << rows << " long" << endl;
30
31 writeAsciiFile(fileout, vect1);
32 cout << mat1 << vect1 << endl;
33 }
34
35 catch (AipsIOError x) {
36 cerr << "aipsioerror: error " << x.getMesg() << endl;
37 return 1;
38 }
39 catch (AipsError x) {
40 cerr << "aipserror: error " << x.getMesg() << endl;
41 return 1;
42 }
43 return 0;
44 }
- 6-
- We inadvertently omitted the reference to Vectors but got away with it!
The reason is that the definition of Vectors is included in the definition
of Matrix. It is NOT good practice to do this since modules may change.
- 13-14-
- We ask for the Vector (rather than Matrix) name. (A minor point.)
- 24-
- We will only demonstrate Float data type for Vectors but they will
be defined for the full variety of data types.
- 28-
- We call for Vector data. Note that the function shape works for
1-dimensional arrays as well as for the 2-dimensional ones.
Next: The expanded include modules
Up: Introduction to Programming for AIPS++
Previous: Sample Application
  Contents
  Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc.,
Washington, D.C.
Return to AIPS++ Home Page
2006-10-15