TAILIEUCHUNG - A Complete Guide to Programming in C++ part 48

A Complete Guide to Programming in C++ part 48. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | EXERCISE 449 Exercise Enhance the numerical class Fraction which you know from the last chapter to convert both double values to fractions and fractions to double. In addition fractions should be rounded after arithmetic operations. First declare the simplify method for the Fraction class and insert the definition on the opposite page in your source method computes the largest common divisor of numerator and denominator. The numerator and the denominator are then divided by this value. Add an appropriate call to the simplify function to all operator functions except and -- . Then add a conversion constructor with a double type parameter to the class. Example Fraction b yields the fraction 1 2 Double values should be converted to fractions with an accuracy of three decimal following technique should suffice for numbers below one million. Multiply the double value by 1000 and add for rounding. Assign the result to the numerator. Set the value of the denominator to proceed to simplify the fraction. You now have a conversion constructor for long and double allow for conversion of int values to fractions you must write your own conversion constructor for int Now modify the class to allow conversion of a fraction to a double type number. Define the appropriate conversion function inline. Use the function main to test various type conversions. More specifically use assignments and arithmetic functions to do compute the sum of a fraction and a floating-point number. Output the operands and the results on screen. 450 CHAPTER 20 TYPE CONVERSION FOR CLASSES SOLUTION --------------------------------------------------------- A numerical class to represent fractions. The class converts Fraction -- double and simplifies fractions. --------------------------------------------------------- ifndef _FRACTION_ define _FRACTION_ include include class Fraction private long numerator denominator public

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.