C++ Class Definition Complex number

Description

C++ Class Definition Complex number

 
#include <iostream>
using namespace std;
class Complex//from  w  ww.  j  av  a  2s  . c  o m
{
   private:
   double realPart;
   double imaginaryPart;
   public:
   Complex(double = 0.0, double = 0.0);   // member function (constructor)
   void assignNewValues(double, double);  // member function
   void showComplexValues();              // member function
};
// implementation section
Complex::Complex(double real, double imag)
{
   realPart = real;
   imaginaryPart = imag;
}
void Complex::assignNewValues(double real, double imag)
{
   realPart = real;
   imaginaryPart = imag;
}
void Complex::showComplexValues()
{
   char sign = '+';
   if (imaginaryPart < 0) sign = '-';
   cout << "The complex number is "
   << realPart << ' ' << sign << ' ' << abs(imaginaryPart) << "i\n";
}
int main()
{
   Complex a, b, c(6.8,9.7);  // declare 3 objects
   // Assign new values to object b's data members
   b.assignNewValues(5.3, -8.4);
   a.showComplexValues();   // display object a's values
   b.showComplexValues();   // display object b's values
   c.showComplexValues();   // display object c's values
   return 0;
}



PreviousNext

Related