CIS 22B - Notes for Thursday 3/22

Announcements and Reminders

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

Polymorphism

Why write a virtual destructor?

Example 1

Example 2

How does polymorphism work?

And what is meant by dynamic binding (or late binding) and static binding?

Non-Virtual, Virtual, and Pure Virtual Functions

Non-Virtual 

  • This is the default type of class member function.  The keyword virtual does not appear in the function prototype.
  • Non-virtual functions, as a rule, are not usually overridden in the derived class.

Virtual 

  • The keyword virtual appears at the beginning of the function prototype in the base class.  It doesn’t have to be used in derived class function prototypes, but it’s not a bad idea to use it.
  • Virtual functions, as a rule, are usually overridden in the derived class.
  • Virtual functions make polymorphism possible.

Pure Virtual 

  • The keyword virtual appears at the beginning and  = 0  at the end of the function prototype in the base class.  The  = 0  is not repeated in derived classes unless that class is intended to serve as a base class for other descendent classes. 
  • Pure virtual functions must be overridden in the derived class, unless, that class is also a base class for other classes.
  • Pure virtual functions are not defined in the class in which they are declared as pure vitual.
  • The presence of a pure virtual function in a class makes it an abstract class Abstract classes may not be instantiated.
Example - Abstract Classes and Pure Virtual Functions

Example - Abstract Class and Polymorphism

Review

      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;
    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