C++ Tutorial/Data Types/Your complex


Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
Перейти к: навигация, поиск

Your Complex Number

#include <iostream>
#include <math.h>
using namespace std;
class complex {
public:
   complex(double r) { real = r; imag = 0; }
   void  assign(double r, double i) { real = r; imag = i; }
   void  print() { cout << real << " + " << imag << "i "; }
   operator double() { return (sqrt(real * real + imag * imag));}
   friend ostream& operator<<(ostream& out, complex x);
private:
   double real, imag;
};
ostream& operator<<(ostream& out, complex x)
{
   out << x.real << " + " << x.imag << "i ";
   return out;
}
int main()
{
   complex c1(50.3);
   cout << c1;
}