What is the big picture around Object Oriented Programming? Can you let users create objects?
-
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? -
@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?
-
@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.
-
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?
-
@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
-
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? -
@DevinQT
Hi
t is the variable name that points to the Topject
t is only know in that function since its being defined therea 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.