Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework #4: Operator Overloading and Strings By J. H. Wang May 12, 2014.

Similar presentations


Presentation on theme: "Homework #4: Operator Overloading and Strings By J. H. Wang May 12, 2014."— Presentation transcript:

1 Homework #4: Operator Overloading and Strings By J. H. Wang May 12, 2014

2 Part I: Hand-Written Exercises 1.What is the difference between a friend function for a class and a member function for the class? What are the advantages of a friend function? 2.Why can’t we overload >> or << as member operators?

3 3. Consider the following code (and assume it is embedded in a complete and correct program and then run): (a) string s1, s2; cout > 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: Los Angeles is the city of Angels.

4 (b) Consider the following code: string s; cout << “Enter a line of input:\n”; cin.getline(s, 11); cout << s << “<End of Output”; If the dialogue begins as follows, what will be the next line of output? Enter a line of input: Los Angeles is the city of Angels.

5 Part II: Programming Exercises 4. Do Programming Project 6.10, the definition of a Weight class, except overload ==, > operators. The == operator should return true if the two weight values are identical, while > should input the weight in pounds. Create appropriate tests for the overloaded operators.

6 (Project 6.10: Create a Weight class that internally stores a weight in kilograms. However, create functions named setWeightPounds and set WeightKilograms that takes an input weight and converts the weight to kilograms, and stores that weight in the class member variable. Also create functions that return the stored weight in kilograms and pounds. Write a main function to test your class. Use the equations below to convert between the two weight scales. 1 kilogram = 2.20462262 pounds 1 pound = 0.45359237 kilogram)

7 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 in a C++ program with a variety of input strings. [Note: Please do not use the standard library functions such as strlen(), strcmp(), and strncmp(). ]

8 Homework Submission Due: 2 weeks (May 26, 2014) Hand-written exercises –Please write your names and answers on papers Programs –Please submit to homework submission Web site: http://mslin.ee.ntut.edu.tw/http://mslin.ee.ntut.edu.tw/

9 Any Questions?


Download ppt "Homework #4: Operator Overloading and Strings By J. H. Wang May 12, 2014."

Similar presentations


Ads by Google