[問題] >>過載與class裡的private之問題!
設立一個數學的虛實數物件(object),例如:0+0i(預設)
目的:輸入實部和虛部的數字並輸出,例如:輸入1 +2i則輸出1+2i
問題:目前輸入1 2後依舊跑出預設(已解決)
紅色字為修正部分
#include<iostream>
#include <cstdlib>
using namespace std;
class Complex
{
private:
double real,imag;
public:
Complex::Complex():real(0),imag(0){};
void input(int a,int b);
double getRe() ;
double getIm() ;
friend istream& operator >>(istream& inStr,Complex& recive);
friend ostream& operator <<(ostream& ouStr,Complex& recive);
void input(double a,double b);
};
void Complex::input(double a,double b)
{
real=a;
imag=b;
}
istream& operator >>(istream& inStr,Complex& recive)
{
cout<<"請輸入實數及虛數部分\n";
double a,b;
inStr>>a>>b;
recive.input(a,b);
return inStr;
}
int main()
{
Complex test;
cin>>test;
cout<<"現在輸出TEST輸入結果:"<<test<<endl;
system("pause");
}
ostream& operator <<(ostream& ouStr,Complex& recive)
{
cout<<"輸出結果為";
ouStr<<recive.getRe();
cout<<"+";
ouStr<<recive.getIm();
cout<<endl;
}
double Complex::getIm()
{ return imag; }
double Complex::getRe()
{ return real; }
※ 編輯: satokuzao 來自: 140.117.181.63 (04/21 22:13)
→
04/21 22:13, , 1F
04/21 22:13, 1F
→
04/21 22:15, , 2F
04/21 22:15, 2F
※ 編輯: satokuzao 來自: 140.117.181.63 (04/21 22:20)
→
04/21 22:21, , 3F
04/21 22:21, 3F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章