#include "complex.h"                // Ex45
#include <cmath>                    // access: sqrt
using namespace std;
// private declarations
static int cplxObjCnt = 0;
struct complex::complexImpl {        // actual implementation, nested class
    double re, im;
};
// interface declarations
void complexStats() { cout << cplxObjCnt << endl; }
complex::complex() : impl(*new complexImpl) { impl.re = 0.; impl.im = 0.; cplxObjCnt += 1; }
complex::complex( double r ) : impl(*new complexImpl) { impl.re = r; impl.im = 0.; cplxObjCnt += 1; }
complex::complex( double r, double i ) : impl(*new complexImpl) { impl.re = r; impl.im = i; cplxObjCnt += 1; }
complex::~complex() { delete &impl; }
complex::complex(const complex &c) : impl(*new complexImpl) {
    impl.re = c.impl.re; impl.im = c.impl.im; cplxObjCnt += 1;
}
complex &complex::operator=(const complex &c) {
    impl.re = c.impl.re; impl.im = c.impl.im; return *this;
}
double complex::abs() { return sqrt( impl.re * impl.re + impl.im * impl.im ); }
complex operator+( complex a, complex b ) { return complex( a.impl.re + b.impl.re, a.impl.im + b.impl.im ); }
ostream &operator<<( ostream &os, complex c ) { return os << c.impl.re << "+" << c.impl.im << "i"; }
