CIS 22B - Notes for Wednesday 12/5

Announcements and Reminders

  • Assignment 10 is due Monday, and is not accepted late
  • Final is Monday, 9:15-11:15
  • Check your grades
The Final & Other Loose Ends


      Operator Overloading:  Matrix Addition (Example 6-7 from CIS 27 notes)
  • This is a lab problem from the "old" CIS 27 final
Create a class, X, consisting of three data members, a char*, an int, and a long.  These data members are to be used to store a name (both first and last), an age, and a social security number.  Add any necessary functions to your class.  Write an overloaded insertion operator so that you can print a class object.  The overloaded insertion operator should display the members as:
  • The name should be left justified in a field of width 32.
  • The age should be right justified in a field of width 5 and displayed in hexadecimal with a base indicator.
  • The social security number should be left justified in field of width 12, displayed in decimal.
Instantiate a class object using your name, age (you can lie about your age), and social security number with a constructor that looks like this:

X object(“Joe Bentley”,25,123456789);

Use your insertion operator to write the X class object into an output file.
Turn in your program code and the output file.
main() should have only 4 lines, like this:

int main()
  X object(“Joe Bentley”,25,123456789);    // your name, age, ssn
  return 0;
  • Here's one more problem from the "old" CIS27 final
What is the output from the following program?

Input file:   input

Have a great day. 
The quick brown fox jumped over the lazy poodle.

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream file("input");
    char buffer[80];
    char ch;
    cout << ch;
    cout << (char)file.peek();
    for (int i=0;i<8;i++) {
        file >> buffer;
    file>> buffer;
    buffer[3] = 0;
    cout << buffer << endl;

  • And here's one more ...

Write a complete program that meets all of the following requirements.

Create a class called ABC with the following:

a non-static data member:  a char*  (you'll be allocating  memory dynamically for this one)

A static data member: pi, initialized to 3.14

Three constructors.  A default ctor, a copy ctor, and another one

If necessary, write a destructor.

Overload the insertion operator so that it does something appropriate.

Overload the ! operator so that it prints the static data member.

Overload the assignment operator, so that you can assign one ABC object to another.


Write a main() that demonstrates at least one call to every function in the ABC class.

  • And this is the last one ...
Guess the output:

#include <iostream>
using namespace std;

class X
        cout << 'a';
    ~ X()
        cout << 'b';

class Y : public X
        cout << 'c';
    ~ Y()
        cout << 'd';
    X array[2];

int main()
    Y object;

   And for practice:

Write a program that reads the “” file, extracts columns 10-14 and writes the number into a file, “file.out” as shown.  The number is to be written 2 times into the file in columns of width 15 each.  The first column should be left justified, the second right justified.  The    second column should display the number divided by 100.0 in scientific notation.




12345            1.234500E+002
23456            2.345600E+002
34567            3.456700E+002
45678            4.567800E+002
56789            5.678900E+002
67890            6.789000E+002
78901            7.890100E+002
89012            8.901200E+002
90123            9.012300E+002
1234             1.234000E+001
12345            1.234500E+002