150 likes | 159 Views
This quiz review covers topics from chapters 4 and 5, including structures, enumeration, pass by value and reference in functions, overloaded functions, and recursion.
E N D
Topics To Study • Chapters 4 and 5 • Structures • Enumeration • Functions: Pass by Value Pass by Reference Overloaded Functions Recursion
Structures struct cppclass { int students; int room; }; int main() { cppclass first = {21, 1105}; cppclass second = {14, 1165}; first = second; cout << first.students << endl; cout << first.room << endl; return 0; }
Program Output 14 1165
Enumeration enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; int main() { days_of_week day1, day2; day1 = Mon; day2 = Thu; int diff = day2 - day1; cout << "Days between = " << diff << endl; if(day1 < day2) cout << "day1 comes before day2\n"; return 0; }
Program Output Days between = 3 day1 comes before day2
Functions Pass By Value #include <iostream> using namespace std; int quiz(int number) { number = 100; return number; } int main() { int variable = 0; cout << variable << endl; cout << quiz(variable) << endl; return 0; }
Program Output 0 100
Functions Pass By Reference #include <iostream> using namespace std; void quiz(int& number) { number = 0; } int main() { int variable = 100; quiz(variable); cout << variable << endl; return 0; }
Function Overloading #include <iostream> using namespace std; void printnum(); void printnum(int); void printnum(int, int); int main() { int var = 15; printnum(); printnum(var); printnum(var, 10); return 0; }
Function Overloading void printnum() { int num = 10; cout << num << endl; } void printnum(int num) { cout << num << endl; } void printnum(int num, int sub) { int ans = num - sub; cout << num << endl; }
Program Output 10 15 15
Functions - Recursion int getcount(int); int main() { int count; count = getcount(5); cout << count << endl; return 0; } int getcount(int cnt) { if (cnt != 1) cnt += getcount(cnt-1); else cnt = 1; return cnt; }