Tut1 TRƯỜNG ĐAỊ HOC̣ BÁCH KHOA TP HCM Khoa Khoa hoc̣ & Kỹ thuật Máy tính Kỹ thuật lập trình 501127 – HK2/2011 2012 1 KỸ THUẬT LẬP TRÌNH Bài tập 6 – Tuần 15 Lớp, thao tác đối tượng và tính kế t[.]
TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa ho ̣c & Kỹ thuật Máy tính KỸ THUẬT LẬP TRÌNH Bài tập – Tuần 15 Lớp, thao tác đối tượng tính kế thừa Bài tập bắt buộc: Bài Cho class biểu diễn số phức a + bi sau: class Complex{ protected: float a,b; public: } Yêu cầu: - Tạo constructor khởi trị a = b = - Tạo hàm set giá trị a, b - Hiện thực operator =; +; -; *; /, ^(luỹ thừa) - Viết hàm tính magnitude - Viết operator ==, !=, =, dựa độ lớn magnitude - Viết hàm in số phức dạng x+iy class Complex{ protected: float a,b; public: Complex(); // constructor void set(float, float); //set a Complex void print(); // print a Complex void sva(Complex); // set value = Complex void add(Complex); // + Complex void sub(Complex); // void mul(Complex); // * void div(Complex); // / void exp(int); // exponentiation float mag(); // |z| bool equ(Complex); // equal a Complex bool neq(Complex); // not equal bool leq(Complex); // less than or equal bool geq(Complex); // greater than or equal bool les(Complex); // less than bool gre(Complex); // greater than }; Complex::Complex(){ a=b=0; Kỹ thuật lập trình 501127 – HK2/2011-2012 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa ho ̣c & Kỹ thuật Máy tính } void Complex::set(float x, float y){ a=x; b=y; } void Complex::print(){ cout