|
|
|
|
1
|
-
|
|
|
2
|
# Using Objects in C++ - Birds
|
1
|
# Using Objects in C++ - Birds
|
3
|
|
2
|
|
4
|
|
3
|
|
5
|
![](images/headerBirds.png)
|
4
|
![](images/headerBirds.png)
|
6
|
|
5
|
|
7
|
-<p> </p>
|
|
|
8
|
|
6
|
|
9
|
-Up to now we've seen how to use variables to store and manipulate data of certain types and how to structure programs by dividing their tasks into functions. An *object* is an entity that it is used in many programming languages to integrate the data and the code that operates on it, simplifying the modification of large programs. In today’s laboratory experience you will use a class called `Bird` to practice some basic skills in C++ to create and manipulate objects.
|
|
|
|
|
7
|
+[version 2016.02.10]
|
|
|
8
|
+
|
|
|
9
|
+Up to now we've seen how to use variables to store and manipulate data of certain types and how to structure programs by dividing their tasks into functions. An *object* is an entity that is used in many programming languages to integrate the data and the code that operates on it, simplifying the modification of large programs. In today’s laboratory experience you will use a class called `Bird` to practice some basic skills in C++ to create and manipulate objects.
|
10
|
|
10
|
|
11
|
|
11
|
|
12
|
|
12
|
|
13
|
##Objectives:
|
13
|
##Objectives:
|
14
|
|
14
|
|
15
|
1. Create objects from a class.
|
15
|
1. Create objects from a class.
|
16
|
-2. Manipulate attributes of the objects using their "getter" and "setter" method functions.
|
|
|
|
|
16
|
+2. Analyze the declaration of a class to understand how to create and manipulate objects of the class.
|
|
|
17
|
+3. Practice the creation and manipulation of objects, and the invocation of "setters" and "getters".
|
17
|
|
18
|
|
18
|
|
19
|
|
19
|
##Pre-Lab:
|
20
|
##Pre-Lab:
|
20
|
|
21
|
|
21
|
-Before the lab session each student should have:
|
|
|
|
|
22
|
+Before arriving to the laboratory you should have:
|
22
|
|
23
|
|
23
|
1. Reviewed the following concepts:
|
24
|
1. Reviewed the following concepts:
|
24
|
|
25
|
|
|
|
|
|
28
|
|
29
|
|
29
|
c. using the "setter" method functions to modify the attributes of an object.
|
30
|
c. using the "setter" method functions to modify the attributes of an object.
|
30
|
|
31
|
|
31
|
-2. Studied the documentation for the class `Bird` available in the documentation for this project (`eip/objetcs-birds/doc/es/html/class_bird.html`).
|
|
|
|
|
32
|
+2. Studied the documentation for the class `Bird` available in the documentation for this project (`objects-birds/doc/en/html/class_bird.html`).
|
32
|
|
33
|
|
33
|
3. Studied the concepts and instructions for the laboratory session.
|
34
|
3. Studied the concepts and instructions for the laboratory session.
|
34
|
|
35
|
|
35
|
-4. Taken the Pre-Lab quiz that can be found in Moodle.
|
|
|
36
|
|
36
|
|
37
|
---
|
37
|
---
|
38
|
|
38
|
|
|
|
|
|
41
|
|
41
|
|
42
|
##Classes and objects in C++
|
42
|
##Classes and objects in C++
|
43
|
|
43
|
|
44
|
-An *object* es 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 that describes the properties of 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
|
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.
|
47
|
|
47
|
|
48
|
-Take a look at the documentation for the `Bird` class which can be found in the documentation for this project (`eip/objetcs-birds/doc/es/html/class_bird.html`).
|
|
|
|
|
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`).
|
49
|
|
49
|
|
50
|
###Classes
|
50
|
###Classes
|
51
|
|
51
|
|
|
|
|
|
89
|
|
89
|
|
90
|
`ClassName objectName(arguments);`
|
90
|
`ClassName objectName(arguments);`
|
91
|
|
91
|
|
92
|
-The creation of the objects is done by functions called `constructors` that will be explained later. By creating an object we have available the methods of the class that the object belongs to.
|
|
|
|
|
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.
|
93
|
|
93
|
|
94
|
###Methods of a class
|
94
|
###Methods of a class
|
95
|
|
95
|
|
|
|
|
|
147
|
|
147
|
|
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.
|
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
|
|
149
|
|
150
|
-
|
|
|
151
|
-
|
|
|
152
|
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:
|
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:
|
153
|
|
151
|
|
154
|
`methodName(type of the parameters);`
|
152
|
`methodName(type of the parameters);`
|
|
|
|
|
163
|
|
161
|
|
164
|
`Bird (int, EyeBrowType, QString, QString, QWidget *parent=0)`
|
162
|
`Bird (int, EyeBrowType, QString, QString, QWidget *parent=0)`
|
165
|
|
163
|
|
166
|
-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 (`eip/objetcs-birds/doc/es/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.
|
|
|
|
|
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.
|
167
|
|
165
|
|
168
|
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:
|
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:
|
169
|
|
167
|
|
170
|
`Bird pitirre;`
|
168
|
`Bird pitirre;`
|
171
|
|
169
|
|
172
|
-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.
|
|
|
|
|
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.
|
173
|
|
171
|
|
174
|
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:
|
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:
|
175
|
|
173
|
|
|
|
|
|
178
|
|
176
|
|
179
|
####Setters (mutators)
|
177
|
####Setters (mutators)
|
180
|
|
178
|
|
181
|
-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:
|
|
|
|
|
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:
|
182
|
|
180
|
|
183
|
* `void setSize (int)`
|
181
|
* `void setSize (int)`
|
184
|
* `void setEyebrow (EyeBrowType)`
|
182
|
* `void setEyebrow (EyeBrowType)`
|
|
|
|
|
252
|
---
|
250
|
---
|
253
|
|
251
|
|
254
|
!INCLUDE "../../eip-diagnostic/birds-objects/en/diag-birds-objects-01.html"
|
252
|
!INCLUDE "../../eip-diagnostic/birds-objects/en/diag-birds-objects-01.html"
|
|
|
253
|
+<br>
|
255
|
|
254
|
|
256
|
!INCLUDE "../../eip-diagnostic/birds-objects/en/diag-birds-objects-02.html"
|
255
|
!INCLUDE "../../eip-diagnostic/birds-objects/en/diag-birds-objects-02.html"
|
|
|
256
|
+<br>
|
257
|
|
257
|
|
258
|
!INCLUDE "../../eip-diagnostic/birds-objects/en/diag-birds-objects-03.html"
|
258
|
!INCLUDE "../../eip-diagnostic/birds-objects/en/diag-birds-objects-03.html"
|
|
|
259
|
+<br>
|
259
|
|
260
|
|
260
|
!INCLUDE "../../eip-diagnostic/birds-objects/en/diag-birds-objects-04.html"
|
261
|
!INCLUDE "../../eip-diagnostic/birds-objects/en/diag-birds-objects-04.html"
|
|
|
262
|
+<br>
|
261
|
|
263
|
|
262
|
!INCLUDE "../../eip-diagnostic/birds-objects/en/diag-birds-objects-05.html"
|
264
|
!INCLUDE "../../eip-diagnostic/birds-objects/en/diag-birds-objects-05.html"
|
|
|
265
|
+<br>
|
263
|
|
266
|
|
264
|
---
|
267
|
---
|
265
|
|
268
|
|
|
|
|
|
276
|
|
279
|
|
277
|
**Instructions**
|
280
|
**Instructions**
|
278
|
|
281
|
|
279
|
-1. Load the `Birds` project onto Qt by double clicking on the `Birds.pro` filein the directory `Documents/eip/Objects-Birds` of your computer. You may also go to `http://bitbucket.org/eip-uprrp/objects-birds` to download the `Objects-Birds` folder to your computer.
|
|
|
|
|
282
|
+1. Download the `objects-birds` folder from `Bitbucket` using a terminal, moving to the `Documents/eip` directory, and writing the command `git clone http://bitbucket.org/eip-uprrp/objects-birds`.
|
|
|
283
|
+
|
|
|
284
|
+2. Load the `Birds` project onto Qt by double clicking on the `Birds.pro` file in the directory `Documents/eip/objects-birds` of your computer. Configure the project.
|
280
|
|
285
|
|
281
|
-2. Study the `Bird` class contained in the `bird.h` file. Identify the methods that are constructors, setters and getters.
|
|
|
|
|
286
|
+3. Study the `Bird` class contained in the `bird.h` file. Identify the methods that are constructors, setters and getters.
|
282
|
|
287
|
|
283
|
-3. In the `main.cpp` file (in Sources) the `main` function does the following:
|
|
|
|
|
288
|
+4. In the `main.cpp` file (in Sources) the `main` function does the following:
|
284
|
|
289
|
|
285
|
a. Creates a Qt object application, called `a`. The only thing you need to know is that thanks to this object we can create a graphical application in Qt and interact with it.
|
290
|
a. Creates a Qt object application, called `a`. The only thing you need to know is that thanks to this object we can create a graphical application in Qt and interact with it.
|
286
|
|
291
|
|
|
|
|
|
292
|
|
297
|
|
293
|
e. In programs that don't have a graphical interface, the `main()` function usually ends with the instruction `return 0;`. In this project, the `return a.exec();` instruction is used so that the object `a` takes charge of the application from that moment on.
|
298
|
e. In programs that don't have a graphical interface, the `main()` function usually ends with the instruction `return 0;`. In this project, the `return a.exec();` instruction is used so that the object `a` takes charge of the application from that moment on.
|
294
|
|
299
|
|
295
|
-4. Execute the program by clicking on the green arrow in the left menu on the Qt Creator window. The program should display a blank window.
|
|
|
|
|
300
|
+5. Execute the program by clicking on the green arrow in the left menu on the Qt Creator window. The program should display a blank window.
|
296
|
|
301
|
|
297
|
###Exercise 2: Create objects of the `Bird` class with certain attributes
|
302
|
###Exercise 2: Create objects of the `Bird` class with certain attributes
|
298
|
|
303
|
|
299
|
-In this exercise you will create objects of the `Bird` class using the default constructor and using constructors where you define specific characteristics for the object. You will also practice the use of getters and setters to obtain and assign attributes to the objects.
|
|
|
|
|
304
|
+In this exercise you will create objects of the `Bird` class using the default constructor and using constructors where you define specific characteristics for the object. You will also practice the use of getters and setters to obtain and assign attributes to the objects.
|
300
|
|
305
|
|
301
|
**Instructions**
|
306
|
**Instructions**
|
302
|
|
307
|
|
303
|
1. Create an object of the `Bird` class called `abelardo` using the default constructor and add it to the `w` window using the method `addBird(int x, int y, Bird b)`. Remember that the method's call should start with the name of the object `w` and a period.
|
308
|
1. Create an object of the `Bird` class called `abelardo` using the default constructor and add it to the `w` window using the method `addBird(int x, int y, Bird b)`. Remember that the method's call should start with the name of the object `w` and a period.
|
304
|
|
309
|
|
305
|
-2. Run the program several times and marvel at seeing `abelardo` have different sizes, colors and eyebrows.
|
|
|
|
|
310
|
+2. Run the program several times and marvel at seeing `abelardo` have different sizes, colors and eyebrows.
|
306
|
|
311
|
|
307
|
3. Use the setters `setSize(int size)`, `setFaceColor(Qstring color)`, `setEyeColor(Qstring color)`, and `setEyebrow(EyeBrowType)` so that `abelardo` looks as in Figure 2 (its size is 200).
|
312
|
3. Use the setters `setSize(int size)`, `setFaceColor(Qstring color)`, `setEyeColor(Qstring color)`, and `setEyebrow(EyeBrowType)` so that `abelardo` looks as in Figure 2 (its size is 200).
|
308
|
|
313
|
|
|
|
|
|
330
|
|
335
|
|
331
|
![figure4.png](images/figure4.png)
|
336
|
![figure4.png](images/figure4.png)
|
332
|
|
337
|
|
333
|
- **Figure 4.** Abelardo, Piolin, Juana and Alondra.
|
|
|
|
|
338
|
+ **Figure 4.** Abelardo, Piolin, Juana, and Alondra.
|
334
|
|
339
|
|
335
|
---
|
340
|
---
|
336
|
|
341
|
|