CIS 181 Intro to Comp Sci
Class 17, April 12, 2005

ASSIGNMENT:

Read: D&D,  through  p. 445 (through 6.14) (previously assigned).

Exercises due Thursday, April 14, pp. 465-466- #6.4 and 6.6 -- For 6.6 you should use the following driver program to test your class (it can be found here:

// Driver Program to test complex class
// Problem 6.6 in D&D

#include #include "complex.h"

main()
{
     complex c1, c2(3.7), c3(8.6, 9);

     cout << "After initialization: " << endl;
     cout << "c1 = ";
     c1.print();
     cout << "c2 = ";
     c2.print();
     cout << "c3 = ";
     c3.print();
     cout << endl;

     c1 = c2;
     cout << "After c1 = c2: " << endl;
     cout << "c1 = ";
     c1.print();
     cout << "c2 = ";
     c2.print();
     cout << endl;

     cout << "After some sets: " << endl;
     cout << "c1.setcomplex(3, 5) ------ ";
     c1.setcomplex(3, 5.5);
     cout << "c1 = ";
     c1.print();
     cout << endl;

     cout << "c2.setcomplex(81.9, 4.7) ----- ";
     c2.setcomplex(81.9, 4.7);
     cout << "c2 = ";
     c2.print();
     cout << endl;

     cout << "c3.setcomplex(1.1, 2.2) ----- ";
     c3.setcomplex(1.1, 2.2);
     cout << "c3 = ";
     c3.print();
     cout << endl;

     cout << "After adding c1 and c2 and storing in c3: " << endl;
     c3 = c1.add(c2);
     cout << "c3 = ";
     c3.print();
     cout << endl;

     cout << "Now set c3 to c3 - c1: " << endl;
     c3 = c3.subtract(c1);

     cout << "c3 = ";
     c3.print();
     cout << endl;

     cout << "For comparison c2 is: " << endl;
     cout << "c2 = ";
     c2.print();
     cout << endl;

     return 0;
}

The following output should be produced:

pluto[9:33am] [/usb/mccoy/]> a.out
After initialization:
c1 = (0, 0)
c2 = (3.7, 0)
c3 = (8.6, 9)

After c1 = c2:
c1 = (3.7, 0)
c2 = (3.7, 0)

After some sets:
c1.setcomplex(3, 5) ------ c1 = (3, 5.5)

c2.setcomplex(81.9, 4.7) ----- c2 = (81.9, 4.7)

c3.setcomplex(1.1, 2.2) ----- c3 = (1.1, 2.2)

After adding c1 and c2 and storing in c3:
c3 = (84.9, 10.2)

Now set c3 to c3 - c1:
c3 = (81.9, 4.7)

For comparison c2 is:
c2 = (81.9, 4.7)


TODAY'S TOPICS
  1. structs: C++ data structures for non-homogeneous data

    Arrays are collections of homogeneous data; structs are collections of (possibly) non-homogeneous data.

    Array elements are accessed by subscript notation. Struct elements are accessed by dot notation.

    However the mechanism for declaring arrays and structs is very different. When one declares an array variable, say
      int A[10];
    the declaration allocates memory for the array A. However when one declares a struct, say
      struct bankAcct
        {
         int acctNo;
         float balance;
        };

    all this does is to DEFINE A NEW TYPE bankAcct (similar to what happens using a typedef). NO NEW VARIABLES ARE DECLARED NOR IS ANY SPACE ALLOCATED.

    To declare struct variables of the above type it is then necessary to do something like the following.

      bankAcct JoeGreen;

    JoeGreen is now a variable of type bankAcct and space has been allocated for it. To assign values to it, we access the two individual components by writing JoeGreen.acctNo and JoeGreen.balance. They can be used like any other variables of type int and float respectively.

  2. Introduction to C++ classes

    See the file $CLASSHOME/examples/part-rec.cc and the files in the directory $CLASSHOME/deitel-files/examples/ch06/fig6_10