BTP200 Mid Term Test 1a February 27 2004
Name ______Student Number ______
1 – Design and code a class named Eboard that holds information for an electronic billboard. Upon instantiation, an Eboard object may receive two integers specifying the width and height of the board and a null-terminated C-string containing the message to be displayed. If the dimensions are omitted, assume a width of 10 characters and a height of 3 lines. If the message is omitted, assume an empty message.
For your Eboard objects to work properly, you will need to include a copy constructor, an assignment operator, and a destructor. The assignment operator should accommodate changes to the size of the display.
Your class should also include the following overloaded functions:
o an extraction operator (>) that accepts a message from standard input
o an insertion operator (<) that displays the message on standard output
o an assignment operator (=) that accepts a new message
o an append operator (+=) that appends text to the existing message (bonus)
Your objects should validate the width and height dimensions received from the user or programmer. Your objects may truncate the message received to fit the dimensions of the billboard. Your objects should display the message by wrapping it to fit the number of characters per line as shown in the example below, but should not display beyond the number of lines available on the billboard.
Both the new message assignment operator and the append operator should leave the size of the object’s display unchanged. The append operator should insert a blank character between the existing message and the additional text whenever necessary.
Example
The following main program
#include <iostream>
using namespace std;
#include "Eboard.h"
int main () {
EBoard test = "This is a long message", help(5, 6, "Help me please");
cout < test;
test = "Test 1 test 2";
cout < test;
test += "test 3"; // bonus
cout < test; // bonus
cout < help;
cin > help;
cout < help;
return 0;
}
should produce something like
This is a
long messa
ge
Test 1 tes
t 2
Test 1 tes <= bonus output
t 2 test 3 <= bonus output
Help
me pl
ease
Enter max number of characters per line : 5
Enter max number of lines : 5
Enter message : It's ok now, I'm doing fine
It's
ok no
w, I'
m doi
ng fi
2 – What is the exact output of the following program? You will not get full marks if you do not include your rough work.
#include <iostream>
#include <iomanip>
#include <string>
#define N 3
using namespace std;
class Member {
int m, p;
char* text;
public:
Member() {text = NULL; m = 0; p = 7;}
Member(const Member& in) {
m = in.m; p = 1;
text = new char[m+1];
strcpy(text, in.text);
}
void operator=(const char* in) {
m = 0;
while (in[m] != '\0' & in[m] != ' ')
m++;
text = new char[m+1];
for (int i = 0; i < m; i++)
text[i] = in[i] + i;
text[m] = '\0';
}
int size() const {return m;}
friend ostream& operator<(ostream& os, const Member in) {
os < ".\n" < setw(5) < in.text < ' ';
return os;
}
~Member() { cout < setw(p) < m < endl; if (text) delete [] text; }
};
class Set {
int n;
Member mem[N];
public:
Set(const char* in) {
int j = 0;
n = 0;
do {
mem[n] = &in[j]; // calls = operator - passes which address?
j += mem[n].size() + 1;
n++;
} while (n < N & in[j-1] != '\0');
}
friend ostream& operator<(ostream& os, const Set& in) {
for (int i = 0; i < in.n; i++)
os < in.mem[i];
return os;
}
};
int main() {
Set str = "Tggp ir is Shp";
cout.fill('.');
cout < left < str < right < ".\n------" < endl;
return 0;
}
Page 3 of 3