-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBasic C++.cpp
530 lines (421 loc) · 10.4 KB
/
Basic C++.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/* --- If you want to run that file, remember to change its name to main.cpp ---*/
/* Sorted from this tutorial video: https://www.youtube.com/watch?v=vLnPwxZdW4Y */
#include <iostream>
#include <cmath>
using namespace std;
/* ---------Data Types--------- */
int datatypes() {
// ...
char grade = 'A';
int age = 50;
double gpa = 2.3;
bool isMale = false;
string phrase = "Giraffe";
return 0;
}
/* -----------Strings----------- */
int working_with_strings() {
// ...
string phrase = "Giraffe Academy"; // index start from 0
phrase[0] = 'A';
cout << phrase.length() << endl;
cout << phrase[1];
cout << phrase.find("Academy", 0);
cout << phrase.substr(8, 3);
string phrasesub;
phrasesub = phrase.substr(8, 3);
cout << phrasesub;
return 0;
}
/* -----------Numbers----------- */
int working_with_numbers() {
// ...
cout << 40;
cout << 5 - 7;
int wnum = 5;
double dnum = 8.8;
wnum++;
wnum += 80;
cout << 10 / 3; // return integer;
cout << 10.0 / 3.0; // return float;
// cmath
cout << pow(3, 2);
cout << sqrt(36);
cout << round(5.3);
cout << floor(5.6);
cout << ceil(5.3);
cout << fmax(3, 10);
return 0;
}
/* ------------Input------------ */
int getting_user_input() {
// ...
int age; // get int
char grade; // get char
cout << "Enter your age: ";
cin >> age;
cout << "Enter your grade: ";
cin >> grade;
cout << "You are " << age << "years old" << "grade" << grade;
string name; // get string
getline(cin, name);
cout << "Your name" << name;
return 0;
}
/* ----------Practice1---------- */
int building_a_calculator() {
// ...
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << num1 + num2;
return 0;
}
/* ----------Practice2---------- */
int building_a_mad_libs_game() {
// ...
string color, pluralNoun, celebrity;
cout << "Enter a color: ";
getline(cin, color);
cout << "Enter a plural Noun: ";
getline(cin, pluralNoun);
cout << "Enter a celebrity: ";
getline(cin, celebrity);
cout << color << "lala";
cout << pluralNoun << "nana";
cout << celebrity << "I love";
return 0;
}
/* ------------Arrays------------ */
int arrays() {
// ...
int luckyNums[] = {4, 8, 15, 16, 23, 42}; // index start from 0;
cout << luckyNums[0];
luckyNums[0] = 19;
cout << luckyNums[0];
int luckyNumswithSize[20] = {4, 8, 15, 16, 23, 42};
luckyNumswithSize[10] = 100;
cout << luckyNumswithSize[10];
return 0;
}
/* -----------Functions---------- */
// If we want to execute the function, we should 'calling it' in main function.
// function with parameters.
void functions_above(string name, int age) {
cout << "Hello " << name << "you are" << age;
}
/* ------------Return------------ */
// return type: int, char, double,
double return_statement(double num) {
//cube
double result = num * num * num;
return result; // get a doubel type value back;
}
/* ------------If_Else------------ */
int if_statements(bool isMale, bool isTall) {
// ...
if(isMale){
cout << "You are a male";
}
else{
cout << "You are not male";
}
// and &&; or ||; not !
if(isMale && isTall){
cout << "You are male and tall";
}
else if(isMale && !isTall){
cout << "You are a short male";
}
else if(!isMale && isTall){
cout << "You are note male and tall";
}
else{
cout << "You are note male and short";
}
return 0;
}
int more_if_statement(int num1, int num2, int num3) {
// getmax
int result;
if(num1 >= num2 && num1 >= num3){
result = num1;
}
// -----------------------------
if(num1 > num2){
result = num1;
}
else if(num1 == num2){ // != not equal;
result = num1;
}
else{
result = num2;
}
return result;
}
/* ------------Switch------------ */
string switch_statement(int dayNum) {
// get day of week
string dayName;
switch (dayNum) {
case 0:
dayName = "Sunday";
break;
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
default:
dayName = "Invalid Day Number";
}
return dayName;
}
/* ----------While_Loop---------- */
int while_loops() {
// ...
int index = 1;
while (index <= 5) {
cout << index << endl;
index++;
}
do {
cout << index << endl;
index++;
} while (index <= 5);
return 0;
}
/* ----------Practice3---------- */
int building_a_guessing_game() {
// ...
int secretNum = 7;
int guess;
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;
while (secretNum != guess && !outOfGuesses) {
if (guessCount < guessLimit) {
cout << "Enter guess: ";
cin >> guess;
guessCount++;
}
else {
outOfGuesses = true;
}
}
if (outOfGuesses) {
cout << "You Lose!";
}
else {
cout << "You Win!";
}
return 0;
}
/* -----------For_Loop----------- */
int for_loops() {
// ...
int nums[] = {1, 2, 5, 7, 3};
for (int num : nums) { // use use range based for loop instead
cout << num << endl;
}
for (int i = 1; i <= 5; i++) {
cout << i << endl;
}
return 0;
}
/* ----------Practice4---------- */
int exponent_function(int baseNum, int powNum) {
// ...
int result = 1;
for (int i = 0; i < powNum; i++) {
result *= baseNum; // result = result * baseNum;
}
return result;
}
/* ----------Nested_Loop---------- */
int twod_array_nested_loops() { // nested loop: a loop inside a loop;
// ...
int numberGrid[3][2] = {
{1, 2},
{3, 4},
{5, 6}
};
cout << numberGrid[0][1];
for (int i = 0; i < 3; i++) {
// nested loop
for (int j = 0; j < 2; j++) {
cout << numberGrid[i][j];
}
}
return 0;
}
/* -----------Comments----------- */
int comments() {
// here is a comment, only works on a single line;
/*
here
is
also
a
comment
works on multi lines
*/
return 0;
}
/* -----------Pointers----------- */
int pointers() {
// ...
int age = 19;
double gpa = 2.7;
string name = "Mike";
// RAM, Memory
cout << &age; // memory address(pointer) of variable age;
int* pAge = &age;
double* pGpa = &gpa;
string* pString = &name;
cout << pAge;
cout << *pAge;
return 0;
}
/* -------Classes & Objects------- */
// To represent any objects in real world, we need to create a class(a new data type).
class Book {
public:
string title;
string author;
int pages;
// Constructor Functions
Book(string aTitle, string aAuthor, int aPages) {
title = move(aTitle);
author = move(aAuthor);
pages = aPages;
}
Book() {
title = "no title";
author = "no author";
pages = 0;
}
};
class Student {
public:
string name;
string major;
double gpa;
Student(string aName, string aMajor, double aGpa) {
name = move(aName);
major = move(aMajor);
gpa = aGpa;
}
bool hasHonors() {
if (gpa >= 3.5) {
return true;
}
else {
return false;
}
}
};
/* -------Setter & Getter------- */
class Movie {
private:
string rating;
public:
string title;
string director;
Movie(string aTitle, string aDirector, string aRating) {
title = move(aTitle);
director = move(aDirector);
setRating(move(aRating));
}
// Setter
void setRating(string aRating) {
if (aRating == "G" || aRating == "PG" || aRating == "PG-13" || aRating == "R") {
rating = move(aRating);
}
else {
rating = "NR";
}
}
// Getter
string getRating() {
return rating;
}
};
/* ---------Inheritance--------- */
class Chef {
public:
void makeChicken() {
cout << "The chef makes chicken" << endl;
}
void makeSalad() {
cout << "The chef makes salad" << endl;
}
void makeSpecialDish() {
cout << "The chef makes bbq ribs" << endl;
}
};
class ItalianChef : public Chef {
public:
void makePasta() {
cout << "The chef makes bbq pasta" << endl;
}
void makeSpecialDish() {
cout << "The chef makes chicken parm" << endl;
}
};
// declaration for the functions defined below the main function.
void functions_below(const string &name, int age);
int main() {
// there is no need a statement when we define the function above the main function.
// but if we define the function below the main funcion, we then need a declare statement, like:
functions_above("Mike", 60); // This tells c++ that I want to execute all of the codes that inside this function;
functions_below("Tom", 45);
functions_below("Jim", 13);
cout<<"Hello, World!"<<endl;
// Classes and Objects
Book book1; // create an instance
cout << book1.pages;
Book book2; // create an instance
book1.title = "Lord of the Ring";
book1.author = "Tolkein";
book1.pages = 700;
cout << book2.pages;
Book book3("Harry Potter", "JK Rowling", 500);
cout << book3.pages;
// Object Functions
Student student1("Jim", "Business", 2.4);
Student student2("Pam", "Art", 3.6);
cout << student1.hasHonors();
cout << student2.hasHonors();
// Getter & Setters
Movie avengers("The Avenagers", "Joss Whedon", "PG-13");
avengers.setRating("R");
cout << avengers.getRating();
// Inheritance
Chef chef;
chef.makeChicken();
ItalianChef italianChef;
italianChef.makeChicken();
chef.makeSpecialDish();
italianChef.makeSpecialDish();
return 0;
}
/* ----------Function Defined Below the main()---------- */
void functions_below(const string &name, int age) {
cout << "Hello " << name << "you are" << age;
}