C++ Tutorial/Data Types/Your complex

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Your Complex Number

<source lang="cpp">#include <iostream>

  1. 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;

}</source>