TAILIEUCHUNG - C++ by Dissection 2002 phần 6

Để kiểm tra sự hiểu biết của bạn, viết một nhà xây dựng hợp lý, được đưa ra hai số nguyên như cổ tức và thương, sử dụng một thuật toán ước số chung lớn nhất để giảm đại diện nội bộ nhỏ nhất của giá trị q. 5. Quá tải sự bình đẳng và các toán tử so sánh cho hợp lý. | ỉra Pohl s C by Dissection Exercises 238 To test your understanding write a rational constructor that given two integers as dividend and quotient uses a greatest common divisor algorithm to reduce the internal representation to its smallest a and q value. 5. Overload the equality and comparison operators for rational. Notice that two rational s are equal in the form given by the previous exercise if and only if their dividends and quotients are equal. See Section Overloading and Signature Matching on page 209. 6. Define class complex as class complex public complex double r real r imag 0 void assign double r double i real r imag i void print cout real imag i operator double return sqrt real real imag imag private double real imag We wish to augment the class by overloading a varietyof opeeators. For exxmple the member function print could be replaced by creating the friend function opera-tor ostream operator ostream out complex x out i return out Also code and test a unary minus operator. It should return a complex whose value in each part is negated. 7. Oor the type compl ex write the binary operator functions add multiply and subtract. Each should return compl ex. Write each as a friend function. Why not write them as member functions 8. Write two friend functions friend complex operator complex double friend complex operator double complex In the absence of a conversion from type double to type complex both types are needed to allow completely mixed expressions of complex and double. Explain why writing one with an int parameter is unnecessary when these friend functions are available. ỉra Pohl s C by Dissection Exercises 239 9. Overload assignment for complex complex complex operator complex c return c If this definition were omitted would this be equivalent to the default assignment that the compiler generates In the presence of the conversion operator for converting complex to doubl e what is the effect of assigning a complex to a double Try

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.