homework #4: operator overloading and strings by j. h. wang may 8, 2012

11
Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

Upload: bruce-shelton

Post on 02-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

Homework #4:Operator Overloading and

StringsBy J. H. WangMay 8, 2012

Page 2: 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?

Page 3: Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

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.

Page 4: Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

(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.

Page 5: Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

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 …]

Page 6: Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

[… 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 …]

Page 7: Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

[… 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 …]

Page 8: Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

[… 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.]

Page 9: Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

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(). ]

Page 10: Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

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/

Page 11: Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012

Any Questions?