You are here: Technology, Web & Business Forum
: Web Development
: Programming
:
Overloading << and >> operators in C++?
|
Welcome to the Technology, Web & Business Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Programming Discuss programming languages such as .NET, PHP, PERL, ASP, ColdFusion, C++
|
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
|||
I'm writing a class(.h) file involving complex numbers and need to overload the << and >> so I can use cin and cout .
The class allows users to use complex numbers in the form a+bi. So, the private parts of the class are real(a part), imag(b part) and expoi (the exponent of i. so 3 = expoi in i^3) I succefully overloaded all the other things, so here's what the class can do. class Complex // keeps track of a Number in the form a +bi { private: double real; // this is the "a" part double imag; // this is the "b" part int expoi; // this is the exponent of i public: double Real();// Returns Real part of number double Imaginary(); // Returns imaginary part of number void Show(); // prints out complex number on screen Complex operator +(Complex Num); Complex operator -(Complex Num); Complex operator *(Complex Num); Complex operator=(Complex Num); Complex operator==(Complex Num); Complex operator<<(Complex Num); Complex operator>>(Complex Num); }; I did the << operator as void funtion but my prof wants me to overload the operator. void Complex :: Show() { cout << real; if(expoi %4 == 0) // if expoi is divisible by 4 then i = 1 { if (imag < 0) cout << " - "<< imag*-1 << endl; else cout << " + " << imag << endl; } else if(expoi %3 == 0) // if expoi is divisible by 3 then i = -i { if (imag < 0) cout << " + "<< imag*-1 << 'i' << endl; else cout << " - " << imag << 'i' << endl; } else if (expoi %2 == 0) // if expoi is not divisible by 4 but divisible by 2 then i = -1 { if (imag < 0) cout << " + "<< imag*-1 << endl; else cout << " - " << imag << endl; } else // if expoi is not divisible by 2,3,or 4 then i = i { if (imag < 0) cout << " - "<< imag*-1 << 'i' << endl; else cout << " + " << imag << 'i' << endl; } }
__________________
Powered by Yahoo! Answers |



Linear Mode
