Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework #4: Operator Overloading and Strings By J. H. Wang May 22, 2015.

Similar presentations


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

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

2 Part I: Hand-Written Exercises 1.Is it possible to overload all the operators? 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: A string is a joy forever!

4 (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: A string is a joy forever!

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 that converts a string into an integer. For example, given the string “1234” the function should return the integer 1234. If you do some research, you will find that there is a function named atoi and also the stringstream class that can do this conversion for you. However, in this Programming Project, you should write your own code to do the conversion. Also test your function in a C++ program.

8 Homework Submission Due: 2 weeks (Jun. 5, 2015) 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 or Comments?


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

Similar presentations


Ads by Google