FINAL EXAM REVIEW Spring 2015 FINAL EXAM DETAILS The Final Exam is on Thursday, April 30 at 5:30-7:30 in our regular classroom, LOV103. Make sure you have your Test 1 and Test 2 to study from! If you are comfortable with every question on those tests, you will be halfway there! The final is cumulative but there is a bit of an emphasis on material after the second midterm. CLASSES Be able to write a class declaration from scratch. Take the following prompt: Write the declaration of a class called Rectangle. An object of type Rectangle will store information about a rectangle shape. Specifically, a Rectangle object should store this data: • width (an integer) • height (an integer) • color (a string) Your class declaration should include these member functions: • A constructor that allows the width, height, and color to be passed in as parameters. The color should default to “red” if not provided. • A default constructor with no arguments. • A SetDimensions function which accepts two arguments for width and height and sets the member data to these values if neither argument is negative. Return a Boolean value indicating success or failure. • A GetArea function which calculates the area and returns the value as an integer. • A RectangleInfo function which displays the width, height, and color to the user. CLASSES Note: Constructors are unique! They do not have a return value and they have a name identical to the class itself. One constructor is called immediately after an object is created. class Rectangle { public: Rectangle(); Rectangle(int w, int h, string c = “red”); bool SetDimensions(int w, int h); int GetArea(); void RectangleInfo(); private: int width; int height; string color; }; Question: What kinds of includes do I need in the header, implementation, and driver files? CLASSES Note: Constructors are unique! They do not have a return value and they have a name identical to the class itself. One constructor is called immediately after an object is created. class Rectangle { public: Rectangle(); Rectangle(int w, int h, string c = “red”); bool SetDimensions(int w, int h); int GetArea(); void RectangleInfo(); private: Question: What kinds of includes do I need in the header, int width; implementation, and driver files? int height; string color; Header needs <string>. }; Implementation needs <iostream> and “rect.h”. Driver needs “rect.h” and <iostream>. CLASSES Given this class declaration, which statements are legal inside of main()? • Rectangle r; • Rectangle r(5); • Rectangle r(3, 2); • Rectangle r(3, 4, “blue”); • int x = r.GetArea(); • int w = r.width; • int y = r.RectangleInfo(); CLASSES Suggestions: • Review lecture notes on classes. • Make sure you know the syntax of class declarations and how to define functions for classes (the “::” operator!). • Know the difference between public and private – why might we make some things private? • Practice creating classes on your own. • Work hard on the last assignment. FUNCTIONS Be able to declare/define functions from scratch. Write a function called VowelCount, which takes in as parameters: a character array, the size of the array, and an integer passed by reference. The function should store the number of times that a vowel appears in the character array in the integer that was passed by reference. Ensure that the function cannot change the array. Example: If the array letters is {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’}, then the call VowelCount(letters, 6, x) should store 2 in x (because there are two vowels in the array). FUNCTIONS void VowelCount(const char letters[], int size, int& num_vowels){ int counter = 0; char temp; for(int i = 0; i < size; i++){ temp = tolower(letters[i]); if(temp == ‘a’ || temp == ‘e’ || temp == ‘i’ || temp == ‘o’ || temp == ‘u’) counter++; } num_vowels = counter; } FUNCTIONS Suggestions: • Know the structure of a function declaration and definition (and know the difference between these two concepts!). • Know the different parameter passing modes: by value and by reference. Also, know how to make an argument to a function “unchangeable”. • How are arrays passed into a function? • Practice writing your own functions – make sure you know how to write the functions from the Midterms. OUTPUT Be able to write the output of a code segment. Be aware of every cout statement! void DoSomething(int x, int& y, char z[] ) { x = x - 11; y=y+5; z[4] = ‘y’; z[8] = ‘g’; z[3] = ‘\0’; } Note: when printing c-style strings with the insertion operator, the program will print until it finds a ‘\0’. int main() { int a = 20, b = 9; char food[] = “Apple pie”; DoSomething(a, b, food); cout << “a = ” << a << “b = ” << b << “\nfood = ” << food << ‘\n’; } OUTPUT Be able to write the output of a code segment. Be aware of every cout statement! void DoSomething(int x, int& y, char z[] ) { x = x - 11; y=y+5; z[4] = ‘y’; z[8] = ‘g’; z[3] = ‘\0’; } Output: a = 20 b = 14 food = App int main() { int a = 20, b = 9; char food[] = “Apple pie”; DoSomething(a, b, food); cout << “a = ” << a << “b = ” << b << “\nfood = ” << food << ‘\n’; } OUTPUT Suggestions: • Practice evaluating the code segments from the tests. • Write small pieces of code using the string manipulation functions and I/O formatting functions and try to “guess” what will pop up on the screen. Run your program to see if you were right. OUTPUT WITH STRING OBJECTS char temp, char str1[20] = “pepper”, str2[20] = “banana”, str3[20] = “kiwi”; What is the output of the following code segment? strcat(str3, str2); cout << str3 << endl; temp = toupper(str2[2]); cout << temp << ‘ ’ << strlen(str1) << ‘\n’; strcpy(str3, “chili”); strcat(str1, “ ”); strcat(str2, str1); strncat(str3, str1, 3); cout << “str1 = ” << str1 << “ str2 = “ << str2 << “ str3 = ” << str3 << ‘\n’; OUTPUT WITH STRING OBJECTS char temp, char str1[20] = “pepper”, str2[20] = “banana”, str3[20] = “kiwi”; What is the output of the following code segment? strcat(str3, str2); Output: cout << str3 << endl; kiwibanana temp = toupper(str2[2]); N6 cout << temp << ‘ ’ << strlen(str1) << ‘\n’; str1 = pepper str2 = bananapepper str3 = chilipep strcpy(str3, “chili”); strcat(str1, “ ”); strcat(str2, str1); strncat(str3, str1, 3); cout << “str1 = ” << str1 << “ str2 = “ << str2 << “ str3 = ” << str3 << ‘\n’; EVALUATE CODE SEGMENTS What is the value of a after the following code segment is executed? int a, x = 5, y = 3; a = ++x * y--; cout << “a is ” << a; EVALUATE CODE SEGMENTS What is the value of a after the following code segment is executed? int a, x = 5, y = 3; a = ++x * y--; cout << “a is ” << a; Output: a is 18 QUESTIONS? On Thursday, April 23, we will have a question-and-answer session for the Final. You do not have to attend if you feel comfortable with the material. I will be in our classroom for the duration of the class and you may come and go as you like with questions. I will not have anything prepared so if you want to review something, you’ll have to specifically ask about it.