Download HW#4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Large numbers wikipedia , lookup

Addition wikipedia , lookup

History of the function concept wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Function (mathematics) wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Homework #4:
Operator Overloading and
Strings
By J. H. Wang
May 8, 2012
Part I: Hand-Written Exercises
1. What is the difference between an
operator and a function?
2. What can’t we overload >> or << as
member operators?
3. Consider the following code (and assume that it is
embedded in a complete and correct program and then
run):
(a)
string s1, s2;
cout << “Enter a line of input:\n”;
cin >> s1 >> s2;
cout << s1 << “*” << s2 << “<End of Output”;
If the dialogue begins as follows, what will be the next
line of output?
Enter a line of input:
Programming languages are a medium of expression.
(b) Consider the following code:
string s;
cout << “Enter a line of input:\n”;
getline(cin, s);
cout << s << “<End of Output”;
If the dialogue begins as follows, what will be the
next line of output?
Enter a line of input:
Programming languages are a medium of
expression.
Part II: Programming Exercises
• 4. Define a class Rational for rational numbers.
A rational number is a number that can be
represented as the quotient of two integers, for
example, 1/2, 3/4, 64/2, and so forth. Represent
rational numbers as two values of type int, one
for the numerator and one for the denominator.
Include the following functions in your code:
[… to be continued …]
[… continued from previous slide …]
•(1) a constructor with two arguments that can be
used to set the member variables of an object to any
legitimate values.
•(2) a constructor with only a single parameter of
type int; call this single parameter wholeNumber
and define the constructor so that the object will be
initialized to the rational number wholeNumber/1.
•(3) a default constructor that initializes an object to
0 (that is, to 0/1).
[… to be continued …]
[… continued from previous slide …]
•(4) overloaded input and output operators >>
and <<. Numbers are to be input and output in the
form 1/2, 15/32, 300/401, and so forth. (Note that
the numerator, the denominator, or both may
contain a minus sign, so -1/2, 15/-32, and -300/401 are also possible inputs.)
•(5) overloaded operators: ==, <, <=, >, >=, +, -, *,
and /.
[… to be continued …]
[… continued from previous slide …]
•(6) a function to normalize the values stored so
that, after normalization, the denominator is positive
and the numerator and denominator are as small as
possible. (For example, after normalization 4/-8
would be represented the same as -1/2.)
Write a test C++ program to test your class.
[Hint: Two rational numbers a/b and c/d are equal if
a*d equals c*b. If b and d are positive rational
numbers, a/b is less than c/d provided a*d is less than
c*b.]
5. Write a function to compare two C-strings for
equality. The function should return true if the
strings are equal and false if they are not. Your
function should ignore case, punctuation, and
whitespace characters. Test your function with
a variety of input strings.
[Note: Do not use the standard library function
strcmp(). ]
Homework Submission
• Due: 2 weeks (May 22, 2012)
• Hand-written exercises
– Please write your names and answers on
papers
• Programs
– Please submit to homework submission Web
site: http://140.124.183.39/oop/
Any Questions?