Results 1 to 15 of 15

Thread: OO programming

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default OO programming

    Hello,
    I have this class:
    Qt Code:
    1. class Value {
    2. public:
    3. double _value;
    4. bool _missed;
    5. };
    6. class Feature {
    7. double _mean;
    8. vector<Value> _featVector;
    9. void computeMean() {
    10. for (int i=0; i < _featVector.size(); ++i) {
    11. sum += _featVec[i]._value;
    12. }
    13. _mean = sum / _featVec.size();
    14. }
    15. void normalize() {
    16. for (int i=0; i < _featVector.size(); ++i) {
    17. _featVec[i]._value = /*normalization equation */
    18. }
    19. }
    20.  
    21. class Datas {
    22. vector<Feature> _fet;
    23. public:
    24.  
    25. void computeMean() {
    26. for(int i=0; i < _feat.size(); ++i)
    27. _feat[i].computeMean();
    28. }
    29. void normalize() {
    30. for(int i=0; i < _feat.size(); ++i)
    31. _feat[i].normalize();
    32. }
    33. //main.cpp
    34. Datas data;
    35. data.computeMean();
    36. data.normalize();
    To copy to clipboard, switch view to plain text mode 
    Beyond that utility of some things, the point is: Feature is able to compute the mean and normalization on itself BUT to compute it It's necessary call it from one other class....; is it good?
    Furthermore: Will it better: class Feature : public vector<Value> { .....}

    I hope of many opinions,
    Last edited by mickey; 24th August 2008 at 18:04.
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: OO programming

    I'm not sure what you mean but if you want the mean to be computed "by itself" simply compute it the first time someone requests to read the mean.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: OO programming

    Quote Originally Posted by wysota View Post
    I'm not sure what you mean but if you want the mean to be computed "by itself" simply compute it the first time someone requests to read the mean.
    see the post #1 I edited it (now it's a bit more complex);
    I thought that when Data is build, computeMean() must be called; when some changes in the datas occur, the mean should be computate again; to retrieve the mean there is a getMean() { return _mean; } method (there isn't in post #1 at moment).
    "itself" mean capability of Feature of iterates on a its vector......
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: OO programming

    I still don't understand, but regarding calculating the mean upon data change - simply invalidate the mean so that it gets calculated next time someone queries for it.

    Qt Code:
    1. ....::append(double value){
    2. vector.append(value);
    3. mean = -1;
    4. }
    5.  
    6. ...::getMean(){
    7. if(mean<0) calculateMean();
    8. return mean;
    9. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: OO programming

    hello,

    my program must be start with creation of a object Data();
    Qt Code:
    1. //Data.h
    2. Data::Data() {
    3. compouteMean();
    4. normalize();
    5. }
    6. //main.cpp
    7. Data d;
    To copy to clipboard, switch view to plain text mode 
    This mechanism when someome call getMean(), the _mean is set to some value.....(bad?)
    My question was: is this OO design good? Data::computeMean() calls Feature::computeMean() ?
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: OO programming

    Well... this is tightly coupling those two objects, so you could certainly try to find a better solution.

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: OO programming

    i have no idea how improve it........anyway is good that Feature as capability to compute its mean? Or maybe is better leave this completly to Data class
    Regards

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: OO programming

    I admit I can't understand your language. Could you try to rephrase your post?

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: OO programming

    Probabily I'm asking something obvious;

    1.I understand that this design isn't good because there isn't object decoupling: how improve it? Any pattern to follow?

    2. class Feature contains a vector plus some methods (and here one my previous question if it was better write "class Feature : public std::vector<Value>"....). Feature contains the value of mean (double _mean); I'm wondering is ok that Feature has a method that computes the Mean on a "ITS" vector (the vector vector<Value> _values ) or if it was better write "Data::computeMean()" in a way that it can compute the Feature::_mean "without" call Feature::computeMean() (and then the method Feature::computeMean() will be unuseful). I.e. (or similar)
    Qt Code:
    1. Data::computeMean() {
    2. for (int i=0; i < _feat.size(); ++i) {
    3. double sum =0;
    4. for (int j=0; j < _feat[i]._featVector[j].size; ++j)
    5. sum += _feat[i]._featVector[j]._value;
    6. _feat[i]._featVector[j].setMean( sum / _feat[i]._featVector[j].size() ) ;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Maybe if you suggest a good decoupling, my doubts will disappear..

    Thanks,
    Last edited by mickey; 24th August 2008 at 22:16.
    Regards

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OO programming

    You don't have to define these functions as methods of any class. computeMean() and normalize() are very good candidates for template functions that can be applied to any collection. If you don't want them lying around in a void, you can put them in a namespace.

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: OO programming

    or I could put it in a class "Statistics".
    Anyway:
    - could anyone get me an idea of how decouple these two class?
    - putting it around, I don't know how template it:
    Qt Code:
    1. template<class T>
    2. T Max(T& t) {
    3. return (*std::max_element( t.begin(), t.end(), Feature::less_than ));
    4. //this will return a Value object; but how template the value of return to be a double???
    5. }
    6.  
    7. //main.cpp
    8. vector<Value> _values;
    9. double maxValue = Max( _values);
    To copy to clipboard, switch view to plain text mode 
    Regards

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: OO programming

    Quote Originally Posted by mickey View Post
    - could anyone get me an idea of how decouple these two class?
    Jacek already gave you two good hints.

    I'd like to note one thing, Mickey. I have noticed some time ago that you desperately try to use existing STL calls or "STL-ize" your code when there is no need of doing it or when the STL call does something similar (as opposed to "the same") to what you want. Placing STL calls everywhere can easily make your code unreadable or can even break it if you happen to use a faulty STL implementation (remember that only the API is the standard, not the internal implementation of the library). Simple solutions are usually the best and there is no benefit from spending hours trying to wrap the functionality you want to obtain into an STL (or place another of ones favourite library name here, Qt included) call.

  13. #13
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: OO programming

    Quote Originally Posted by wysota View Post
    Jacek already gave you two good hints.

    I'd like to note one thing, Mickey. I have noticed some time ago that you desperately try to use existing STL calls or "STL-ize" your code when there is no need of doing it or when the STL call does something similar (as opposed to "the same") to what you want. Placing STL calls everywhere can easily make your code unreadable or can even break it if you happen to use a faulty STL implementation (remember that only the API is the standard, not the internal implementation of the library). Simple solutions are usually the best and there is no benefit from spending hours trying to wrap the functionality you want to obtain into an STL (or place another of ones favourite library name here, Qt included) call.
    It's not clear;
    Are you saying to don't use std:max_element? (and write it by myself)
    I can simply put into a namespace a function double computeMax() { }, but for my application the datas are in vector<Value> and I think that I'll have to pass this vector to computeMax()
    so I'll have computeMax(vector<Value>& vec) { }, BUT in this way I have a function that is coupled with that vector.......
    Probabibly I don't understand....
    Regards

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: OO programming

    I say not to try to find a matching STL call where a single line of simple hand written code would be sufficient. This was a general remark, not a remark to this particular situation, at least not only.

  15. #15
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: OO programming

    Quote Originally Posted by mickey View Post
    or I could put it in a class "Statistics".
    Anyway:
    - could anyone get me an idea of how decouple these two class?
    - putting it around, I don't know how template it:
    Qt Code:
    1. template<class T>
    2. T Max(T& t) {
    3. return (*std::max_element( t.begin(), t.end(), Feature::less_than ));
    4. //this will return a Value object; but how template the value of return to be a double???
    5. }
    6. //main.cpp
    7. vector<Value> _values;
    8. double maxValue = Max( _values);
    To copy to clipboard, switch view to plain text mode 
    How can I template this above, please???
    Regards

Similar Threads

  1. Windows programming in Qt (serial communication)
    By bnilsson in forum General Programming
    Replies: 17
    Last Post: 9th February 2010, 18:17
  2. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  3. QT COM Programming
    By sarav in forum Newbie
    Replies: 5
    Last Post: 24th February 2007, 13:41
  4. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04
  5. MODEL/VIEW programming
    By mira in forum Newbie
    Replies: 3
    Last Post: 21st April 2006, 11:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.