By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,847 Members | 2,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,847 IT Pros & Developers. It's quick & easy.

Class design

P: n/a
Can I write methods in one class A, that use methods in another base
class B, in such a way that I can make a class C1 that inherits A and
B1 (a child of B) so that the methods in A use the implementations of
B's methods provided by B1? (And also make C2 inheriting A and B2
(another child of B) and C3 etc.). Is it possible to do this entirely
with inheritance, or do you have to store a pointer to a class of type
B in A in order to get this functionality?
In case that was rather dry, here is an example:

Suppose I have an abstract base class Shape, with a few children like
Square, Triangle and Circle. Shape has abtract functions for
rendering, computing the area and so on, but doesn't constrain the
way the shape is specified in the children. render() and area() are
therefore implemented in different ways in each child.

I also have another, separate abstract base class Prism, with children
like SquarePrism, TrianglarPrism, Cylinder. Prism has a pure virtual
function volume(), and this is implemented in each child.

Now, if I make SquarePrism inherit Square, then I can use the area()
function to compute the volume. If I store the length in the base
class Prism then the volume function will be the same for
TrianglarPrism and Cylinder, i.e., area() * length.

My question is: can I write the volume function as a method in Prism,
using the area() function defined in the Shape base class, but make
SquarePrism inherit Prism and Square, TriangularPrism inherit Prism
and Triangle etc. so that the area() function corresponds to the
appropriate shape?

Do I have to make Prism contain a Shape class instead of inherit one?

Jul 10 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

kikazaru wrote:
Can I write methods in one class A, that use methods in another base
class B, in such a way that I can make a class C1 that inherits A and
B1 (a child of B) so that the methods in A use the implementations of
B's methods provided by B1? (And also make C2 inheriting A and B2
(another child of B) and C3 etc.). Is it possible to do this entirely
with inheritance, or do you have to store a pointer to a class of type
B in A in order to get this functionality?
In case that was rather dry, here is an example:

Suppose I have an abstract base class Shape, with a few children like
Square, Triangle and Circle. Shape has abtract functions for
rendering, computing the area and so on, but doesn't constrain the
way the shape is specified in the children. render() and area() are
therefore implemented in different ways in each child.

I also have another, separate abstract base class Prism, with children
like SquarePrism, TrianglarPrism, Cylinder. Prism has a pure virtual
function volume(), and this is implemented in each child.

Now, if I make SquarePrism inherit Square, then I can use the area()
function to compute the volume. If I store the length in the base
class Prism then the volume function will be the same for
TrianglarPrism and Cylinder, i.e., area() * length.

My question is: can I write the volume function as a method in Prism,
using the area() function defined in the Shape base class, but make
SquarePrism inherit Prism and Square, TriangularPrism inherit Prism
and Triangle etc. so that the area() function corresponds to the
appropriate shape?

Do I have to make Prism contain a Shape class instead of inherit one?
I'm not sure I understand what you want, but it seems to me that you
can probably accomplish what you want by passing area() as an argument
to the volume function. Since each inherited class can only use the
area() function of the Square/Circle/Triangle shape, you should be fine
on that aspect.

Bill

Jul 10 '06 #2

P: n/a
Thanks for the reply. I realize there are a few ways I could calculate
the volume, like passing the area, computed with area() to the volume
computation function. The Shape and Prism case is just an example
however.

What I am really wondering, is if there is a way to write methods in
Prism, that rely on methods in Shape, and then create children of
Shape that also inherit Prism so that the methods inherited from
Prism return results based on the particular implementation of the
Shape methods in the child inherited.

Jul 10 '06 #3

P: n/a
Hi,

If your intention is to do the volume calculation just once in the base
class - Prism for all derived classes, then there seems to be a design
problem; since volume is an intrinsic property of derived classes and
should not be calculated in the base.

Anyway, I think what you require can be done using virtual inheritance,
here's a quick and dirty version, note the dreaded diamond in the
design...

class Shape
{
public:
virtual double area()=0;
};

class Square:virtual public Shape
{
public:
double area(){ ... }
};

class Prism:virtual public Shape
{
public:
double vol()
{
return l*area();
}

double l;
};

class SqPrism:public Prism, public Square
{
};

int main()
{
SqPrism s;

s.l = 100;
cout << "vol=" << s.vol() << endl;
}
Thanks and regards
Sonison James

kikazaru wrote:
Can I write methods in one class A, that use methods in another base
class B, in such a way that I can make a class C1 that inherits A and
B1 (a child of B) so that the methods in A use the implementations of
B's methods provided by B1? (And also make C2 inheriting A and B2
(another child of B) and C3 etc.). Is it possible to do this entirely
with inheritance, or do you have to store a pointer to a class of type
B in A in order to get this functionality?
In case that was rather dry, here is an example:

Suppose I have an abstract base class Shape, with a few children like
Square, Triangle and Circle. Shape has abtract functions for
rendering, computing the area and so on, but doesn't constrain the
way the shape is specified in the children. render() and area() are
therefore implemented in different ways in each child.

I also have another, separate abstract base class Prism, with children
like SquarePrism, TrianglarPrism, Cylinder. Prism has a pure virtual
function volume(), and this is implemented in each child.

Now, if I make SquarePrism inherit Square, then I can use the area()
function to compute the volume. If I store the length in the base
class Prism then the volume function will be the same for
TrianglarPrism and Cylinder, i.e., area() * length.

My question is: can I write the volume function as a method in Prism,
using the area() function defined in the Shape base class, but make
SquarePrism inherit Prism and Square, TriangularPrism inherit Prism
and Triangle etc. so that the area() function corresponds to the
appropriate shape?

Do I have to make Prism contain a Shape class instead of inherit one?
Jul 10 '06 #4

P: n/a
Yes! That is exactly what I was looking for. "Virtual
inheritance"... I'm going to have to study that a bit...

Thank you for bothering to write all the class code.

Jul 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.