CIS 22B - Notes for Wed Dec 06

Announcements and Reminders

  • Assignment 10 is due Monday, and is not accepted late
  • Final is Monday, 3/27, 1:45 - 3:45 pm
  • Check your grades

What is wrong with Exercise 11?


Review

  • 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.  You do not have to write a copy constructor for this problem.  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;
    file.getline(buffer,sizeof(buffer));
    file.seekg(5,ios::cur);
    file.get(ch);
    cout << ch;
    file.getline(buffer,sizeof(buffer));
    cout<<buffer[5];
    file.seekg(7,ios::beg);
    cout << (char)file.peek();
    file.getline(buffer,sizeof(buffer));
    for (int i=0;i<8;i++) {
        file >> buffer;
    }
    file.get(buffer,sizeof(buffer),'o');
    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
{
public:
    X()
    {
        cout << 'a';
    }
    ~ X()
    {
        cout << 'b';
    }
};

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


int main()
{
    Y object;
}

And for practice:

Write a program that reads the “file.in” 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.

“file.in”

abcdefghi123456789012345678901234567890
abcdefghi23456789012345678901234567890s
abcdefghi3456789012345678901234567890gj
abcdefghi456789012345678901234567890dnf
abcdefghi56789012345678901234567890djdi
abcdefghi6789012345678901234567890rrggg
abcdefghi789012345678901234567890fggffg
abcdefghi89012345678901234567890gbbfgfg
abcdefghi9012345678901234567890fgfgfgfg
abcdefghi012345678901234567890fgfgfgfgf
abcdefghi123456789012345678901234567890

 “file.out”

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