C++ Tutorial6. Functions6.1 Void functions6.2 Return functions7. If7.1 If Logical7.2 If Comparison8. A Calculator9. Switch10. While Loop10.2 do While Loop11. Guessing Game12. For Loop12.1 Syntax12.2 Example12.3 Example 212.3 Example 313. Exponent Function14. 2D Array & Nested Loop15. Pointer16. Class16.1 Object16.2 Constructor16.3 Function16.4 Getter & Setter16.5 Inheritance
241234
5using namespace std;6
7void SayHi(){8 cout << "Hello User!";9}10
11void SaySomething(string name, int age){12 cout << "Hello " << name << "! You are " << age << " years old.";13}14
15int main(){16 cout << "Top" << endl;17 SayHi(); // the SayHi() function does not return any value, so it cannot be chained with << endl.18 cout << endl << "Bottom" << endl;19
20 SaySomething("Mike", 35) ;21
22 cout << endl;23 return 0;24}Result:
41Top2Hello User!3Bottom4Hello Mike! You are 35 years old.
191234
5using namespace std;6
7double cube(double num){8 double result = num * num * num;9 return result;10}11
12int main(){13 double num; 14 cout << "Enter a number: ";15 cin >> num; 16 double answer = cube (num);17 cout << answer << endl;18 return 0;19}Result:
21Enter a number: 52125
21123
4using namespace std;5
6int main(){7 bool is_male = false;8 bool is_tall = false;9
10 if(is_male && is_tall){11 cout << "You are a tall male." << endl;12 } else if(is_male && !is_tall){13 cout << "You are a short male." << endl;14 } else if(!is_male && is_tall){15 cout << "You are a tall but not male." << endl;16 } else if(!is_male && !is_tall){17 cout << "You are not tall and not male." << endl;18 }19
20 return 0;21}Result:
11You are not tall and not male.
281234
5using namespace std;6
7
8int getMax(double num_1, double num_2, double num_3){9 10 double result;11
12 if(num_1 >= num_2 && num_1 >= num_3){13 result = num_1;14 } else if(num_2 >= num_3 && num_2 >= num_1){15 result = num_2;16 } else{17 result = num_3;18 }19
20 return result;21}22
23int main(){24 25 cout << getMax(2, 5, 8) << endl;26
27 return 0;28}Result:
118
xxxxxxxxxx34123
4using namespace std;5
6int main(){7 8 double num_1, num_2;9 char op;10
11 cout << "Enter first number: ";12 cin >> num_1;13 cout << "Enter operator: ";14 cin >> op;15 cout << "Enter second number:";16 cin >> num_2;17
18 double result;19 if(op == '+'){20 result = num_1 + num_2;21 } else if(op == '-'){22 result = num_1 - num_2;23 } else if(op == '*'){24 result = num_1 * num_2;25 } else if(op == '/'){26 result = num_1 / num_2;27 } else {28 cout << "Invalid Operator!" << endl;29 }30
31 cout << result << endl;32 33 return 0;34}Result:
xxxxxxxxxx41Enter first number: 52Enter operator: *3Enter second number:7435
xxxxxxxxxx51123
4using namespace std;5
6string getDayOfWeek(int day_num){7
8 string day_name;9 10 switch (day_num) {11 case 1:12 day_name = "Monday";13 break;14 case 2:15 day_name = "Tuesday";16 break;17 case 3:18 day_name = "Wednesday";19 break;20 case 4:21 day_name = "Thursday";22 break;23 case 5:24 day_name = "Friday";25 break;26 case 6:27 day_name = "Saturday";28 break;29 case 7:30 day_name = "Sunday";31 break;32 default: 33 day_name = "Invalid Day Number!";34 }35
36 return day_name;37}38
39
40int main(){41 42 string answer;43 int day_num;44
45 cout << "Enter a number:";46 cin >> day_num;47 answer = getDayOfWeek(day_num);48 cout << answer << endl;49 50 return 0;51}Result:
xxxxxxxxxx21Enter a number:42Thursday
xxxxxxxxxx16123
4using namespace std;5
6int main(){7 8 int index = 1;9 // index <=5 called while guard statement10 while(index <=5){11 cout << index << endl;12 index++;13 }14 15 return 0;16}Result:
xxxxxxxxxx51122334455
xxxxxxxxxx17123
4using namespace std;5
6
7int main(){8 9 int index = 6;10
11 do{12 cout << index << endl;13 index++;14 } while(index <=5);15 16 return 0;17}Result:
xxxxxxxxxx116
xxxxxxxxxx31123
4using namespace std;5
6
7int main(){8 9 int guess;10 int guess_count =0;11 int guess_limit = 3;12 int true_num = 88;13
14 while( guess != true_num && guess_count < guess_limit){15 cout << "Enter a number: " ;16 cin >> guess;17 if(guess == true_num){18 cout << "You Win!" << endl;19 }20 if(guess != true_num && guess_count < guess_limit-1){21 cout << "Guess again!" << endl;22 }23 guess_count++;24 }25
26 if(guess_count == guess_limit && guess != true_num){27 cout << "You Lose!" << endl;28 }29 30 return 0;31}Solution:
xxxxxxxxxx32123
4using namespace std;5
6
7int main(){8 9 int guess;10 int guess_count =0;11 int guess_limit = 3;12 int true_num = 88;13 bool out_of_guess = false;14
15 while( guess != true_num && !out_of_guess){16 if(guess_count < guess_limit){17 cout << "Enter a number: " ;18 cin >> guess;19 guess_count++;20 } else{21 out_of_guess = true;22 }23 }24
25 if(out_of_guess){26 cout << "You Lose!" << endl;27 } else{28 cout << "You Win!" << endl;29 }30 31 return 0;32}
xxxxxxxxxx31for (statement 1; statement 2; statement 3) {2 // code block to be executed3}Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
xxxxxxxxxx912using namespace std;3
4int main() {5 for (int i = 0; i < 5; i++) {6 cout << i << "\n";7 }8 return 0;9}Result:
xxxxxxxxxx51021324354xxxxxxxxxx15123
4using namespace std;5
6int main(){7
8 int nums[] = {1, 2, 3, 4};9 10 for(int i =0 ; i < 5; i++){11 cout << nums[i] << endl;12 }13 14 return 0;15}Result:
xxxxxxxxxx511223344578216376
xxxxxxxxxx16123
4using namespace std;5
6
7int main(){8
9 int nums[] = {1, 2, 3, 4, 5};10 11 for(int i =1 ; i < 6; i++){12 cout << nums[i] << endl;13 }14 15 return 0;16}Result:
xxxxxxxxxx51223344551
xxxxxxxxxx21123
4using namespace std;5
6
7int power(int base_num, int pow_num){8 int result = 1;9 for(int i = 0; i < pow_num; i++){10 result = result * base_num;11 }12
13 return result;14}15
16int main(){17
18 cout << power(2, 3) << endl;19 20 return 0;21}Result:
xxxxxxxxxx1116
xxxxxxxxxx23123
4using namespace std;5
6
7int main(){8
9 int number_grid[3][2] = {10 {1,2},11 {3,4},12 {5,6}13 };14 15 for(int i = 0; i < 3; i++){16 for(int j = 0; j < 2; j++){17 cout << number_grid[i][j] << endl;18 }19
20 }21
22 return 0;23}Result:
xxxxxxxxxx6112233445566
xxxxxxxxxx26123
4using namespace std;5
6
7int main(){8
9 int age = 19;10 int *pAge = &age;11 double gpa = 3.5;12 double *pGpa = &gpa;13 string name = "Mike";14 string *pName = &name;15
16 cout << &age << endl;17 cout << pAge << endl;18 // dereference19 cout << *pAge << endl;20 cout << *&age << endl;21
22 cout << &*pAge << endl;23 cout << &*&age << endl;24
25 return 0;26}Result:
xxxxxxxxxx610x16db0f07820x16db0f07831941950x16db0f07860x16db0f078
xxxxxxxxxx31123
4using namespace std;5
6
7class Book {8 public:9 string title;10 string author;11 int pages;12
13};14
15int main(){16
17 Book book_1;18 book_1.title = "Harry Potter";19 book_1.author = "JK Rowling";20 book_1.pages = 500;21
22 Book book_2;23 book_2.title = "Lord of the Rings";24 book_2.author = "Tolkein";25 book_2.pages = 700;26
27 cout << book_1.author << endl;28
29 return 0;30}31
Result:
xxxxxxxxxx11JK Rowling
xxxxxxxxxx40123
4using namespace std;5
6
7class Book {8 public:9 string title;10 string author;11 int pages;12
13 Book(){14 title = "no title";15 author = "no author";16 pages = 0;17 }18
19 Book(string aTitle, string aAuthor, int aPages){20 title = aTitle;21 author = aAuthor;22 pages = aPages;23 }24
25};26
27
28int main(){29
30 Book book_0;31
32 Book book_1("Harry Potter", "JK Rowling", 500 );33
34 Book book_2("Lord of the Rings", "Tolkein", 700 );35
36 cout << book_0.author << endl;37 cout << book_1.author << endl;38
39 return 0;40}Result:
xxxxxxxxxx21no author2JK Rowling
xxxxxxxxxx49123
4using namespace std;5
6
7class Student {8 public:9 string name;10 string major;11 double gpa;12
13 Student(){14 name = "no title";15 major = "no author";16 gpa = 0;17 }18 Student(string aName, string aMajor, double aGpa){19 name = aName;20 major = aMajor;21 gpa = aGpa;22 }23
24 bool isHonor(){25 if(gpa >= 3.5){26 return true;27 }28 return false;29 }30
31};32
33
34int main(){35
36 Student student_0;37
38 Student student_1("Jim", "Art", 2.4 );39
40 Student student_2("Mike", "Business", 3.8 );41
42 cout << student_0.name << endl;43 cout << student_1.major << endl;44
45 cout << student_0.isHonor() << endl;46 cout << student_2.isHonor() << endl;47
48 return 0;49}Result:
xxxxxxxxxx41no title2Art3041xxxxxxxxxx49123
4using namespace std;5
6
7class Movie {8
9 private:10 string rating;11
12 public:13 string name;14 string director;15 16 Movie(){17 name = "no name";18 director = "no director";19 rating = "no rating";20 }21 Movie(string aName, string aDirector, string aRating){22 name = aName;23 director = aDirector;24 setRating(aRating);25 }26
27 void setRating(string aRating){28 if(aRating == "G" || aRating == "PG" || aRating == "PG-13" || aRating == "R" || aRating == "NR"){29 rating = aRating;30 } else {31 rating = "NR";32 }33 }34
35 string getRating(){36 return rating;37 }38
39};40
41int main(){42
43 Movie avangers("The Avengers", "Joss Whedon", "PG-13");44 avangers.setRating("adhkjh");45
46 cout << avangers.getRating() << endl;47
48 return 0;49}Result:
xxxxxxxxxx11NR
xxxxxxxxxx46123
4using namespace std;5
6
7class Chef {8
9 public:10 void makeChicken(){11 cout << "The chef makes chicken." << endl;12 }13 void makeSalas(){14 cout << "The chef makes salad." << endl;15 }16 void makeSpecialDish(){17 cout << "The chef makes BBQ beef." << endl;18 }19
20};21
22class ItialianChef : public Chef{23
24 public:25 void makePasta(){26 cout << "The chef makes salad." << endl;27 }28 // You can over write the function29 void makeSpecialDish(){30 cout << "The chef makes salad." << endl;31 }32
33};34
35int main(){36
37 Chef chef;38 chef.makeSpecialDish();39
40 ItialianChef itialianChef;41 itialianChef.makeSpecialDish();42 itialianChef.makeChicken();43
44
45 return 0;46}Result:
xxxxxxxxxx31The chef makes BBQ beef.2The chef makes salad.3The chef makes chicken.