|
|
|
|
39
|
---
|
39
|
---
|
40
|
|
40
|
|
41
|
|
41
|
|
42
|
-##Classes and objects in C++
|
|
|
|
|
42
|
+##Classes and Objects in C++
|
43
|
|
43
|
|
44
|
-An *object* is an entity that contains data and procedures to manipulate them. Similar to how each variable has a *type* of data associated to it, each object has a *class* associated that describes the properties of the objects: its data (*attributes*), and the procedures that can be used to manipulate its data (*methods*).
|
|
|
|
|
44
|
+An *object* is an entity that contains data and procedures to manipulate them. Similar to how each variable has a *type* of data associated to it, each object has a *class* associated to it, which describes the properties of the the objects: its data (*attributes*), and the procedures that can be used to manipulate its data (*methods*).
|
45
|
|
45
|
|
46
|
-To define and use an object it is not necessary to know all of the details about the methods of the object, but you must know how to create it and how to interact with it. The necessary information is available in the class' documentation. Before creating objects of any class we should familiarize ourselves with its documentation. The documentation indicates, among other things, what entity is trying to be represented in the class, and its interface or methods available to manipulate the objects of the class.
|
|
|
|
|
46
|
+It is not necessary to know all of the details about the methods of the object to define and use an object, but you must know how to create it and how to interact with it. The necessary information is available in the class' documentation. Before creating objects of any class, we should familiarize ourselves with its documentation. The documentation indicates, among other things, what entity is trying to be represented in the class, and its interface or methods available to manipulate the objects of the class.
|
47
|
|
47
|
|
48
|
-Take a look at the documentation for the `Bird` class which can be found in the documentation for this project (`objects-birds/doc/en/html/class_bird.html`).
|
|
|
|
|
48
|
+Take a look at the documentation of the `Bird` class which can be found in [this link.](http://ada.uprrp.edu/~ranazario/bird-html/class_bird.html).
|
49
|
|
49
|
|
50
|
###Classes
|
50
|
###Classes
|
51
|
|
51
|
|
52
|
-A class is a template for the creation of objects that establishes its attributes and behavior. If it isn't specified otherwise, the attributes and methods defined in a class will be private. This means that the variables can only be accessed and changed by the methods of the class (*constructors*, *setters*, and *getters*, among others).
|
|
|
|
|
52
|
+A class is a description of the data and processes of an object. The class’ declaration establishes the attributes that each of the objects of the class will have, and the methods that it can invoke.
|
|
|
53
|
+
|
|
|
54
|
+If it isn't specified otherwise, the attributes and methods defined in a class will be private. This means that the variables can only be accessed and changed by the methods of the class (*constructors*, *setters*, and *getters*, among others).
|
53
|
|
55
|
|
54
|
The following is the skeleton of the declaration of a class:
|
56
|
The following is the skeleton of the declaration of a class:
|
55
|
|
57
|
|
56
|
---
|
58
|
---
|
57
|
|
59
|
|
58
|
-```cpp
|
|
|
59
|
-
|
|
|
|
|
60
|
+```
|
60
|
class ClassName
|
61
|
class ClassName
|
61
|
{
|
62
|
{
|
62
|
// Declarations
|
63
|
// Declarations
|
|
|
|
|
78
|
type nameOfPublicMemFunc(type of the parameters);
|
79
|
type nameOfPublicMemFunc(type of the parameters);
|
79
|
};
|
80
|
};
|
80
|
```
|
81
|
```
|
81
|
-
|
|
|
82
|
---
|
82
|
---
|
83
|
|
83
|
|
84
|
You can see the declaration of the `Bird` class in the file `bird.h` included in this laboratory experience's program.
|
84
|
You can see the declaration of the `Bird` class in the file `bird.h` included in this laboratory experience's program.
|
85
|
|
85
|
|
86
|
###Objects
|
86
|
###Objects
|
87
|
|
87
|
|
88
|
-An object is an entity that contains data (as well as a variable), called its `attributes`, and also contain procedures, called `method`, that are used to manipulate them. The objects are "instances" of a class that are created in a similar manner as how variables are defined, with the exception that in some occasions we have to add values for the parameters:
|
|
|
|
|
88
|
+An object is an entity that contains data (same as a variable), called its `attributes`, and it also contains procedures, called `method`, that are used to manipulate them. The objects are "instances" of a class that are created in a similar manner as how variables are defined:
|
89
|
|
89
|
|
90
|
-`ClassName objectName(arguments);`
|
|
|
|
|
90
|
+`ClassName objectName;`
|
91
|
|
91
|
|
92
|
-The creation of the objects is done by functions called `constructors` that will be explained later. Once we have created an object, we can interact with it using the methods that belong to the class.
|
|
|
|
|
92
|
+By creating an object we have available the methods of the class that the object belongs to.
|
93
|
|
93
|
|
94
|
-###Methods of a class
|
|
|
|
|
94
|
+###Methods of a Class
|
95
|
|
95
|
|
96
|
-The methods of a class determine the actions that we can take on the objects of that class. The methods are similar to functions in the sense that they can receive parameters and return a result. An elementary way to know the methods of a class is reading de class declaration. For example, the following is a section of the declaration of the class `Bird` in the file `bird.h`.
|
|
|
|
|
96
|
+The methods of a class determine the actions that we can take on the objects of that class. The methods are similar to functions in the sense that they can receive parameters and return a result. An elementary way to know the methods of a class is reading the class declaration. For example, the following is a section of the declaration of the class `Bird` in the file `bird.h`.
|
97
|
|
97
|
|
98
|
---
|
98
|
---
|
99
|
|
99
|
|
100
|
|
100
|
|
101
|
-```cpp
|
|
|
102
|
-
|
|
|
|
|
101
|
+```
|
103
|
class Bird : public QWidget
|
102
|
class Bird : public QWidget
|
104
|
{
|
103
|
{
|
105
|
-
|
|
|
106
|
-//...
|
|
|
107
|
-
|
|
|
|
|
104
|
+.
|
|
|
105
|
+.
|
|
|
106
|
+.
|
108
|
Bird(int , EyeBrowType , QString , QString, QWidget *parent = 0) ;
|
107
|
Bird(int , EyeBrowType , QString , QString, QWidget *parent = 0) ;
|
109
|
-
|
|
|
|
|
108
|
+
|
110
|
int getSize() const;
|
109
|
int getSize() const;
|
111
|
EyeBrowType getEyebrow() const ;
|
110
|
EyeBrowType getEyebrow() const ;
|
112
|
QString getFaceColor() const;
|
111
|
QString getFaceColor() const;
|
|
|
|
|
117
|
void setEyebrow(EyeBrowType) ;
|
116
|
void setEyebrow(EyeBrowType) ;
|
118
|
void setFaceColor(QString) ;
|
117
|
void setFaceColor(QString) ;
|
119
|
void setEyeColor(QString) ;
|
118
|
void setEyeColor(QString) ;
|
120
|
-
|
|
|
121
|
-//...
|
|
|
122
|
-
|
|
|
|
|
119
|
+.
|
|
|
120
|
+.
|
|
|
121
|
+.
|
123
|
};
|
122
|
};
|
124
|
```
|
123
|
```
|
125
|
|
124
|
|
126
|
|
125
|
|
127
|
---
|
126
|
---
|
128
|
|
127
|
|
129
|
-Once the object is created, its methods provide the only way to change its attributes, to obtain information about them, or to compute with them. This is why the set of methods is commonly called *interface*. The methods are the interface between the object’s user and its content.
|
|
|
|
|
128
|
+Once the object is created, its methods provide the only way to change its attributes, to obtain information about them, or do computations with them. This is why the set of methods is commonly called the *interface*. The methods are the interface between the object’s user and its content.
|
130
|
|
129
|
|
131
|
In general, in each class the prototypes of the methods are defined to construct the objects, and to search, manipulate and store the data. The following is a general format of a method prototype:
|
130
|
In general, in each class the prototypes of the methods are defined to construct the objects, and to search, manipulate and store the data. The following is a general format of a method prototype:
|
132
|
|
131
|
|
|
|
|
|
136
|
|
135
|
|
137
|
`TypeReturned ClassName::MethodName(parameters)`
|
136
|
`TypeReturned ClassName::MethodName(parameters)`
|
138
|
|
137
|
|
139
|
-We declare public methods within the class so that objects that are instances of a class have permission to access private variables (these are the setters and getters). It's prefered to use private variables and access them through the setters and getters, instead of declaring them public since the object that is associated to these variables has control over the changes that are made.
|
|
|
|
|
138
|
+We declare public methods within the class so that objects that are instances of a class have permission to access private variables (these are the setters and getters). It's preferred to use private variables and access them through the setters and getters, instead of declaring them public since the object that is associated to these variables would have control over the changes that are made.
|
140
|
|
139
|
|
141
|
To invoke a method we write the name of the object, followed by a period and then the name of the method:
|
140
|
To invoke a method we write the name of the object, followed by a period and then the name of the method:
|
142
|
|
141
|
|
143
|
`objectName.methodName(arguments);`
|
142
|
`objectName.methodName(arguments);`
|
144
|
|
143
|
|
145
|
|
144
|
|
146
|
-####Constructors
|
|
|
|
|
145
|
+#### Constructors
|
|
|
146
|
+
|
|
|
147
|
+The first methods of a class that we should understand are the *constructors*. A class can have multiple constructors. One of the constructors will be invoked automatically each time an object of that class is created. In most cases, the constructors are used to initialize the values for the object’s attributes. To create objects of a class, we must know which are the constructors of the class.
|
|
|
148
|
+
|
147
|
|
149
|
|
148
|
-The first methods of a class that we should understand are the *constructors*. A class can have multiple constructors. One of the constructors will be invoked automatically each time an object of that class is created. In most of the cases, the constructors are used to initialize the values for the object’s attributes. To create objects of a class, we must know which are the constructors of the class.
|
|
|
149
|
|
150
|
|
150
|
In C++, the constructors have the same name as the class. The type returned by these functions is not declared since they do not return any value. Their declaration (included in the definition of the class) is like this:
|
151
|
In C++, the constructors have the same name as the class. The type returned by these functions is not declared since they do not return any value. Their declaration (included in the definition of the class) is like this:
|
151
|
|
152
|
|
|
|
|
|
161
|
|
162
|
|
162
|
`Bird (int, EyeBrowType, QString, QString, QWidget *parent=0)`
|
163
|
`Bird (int, EyeBrowType, QString, QString, QWidget *parent=0)`
|
163
|
|
164
|
|
164
|
-You can see the declarations of the method prototypes in the declaration of the `Bird` class in the project's `bird.h` file. The documentation for the class can be found in the documentation for this project (`objects-birds/doc/en/html/class_bird.html`). The first constructor, `Bird (QWidget *parent=0)`, is a method that can be invoked with one or no argument. If no argument is used, the function's parameter has a value of 0.
|
|
|
|
|
165
|
+You can see the declarations of the method prototypes in the declaration of the `Bird` class in the project's `bird.h` file. The documentation can be found in [this link.](http://ada.uprrp.edu/~ranazario/bird-html/class_bird.html) The first constructor, `Bird (QWidget *parent=0)`, is a method that can be invoked with one or no argument. If no argument is used, the function's parameter has a value of 0.
|
165
|
|
166
|
|
166
|
A class' constructor that can be invoked without using an argument is the class' *default constructor*; that is, the constructor that is invoked when we create an object using an instruction like:
|
167
|
A class' constructor that can be invoked without using an argument is the class' *default constructor*; that is, the constructor that is invoked when we create an object using an instruction like:
|
167
|
|
168
|
|
168
|
`Bird pitirre;`
|
169
|
`Bird pitirre;`
|
169
|
|
170
|
|
170
|
-You can see the implementations of the class `Bird` in the file `bird.cpp`. Notice that the first constructor, `Bird (QWidget *parent=0)`, will assign random values to each of the object's attributes. Later on there is a brief explanation for the `randInt` function.
|
|
|
|
|
171
|
+You can see the implementations of the class `Bird` in the file `bird.cpp`. Note that the first constructor, `Bird (QWidget *parent=0)`, will assign random values to each of the object's attributes. Later on there is a brief explanation for the `randInt` function.
|
171
|
|
172
|
|
172
|
Have a look at the documentation for the second constructor, `Bird (int, EyeBrowType, QString, QString, QWidget *parent=0)`. This function requires four arguments and has a fifth argument that is optional since it has a default value. One way to use this constructor is creating an object like this:
|
173
|
Have a look at the documentation for the second constructor, `Bird (int, EyeBrowType, QString, QString, QWidget *parent=0)`. This function requires four arguments and has a fifth argument that is optional since it has a default value. One way to use this constructor is creating an object like this:
|
173
|
|
174
|
|
174
|
`Bird guaraguao(200, Bird::UPSET, "blue", "red");`
|
175
|
`Bird guaraguao(200, Bird::UPSET, "blue", "red");`
|
175
|
|
176
|
|
176
|
|
177
|
|
177
|
-####Setters (mutators)
|
|
|
|
|
178
|
+#### Setters (mutators)
|
178
|
|
179
|
|
179
|
-Classes provide methods to modify the values of the attributes of an object that has been created. These methods are called *setters* or *mutators*. Usually, we declare one setter for each attribute that the class has. The `Bird` class has the following setters:
|
|
|
|
|
180
|
+Classes provide methods to modify the values of the attributes of an objected that has been created. These methods are called *setters* or *mutators*. Usually, we declare one setter for each attribute that the class has. The `Bird` class has the following setters:
|
180
|
|
181
|
|
181
|
-* `void setSize (int)`
|
|
|
182
|
-* `void setEyebrow (EyeBrowType)`
|
|
|
183
|
-* `void setFaceColor (QString)`
|
|
|
184
|
-* `void setEyeColor (QString)`
|
|
|
|
|
182
|
+* `void setSize (int)`
|
|
|
183
|
+* `void setEyebrow (EyeBrowType)`
|
|
|
184
|
+* `void setFaceColor (QString)`
|
|
|
185
|
+* `void setEyeColor (QString)`
|
185
|
|
186
|
|
186
|
|
187
|
|
187
|
You can see the method's declarations in Figure 1 and in the `Bird` class declaration in `bird.h`, and the implementation of the some of the methods in `bird.cpp`. The code in the following example creates the object `bobo` of the `Bird` class and then changes its size to 333.
|
188
|
You can see the method's declarations in Figure 1 and in the `Bird` class declaration in `bird.h`, and the implementation of the some of the methods in `bird.cpp`. The code in the following example creates the object `bobo` of the `Bird` class and then changes its size to 333.
|
|
|
|
|
191
|
bobo.setSize(333);
|
192
|
bobo.setSize(333);
|
192
|
```
|
193
|
```
|
193
|
|
194
|
|
194
|
-####Getters (accessors)
|
|
|
|
|
195
|
+#### Getters (accessors)
|
195
|
|
196
|
|
196
|
Classes also provide methods to access (get) the value of the attribute of an object. These methods are called *getters* or *accessors*. We usually declare one getter for each attribute a class has. The `Bird` class has the following getters:
|
197
|
Classes also provide methods to access (get) the value of the attribute of an object. These methods are called *getters* or *accessors*. We usually declare one getter for each attribute a class has. The `Bird` class has the following getters:
|
197
|
|
198
|
|
198
|
-* `int getSize ()`
|
|
|
199
|
-* `EyeBrowType getEyebrow ()`
|
|
|
|
|
199
|
+* `int getSize ()`
|
|
|
200
|
+* `EyeBrowType getEyebrow ()`
|
200
|
* `QString getFaceColor ()`
|
201
|
* `QString getFaceColor ()`
|
201
|
-* `QString getEyeColor ()`
|
|
|
|
|
202
|
+* `QString getEyeColor ()`
|
202
|
|
203
|
|
203
|
You can see the declarations of the methods in Figure 1 and in the `Bird` class declaration in `bird.h`, and the implementations of some of the methods in `bird.cpp`. The code in the following example creates the object `piolin` of the `Bird` class and prints its size:
|
204
|
You can see the declarations of the methods in Figure 1 and in the `Bird` class declaration in `bird.h`, and the implementations of some of the methods in `bird.cpp`. The code in the following example creates the object `piolin` of the `Bird` class and prints its size:
|
204
|
|
205
|
|
|
|
|
|
207
|
cout << piolin.getSize();
|
208
|
cout << piolin.getSize();
|
208
|
```
|
209
|
```
|
209
|
|
210
|
|
210
|
-####Other functions or methods you will use in this laboratory experience
|
|
|
|
|
211
|
+#### Other Functions or Methods You will use in this Laboratory Experience
|
211
|
|
212
|
|
212
|
**MainWindow:** The file `mainwindow.h` contains the declaration of a class called `MainWindow`. The objects that are instances of this class will be able to use the overloaded methods
|
213
|
**MainWindow:** The file `mainwindow.h` contains the declaration of a class called `MainWindow`. The objects that are instances of this class will be able to use the overloaded methods
|
213
|
|
214
|
|
|
|
|
|
217
|
|
218
|
|
218
|
that will add to the screen a drawing of an object of the `Bird` class that is received as an argument. The code in the following example creates an object `w` of the `MainWindow` class, creates an object `zumbador` of the `Bird` class and adds it in the position (200,200) on the screen `w` using the first method.
|
219
|
that will add to the screen a drawing of an object of the `Bird` class that is received as an argument. The code in the following example creates an object `w` of the `MainWindow` class, creates an object `zumbador` of the `Bird` class and adds it in the position (200,200) on the screen `w` using the first method.
|
219
|
|
220
|
|
220
|
-```cpp
|
|
|
221
|
-
|
|
|
|
|
221
|
+```
|
222
|
MainWindow w;
|
222
|
MainWindow w;
|
223
|
Bird zumbador;
|
223
|
Bird zumbador;
|
224
|
w.addBird(200,200,zumbador);
|
224
|
w.addBird(200,200,zumbador);
|
|
|
|
|
244
|
|
244
|
|
245
|
to generate random numbers in the range [min, max]. The method `randInt` depends on another function to generate random numbers that require a first element or *seed* to be evaluated. In this project, that first element is generated with the function call `srand(time(NULL)) ;`.
|
245
|
to generate random numbers in the range [min, max]. The method `randInt` depends on another function to generate random numbers that require a first element or *seed* to be evaluated. In this project, that first element is generated with the function call `srand(time(NULL)) ;`.
|
246
|
|
246
|
|
247
|
-
|
|
|
248
|
---
|
247
|
---
|
249
|
|
248
|
|
250
|
---
|
249
|
---
|