Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]Parents and inheritance
Forum Updated to NodeBB v4.3 + New Features

[solved]Parents and inheritance

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dotK
    wrote on last edited by
    #1

    Hi everyone, I need some help.

    I'm a newbie yet, and I need do a class with a base class create by me, I need it get some variables, methods and slots of other class(its parent)...So, how I can do this.

    Sorry if I'm not being clear, I don't know how describe.

    [.k] - A Mad Programmer

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Well, not terribly clear, I agree, but I'll try to guess.

      There are 2 basic ways to get some stuff from another class in c++: inheritance and friend classes. I'll focus on inheritance.

      Let's say you have a class:
      @
      class Class1 {
      public:
      void doSomeStuff();
      private:
      int variable1;
      };
      @

      To get access to members of Class1 in another class, all you need to do is:
      @
      Class2 : public Class1 {
      public:
      void doSomeOtherStuff();
      };
      @

      That's it. Now, if you want to get another class to participate, it's still possible in c++ (but not in many other languages):
      @
      Class3 : public Class2, QObject {
      // even more stuff!
      };
      @

      Class3 will get members from both Class2 and QObject. Usually it's better not to get too much into multiple inheritance.

      Hope I guessed what you did not understand. If not, feel free to ask more.

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dotK
        wrote on last edited by
        #3

        Thank you, I understand

        [.k] - A Mad Programmer

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Just one more thing. Since this is Qt Project forums, I should add that "QObject *parent" has nothing to do with this. It's a separate hierarchy (of meta-objects) that allows for some cool additional features.

          (Z(:^

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Note that the inheritance with Class3 will not work as described. At least, QObject must be first, however, I would not recommend multiple inheritance as an approach to be taken lightly (with the exception of interfaces, perhaps).

            Furthermore, I really miss a discussion of using encapsulation instead of inheritance. It is just as important a principle, if you ask me. It is about the difference between an is-a and a has-a relationship between classes.

            For gaining access to members of another class, I would first consider using proper access methods instead of resorting to friends. Friends can (and IMHO should) usually be avoided.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dotK
              wrote on last edited by
              #6

              @Andre can you tell me more about encapsulation?

              [.k] - A Mad Programmer

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                Any good C++ book will tell you more about it.

                The idea is that in order to build something, you can build it out of smaller parts. The bigger class doesn't behave like any of its parts individually, but forms a new entity with its own properties. However, it is composed of (has) the smaller parts.

                In Qt, a nice example is the QComboBox widget. In its editable mode, it is composed of a QLineEdit, a button* and when extended, a QListView. Yet it is none of these. It does not inherit them, it has them. The API even provides direct access to these components in this case. QComboBox still uses inheritance though: QComboBox is a QWidget (it inherits QWidget), just like the objects it is composed of do too.

                So, in order to use properties of a class, there are more ways than inheriting. Only if it makes sense to think of the relation between your base class and your extended class in terms of an is-a relation should you considder inheritance. It is meaningful to think of a QComboBox being a QWidget, and you can handle a QComboBox using only its QWidget interface. QComboBox is not a QListWidget (and a QLineEdit, and a QPushButton) however: it has those. It is composed of these.

                *) QComboBox, from the top of my head, does not actually use a QPushButton for the button, but that is not relevant for the example.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  panosk
                  wrote on last edited by
                  #8

                  I fully agree with Andre that you should carefully consider if you really need inheritance (is-a relationship), simple access methods (encapsulation), or composition (has-a relationship). Deciding if inheritance is really needed is very important and not always very clear and can have a great impact on your design.

                  For an excellent explanation on these concepts, see http://www.learncpp.com/cpp-tutorial/102-composition. I also encourage you to read all related sections (inheritance, aggregation, encapsulation).

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dotK
                    wrote on last edited by
                    #9

                    I understand, I think I need learn too much

                    [.k] - A Mad Programmer

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved