(a) Briefly explain the difference between
virtual functions and overloading. [2 marks]
(a) Overloading is where a function
can be called with different parameter lists; (1 mark)
Virtual functions are where the interpretation of what a member
call means must be decided at run time; (1 mark)
[2 marks]
(b) Consider the following definition
of the class String:
class String {
char* str;
int length;
public:
String();
String(char s[]);
int Length();
char* CharAt(int index);
}
(i) Add to the class a member function, called UpperCase, the signature
of which is given below, which converts all of the lower case elements
of the string to upper case, leaving upper case characters untouched.
You may wish to use the library function ToUpper, the signature of
which is given below, which takes in a single character and returns
its upper case equivalent. You are not required to implement ToUpper.
char ToUpper(char* x);
void String::UpperCase(); [3 marks]
(i) A sample definition of UpperCase follows:
void string::UpperCase() {
int
i;
for(i=
0; i< Length(); i++) {
str[i]=
ToUpper(CharAt(i));
}
}
And the following marking scheme should be used:
Declaring a loop bound by the length of the string; (1 mark)
Using CharAt to examine the ith element; (1 mark)
And updating it using ToUpper; (1 mark)
[3 marks]
(ii) Implement the function, StrCaseCompare, the signature of which
is given below, which takes in two references to string objects and
returns the result of comparing the two string objects, ignoring the
case of respective characters. If the two strings are the same irrespective
of case, the function should return 1, or 0 otherwise.
int StrCaseCompare(String a, String b); [5 marks]
(ii) A sample definition of StrCaseCompare
follows:
int StrCaseCompare(string a, string b) {
int
i;
if(a.Length()
!= b.Length())
return
0;
for(i=
0; i< a.Length(); i++) {
if(
(a.CharAt(i)!= b.CharAt(i)) ||
(*(a.CharAt(i))!=
b.ToUpper(b.CharAt(i))) ) {
return
0;
}
}
return
1;
}
And the following marking scheme should be used:
Declaring a loop bound by the length of the strings; (1 mark)
Returning 0 if the strings are different length; (1 mark)
Checking the ith character on each iteration; (1 mark)
With an or condition to see if it matches either the upper
or lower case
equivalent; (1 mark)
Returning 1 if all the comparisons are correct; (1 mark)
[5 marks]
(iii) Give an overloading of the operator ==, such that it compares
the two strings given to it as operands, and returns a pointer to
the first character in the strings which differ. Note that this test
should not be case independent. [5 marks]
(iii) A sample definition of the operator
follows:
char* operator == (string& S) {
int
i;
for(i=
0; i < S.Length(); i++) {
if(CharAt(i)!=
S.CharAt(i))
return
CharAt(i);
}
retrun
null;
}
And the following marking scheme should be used:
Declaring the signature of the operator; (1 mark)
With the correct parameters; (1 mark)
Declaring a loop bound by the length of one of the strings;
(1 mark)
Comparing the characters on each iteration; (1 mark)
And returning the reference; (1 mark)
[5 marks]
|