473,973 Members | 8,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1871

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
4823
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. The trick is, the instatiation will be based on a string with the exact same name as the type that I am trying to instantiate. Obviously, writing a fifty element long switch statement is an inferior solution. So, how can I instantiate based on...
15
9129
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 the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
3
1829
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 abstract parent with four basic functions: Open, Read, Write, and Close. // Note alot of the stuff has been left out // just to give a basic picture class IO { public:
9
2251
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 all have a different enumeration in them called Properties. I want to basically have a variable The_Class that I can dynamically point to either Class_A, Class_B, or Class_C and then do The_Class.properties to get the correct enumeration. How...
3
1947
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. Would someone be so kind as to provide a small demo about classes for some
6
2094
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 DeviceManager which is only interested in CD/DVD devices. I want to get the list of CD/DVD devices and "be informed when a disc inserted into a device". I am developing this on Linux. So, I used HAL API and read some system (actually /proc) files to gather...
25
2173
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 if the parameter model_name is "aa", then choose ModelAA. Currently I do this as follows:
6
2151
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 times when I program, I will create objects for a specific purpose for a program and if I need to add to it I just add the code.
5
1756
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 members Y is the location of a file to be read. The original design assumes that this class will be instantiated and each instance will happily mange its own members. (ie One file location per instance...no thread-safety). Now another class A...
6
8181
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 situation where I am implementing base class functions by including a pointer to subclass member in base class. Reason being functionality is common for subclasses but the members are common within subclass only (static member of subclass) but...
0
10347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10901
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8453
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6410
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5147
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4726
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3755
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.