• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

group functions to a class ...

Hi experts,

     I have written several functions, which do some calculation using some objects.
  It is something like :
 
//----------------------------
  void myFunA() ;
  myImg myFunB(myImg &Img1) ;
  void myFunC(myImg &Img1, myImg &Img2) ;
    :
    blah blah and some more
//----------------------------
    Now, in order to make the code management easy, I am asked to design a "class" for all these functions which do some algorithms....

    So, here is my question ... these functions really do not look like a class to me ! To me, they seem more like functions than objects ... how do I design a class to group all these functions ?
I have a rough idea that maybe I can create a class, maybe like "myAlgorithmClass", and then make these functions as static functions of the class. Then I don't need to new a class in order to call these functions ???

    Anyway, I would appreciate if anyone can give me some suggestions or examples about this problem !
   many thanks !  
0
meow00
Asked:
meow00
4 Solutions
 
fatalXceptionCommented:
You could make a very simple wrapper class and put those functions in it, it makes no odds really whether you have to create an object of the class or not - the functions will be loaded into memory anyway when you call them.

e.g
class AlgLib
{
public:
    AlgLib();
   ~AlgLib();
  void myFunA() ;
  myImg myFunB(myImg &Img1) ;
  void myFunC(myImg &Img1, myImg &Img2) ;

};
void AlgLib::myFunA()
{
   //implement myFunA here and so on
}

int main()
{
AlgLib myLib;
myLib.myFunA();
return 1; //or whatever
}

But if they are all totally unrelated, I agree it's probably pointless to bundle them into a class just for the sake of it, maybe just a library instead?

However looking at the function protos, they do take similar data so maybe you can design a class which will allow you to leverage this - maybe by having some of the shared functionality of the functions - if ther is any - hidden away in private member functions of the class, thus "black-boxing" the nasty stuff neatly away so that the users of the class can focus on using the functions, not how they work internally

Or you could use some helper functions in the class to set the data before the function calls (ie store them in private members) which would mean if you had multiple calls to the same function in rapidi sucession you would be saving some time on parameter passing....not a lot though if you're using references
0
 
itsmeandnobodyelseCommented:
You would need a common baseclass - say Img - both for myImg and yourImg and work mit

  class Img
  {
  public:
        virtual void FunA() ;
        virtual Img* FunB();                            // pImg1 == this
        virtual void FunB(const Img* pImg2);    // pImg1 == this
  };

  class myImg : public Img
  {
  public:
        virtual void FunA();
        virtual Img* FunB();
        virtual void FunB(const Img* pImg2);
  };
  class yourImg : public Img
  {
  public:
        virtual void FunA() ;
        virtual Img* FunB();                          
        virtual void FunB(const Img* pImg2);  
  };

Now you may have different implementations of the virtual functions in myImp and yourImp.


int main()
{
     myImg mImg;
     yourImg yImg;

     Img*  pImgs[2] = { &mImg, &yImg };
     for (int i = 0; i < 2; ++i)
     {
           pImgs[i]->FunA();                    // virtual calls either of
           Img* p = pImgs[i]->FunB();      // myImg or yourImg
           pImgs[i]->FunC(p);                   // functions
     }
     return 0;
}
 
Regards, Alex
0
 
NievergeltCommented:
If you really want to group functions versus adding methods to a class, which is the object-oriented way, I suggest the following scheme.

But first I have to get rid of a disclaimer: This only makes sense, when you are grouping utility functions that are used throughout your classes and make no sense to be added to a class.
In my experience this hardly ever the case, but mostly the result of sub-optimal design.

But here it goes:
Make a header file defining the class as a collection of static functions. That way you will not have to construct objects to use the methods, but when using a function you can just write something like
  myImg newImage = ImageManipulators::myFunB(Img1);
thereby always making clear where the function is implemented as opposed by doing the same thing without a class.

Just my $ 0.02   Christoph
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
andrewjbCommented:
Or use a namespace?

namespace ImageManipStuff
{
   void Funca();
  void Funcb( Image &a );
};

etc.

and call with ImageManipStuff::Funca() etc.

- kind of looks like a class with all static member functions really.

0
 
NievergeltCommented:
Sorry andrewjb,

I do not think your idea too cool. A namespace can be defined anywhere and will not help you find the implementation, whereas a class name should be reflected in the file names of header and implementation files.

Share and Enjoy  Christoph
0
 
andrewjbCommented:
Niever - Valid comment, but I'd point out that

a) There's nothing to force you to implement classes in nicely-named header and cpp files.
b) There's nothing to stop you implementing a namespace in nicely-named header and cpp files.
c) Your development environment will probably have tools to let you find implementations if required
d) You shouldn't need to know about the implementation if you're just using the functions :-)

I don't think it's worth getting into a big argument about this (!) - just thought I'd mention namespaces as a potentially useful alternative...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now