473,811 Members | 3,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

object-oriented issue - when to use classes

Hi,
I have a question regarding the object-oriented issue. I understand
that a class is a type. I have an array class. Now say that I want
to implement an algorithm A that uses the array class. Does it make
sense to make a class for the algorithm such as

class MyAlg
{
private:
Array A1;
Array A2;
// more data members

public:
// my functions
};

Right now I have my algorithm as an interface to the Array class (i.e.
just a bunch of global functions). The problem is that it gets pretty
messy and unorganized since there are so many parameters associated
with the algorithm. Since I now added a GUI for my algorithm, I
thought that maybe if I wrap my algorithm in a class, the code will be
cleaner. So in short, my current design is 3-tiered:

GUI -> My algorithm as a set of global functions -> Array class
implementation

I want to make it into a 2-tier design:

GUI -> My algorithm as a class (with Array contained in this
class)

Can someone please tell me if this is the right way to do it? I am
puzzled because it doesn't sound semantically correct -- since the
algorithm is not really a TYPE, but rather a method. If I shouldn't
define a class for it, what are my alternatives? I just don't feel
that using a bunch of global functions is the way to go. Thanks a
lot!

Jessica
Jul 19 '05 #1
6 2139
Jessica <je********@yah oo.com> wrote in message
news:76******** *************** ***@posting.goo gle.com...
Hi,
I have a question regarding the object-oriented issue. I understand
that a class is a type. I have an array class. Now say that I want
to implement an algorithm A that uses the array class. Does it make
sense to make a class for the algorithm such as

class MyAlg
{
private:
Array A1;
Array A2;
// more data members

public:
// my functions
};

Right now I have my algorithm as an interface to the Array class (i.e.
just a bunch of global functions).
If it's an interface to the Array class then it's not a bunch of global
functions. Someone else recently called member variables 'global'. Is there
a teacher out there teaching the wrong meaning of 'global'? Global functions
are those that are not inside a class and can be called from anywhere.
The problem is that it gets pretty
messy and unorganized since there are so many parameters associated
with the algorithm. Since I now added a GUI for my algorithm, I
thought that maybe if I wrap my algorithm in a class, the code will be
cleaner. So in short, my current design is 3-tiered:
I hope this means that your GUI code is completely separate from your
algorithm code.
GUI -> My algorithm as a set of global functions -> Array class
implementation

I want to make it into a 2-tier design:

GUI -> My algorithm as a class (with Array contained in this
class)

Can someone please tell me if this is the right way to do it?


It's hard to tell without more details. If your algorithm has many inputs,
and some of these are the same while others vary, then it's a good candidate
for a class. Otherwise a single function (a _real_ global function) might be
the most appropriate, regardless of the number of inputs. A class would have
the advantage for some algorithms of being able to do optimizations, such as
caching certain intermediate values. For instance, a FastFourierTran sform
object could pre-calculate an array of sines and cosines, or whatever it
needs, for a given number of points, which can then be used until the number
of points changes.

If possible, it's best not to limit an algorithm to using something like
Array objects. If your array is a simple Array of doubles, for example, then
it would be better for your algorithm to take pointers to doubles, e.g.,
void someAlgorithm(c onst double *begin, const double *end, /* other
parameters */);

Then you can use any kind of array of doubles, e.g.,
void f()
{
std::vector<dou ble> v;
// fill vector with numbers
someAlgorithm(v .begin(), v.end(), /* other parameters */);
}

Try to make your algorithm is flexible as possible, rather than limiting its
use to such specific types as Array.

I agree that your algorithm should not be in the Array class. Again, that
would be a very-specific, very limiting design. Arrays should just be
containers.

Do you have a good reason for using your own Array class rather than a
container supplied by the standard library?

DW

Jul 19 '05 #2
Jessica wrote:
I have a question regarding the object-oriented issue.
I understand that a class is a type.
I have an array class. Now say that
I want to implement an algorithm A that uses the array class.
Does it make sense to make a class for the algorithm such as
class MyAlg {
private:
Array A1;
Array A2;
// more data members

public:
// my functions
};
I presume that your Array is some kind of container class.
What kinds of elements does your Array contain?
Your description is vague and that's a bad sign.
Right now I have my algorithm as an interface to the Array class
(i.e. just a bunch of global functions).
That's just fine and necessary if those functions implement *methods*
that apply to objects of type Array. It might be a good idea
if your "global" functions belonged to a namespace instead.
The problem is that it gets pretty messy and unorganized
since there are so many parameters associated with the algorithm.
Since I now added a GUI for my algorithm, I thought that
maybe if I wrap my algorithm in a class, the code will be cleaner.
So in short, my current design is 3-tiered:

GUI -> My algorithm as a set of global functions -> Array class
implementation

I want to make it into a 2-tier design:

GUI -> My algorithm as a class (with Array contained in this
class)

Can someone please tell me if this is the right way to do it?
I am puzzled because it doesn't sound semantically correct --
since the algorithm is not really a TYPE, but rather a method.
If I shouldn't define a class for it, what are my alternatives?
I just don't feel that
using a bunch of global functions is the way to go.


Suppose that your Array is actually an array of numbers -- a vector.
You probably want a *free* function that can perform
a Direct Discrete Fourier Transform (DDFT) on a complex vector

Array x(n);
Array y = ddft(x);

But you may also want to define DDFT object
that you can apply repeatedly to vectors of a given size
the way that FFTW does (http://www.fftw.org/).

Array x(n);
DFT ddft(n, -1);
Array y = ddft(x);
Array z = ddft(y);

Matrix decompositions are also a good candidate for this approach.

Take a look at
The C++ Scalar, Vector, Matrix and Tensor Class Library

http://www.netwood.net/~edwin/svmtl/

Visit The Object-Oriented Numerics Page

http://www.oonumerics.org/oon/

for lots more good information

Jul 19 '05 #3
"Jessica" <je********@yah oo.com> wrote in message
news:76******** *************** ***@posting.goo gle.com...
Hi,
I have a question regarding the object-oriented issue. I understand
that a class is a type. I have an array class. Now say that I want
to implement an algorithm A that uses the array class. Does it make
sense to make a class for the algorithm such as

class MyAlg
{
private:
Array A1;
Array A2;
// more data members

public:
// my functions
};

Right now I have my algorithm as an interface to the Array class (i.e.
just a bunch of global functions). The problem is that it gets pretty
messy and unorganized since there are so many parameters associated
with the algorithm. Since I now added a GUI for my algorithm, I
thought that maybe if I wrap my algorithm in a class, the code will be
cleaner. So in short, my current design is 3-tiered:

GUI -> My algorithm as a set of global functions -> Array class
implementation

I want to make it into a 2-tier design:

GUI -> My algorithm as a class (with Array contained in this
class)

Can someone please tell me if this is the right way to do it?
I think you are on the right track. If I am not terribly mistaken, I have
seen a similar technique in Bjarne Stroustrup's book "The C++ Programming
Language, 3rd edition", chapter 18.4 "Function Objects".
I am
puzzled because it doesn't sound semantically correct -- since the
algorithm is not really a TYPE, but rather a method.
I think it is semantically correct -- Let's imagine a complicated algorithm
which uses Array A1 and A2 to convert doubles into strings:
you could supply std::string operator()(doub le) to your class 'MyAlg'. This
operator() could then implement any algorithm operating on the internal
Array A1 and A2 data members. You then create an object of class 'MyAlg'
(let's say, for example 'MyAlgObj'):

MyAlg MyAlgObj;
std::cout << "The first string representation is: " << MyAlgObj(-5.547) <<
std::endl;
std::cout << "The second string representation is: " << MyAlgObj(76.002 ) <<
std::endl;

Not only does MyAlgObj look like a function, it also behaves like a
function. This seems Ok to me and is a very powerful technique to implement
algorithms. However, I don't know whether this technique can be called
object-oriented.
If I shouldn't
define a class for it, what are my alternatives? I just don't feel
that using a bunch of global functions is the way to go. Thanks a
lot!

Jessica

Jul 19 '05 #4
> I have a question regarding the object-oriented issue. I understand
that a class is a type. I have an array class. Now say that I want
to implement an algorithm A that uses the array class.
Does it make sense to make a class for the algorithm such as


In terms of encapsulation: yes. But look at the generic
functions found in C++ like copy, sort, unique, transform etc.
These are all functions that operate on STL-containers but do
not belong to a class themselves. They take classes as arguments,
usually iterators, but are 'global' themselves.

Try to make your 'global' functions more general so that they
work on class hierarchies. There is nothing wrong with a few
globals. Your function main() will always be global, for instance.

-X

Jul 19 '05 #5
je********@yaho o.com (Jessica) wrote in message news:<76******* *************** ****@posting.go ogle.com>...
[snip]
Can someone please tell me if this is the right way to do it?


When it comes to doing algorithms with classes, especially
container classes, you'd be working pretty hard to beat how
the standard library does it. Get yourself a good book on
how to do the standard lib and see how they've done it.
Here is a link to a review of one such good book.

http://www.accu.org/cgi-bin/accu/rvo...file=cp003310a

Of course, that's only one approach.
Socks
Jul 19 '05 #6
Hi David,

Thanks for your wonderful inputs (as well as those who replied)!
However, I think you misunderstood my posting.. Perhaps I didn't
phrase it very carefully. When I said "global" functions, I really
meant global functions. The class declaration (MyAlg) I wrote here is
what I intended to do, but haven't actually done it. Currently the
only class I have is the Array class which, by the way, is just a
wrapper class for the STL vector. My algorithm is a bunch of
functions in a separate file. I call them global because they don't
belong to any classes. My GUI then calls those functions, so I guess
I can say that the GUI is well separated from the algorithm code.

Sorry if I didn't make my original posting more clear. I truely
appreciate your help -- I just felt the need to defend myself since my
teachers should not be the ones to blame for my vague postings.

Jessica
"David White" <no@email.provi ded> wrote in message news:<F9******* *********@nasal .pacific.net.au >...
Jessica <je********@yah oo.com> wrote in message
news:76******** *************** ***@posting.goo gle.com...
Hi,
I have a question regarding the object-oriented issue. I understand
that a class is a type. I have an array class. Now say that I want
to implement an algorithm A that uses the array class. Does it make
sense to make a class for the algorithm such as

class MyAlg
{
private:
Array A1;
Array A2;
// more data members

public:
// my functions
};

Right now I have my algorithm as an interface to the Array class (i.e.
just a bunch of global functions).


If it's an interface to the Array class then it's not a bunch of global
functions. Someone else recently called member variables 'global'. Is there
a teacher out there teaching the wrong meaning of 'global'? Global functions
are those that are not inside a class and can be called from anywhere.
The problem is that it gets pretty
messy and unorganized since there are so many parameters associated
with the algorithm. Since I now added a GUI for my algorithm, I
thought that maybe if I wrap my algorithm in a class, the code will be
cleaner. So in short, my current design is 3-tiered:


I hope this means that your GUI code is completely separate from your
algorithm code.
GUI -> My algorithm as a set of global functions -> Array class
implementation

I want to make it into a 2-tier design:

GUI -> My algorithm as a class (with Array contained in this
class)

Can someone please tell me if this is the right way to do it?


It's hard to tell without more details. If your algorithm has many inputs,
and some of these are the same while others vary, then it's a good candidate
for a class. Otherwise a single function (a _real_ global function) might be
the most appropriate, regardless of the number of inputs. A class would have
the advantage for some algorithms of being able to do optimizations, such as
caching certain intermediate values. For instance, a FastFourierTran sform
object could pre-calculate an array of sines and cosines, or whatever it
needs, for a given number of points, which can then be used until the number
of points changes.

If possible, it's best not to limit an algorithm to using something like
Array objects. If your array is a simple Array of doubles, for example, then
it would be better for your algorithm to take pointers to doubles, e.g.,
void someAlgorithm(c onst double *begin, const double *end, /* other
parameters */);

Then you can use any kind of array of doubles, e.g.,
void f()
{
std::vector<dou ble> v;
// fill vector with numbers
someAlgorithm(v .begin(), v.end(), /* other parameters */);
}

Try to make your algorithm is flexible as possible, rather than limiting its
use to such specific types as Array.

I agree that your algorithm should not be in the Array class. Again, that
would be a very-specific, very limiting design. Arrays should just be
containers.

Do you have a good reason for using your own Array class rather than a
container supplied by the standard library?

DW

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2338
by: Timo J | last post by:
Hi - Im sitting and trying to understand this OOP - and need to create a class wich can do the following.. ShowBlocks() - Displays the data wich is inside it - If empty creates a form wich sends the info back to the Object, wich show the stuff.. AddBlock( $type ) - Creates an empty blok - wich is addet to a blocks area object...( Or somethin like it ) Store() - safes the info into a file, and updates mySQL... I have figured out most...
2
1954
by: Aaron | last post by:
Hi, I've seen javascript code where a constructor function is passed an argument "document", and inside the function itself the assignment "this.document = document;" is made. This is the code (or the part necessary for the example): function ToggleButton(document) { ToggleButton.images = new Array(4); for(i=0;i<4;i++) { ToggleButton.images = new
11
2209
by: Vani Murarka | last post by:
Hi Everyone, Does .NET offer any collection class which will give me objects last *accessed* such that I may build a least-recently-used cache that kills off objects that haven't been used for awhile? Or is there any other way to implement this kind of a cache / collection where one can do this kind of cleanup based on least-recently-used objects?
9
8609
by: Keith Rowe | last post by:
Hello, I am trying to reference a Shockwave Flash Object on a vb code behind page in an ASP.NET project and I receive the following error: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). On the aspx page I have the object tag as follows:
11
3102
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
6
2670
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate the object and set default blank/null values), and then does all the Db access and number crunching to populate the new object. The factory returns the fully populated object to the caller. All the fields in the object are private, but have...
11
1965
by: emailscotta | last post by:
Below I declared a basic object literal with 2 methods. The "doSomething" method is call from the "useDoSomething" method but the call is only sucessful if I use the "this" keyword or qualify the call with "SomeObj". Can someone describe why this is happening? var SomeObj = { doSomething : function() {
4
1981
by: gg9h0st | last post by:
i worte a simple code below. ------------------------------------------------------------------------------------ #include "stdafx.h" class Object { public: int a;
11
3974
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject is instance of MyEntity class. There is no MyProperty property in MyObject at design time.
275
12445
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
9607
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
10652
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...
1
10408
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10137
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
7673
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
6895
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5561
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...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4346
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

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.