簡體   English   中英

使用構造函數的運算符重載加兩個復數

[英]Operator overloading using constructor add two Complex number

我想通過使用構造函數從用戶那里獲得一個值,另一個值將在程序本身中。 我嘗試將其編碼如下。 但是構造函數正在將所有值初始化為0,0(無參數)。 該怎么辦?

#include<iostream>

using namespace std;

class complex
{
      float x;
      float y;
      public : 
             complex() //no argument constructor
             {
                    /* cout<<"Enter real = ";
                      cin>>x;
                      cout<<"Enter imaginary = ";
                      cin>>y;*/
             }
             complex(float real, float imag)
             {

                      cout<<"Enter real = ";
                      cin>>x;
                      cout<<"Enter imaginary = ";
                      cin>>y;
                           x = real;
                           y = imag;
             }
             complex operator+(complex);
             void display(void);
};

complex complex :: operator+(complex c)
{
        complex temp;
        temp.x = x + c.x;
        temp.y = y+c.y;
        return(temp);
}
void complex :: display(void)
{
     cout<<x<<" +i"<<y<<"\n";
}
int main()
{
    complex c1,c2(2.5,1.7),c3(0,0);
    c3 = c1+c2;
    c1.display();
    c2.display();
    c3.display();
    system ("PAUSE");
   // return 0;
}

以下可能是您想要的。 我將您的復數類更改為僅使用一個具有默認參數的構造函數,如果您不提供任何參數,則兩者都默認為0。

在您的代碼中,參數少函數不執行任何操作,並且默認情況下僅對x和y使用默認構造函數(對於使用0的浮點數)。 您可以使用默認參數將其與帶參數的構造函數結合使用,如上所述。

這明確表明,如果不向Complex構造函數提供值,則應該期望x和y為0。

我還添加了輸入和輸出流運算符,但這對於您的使用可能不是必需的。

但是它允許您使用cin >> c1; 很明顯,您希望用戶輸入c1的值,而不是將該代碼嵌入默認構造函數中。

#include<iostream>
using namespace std;

class complex{
    float x;
    float y;
  public :
    // it seems like one constructor with default parameters
    // should work for your case.
    complex(float real = 0, float imag = 0):x(real),y(imag){}
    complex operator+(complex);
    friend ostream& operator << (ostream& outs, Complex C);
    friend istream& operator << (istream& ins, Complex C);
};

ostream& operator << (ostream& outs, Complex C){
  cout << C.x << " + i" << C.y;
  return outs;
}

istream& operator << (istream& ins, Complex C){
  if (ins == cin){
    cout << "Enter real part" << endl;
    ins >> C.x;
    cout << "Enter imaginary part" << endl;
    ins >> C.y;
  } else {
    ins >> C.x >> C.y;
  }
  return ins;
}

// your plus operator is fine.

int main(){
    complex c1,c2(2.5,1.7),c3(0,0); //c1 will have x = 0, y = 0
    c3 = c1+c2;  // c3.x = 2.5, c3.y = 1.7
    cout << c1 << endl;  // displays 0 + i0
    cout << c2 << endl;  // displays 2.5 + i1.7
    cout << c3 << endl;  // displays 2.5 + i1.7
    return 0;
}

輸出:

0 + i0
2.5 + i1.7
2.5 + i1.7

如果這不是您期望的輸出,您會期望什么?

我做了幾個假設,並相應地修改了您的代碼,這些假設是:

  1. 第二個構造函數從main函數中存在的值構造值,因此我們需要一個不同的構造函數來使用輸入創建對象
  2. c1包含用戶輸入的值,而c3是使用默認構造函數創建的

牢記以上幾點,可以對代碼進行如下修改:

    #include<iostream>

using namespace std;

class complex
{
      float x;
      float y;
      public :
             complex() {}
             complex(float real, float imag) { x=real; y=imag; } //constructor for creating x and y from values given in the code
             complex(istream&);//constructor for creating values entered as input
             complex operator+(complex);
             void display(void);
};

//definition for constructor for taking user input
complex::complex(istream& in)
{
    cout<<"Enter real = ";
    in>>x;
    cout<<"Enter imaginary = ";
    in>>y;
}

complex complex :: operator+(const complex& c) const
{
        complex temp;
        temp.x = x + c.x;
        temp.y = y+c.y;
        return(temp);
}
void complex :: display(void)
{
     cout<<x<<" +i"<<y<<"\n";
}
int main()
{
    complex c1(cin),c2(2.5,1.7),c3;
    //note the changes, c1 is made from user input, c2 from constructor and c3 using default constructor
    c3 = c1+c2;
    c1.display();
    c2.display();
    c3.display();
}

記下我在注釋行中指出的修改

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM