You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Update C++ - Functions and File IO.md
* Fix indentation to make it more readable
* Update C++ - Object-Oriented Syntax.md
* Fix indentation to make it more readable
* Fix some compilation errors
* Fix typos
Copy file name to clipboardExpand all lines: C++/03 C++ - Object-Oriented Syntax.md
+47-46Lines changed: 47 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -259,42 +259,42 @@ Objects can also be categorised! So in OOP, objects can be created as instances
259
259
using namespace std;
260
260
261
261
class Animal {
262
-
private:
263
-
int height;
264
-
int weight;
265
-
string name;
266
-
267
-
static int numOfAnimals; // Static variables are shared by every object of the class
268
-
// Static variables are normally attributes that the class object normally wouldn't have
269
-
// They are usually properties of groups, not individuals!
270
-
271
-
public: // The way this has been done is called encapsulation! It increases security.
272
-
273
-
// Getter methods
274
-
int getHeight(){ return height; }
275
-
int getWeight(){ return weight; }
276
-
string getName(){ return name; }
277
-
278
-
// Setter methods
279
-
void setHeight(int cm){ height = cm; }
280
-
// You can use a conditional here to keep things sensible
281
-
void setWeight(int kg){ height = kg; }
282
-
void setName (string animalName){ name = animalName; }
283
-
284
-
void setAll(int, int, string);
285
-
286
-
// This is our constructor! Constructors are named the same name as the class
287
-
Animal(int, int, string);
288
-
Animal(); // Or an overloaded function call
262
+
private:
263
+
int height;
264
+
int weight;
265
+
string name;
266
+
267
+
static int numOfAnimals; // Static variables are shared by every object of the class
268
+
// Static variables are normally attributes that the class object normally wouldn't have
269
+
// They are usually properties of groups, not individuals!
270
+
271
+
public: // The way this has been done is called encapsulation! It increases security.
289
272
290
-
// Destructor here
291
-
~Animal();
292
-
293
-
static int getNumOfAnimals() { return numOfAnimals; }
294
-
// Static methods are attached to classes and not objects
295
-
// They can only access static variables!
296
-
297
-
void toString();
273
+
// Getter methods
274
+
int getHeight(){ return height; }
275
+
int getWeight(){ return weight; }
276
+
string getName(){ return name; }
277
+
278
+
// Setter methods
279
+
void setHeight(int cm){ height = cm; }
280
+
// You can use a conditional here to keep things sensible
281
+
void setWeight(int kg){ height = kg; }
282
+
void setName (string animalName){ name = animalName; }
283
+
284
+
void setAll(int, int, string);
285
+
286
+
// This is our constructor! Constructors are named the same name as the class
287
+
Animal(int, int, string);
288
+
Animal(); // Or an overloaded function call
289
+
290
+
// Destructor here
291
+
~Animal();
292
+
293
+
static int getNumOfAnimals() { return numOfAnimals; }
294
+
// Static methods are attached to classes and not objects
295
+
// They can only access static variables!
296
+
297
+
void toString();
298
298
}; // This semi-colon is required!
299
299
```
300
300
@@ -321,6 +321,7 @@ Animal::Animal(int height, int weight, string name){
321
321
}
322
322
323
323
Animal::~Animal(){
324
+
Animal::numOfAnimals--;
324
325
cout << "Animal " << this -> name << " destroyed" << endl;
325
326
}
326
327
@@ -365,7 +366,7 @@ Animal::Animal(int height, int weight, string name){
365
366
366
367
Remember **direct initialisation** in part 1 of this crash course?
367
368
368
-
No? Just recall that it's more efficient than assignment, since assignment createanother object in memory. So it's generally always better to do direct initialisation.
369
+
No? Just recall that it's more efficient than assignment, since assignment create another object in memory. So it's generally always better to do direct initialisation.
369
370
370
371
How do we do it for multiple variables at a time? Use **initialisation lists**
371
372
@@ -390,7 +391,7 @@ So just remember that the next time you see a random `:` where it doesn't seem l
0 commit comments