Results 1 to 4 of 4

Thread: Object Composition

  1. #1
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Object Composition

    Hello,

    i have the following code, which has an object composition relationship between the class Group and Person:


    dialog.cpp

    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. #include "group.h"
    5.  
    6. Dialog::Dialog(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::Dialog)
    9. {
    10. ui->setupUi(this);
    11.  
    12.  
    13. Group g1;
    14. g1.setGroupId(1);
    15.  
    16. // this function should print the groupID
    17. g1.p1.printGroupId();
    18.  
    19. }
    20.  
    21. Dialog::~Dialog()
    22. {
    23. delete ui;
    24. }
    To copy to clipboard, switch view to plain text mode 


    group.h
    Qt Code:
    1. #include "person.h"
    2.  
    3. class Group
    4. {
    5. public:
    6. Group();
    7.  
    8. int getGroupId() const;
    9. void setGroupId(int value);
    10.  
    11. Person p1;
    12. private:
    13. int groupId;
    14.  
    15. };
    To copy to clipboard, switch view to plain text mode 

    group.cpp
    Qt Code:
    1. #include "group.h"
    2.  
    3. Group::Group()
    4. {
    5. }
    6. int Group::getGroupId() const
    7. {
    8. return groupId;
    9. }
    10.  
    11. void Group::setGroupId(int value)
    12. {
    13. groupId = value;
    14. }
    To copy to clipboard, switch view to plain text mode 

    person.h
    Qt Code:
    1. #include "group.h"
    2.  
    3.  
    4. class Person
    5. {
    6. public:
    7. Person();
    8. void printGroupId();
    9.  
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    person.cpp
    Qt Code:
    1. #include "person.h"
    2.  
    3. Person::Person()
    4. {
    5. }
    6.  
    7. void Person::printGroupId()
    8. {
    9. //How it is possible to access the member variable groupID of the Group object to which this Person objects belongs ?
    10. qDebug()<<"The GroupID is:";
    11. }
    To copy to clipboard, switch view to plain text mode 


    Thx
    Last edited by Andre008; 25th February 2016 at 21:41.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Object Composition

    The group either needs to tell the Person about the group id, or let it access the group id.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Object Composition

    But how the group tell the person this, or how to give this access ?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Object Composition

    Quote Originally Posted by Andre008 View Post
    But how the group tell the person this, or how to give this access ?
    Group has access to the person instance, it can call methods on it.

    E.g. Group::setGroupId() can call Person::setGroupId()

    Cheers,
    _

Similar Threads

  1. Composition Translucent (Windows 7)
    By Lodhart in forum General Programming
    Replies: 1
    Last Post: 30th January 2012, 10:35
  2. Composition
    By franco.amato in forum Qt Programming
    Replies: 8
    Last Post: 1st December 2009, 00:52
  3. Replies: 3
    Last Post: 11th April 2008, 12:25
  4. colors composition
    By elcuco in forum Qt Programming
    Replies: 5
    Last Post: 12th November 2007, 20:51
  5. Interface composition and QObject
    By brcain in forum Qt Programming
    Replies: 9
    Last Post: 20th November 2006, 17:56

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.