0 喜歡 0 不喜歡
261 瀏覽

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”.

寫一個Complex類別為滿足下列條件:

(a) 私有變數型態為double

(b) 提供預設建構函數

(c) 提供多載加減複數運算子函數

(d) 提供印出複數"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();
      cout << endl;

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

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

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

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

    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

 

[正常] Coding (C++) - 最新提問 分類:Introduction to Computer Programming II (C++) |
ID: 52071 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 261 瀏覽
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.126.11
©2016-2024

相關問題

6 喜歡 0 不喜歡
0 回答
[正常] Coding (C) - 最新提問 10月 10, 2017 分類:Introduction to Computer Programming I (C) | 用戶: thopd (12.1k 分)
ID: 25999 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 234 瀏覽
12,783 問題
183,443 回答
172,219 留言
4,824 用戶