/* kinds.cpp: what the three kinds of constructors are for
 *   g++ -Wall -Wextra -ansi -pedantic kinds.cpp -o kinds && ./kinds
 */
#include <stdio.h>

/* ---- an abstract data type: a value, like double ---- */

class Complex {
    double re, im;
public:
    Complex() : re(0), im(0) { printf("  default\n"); }
    Complex(double r) : re(r), im(0) { printf("  conversion\n"); }
    Complex(const Complex& c) : re(c.re), im(c.im) { printf("  copy\n"); }
    Complex(double r, double i) : re(r), im(i) {}
    double Re() const { return re; }
    double Im() const { return im; }
};

Complex operator+(const Complex& a, const Complex& b)
{
    return Complex(a.Re() + b.Re(), a.Im() + b.Im());
}

double modulus2(Complex c)              /* by value */
{
    return c.Re() * c.Re() + c.Im() * c.Im();
}

/* ---- objects in the OOP sense ---- */

class Animal {
public:
    virtual ~Animal() {}
    virtual const char *Voice() const { return "..."; }
};

class Dog : public Animal {
public:
    virtual const char *Voice() const { return "woof"; }
};

void by_address(const Animal& a) { printf("  %s\n", a.Voice()); }
void by_value(Animal a) { printf("  %s\n", a.Voice()); }

int main()
{
    printf("Complex a;\n");
    Complex a;
    printf("Complex b = 2.5;\n");
    Complex b = 2.5;
    printf("Complex c = b;\n");
    Complex c = b;
    printf("Complex z[3];\n");
    Complex z[3];
    printf("b + 1\n");
    Complex d = b + 1;
    printf("modulus2(d)\n");
    double m = modulus2(d);
    printf("%g %g %g\n", a.Re() + c.Re() + z[0].Re(), d.Re(), m);

    Dog dog;
    printf("by_address(dog)\n");
    by_address(dog);
    printf("by_value(dog)\n");
    by_value(dog);
    return 0;
}
