Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. class ComplexNumber {
  8.     private:
  9.         double real;
  10.         double imaginary;
  11.     public:
  12.         ComplexNumber(void) {
  13.             real = 0.0;
  14.             imaginary = 0.0;
  15.         }
  16.         ComplexNumber(double re, double img) {
  17.             real = re;
  18.             imaginary = img;
  19.         }
  20.         void setnumber(void) {
  21.             cout << "Enter the real part\n";
  22.             cin >> real;
  23.             cout << "Enter the imaginary part\n";
  24.             cin >> imaginary;
  25.             printnumber();
  26.         }
  27.         void printnumber(void) {
  28.             if (imaginary > 0.0) {
  29.                 cout << "The number is: " << real << " + " << imaginary << "i" << endl;
  30.             }
  31.             else {
  32.                 cout << "The number is: " << real << " " << imaginary << "i" << endl;
  33.             }
  34.         }
  35.         double absolute_value(void) {
  36.             double ab_val = sqrt(real*real + imaginary*imaginary);
  37.             return ab_val;
  38.         }
  39.         void addition(double re, double img) {
  40.             cout << "The number is: " << real + re << " " << imaginary + img << "i" << endl;
  41.         }
  42. };
  43.  
  44. int main(void) {
  45.     ComplexNumber c1(2, -2);
  46.     c1.printnumber();
  47.     c1.addition(-2, 2);
  48.     cout << c1.absolute_value();
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement