#include #include "Complex.h" int Complex::counter=0; // class variables can't be initialized in class definition //const int Complex::counter1=0; // constant class variable /* int Complex::getCounter() { // class method (modifier static must be omitted) return counter; } */ Complex::Complex(): real(0), imag(0) { counter++; } Complex::Complex(double r, double i): real(r), imag(i) { counter++; } Complex::Complex(const Complex& c): real(c.real), imag(c.imag) { counter++; } Complex::~Complex() { counter--; //std::cout << "Destructor" << std::endl; } void Complex::print() const { std::cout << "(" << real << ", " << imag << "i)" << std::endl; //std::cout << "(" << real << ", " << imag << "i)" << counter << std::endl; // methods can access class variables //std::cout << "(" << real << ", " << imag << "i)" << counter++ << std::endl; // class variables can be updated //std::cout << "Object address: " << this << "=(" << this->real << ", " << this->imag << "i)" << this->counter << std::endl; } Complex Complex::plus(const Complex& c) const { Complex temp(real+c.real, imag+c.imag); return temp; }