Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Explain "subclass"



  • I have done a significant amount of C++ programming, but only with basic classes and mostly procedural code. Now I am following examples in the book: “C++ GUI Programming with QT 4” and find word “subclass.” Searching on that, a Stackoverflow thread said there is no such thing as “subclass.” I presume that is a purist view and while they seem to have a point I find subclass used in the book and in my installed QT documentation. The book instructs the reader to create a dot h file with this:

    Class GoToCellDialog : public Qdialog, public Ui::GoToCellDialog { Q_OBJECT … }

    Is this a subclass of Qdialog and of Ui::GotoCellDialog?
    I keep this short by positing no further and asking:
    What is the concept of subclass? I presume the above is an example. Please tell me how to read it.


  • Moderators

    @BKBK said in Explain "subclass":

    Is this a subclass of Qdialog and of Ui::GotoCellDialog?

    i would say so. Actually IMO subclass is a common term in programming?!



  • @BKBK
    Just to say: I don't blame you for asking about your example! But since you say you are starting out (you have done classes, but not sob-classes, which seems a little odd, but never mind), you have enough to absorb if you can just stick to classes which only sub-class (or "inherit", you'll see that too) from one other class. Most (but not all) Qt classes are sub-classes of one other class, up a chain.



  • @raven-worx Smalltalk used the term subclass, while C++ uses the term derived class. In the Smalltalk development environment, you started with a class, the superclass, and defined a class, the subclass, that inherited all of the behavior (methods, which are C++ member functions) and variables of the superclass. You then added variables and behavior to differentiate (specialize) the subclass from the superclass.



  • Wow, thanks. I did real time telemetry work using Visual Studio and C++. The classes helped out quite a bit but the basic processing was conceptually simple so I did not go deep. It was really plain C with some classes for significant convenience. Back to today: I read this:

    Class GoToCellDialog : public Qdialog, public Ui::GoToCellDialog { Q_OBJECT … }

    As: Create a class named GoToCellDialog that inherits from Qdialog and make the public attributes/methods/etc that are in QDialog, public within this new class. Understand that, hopefully.

    Now it becomes opaque, for me. The book specifically has the reader name the form: GoToCellDialog. I see that is important. I don’t know how to read that phrase: “…, public Ui::GoToCallDialog…”
    I did visit the on-line documentation but was unable to fully understand it.
    Thanks for your time and patience.


  • Moderators

    @BKBK said in Explain "subclass":

    Class GoToCellDialog : public Qdialog, public Ui::GoToCellDialog { Q_OBJECT … }

    As: Create a class named GoToCellDialog that inherits from Qdialog and make the public attributes/methods/etc that are in QDialog, public within this new class. Understand that, hopefully.

    Now it becomes opaque, for me. The book specifically has the reader name the form: GoToCellDialog. I see that is important. I don’t know how to read that phrase: “…, public Ui::GoToCallDialog…”

    You're partially correct.

    The code actually means, create a class named GoToCellDialog that publicly inherits from two other classes:

    • QDialog
    • Ui::GoToCellDialog

    Deriving from 2 (or more) classes is called multiple inheritance.

    The least obvious part is probably this: There are two different and separate classes called "GoToCellDialog" in this example!

    1. GoToCellDialog is a class called "GoToCellDialog" in the global namespace
    2. Ui::GoToCellDialog is a class called "GoToCellDialog" in the Ui namespace Ui.

    Class 1 inherits from Class 2.

    Ui and Ui::GoToCellDialog are auto-generated by Qt Creator when you create a Qt Designer form for GoToCellDialog.


Log in to reply