- (____ / 3 points) Below is a partial definition for a Letter
class, which represents case-insensitive letters of the alphabet.
Notice that letter.h uses the isalpha() function, and
letter.cpp uses the tolower() function, both of
which require #include <ctype.h>. Use this observation
to answer the following questions:
letter.h |
a) Putting good coding style aside for the moment, what is the
absolute minimum number of #include <ctype.h> lines
you would add to one or both files to make the program compile?
Please circle Part A at each place you would add the line.
b) Now consider good coding style. Where should the
#include <ctype.h> be placed? Please circle Part B
in one or both files where you would add the line.
c) Suppose the definition of Letter (char) was moved to
letter.cpp Circle Part C in one or both files where
you would add #include <ctype.h>.
|
#if !defined (__LETTER_H__)
#define __LETTER_H__
#include <ctype.h> // Circle: Part A, Part B, Part C
class Letter {
public:
Letter (char c) :_c(c) { if (!isalpha(c)) exit(1); }
friend bool operator == (const Letter & L1, const Letter & L2);
// Other prototypes
private:
char _c;
};
#endif
|
letter.cpp |
#include "letter.h"
#include <ctype.h> // Circle: Part A, Part B, Part C
bool operator == (const Letter & L1, const Letter & L2) {
return (tolower(L1._c) == tolower(L2._c));
}
// Other definitions
|
- (____ / 4 points) Seperate Compilation, Short Answer
a) When a cpp file with no errors is compiled,
what kind of file is produced?
Object File: (.obj)
b) Assuming that the entire compilation process goes
well, what file is produced as a result?
Executable File: (.exe)
c) In which file is the class implementation found?
Implementation File (.cpp)
d) In which file is the class interface found?
Interface (Header) File (.h)
- (____ / 2 points) Explain in one or two short sentences the purpose of
the following lines in the header file above:
#if !defined (__LETTER_H__)
#define __LETTER_H__
// contents of header file here
#endif
|
Prevents the contents of the header file from being processes more than once.
If the header file contains definitions (like variable or class definitions),
then multiple definitions are avoided.
|
- (____ / 5 points) Strings, Short Answer
a) (____ / 1 point) How is a C-string (old-style string) represented
in C++?
As a character array, for example: char s[50];
|
b) (____ / 1 point) By convension, all C-strings are terminated with
what exactly?
c) (____ / 1 point) How many characters will message
hold? char message[] = "I'm hungry";
Including the null character: 11
|
d) (____ / 2 points) Name two advantages C++ string objects have over
C-strings.
- The string class is an ADT, so conventional operators (like +) are
overloaded. With C-string, you must call external functions like
strcat.
- A string object can be returned from a function, whereas a character
array cannot.
- With string objects, there is no need to worry about the '\0'
character.
- String objects can have dynamic length, whereas C-strings have fixed
maximum length.
|
- (____ / 6 points) Writing Code
a) (____ / 4 points)
Write the definition of the following member function
Reverse which reverses the order of the characters in a string.
For example, the reverse of "I'm hungry" is
"yrgnuh m'I". Assume that the private data is a C-string
called _s.
// Assume that swap and strlen functions exist
void swap (char & c1, char & c2); // Swaps two characters
int strlen (const char s[]); // Returns length of C-string
void string::Reverse () {
int L = strlen (_s);
for (int i = 0 ; i < L/2 ; i++) swap (_s[i], _s[L-i-1]);
}
|
b) (____ / 2 points) Now demonstrate the use of your Reverse
function by declaring a string object, initialize it to a string
of your choice, and then calling your Reverse function on that
string.
string s = "I'm hungry";
s.Reverse();
|
|