Path: utzoo!attcan!uunet!know!zaphod.mps.ohio-state.edu!wuarchive!usc!rutgers!aramis.rutgers.edu!remus.rutgers.edu!schaum From: schaum@remus.rutgers.edu (schaum) Newsgroups: comp.lang.c++ Subject: c++ followup: abstract classes Keywords: idiot stupid tired Message-ID: Date: 11 Dec 90 18:41:32 GMT Organization: Rutgers Univ., New Brunswick, N.J. Lines: 97 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!