[comp.lang.c++] subclasses with missing members

idsardi@wheaties.ai.mit.edu (Bill Idsardi) (05/18/89)

In reading a book recommended in this newsgroup recently
(Ira Pohl: C++ for C Programmers), there is a discussion
of a class, student, and an inheriting subclass, grad_student,
(pp 147ff):

	enum year {fresh, soph, junior, senior};
	enum support {ta, ra, fellowship, other};
	class student {
	protected:
		int	student_id;
		float	gpa;
	public:
	...
		year	yr;
	...
	}

	class grad_student : public student {
		support	s;
	public:
	...
	}

My question is:  Is there a (simple) way to cause grad_student
to fail to inherit the data member yr from student?

Another example of this would be a class of quadrilaterals
and a subclass of squares.  We might have 4 members representing
side lengths in quad, but only need 1 side length in square.
A similar point could be made about angles.

The obvious way is to invent an abstract class from which
student and grad_student would inherit.  Is there a better
way?

Bill Idsardi
MIT

cowan@marob.MASA.COM (John Cowan) (05/20/89)

In article <2484@wheat-chex.ai.mit.edu> idsardi@wheat-chex.ai.mit.edu (Bill Idsardi) writes:
>In reading a book recommended in this newsgroup recently
>(Ira Pohl: C++ for C Programmers), there is a discussion
>of a class, student, and an inheriting subclass, grad_student,
>(pp 147ff):

[code fragments deleted]

>My question is:  Is there a (simple) way to cause grad_student
>to fail to inherit the data member yr from student?
>
>Another example of this would be a class of quadrilaterals
>and a subclass of squares.  We might have 4 members representing
>side lengths in quad, but only need 1 side length in square.
>A similar point could be made about angles.
>
>The obvious way is to invent an abstract class from which
>student and grad_student would inherit.  Is there a better
>way?

No "better way" is needed.  "Student" is indeed an abstract class by nature,
since all students are either "undergrad_student" or "grad_student".  The
member "yr" is proper to "undergrad_student", and the member "support"
is proper to "grad_student".  The notion that "undergrad_student" should be
called just "student" is, well, undergraduate.  :-)

The question about "square" as a subclass of "quadrilateral" is more
interesting and fundamental.  First of all, the whole question should be
hidden from the outside world by a member function "sidelen" which returns
the length of the Nth side; this would be a virtual function.  Now only the
member function has to know about the internal representation.  Class
"quadrilateral" could have a pointer to a vector of side lengths; in class
"square" this vector contains only one element.
In fact, the whole idea of lengths of sides could perhaps profitably be
"pushed up" the hierarchy to class "polygon", if that class exists.