[comp.lang.c++] c++ followup: abstract classes

schaum@remus.rutgers.edu (schaum) (12/12/90)

I wrote:

>I and my group partners are constructing a calculator which operates
>on integers, matrices and sparse matrices.  Our current heirarchy is
>as follows:
>
>			Operand
>			    |
>			   /|\
>			  / | \
>		    Integer |  Sparse
>			    |
>			Matrix
>
>Class operand is an abstract class containing no data and only virtual
>member functions which are defined in the subclasses.  Our goal is to
>produce a client class Calculator which evaluates expressions in
>reverse Polish notation, doing addition, subtraction and
>multiplication on objects of subclasses of operand, passing parameters
>of type operand in order to be able to pass any of the subclasses as
>parameters.
>
>In Eiffel, this would be the case, that something of operand could be
>assigned any of the subclasses.  Can this be done in C++, or are we
>WAY off base on this one?
>
>Suggestions are VERY welcome!
>
>(i.e., could we say:
>
>	operator *op1, *op2;


	/*This a typo -- 8hrs of sleep in 48 gets me kinda funny that way*/
	/*Sorry about that  !!!!!!!!!!!*/

>
>	op1 = new integer;
>	op2 = new matrix;
>
>//this yields errors, so obviously, we screwed up.)
>
>:Chaz:
>
>schaum@remus.rutgers.edu
>

NOTE:
	I had defined class operator with a set of virtual functions.
	Each class: integer, matrix, sparse_matrix inherited publicly
	from operand.  Yet I could not assign a pointer of type class
	operand to an integer, matrix, or sparse_matrix.  I eventually
	wrote a struct within class operand containing one object of
	integer, one of matrix and one of sparse_matrix.  I got kind
	of pissed at Borland Turbo C++ for this.

ex:

class base
{
	public:
	virtual void foo(){};
};

class sub1 : public base
{
	void foo() {printf("sub1\n");}
};

class sub2 : public base
{
	void foo() {printf("sub2\n");}
};

main()
{
	base *a;
	sub1 d;

	a = &d;
	a->foo();
}

//Do not assume that I am using correct syntax.

//I tried a similar test program with Turbo C++.  The program did not
//compile and I could not find my error.  C++ should have
//polymorphism.  Obviously, I must be stupid, ignorant, or something.

:Chaz:
-- 

	  o  ___          ___   o	Old enough to know,
	    /    /  /  /|   /		but young enough not to care.
	   /    /--/  /-| -/-
	o /___ /  /  /  | /__ o		Snagglepuss evannahh!