Advertisement
marwanpro

complex number (class example)

Oct 16th, 2017
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. class NbComplexe
  8. {
  9. public:
  10.    
  11.  
  12.     NbComplexe();
  13.     NbComplexe(float re, float im);
  14.     NbComplexe(NbComplexe const&);
  15.     ~NbComplexe();
  16.  
  17.     void input ()
  18.     {
  19.         cout << "Saisir la partie réelle: ";
  20.         cin >> re;
  21.         cout << "Saisir la partie imaginaire: ";
  22.         cin >> im;
  23.     }
  24.  
  25.     void output ()
  26.     {
  27.         if (im >= 0) { cout << re << "+" << im << "i" << endl; }
  28.         else { cout << re << "-" << abs(im) << "i" << endl; }
  29.     }
  30.  
  31.     float real() const
  32.     {
  33.         return re;
  34.     }
  35.  
  36.     float imaginary() const
  37.     {
  38.         return im;
  39.     }
  40.  
  41. private:
  42.     float re, im;
  43.  
  44. };
  45.  
  46. NbComplexe::NbComplexe(void)
  47. {
  48.     re = 0;
  49.     im = 0;
  50. }
  51.  
  52. NbComplexe::NbComplexe(float real, float imaginary)
  53. {
  54.     re = real;
  55.     im = imaginary;
  56. }
  57.  
  58. NbComplexe::NbComplexe(NbComplexe const &nb_complexe)
  59. {
  60.     re = nb_complexe.real();
  61.     im = nb_complexe.imaginary();
  62. }
  63.  
  64. NbComplexe::~NbComplexe()
  65. {
  66.    
  67. }
  68.  
  69.  
  70.  
  71.  
  72. int main (int, char**)
  73. {
  74.     NbComplexe* complexe1 = new NbComplexe();
  75.     complexe1->input();
  76.     complexe1->output();
  77.     system("pause");
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement