What is the big picture around Object Oriented Programming? Can you let users create objects?
-
@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.