6 thích 0 k thích
100 đã xem

Finish a class called Complex for complex numbers. Write the full runnable program with this class. Following requirements should be satisfied: 

(a) Use double variables for the private data. 
(b) Provide a constructor with default values. 
(c) Provide functions support arithmetic calculation of adding and subtracting with Complex number objects. 
(d) Provide function printing Complex numbers in the form “a + bi”.

class Complex
{
    private:
        double r;
        double i;
    public:
    Complex(double, double);       // Constructor with two arguments
    Complex(double );              // Constructor with one argument
    Complex();                     // default constructor

    Complex operator+ (Complex&);  //Operator overloading
    Complex operator- (Complex&);

    void    print();               // prints the complex number
};

main() function:

int main()
{
    double ar,ai,br,bi,cr;
    cin>>ar>>ai>>br>>bi>>cr;
    Complex a(ar,ai);
    cout << "Constructor with two values: a = ";
    a.print();

    Complex b(br,bi);
    cout << "b = ";
    b.print();

    Complex c(cr);
    cout << "Constructor with one value: c = ";
    c.print();

    c = a + b;
    cout << "a + b = ";
    c.print();

    Complex d;
    cout << "Default constructor: d = ";
    d.print();

    d = a - b;
    cout << "a - b = ";
    d.print();

    return 0;
}

Example input:

1 2 3 4 5

Example output:

Constructor with two values: a = 1 + 2i
b = 3 + 4i
Constructor with one value: c = 5 + 0i
a + b = 4 + 6i
Default constructor: d = 0 + 0i
a - b = -2 - 2i

 

[Normal] Coding (C) - đã hỏi trong Introduction to Computer Programming I (C) bởi (12.1k điểm)
ID: 25999 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn

đã sửa bởi | 100 đã xem
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.38.153
©2016-2024

Những câu hỏi liên quan

0 thích 0 k thích
0 trả lời
[Normal] Coding (C++) - đã hỏi ngày 17 tháng 5 năm 2018 trong Introduction to Computer Programming II (C++)
ID: 52071 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn
| 131 đã xem
12,783 câu hỏi
183,443 trả lời
172,219 bình luận
4,824 thành viên