Skip to content

Commit ed210a4

Browse files
authored
[02-03] - Update C++ (#8)
* 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
1 parent 8757674 commit ed210a4

File tree

2 files changed

+61
-58
lines changed

2 files changed

+61
-58
lines changed

C++/02 C++ - Functions and File IO.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,10 @@ These are functions that call THEMSELVES. Trippy.
197197
```c++
198198
int getFactorial(int number){
199199
int sum;
200-
if(number == 1) sum = 1; //Yeah you can do this
201-
else sum = getFactorial(number - 1) * number;
200+
if(number == 1)
201+
sum = 1; //Yeah you can do this
202+
else
203+
sum = getFactorial(number - 1) * number;
202204
return sum;
203205
)
204206
```
@@ -227,12 +229,11 @@ if(! writer) { //Check to see if the filestream is open
227229
cout << "Error opening file" << endl;
228230
return -1; // Return -1 if failed
229231

230-
) else {
232+
} else {
231233

232234
writer << dragonQuote << endl; // Write dragonQuote to writer, which causes dragonQuote.txt to contain only dragonQuote
233235
writer.close(); // Close the file
234236

235-
}
236237
}
237238

238239
ofstream writer2("dragonQuote.txt", ios::app); //Create a writer object that appends to dragonQuote.txt
@@ -248,13 +249,14 @@ if(! writer2) { //Check to see if the filestream is open
248249
cout << "Error opening file" << endl;
249250
return -1; // Return -1 if failed
250251

251-
) else {
252+
} else {
252253

253254
writer2 << "\n -methylDragon" << endl; // Append "\n -methylDragon"
254255
writer2.close(); // Close the file
255256

256-
}
257-
257+
}
258+
259+
{
258260
char letter;
259261

260262
ifstream reader("dragonQuote.txt"); // Open an input filestream that reads dragonQuote.txt
@@ -266,10 +268,10 @@ if(! writer2) { //Check to see if the filestream is open
266268

267269
} else {
268270

269-
for(int i = 0; ! reader.eof(); i++){ // Read until end of file
270-
271-
reader.get(letter); // Get the next letter
272-
cout << letter; // And print it
271+
for(int i = 0; ! reader.eof(); i++) { // Read until end of file
272+
reader.get(letter); // Get the next letter
273+
cout << letter; // And print it
274+
}
273275

274276
}
275277

@@ -475,4 +477,4 @@ inline int S::square(int s) // use inline prefix
475477

476478
---
477479

478-
[![Yeah! Buy the DRAGON a COFFEE!](../_assets/COFFEE%20BUTTON%20%E3%83%BE(%C2%B0%E2%88%87%C2%B0%5E).png)](https://www.buymeacoffee.com/methylDragon)
480+
[![Yeah! Buy the DRAGON a COFFEE!](../_assets/COFFEE%20BUTTON%20%E3%83%BE(%C2%B0%E2%88%87%C2%B0%5E).png)](https://www.buymeacoffee.com/methylDragon)

C++/03 C++ - Object-Oriented Syntax.md

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -259,42 +259,42 @@ Objects can also be categorised! So in OOP, objects can be created as instances
259259
using namespace std;
260260
261261
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.
289272
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();
298298
}; // This semi-colon is required!
299299
```
300300

@@ -321,6 +321,7 @@ Animal::Animal(int height, int weight, string name){
321321
}
322322

323323
Animal::~Animal(){
324+
Animal::numOfAnimals--;
324325
cout << "Animal " << this -> name << " destroyed" << endl;
325326
}
326327

@@ -365,7 +366,7 @@ Animal::Animal(int height, int weight, string name){
365366
366367
Remember **direct initialisation** in part 1 of this crash course?
367368
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.
369370
370371
How do we do it for multiple variables at a time? Use **initialisation lists**
371372
@@ -390,7 +391,7 @@ So just remember that the next time you see a random `:` where it doesn't seem l
390391

391392
Read more: http://www.cplusplus.com/doc/tutorial/structures/
392393

393-
They're just like classes! Except that their default member visibility is public.
394+
They're just like classes! Except that their default member visibility is **public**.
394395

395396
That means you can also write a constructor into them!
396397

@@ -497,14 +498,13 @@ tom.toString();
497498
>
498499
499500
500-
501501
### 2.5 Virtual Methods and Polymorphisms <a name="2.5"></a>
502502
503503
[go to top](#top)
504504
505505
Virtual methods are used when you know that the class you're defining will be a base class that may have the method be overridden by a child class!
506506
507-
FYI: You can also create virtual **attributes** that can get overriden in the same way! I didn't write the example here, but it still works!
507+
**FYI**: You can also create virtual **attributes** that can get overriden in the same way! I didn't write the example here, but it still works!
508508
509509
Polymorphisms are when subclasses implement different methods/functions differently from their base class!
510510
@@ -514,18 +514,19 @@ Polymorphisms are when subclasses implement different methods/functions differen
514514
using namespace std;
515515
516516
class Animal{
517-
public:
518-
void getFamily() { cout << "We are animals" << endl; }
519-
/*virtual*/ void getClass() { cout << "I'm an Animal" << endl; }
517+
public:
518+
void getFamily() { cout << "We are animals" << endl; }
519+
/*virtual*/ void getClass() { cout << "I'm an Animal" << endl; }
520520
};
521521
522522
class Dragon : public Animal{
523-
public: getClass() { cout << "I'm a Dragon!" << endl; } // Polymorphism here!
523+
public:
524+
void getClass() { cout << "I'm a Dragon!" << endl; } // Polymorphism here!
524525
};
525526
526527
void whatClassAreYou(Animal *animal){
527-
animal -> getClass();
528-
};
528+
animal -> getClass();
529+
}
529530
```
530531
531532
```c++
@@ -775,4 +776,4 @@ p= new int(*(a.p))
775776
776777
---
777778
778-
[![Yeah! Buy the DRAGON a COFFEE!](../_assets/COFFEE%20BUTTON%20%E3%83%BE(%C2%B0%E2%88%87%C2%B0%5E).png)](https://www.buymeacoffee.com/methylDragon)
779+
[![Yeah! Buy the DRAGON a COFFEE!](../_assets/COFFEE%20BUTTON%20%E3%83%BE(%C2%B0%E2%88%87%C2%B0%5E).png)](https://www.buymeacoffee.com/methylDragon)

0 commit comments

Comments
 (0)