|
@@ -37,6 +37,51 @@ Before coming to the laboratory you should have:
|
37
|
37
|
|
38
|
38
|
---
|
39
|
39
|
|
|
40
|
+##Classes and Objects in C++
|
|
41
|
+
|
|
42
|
+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*).
|
|
43
|
+
|
|
44
|
+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.
|
|
45
|
+
|
|
46
|
+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).
|
|
47
|
+
|
|
48
|
+###Classes
|
|
49
|
+
|
|
50
|
+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.
|
|
51
|
+
|
|
52
|
+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
|
+
|
|
54
|
+The following is the skeleton of the declaration of a class:
|
|
55
|
+
|
|
56
|
+---
|
|
57
|
+
|
|
58
|
+```
|
|
59
|
+ class ClassName
|
|
60
|
+ {
|
|
61
|
+ // Declarations
|
|
62
|
+
|
|
63
|
+ private:
|
|
64
|
+ // Declaration of variables or attributes
|
|
65
|
+ // and prototype member functions
|
|
66
|
+ // that are private for this class
|
|
67
|
+
|
|
68
|
+ type privateVar
|
|
69
|
+ type nameOfPrivateMemFunc(type of the parameters);
|
|
70
|
+
|
|
71
|
+ public:
|
|
72
|
+ // Declarations of attributes
|
|
73
|
+ // and prototypes of method functions
|
|
74
|
+ // that are public for the entire program
|
|
75
|
+
|
|
76
|
+ type publicVar;
|
|
77
|
+ type nameOfPublicMemFunc(type of the parameters);
|
|
78
|
+ };
|
|
79
|
+```
|
|
80
|
+
|
|
81
|
+---
|
|
82
|
+
|
|
83
|
+---
|
|
84
|
+
|
40
|
85
|
## The Game
|
41
|
86
|
|
42
|
87
|
The skeleton for the program that we provide simulates a basketball game between two players. The program uses OOP and simulates the game by creating an object of class BBPlayer for every player and calling its methods. The object that represents every player contains attributes such as the name of the player and the game statistics, i. e. throws taken and made. During the game, a successful throw into the basket receives two points. The program includes random functions and formulas to determine the player that attempts a throw, if the player scores, and the player that takes the rebound. During the simulated game the score for each player is kept and each player's data is updated. At the end of the game, a table with the statistics is displayed.
|