What is the big picture around Object Oriented Programming? Can you let users create objects?



  • Hey,

    I am watching beginners courses on C++ and Qt, and so far i've created classes and objects myself by programming or have used Qt classes to create Qt objects (widgets). But i still wonder what the big picture is, the big goal, of object oriented programming.
    Questions like this wander around my head:

    • Is a Photo in a Photos app an object?
    • Is a path in Adobe Illustrator an (user created) object?
    • Can you even let users create objects?
    • How do you let users make objects?

    I would really appreciate your thoughts and insights on this subject.


  • Lifetime Qt Champion

    Hi,

    1. Most likely
    2. Depend on what is the definition of path for you. There are likely going to be several objects related to it. For example, the string object used to contain the path itself, the title bar were it's shown, etc.
    3. It happens all the time
    4. Do you mean programmatic wise or when they use your application ?

    What is your definition of an object ?



  • I think my definition of an object is just an instance of a class?

    With a path in Adobe Illustrator i mean a vector line, with which you build your drawings. A vector line can have a stroke and a fill, etc etc. It is just an example of an application where the user can create and modify things (which i assume are object instances of classes).

    I mean when they use my application. Right now i've created objects only by programming but i have no idea how i could let the user make objects, and how to do automatic naming and stuff of objects.


  • Lifetime Qt Champion

    I think you might be mixing two concepts here.

    Your definition of objects is correct.

    What will your application do ? They don't create objects directly, your application does that depending on what your user asks it to do. For example if it's an image preview application, your application is going to create many different objects of the various classes needed to show the window containing the image the user wants to see.

    A short example is the QPixmap set on the QLabel used to show an image. QPixmap that will get a QString
    containing the path to the image file.


  • Lifetime Qt Champion

    Hi
    Object Oriented Programming is not about what you see on the screen or what the user can or cannot do.
    Its about how the application is structured internally and how it is designed.
    Using objects, its allow the programmer to implement app features using methods/constructs
    supported by an objected orientated language that make the app solid, easy to extend and
    maintain. Those methods/constructs are encapsulation, abstraction, inheritance and polymorphism.

    This short video explains it very well.
    https://www.youtube.com/watch?v=pTB0EiLXUC8



  • SGaist,
    Okay so the user indirectly creates objects by asking the program to create them right? Let's say we make an application where the user can let the user add rectangles (like the window for containing an image) in a grid form to an empty board by clicking a + button.

    How do we let the program create these rectangles?
    Where do we call and instantiate these new rectangle objects? Is it in a function thats in a slot linked to the clicked signal of the + putton? Who decides the name of new rectangles? This has to go automatic right? (rectangle 1, rectangle 2, rectangle 3 rec....... or something like that)

    I guess these are classes, because i imagine each rectangle has properties which have been implemented in the rectangle class. (Like stroke width, for example) Maybe we've made buttons that can modify these properties of the rectangles once one is selected.

    Mrjj,
    So no new objects are created in the course of interaction with a program by the user? Is it just a fixed thing that decides the aspects of a program, with no interactivity with the user. Are these 'rectangles' no object instances? I don't understand this yet. Can't these app features the programmer implements with methods not create new objects or modify existing objects instances 'activated' in a program?


  • Lifetime Qt Champion

    @DevinQT
    Hi
    It has nothing to do with if user can create new objects or not. Not at all.
    Its about how the programmer can design and structure the program internally.
    So Object Oriented Programming does not define what user can do it the program or how he will do it.
    Its simply a way of structuring the internals of the program.
    A way of creating applications.



  • @mrjj
    Okay apparently i need to do some huge reconceptualisation then of what the big picture of an application is in my head..

    So. What would something like, for example, a layer in Photoshop be then? How is data/variables for each layer stored? Like the layer's name and the layer's opacity value? Each layer has to have it's own variables that control these aspects right? And i think that when an object is created each variables implemented in a class is getting his own version of that variable related to that specific object. But how is that done without objects?

    And in many tutorials i've seen about classes, classes are always presented with simple (console app) usage examples like a 'person'.. with things like 'name', 'age' and 'gender' instantiated through the constructor.. or maybe books.. with 'title', 'genre', and 'releaseDate' instantiated through the constructor... So i always assumed that a user could add 'persons' with an app, like you can in an app for managing contacts, or clients or ebooks for that matter.

    But what are these contacts then, in apps where you can create new contacts? How are these 'persons' and their variables stored, if they are not objects? Do these examples, so often used in tutorials about classes, only relate to 'persons' predefined for usage in an application by the programmer, like the characters of a game?


  • Lifetime Qt Champion

    @DevinQT
    Well yes a layer in photoshop would most likely be a class inside the program.
    So when use press New layer, program does
    Layer *Newlayer = new Layer();

    • So i always assumed that a user could add 'persons' with an app, like you can in an app for managing contacts, or clients or ebooks for that matter.

    Yes, they can. The application simply create new instances of that class.

    So in short, when using Object Oriented Programming, you center your application around classes
    like, the sample you mentions with Person etc.
    and each class keeps its own data and also methods that alters the data.
    In non Object Oriented Programming, the data and the methods that changes data, is not
    keep inside a class but are separate.

    Also, an Object might be able to draw on screen. LIke many can in Qt or be completely invisible to the user and
    only used internally by the application.
    However, its the use of classes and language features that makes it Object Oriented.



  • @mrjj
    Okay i think we're on the same page. (Glad i don't need to reconceptualize my whole idea of an app) The example of creating new layers something like the thing i try to express when i talk about the user 'creating' objects. (Of which the possibilities of creation and modification are of course predefined by us, the programmers).

    So in the case of Photoshop saying "Layer* Newlayer = new Layer();", how is this done? Does this mean new objects can be instantiated in functions or slots? How is, for example, the automatic naming of these new objects done? (Newlayer1, Newlayer2, Newlayer3 etc.) Is something like that is even possible. They can't have the same names right? And it seems like we must define the names of the objects using code.



  • @mrjj

    Let's get really simple and really practical. I just wanna know if i need to know about new syntax or concepts..
    In this very simple console application example; I have this plain object :

    Topject::Topject(QObject *parent) : QObject(parent)
    {
        qDebug() << "Topject constructed" << endl;
    }
    

    And i want a new Topject to be born, initialized and constructed everytime the pushButton is clicked in my Widget:

    void Widget::on_pushButton_clicked()
    {
       ???????????????
    }
    

    How would i build this? I have to give a name to a object when i make it with programming... But not all these objects can have the same name right?


  • Qt Champions 2018

    @DevinQT said in What is the big picture around Object Oriented Programming? Can you let users create objects?:

    How would i build this? I have to give a name to a object when i make it with programming... But not all these objects can have the same name right?

    Correct, but you can (for example) have a list of objects:

    // header file
    #include <QList>
    
    class Widget {
    // ...
    private:
      QList<Topject *> m_topjects;
    }
    

    Then in your function you can do this:

    void Widget::on_pushButton_clicked()
    {
       Topject *t = new Topject;
       m_topjects.append(t);
    }
    

    Regards



  • Allright that's nice. I didn't know about Lists. Thanks. 🙂

    Is every time the function is done executing the Topject *t resetted? So that it is every time i click a different Topject?


  • Lifetime Qt Champion

    The way it's implemented, the Tobject will be initialized the way it was written in the constructor, that's all.



  • @SGaist
    I understand, but my question was; what is the function 't' in the slot function. It is the name of the object.. Is this name still intact when it's passed into a list? And if it is; is the next 't', created with the next click, a different 't' then the first one? Does 't' only last in the scope of the function?


  • Lifetime Qt Champion

    @DevinQT
    Hi
    t is the variable name that points to the Topject
    t is only know in that function since its being defined there

    a new t variable is create each time on_pushButton_clicked is called.

    the t name is not added to the list. its the variable name.
    in the list - an index in the list- points to the Topject
    One could say its the same as t. but only in concept. its
    not t anymore. but points to the same thing as t did.



  • Allright that all clears the fog a bit. Thanks. 🙂


  • Banned

    This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.