1.11M Members

objects or classes?

 
0
 

Hi
I want to build some objects that have a little difference between them. But i wonder how would i do it - 1. build a class with some variables and build objects out of him, or - 2.build class for each object, with the functions dedicated to this 'only one' object.
Is there any difference between the two methods? It is clear that its much more simple to build a class for each object, and define the functions for this object in the class. What is the profit of make one class, and inherit many objects out of it, make it complicated to implement different functions for each object.
Someone?

 
0
 

I think that depends on your definition of "a little difference between them". Are there data differences, functional differences, or both? Do they have the same basic functionalities and just implement the functions differently?

This sounds like a generalization/specialization relationship, which would tend to imply an inheritance structure, but more info is desperately needed.

 
0
 

I think that depends on your definition of "a little difference between them". Are there data differences, functional differences, or both? Do they have the same basic functionalities and just implement the functions differently?

This sounds like a generalization/specialization relationship, which would tend to imply an inheritance structure, but more info is desperately needed.

These are pages in Qt. They have different places on them that when i press them i want different things to happen. Formally, they look the same, but they behave a little differen in their functionality.

 
0
 

That's a GUI programming thing, about which I know nothing. Hopefully someone else can help you out.

 
0
 

There are many more options available than the two you mentioned, but without details on what you are exactly trying to achieve, it's hard to tailor the answer. I don't know Qt at all, but I have done quite a bit of GUI, so let me give a few "generic" options that you could consider, depending on what your needs are:

1) Composition: basically, you can allow your class (page) to include polymorphic objects to customize it. This would be appropriate if the code in the class (page) has a lot of other stuff that is common to all cases (as you mentioned that the pages are basically the same) and you don't want to copy-paste that code for each new customized page. This option imposes a clear divide between the basic and custom functionality and data. Say, all you need to customize is the response to a button in the page that is clicked:

class ButtonHandler {
  public:
    virtual void OnClick() = 0;
};

class CustomPage : ... Qt stuff ... {
  private:
    ButtonHandler* hdlr;
  public:
    ... lots of Qt stuff ...
    //give a button handler on construction
    CustomPage(ButtonHandler* aHdlr) : hdlr(aHdlr), ... { ... };
    //handle the button click
    void OnClick() {
      if(hdlr)
        hdlr->OnClick();
    };
};

class HelloWorld : public ButtonHandler {
  public:
    virtual void OnClick() { Message("Hello World!"); };
};

int main() {
  CustomPage page1(new HelloWorld); 
  ...
};

2) Inheritance: have a base (page) class for all the common functionality and provide protected virtual functions for the customizable features. Then, you have a derived class for each special case. This is appropriate if, for the purpose of implementing the special functionality, you need access to data or functions that are within the base (page) class (mainly for reading it, writing it is not as nice). This option imposes a clear divide between the basic and custom functionality but not as strict of a divide between basic and custom data. Reiterating the same example:

class BasicPage : .... Qt stuff .... {
    ...
  protected:
    virtual void DerivedOnClick() = 0;
  public:
    void OnClick() {
      DerivedOnClick();
    };
};

class HelloWorldPage : public BasicPage {
  protected:
    virtual void OnClick() {
      Message("Hello World!");
    };
};

int main() {
  HelloWorldPage my_page;
};

3) If it is difficult to divide functionality between basic and custom (which I would say is poor design), then you would have to implement different classes altogether for each object. This has the big problem of not reusing similar code (which can be solved with option 4). Moreover, not reusing similar code is a problem that reaches beyond the simple annoyance of copy-pasting code, because it becomes very difficult to maintain and is error-prone.

4) Generic Programing (using Policy types): now this is much better because you can mix custom functionalities while reusing code and not necessarily imposing a clear divide between the basic and custom functionality. However, it is a bit more advanced. I will not give an example for this, but if you like one, after looking up that programming style, just ask.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: