473,397 Members | 2,099 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,397 software developers and data experts.

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 2115
Jessica <je********@yahoo.com> wrote in message
news:76**************************@posting.google.c om...
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 FastFourierTransform
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(const double *begin, const double *end, /* other
parameters */);

Then you can use any kind of array of doubles, e.g.,
void f()
{
std::vector<double> 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********@yahoo.com> wrote in message
news:76**************************@posting.google.c om...
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()(double) 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********@yahoo.com (Jessica) wrote in message news:<76**************************@posting.google. 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.provided> wrote in message news:<F9****************@nasal.pacific.net.au>...
Jessica <je********@yahoo.com> wrote in message
news:76**************************@posting.google.c om...
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 FastFourierTransform
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(const double *begin, const double *end, /* other
parameters */);

Then you can use any kind of array of doubles, e.g.,
void f()
{
std::vector<double> 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
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...
2
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...
11
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...
9
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...
11
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
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...
11
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...
4
by: gg9h0st | last post by:
i worte a simple code below. ------------------------------------------------------------------------------------ #include "stdafx.h" class Object { public: int a;
11
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...
275
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.