/* AP 11-04-00	C++ examples	*/

# include <stdio.h>

class number {
protected:
	double _real;
public:
	number( double re = 0 ) { _real = re; }
	virtual ~number() {}
	number & operator=( double re )
		 { _real = re; return *this; }
	operator double() { return _real; }

	virtual void show() 
		{ printf("show number: %f\n", _real ); }
};

class complex : public number {
protected:
	double _img;
public:
	complex( double re = 0, double img = 0 )
		{ _real = re; _img = img; }
	virtual ~complex() {}
	complex & operator=( double re )
		{ _real = re; _img = 0; return *this; }

	virtual void show() 
		{ printf("show complex: %f,%fi\n", _real, _img ); }
};

class complexPtr {
	complex *_p;
public:
	complexPtr( complex * p = NULL ) { _p = p; }
	complexPtr & operator=( complex * p )
		{ _p = p; return *this; }
	operator complex*()
		{ return _p; }
	complex * operator->()
		{ printf("pointer dereferencing: %x\t", this ); return _p; }
};
number a(10);
number b = 200;
number *np;

complex c(3,4);
complexPtr cp;

main() {
	a.show();
	b.show();

	np = new number( 3456 );
	np->show();

	c.show();
	c = 5;
	c.show();

	cp = new complex( 3.4, 76.4 );
	cp->show();

	np = cp;
	np->show();

	return 0;
}

