Results 1 to 9 of 9

Thread: construction of classes within classes

  1. #1
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default construction of classes within classes

    Hi, Im not sure if this is the right place to post this but im new to QT and C++ and i have a question regarding classes and their creation so i thought id try here!

    So want to create a class containg values and then i want to create antoher class inside with more values for organisation. (in matlab which i have used before I would create a structure within a structure). I have this code:

    Qt Code:
    1. class atom
    2. {
    3. public:
    4. QString type;
    5. };
    6.  
    7.  
    8. class data
    9. {
    10. //int size;
    11. public:
    12. QString name; //string for standard name from CIF
    13. // QList < QList <QList<float> > >xyz; //3d array for XYZ values from CIF
    14. // data(int);
    15. int size;
    16. double aSide;
    17. double bSide;
    18. double cSide;
    19. double alpha;
    20. double beta;
    21. double gamma;
    22. double volume;
    23.  
    24. atom atom1;
    25. atom atom2;
    26.  
    27. };
    To copy to clipboard, switch view to plain text mode 
    So now i get a class containing aSide, bSide etc and then two classes for two atoms each containing type. Ok however as the number of atoms can change each time the class data is instanced i need to dynamically assign the number of atom classes on creation.

    e.g.

    data data1(3) - where 3 is the number of atoms i need and would automatically create the instance data1 with 3 atom classes (i.e. atom1 atom2 and atom3...)

    then data data2(5) would create 5 atom classes etc......

    Can anyone explain how to do this please? oes it require the use of a constructor?

    Thanks in advance,

    Matt
    Last edited by high_flyer; 9th January 2011 at 17:35. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: construction of classes within classes

    however as the number of atoms can change each time the class data is instanced i need to dynamically assign the number of atom classes on creation.
    In C this would be done with an array, in C++ it is better to use container classes.
    See QVector, QList, QSet and probably others.
    STL has also a large collection of containers, that differ in various ways.
    You should read about it.

    However, in your case, QList or QVector would probably be good enough.
    Qt Code:
    1. QVector<atom *> m_vecAtoms;
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: construction of classes within classes

    Hi High_flyer,

    Thanks for your reply. Regarding container classes, i have used these previously, however my problem is that i want to organize my data within these classes with a name and i dont think i can do this with (for example) QList. Indeed i asked a question on this recently and i was told to use structures and classes! see:

    http://www.qtcentre.org/threads/3706...ht=#post170556

    Maybe i was not so clear in my previous example:

    so in the class data i will have different variables including container classes and then another custom class inside this which i can then manage:

    Qt Code:
    1. class atom // nested class
    2. {
    3. public:
    4. QString type;
    5. QList hkl
    6. Qlist IJK
    7. };
    8.  
    9. class data // main class
    10. {
    11. public:
    12. QString name; //string for standard name from CIF
    13. QList < QList <QList<float> > >xyz; //3d array for XYZ values from CIF
    14. int size;
    15. double aSide;
    16. double alpha;
    17. double gamma; //etc
    18. atom atom1; //occurence 1 of custom nested class
    19. atom atom2;
    20. };
    To copy to clipboard, switch view to plain text mode 

    So now i have 2 custom classes within the class data in which i can access the variables type, hkl and IJK my name using data.atom1.type for example. If i use a container class directly (as far as i know) i can only give the container a name (i.e. atom) and the variables inside (i.e. QString, QList, QList) are not named this makes organizing the data more difficult.

    This is the reason i was trying to use structures of container classes.

    Hope this helps and indicates why i asked the original question.

    Many thanks,

    Matt
    Last edited by high_flyer; 10th January 2011 at 10:14. Reason: code tags

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: construction of classes within classes

    If i use a container class directly (as far as i know) i can only give the container a name (i.e. atom) and the variables inside (i.e. QString, QList, QList) are not named this makes organizing the data more difficult.
    For this there are maps.
    Have a look at QMap.
    So you can do things like:
    Qt Code:
    1. QMap<QString,atom*> m_mapAtoms;
    2. ...
    3. atom *pAtom = new atom;
    4. ...
    5. //Storing
    6. m_mapAtoms["atom1"] = pAtom;
    7.  
    8. //using
    9. pAtom = m_mapAtoms["atom1"];
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. The following user says thank you to high_flyer for this useful post:

    mobucl (10th January 2011)

  6. #5
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: construction of classes within classes

    Hi high_flyer,

    Thanks again. I think i see how to use this, however using

    Qt Code:
    1. class atom
    2. {
    3. public:
    4. QString type;
    5. QList hkl
    6. Qlist IJK
    7. };
    To copy to clipboard, switch view to plain text mode 
    in the header file to create the class atom and then running the code you gave below
    i get an error:

    aggregate 'QMap<QString, atom*> m_mapAtoms' has incomplete type and cannot be defined

    Sorry but im not sure why i get this as if i just create an instance of atom (atom atom1) it works fine.

    Can you please help?

    Thanks

    Matt
    Last edited by wysota; 10th January 2011 at 15:33. Reason: missing [code] tags

  7. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: construction of classes within classes

    Show the definition code and the code where you use the map. (not just these lines, but a bit before and after)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. The following user says thank you to high_flyer for this useful post:

    mobucl (10th January 2011)

  9. #7
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: construction of classes within classes

    HI high_flyer.

    This is all the code from the header file (including the previously discussed attempts at creating nested classes and some function defs):

    Qt Code:
    1. #ifndef ATTEMPT1_H
    2. #define ATTEMPT1_H
    3.  
    4. //#include <QWidget>
    5. //#include <iostream>
    6. //#include <fstream>
    7. //#include <string>
    8. //#include <vector>
    9. //#include <windows.h>
    10. //#include <QTextEdit>
    11. //#include <QString>
    12. #include <QFileDialog>
    13. //using namespace std;
    14. //#include <QtCore/QFile>
    15. //#include <QtCore/QTextStream>
    16. void FileOpen(QString fileName, QVector<double>& x, QVector<QVector<double> >& y); // this is using pointers for output
    17. void FiCalc(double lambda, double TwoThetaMin, double TwoThetaMax);
    18. QList<QList<int> > Combinations(int a, int b, int c,int aa,int bb,int cc);
    19. QList<double> AtomicScattering(QString Symbol,QList<float> dSpace);
    20. namespace Ui {
    21. class attempt1;
    22. }
    23.  
    24. class attempt1 : public QWidget
    25. {
    26. Q_OBJECT
    27.  
    28. public:
    29. explicit attempt1(QWidget *parent = 0);
    30. ~attempt1();
    31.  
    32. private:
    33. Ui::attempt1 *ui;
    34.  
    35. private slots:
    36. void on_save_clicked();
    37. void on_run_clicked();
    38. void on_load_clicked();
    39. };
    40.  
    41. #endif // ATTEMPT1_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. class atom
    2. {
    3. public:
    4. QString type;
    5. };
    6.  
    7.  
    8. class data
    9. {
    10. //int size;
    11. public:
    12. QString name; //string for standard name from CIF
    13. // QList < QList <QList<float> > >xyz; //3d array for XYZ values from CIF
    14. // data(int);
    15. int size;
    16. double aSide;
    17. double bSide;
    18. double cSide;
    19. double alpha;
    20. double beta;
    21. double gamma;
    22. double volume;
    23. QList<float> dSpace; //use a float as we only want to compare to 8 significant figures
    24. QList<QList<int> > hkl;
    25. QList<float> TwoTheta;
    26. QList<float> I;
    27. atom atom1;
    28. atom atom2;
    29.  
    30. };
    To copy to clipboard, switch view to plain text mode 
    The code im using in the cpp file id just what you wrote below:

    Qt Code:
    1. QMap<QString,atom*> m_mapAtoms;
    2. atom *pAtom = new atom;
    3. //Storing
    4. m_mapAtoms["atom1"] = pAtom;
    5. //using
    6. pAtom = m_mapAtoms["atom1"];
    To copy to clipboard, switch view to plain text mode 

    Is this the information you need?

    thanks

    Matt


    Added after 19 minutes:


    Sorry high_flyer I just realised i didnt include <QMap> (im still getting used to including things as in matlab you don't do this!).

    I will have a look at QMap now - in your opinion is this the best way to organize data sets each containing lots of different data types?

    Thanks for your help again!

    Matt
    Last edited by wysota; 10th January 2011 at 15:34. Reason: missing [code] tags

  10. #8
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: construction of classes within classes

    please use "code"-tags.

    do you include <QMap>?

  11. The following user says thank you to FelixB for this useful post:

    mobucl (10th January 2011)

  12. #9
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: construction of classes within classes

    sorry will do in future

    yes include was the problem - i did notice and updated the post above about 20 mins after!

    Thanks

    Matt

Similar Threads

  1. QT WMI Classes
    By justatiq in forum Qt Programming
    Replies: 31
    Last Post: 10th April 2011, 16:44
  2. Using WMI CLasses
    By newb in forum Newbie
    Replies: 1
    Last Post: 4th June 2010, 20:19
  3. Relationship b/w different classes
    By salmanmanekia in forum Newbie
    Replies: 0
    Last Post: 2nd June 2010, 19:24
  4. fax classes in qt
    By dyams in forum Qt Programming
    Replies: 5
    Last Post: 7th September 2007, 10:14

Tags for this Thread

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.