473,378 Members | 1,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Class design

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
4 1830

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Aguilar, James | last post by:
My previous example used the concept of a Shape class heirarchy, so I will continue with that. Suppose I have something like fifty different shapes, and I am trying to instantiate one of them. ...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
3
by: fernandez.dan | last post by:
I'm still learning how to use Object Oriented concepts. I'm have a basic question dealing with design. I have two classes that deal with I/O pertaining to network and usb that inherit from an...
9
by: David A. Osborn | last post by:
I have a set of classes that each have an enumeration in them, and based on dynamic input I need to access a different enumeration. For example Three classes Class_A, Class_B, and Class_C that...
3
by: Trammel | last post by:
Hi, I recently upgraded to VB.net from VB6.. and woah... I feel lost :¬O One of my reasons for upgrading is I was told that VB.net can do class inheritance and subclassing easier. ...
6
by: Orgun | last post by:
Hi, I sent this message to the moderated c++ group too but it is waiting for moderator approval and I wanted to send here too. I am new to Design Patterns. I want to write a simple...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
5
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the...
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.