Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Complex {
- private:
- float x; float y;
- public:
- Complex(){ x = 0; y = 0; }
- Complex operator +(Complex PARAM){
- Complex tempa;
- tempa.x = x+PARAM.x;
- tempa.y = y+PARAM.y;
- return (tempa);
- }
- Complex operator *(Complex PARAM) {
- Complex temp;
- temp.x = (x*(PARAM.x)-y*(PARAM.y));
- temp.y = (x*(PARAM.y)+y*(PARAM.x));
- return (temp);
- }
- friend istream &operator >> (istream &INPUT, Complex &OBJ){
- cout << "Enter Real Part: "; INPUT >> OBJ.x;
- cout << "Enter Imaginary Part: "; INPUT >> OBJ.y;
- }
- friend ostream &operator << (ostream &OUTPUT, Complex &OBJ){
- OUTPUT << OBJ.x << "+" << OBJ.y << "i" << endl;
- }
- };
- int main(){
- Complex c1, c2, c3, c4, c5;
- cout << "\nDefault Initialized Complex Number: "; cout << c1 << endl;
- cout << "For the First Complex Number: " << endl; cin >> c2;
- cout << "\nFor the Second Complex Number: " << endl; cin >> c3;
- cout << "\nThe First Complex Number is: "; cout << c2;
- cout << "The Second Complex Number is: "; cout << c3;
- c4 = c2 + c3;
- cout << "\nSum of these Complex Numbers is: "; cout << c4;
- c5 = c2 * c3;
- cout << "Product of these Complex Numbers is: "; cout << c5;
- cout << endl;
- return 0;
- }
- /*
- student@student:~/Documents/SB57_LAB/OOP/complex$ ./main.exe
- Default Initialized Complex Number: 0+0i
- For the First Complex Number:
- Enter Real Part: 8
- Enter Imaginary Part: 5
- For the Second Complex Number:
- Enter Real Part: 4
- Enter Imaginary Part: 3
- The First Complex Number is: 8+5i
- The Second Complex Number is: 4+3i
- Sum of these Complex Numbers is: 12+8i
- Product of these Complex Numbers is: 17+44i
- */
Add Comment
Please, Sign In to add comment