Lab 11

More Classes

Overloading Operators, Friends, Constructors etc.

There is one problem this week.

Sequence Arithmetic
Create a program titled Lab11_Sequence. A sequence is just a list of integers where there is no bound on the length of the the list, For example, we can picture a sequence as

x = (-3, 6, 4, 7, 23) or y = (1,4,5)

You will design a class called sequence that is a vector of integers. (You might want to review Section 8.3,pg 482-488.)

Your program should ask the user for the number of integers to be entered. Then the program allocates the appropriate-size vector, and stores the integers. Here is a sample dialog:.

Enter number of integers: 5
Enter integers: -3 6 4 7 23

This produces the sequence (-3,6,4,7,23).

We can perform arithmetic on sequences as below:

ADDITION:

Two sequences of the same size can be added by adding componentwise- i.e.

If x = (-3,6,4,7,23) and y = (4,1,2,3,-10)

x + y = (-3+4,6+1,4+2,7+3,23+-10) = (1,7,6,10,13)

If the sequences are of different sizes, assume the shorter one is padded with zeroes on the right and then proceed as above:

If x = (-3,6,4,7,23) and y = (1,4,5), we assume we are adding x = (-3,6,4,7,23) and (1,4,5,0,0)

MULTIPLY BY A CONSTANT:

A sequence can be multiplied by a constant by multiplying componentwise - i.e.

If x = (-3,6,4,7,23), then -3*x = (9,-18,-12,-21,-69)

ADDITIVE INVERSES and SUBTRACTION:

Define the additive inverse of x as (-1)* x. We can then define subtraction as

x = y = x + (-1)*y.

MULTIPLICATION:

Two sequences of even unequal sizes can be multiplied as shown by the next example which is more complicated:

Assume x = (-3,6,4,7,23) and y = (2,4,5). Note the additions of zero in the seq (-3,6,4,7,23):

x*y = 2*(-3,6,4,7,23) + 4*(0,-3,6,4,7,23) + 5*(0,0,-3,6,4,7,23) =

(-6,12,8,14,46) + (0,-12,24,16,28,92) + (0,0,-15,30,20,35,615)

or you could do

x*y = (-3)*(2,4,5) + 6*(0,2,4,5) + 4*(0,0,2,4,5) + 7*(0,0,0,2,4,5) + 23*(0,0,0,0,2,4,5)

The first is the easiest when computing by hand, but it is not necessary that the shortest one is the multiplier.

EQUALITY:

Two sequences are equal if they are equal as vectors.

OUTLINE OF PROGRAM:

0. Define a class sequence that represents the sequence as a vector. You should decide whether a member function or data is public, private, or a friend using good object-oriented principles.


1. Create member functions of the class that will act as an interface to the class. Examples might be,
but are not limited to the following. You might find it is useful to implement these in order.

void read()
Reads the sequence from cin. Your code should prompt the user for the size of the vector, as above, create a vector with the correct size, and then use a loop to prompt and input each element.

void print() const

Outputs the sequence to cout. The sequence should be printed out using a format that resembles the following: (3,5,10)

int getelement(sequence x,int i) which returns the ith element of x.

Ex. If x = (-3,6,4,7,23), then getelement(x,2) returns 4.

void putelement(sequence x,int i,int element) which puts element in the ith position of x and moves all other elements to the right.

Ex. If x = (-3,6,4,7,23), then putelement(x,0,0) returns (0,-3,6,4,7,23).

sequence add(const sequence& y)
Returns the sum x + y, where x is the sequence object whose member function is called.

sequence subtract(const sequence& y)

Returns the difference x = y, where x is the sequence object whose member function is called.

sequence multiply(const sequence& y) const

Returns the product x*y, where x is the polynomial object whose member function is called.

bool equal(const sequence& y) const;
Returns true if x is the same sequence as y, and false otherwise, where y is the sequence object whose member function is called. Two sequences are equal if and only if (1) they have the same size and (2) Componentwise the elements are equal.

Additionally, the class should have the following constructors:
sequence()
The sequence is initialized to be a squence (0)

sequence (int s);

The sequence is initialized to be the vector with size s whose elements are all zero.

2. Test each member function with a variety of inputs values, and verify that they work.

3. Now, divide the modified sequence.cpp file into header files and source files suitable for separate compilation, and add appropriate #include statements.

4. Overload the operators +, *, ==, << and >> to perform sequence addition, multiplication, equality, insertion into a stream (e.g. cout) and extraction from a stream (e.g. cin) respectively.

Your test program after #4 is implemented should be able to handle the following syntax for sequences x, y, and z:

z = x + y; // Assume z is (2,1,5)

cout >> z ; //produces the printout of (2,1,5)

z = x - y;

z = x*y;