| | |
complex number
![]() |
•
•
Join Date: Nov 2009
Posts: 5
Reputation:
Solved Threads: 0
the program is ok(i think)but the data is fixed.i want it able to input your own data.i tried cin>>c1.real>>c1.image;
cin>>c2.real>>c2.image;
but it cannot use them because they are private...
cin>>c2.real>>c2.image;
but it cannot use them because they are private...
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; class complex { public: complex(){real=0;image=0;} complex(double r,double i){real=r;image=i;} complex complex_add(complex &c2); complex complex_sub(complex &c2); complex complex_mult(complex &c2); complex complex_div(complex &c2); void display(); private: double real; double image; }; complex complex::complex_add(complex &c2) { complex c; c.real=real+c2.real; c.image=image+c2.image; return c; } complex complex::complex_sub(complex &c2) { complex d; d.real=real-c2.real; d.image=image-c2.image; return d; } complex complex::complex_mult(complex &c2) { complex f; f.real=real*c2.real; f.image=image*c2.image; return f; } complex complex::complex_div(complex &c2) { complex g; g.real=real/c2.real; g.image=image/c2.image; return g; } void complex::display() { cout<<"("<<real<<","<<image<<"i)"<<endl; } int main() { complex c1(2,5),c2(4,9),c3; //i want input my own complex number. c3=c1.complex_add(c2); cout<<"c1=";c1.display(); cout<<"c2=";c2.display(); cout<<"c1+c2=";c3.display(); c3=c1.complex_sub(c2); cout<<"c1=";c1.display(); cout<<"c2=";c2.display(); cout<<"c1-c2=";c3.display(); c3=c1.complex_mult(c2); cout<<"c1=";c1.display(); cout<<"c2=";c2.display(); cout<<"c1*c2=";c3.display(); c3=c1.complex_div(c2); cout<<"c1=";c1.display(); cout<<"c2=";c2.display(); cout<<"c1/c2=";c3.display(); return 0; }
Last edited by Nick Evan; Nov 19th, 2009 at 3:05 pm. Reason: Removed code-tags, added code-tags
•
•
Join Date: Sep 2009
Posts: 1,823
Reputation:
Solved Threads: 227
0
#2 Nov 19th, 2009
Take in 2 doubles from the user and then use your constructor to create a new object.
C++ Syntax (Toggle Plain Text)
double r,im; cin >> r >> im; complex num(r,im);
•
•
Join Date: Nov 2008
Posts: 409
Reputation:
Solved Threads: 77
0
#3 Nov 19th, 2009
I am sorry I don't like jonsca's solution, it has ugly temporary variables, that aren't necessary.
The better way to do this (IMHO) is to add two operators. One is operator<< and the other is operator>>
Now those two functions allow your to write
The only thing you have to write is read/write
I would add checking etc, to read and allow stuff like 4+i5 etc. I normally don't put the endl within the write function, because it is normal to allow a chained set of functions e.g.
The better way to do this (IMHO) is to add two operators. One is operator<< and the other is operator>>
c++ Syntax (Toggle Plain Text)
std::istream& operator<<(std::istream& FX,complex& A) { A.read(FX); return FX; } std::ostream& operator>>(std::ostream& FX,const complex& A) { A.write(FX); return FX; }
Now those two functions allow your to write
c++ Syntax (Toggle Plain Text)
complex c; std::cin>>c; std::cout<<"You entered "<<c<<std::endl; }
The only thing you have to write is read/write
c++ Syntax (Toggle Plain Text)
void complex::write(std::ostream& FX) const { FX<<r<<" "<<i; } // AND void complex::read(std::ostream& FX) { FX>>r>>i; }
I would add checking etc, to read and allow stuff like 4+i5 etc. I normally don't put the endl within the write function, because it is normal to allow a chained set of functions e.g.
std::cout<<"Complex to add are "<<cA<<" + "<<cB<<std::endl; Last edited by StuXYZ; Nov 19th, 2009 at 2:18 am.
experience is the most expensive way to learn anything
![]() |
Similar Threads
- complex number module (C)
- Complex Number Class (C++)
- Complex Number Calculator in C++ (C++)
- Need Help with complex numbers (C++)
- Complex Numbers (C)
- Help on Complex Number Calculator (C)
Other Threads in the C++ Forum
- Previous Thread: Lotto array program with void function
- Next Thread: Set return issue with templated set.
Views: 434 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment beginner binary browser c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete desktop dll dynamic encryption error exception file files filestream fstream function functions game givemetehcodez graph gui helpwithhomework homework i/o iamthwee input int integer lazy library link linker list loop looping math matrix member memory newbie news number object objects opengl operator output parameter path pointer pointers problem program programming project random read recursion recursive reference server sort spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock






