Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Complex
- {
- private:
- double real, imag;
- public:
- Complex(double, double);
- void print();
- double abs();
- friend Complex add(Complex, Complex);
- friend Complex sub(Complex, Complex);
- friend Complex mul(Complex, Complex);
- };
- Complex::Complex(double real, double imag)
- {
- this->real = real;
- this->imag = imag;
- }
- void Complex::print()
- {
- cout << real << " + " << imag << "*i" << endl;
- }
- double Complex::abs()
- {
- return(sqrt(real*real + imag * imag));
- }
- Complex add(Complex c1, Complex c2)
- {
- Complex c3(c1.real + c2.real, c1.imag + c2.imag);
- return c3;
- }
- Complex sub(Complex c1, Complex c2)
- {
- Complex c3(c1.real - c2.real);
- }
- Complex mul(Complex c1, Complex c2)
- {
- }
- int main()
- {
- double r, i;
- cout << "Enter the real and imaginary parts of the first complex number: ";
- cin >> r >> i;
- Complex c1(r, i);
- cout << "You have entered the following complex number: ";
- c1.print();
- cout << "Its absolute value is " << c1.abs() << endl << endl;
- cout << "Enter the real and imaginary parts of the second complex number: ";
- cin >> r >> i;
- Complex c2(r, i);
- cout << "You have entered the following complex number: ";
- c2.print();
- cout << "Its absolute value is " << c2.abs() << endl << endl;
- Complex c3 = add(c1, c2);
- cout << "Their sum is: ";
- c3.print();
- cout << "The absolute value of the sum is " << c3.abs() << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement