170 likes | 298 Views
CSE 143 Section AD. Quiz Section 7. Announcements. If you are not yet officially registered for this course, please make sure to talk to me after class. There will be two debugger tutorials this week… one on Tuesday at 3:30 in MGH Room 44 and one Thursday at 3:30 in MGH Room 30.
E N D
CSE 143 Section AD Quiz Section 7 Jeff West - Quiz Section 7
Announcements • If you are not yet officially registered for this course, please make sure to talk to me after class. • There will be two debugger tutorials this week… one on Tuesday at 3:30 in MGH Room 44 and one Thursday at 3:30 in MGH Room 30. • I’m handing quizzes back today. If you are still having trouble understanding why the answers are what they are please feel free to talk to me about it. • I’d like to welcome as much feedback as possible about how well quiz sections are preparing you so please feel free to talk to me about them… I have linked a feedback form from the Section AD web page if you want to remain anonymous… • It seems like most homework questions have been implementation specific… if you have any questions about the assignment that we can discuss now I’d like to hear them. If you have questions specific to your implementation I have an office hour after class. • There is a midterm in 9 days… if there is anything at all that is unclear to you please email me or visit my office hours this week so that studying for the midterm next week can be as painless as possible. Jeff West - Quiz Section 7
About the Quizzes • I was actually slightly disappointed in answers to question 1… • How many times is the VideoTape default class constructor called in the following code? VideoTape tapes[100]; • If the VideoTape default constructor prints “hi\n” to your screen, how many times is hi printed to your screen? Jeff West - Quiz Section 7
What is Printed To The Screen class GameBoard { GameBoard() { cout << “Nothing ”; } GameBoard(int i) { cout << “Integer ”; } GameBoard(double d) { cout << “Double ”; } GameBoard(char c) { cout << “Character ”; } }; int main() { double d = 3.1; GameBoard boards[3]; GameBoard boards(d); GameBoard boards(3); return 0; } Jeff West - Quiz Section 7
Common Mistake 1:Check Your Strings… The following function is supposed to print a string to the screen one token at a time: void printString() { string token; string sentence = “These are a few words.”; char seps[] = “ .\n”; token = newStrtok(line, seps); while(token != “”) { new_sentence = newtoken + “ “; token = newStrtok(“”, seps); } cout << new_sentence; } Jeff West - Quiz Section 7
Common Mistake 2:Trace Your Loops… Given: int myIntArray[SIZE]; We want the following loop to add up all of the numbers in myIntArray: int total = 0, index = 0; while(index < SIZE) { index++; total = total + myIntArray[index]; } Jeff West - Quiz Section 7
istream vs. ifstream ifstream is a type of istream – what this mean in C++ is that an ifstream can be used wherever a istream is called for but not the reverse!! This is a common theme in C++ classes that a subtype can be used where a supertype is asked for (i.e. if a function asks for a mammal and gets a human it will work properly, but if it asks for a human and gets a generic mammal it may not know how to handle it)… #incude<fstream> #include<iostream> void getFile(ifstream &inFile); void getStream(istream &instream); ifstream inFile; ofstream outFile; Given the above function prototypes and variable declarations, which of the following statements is legal? getFile(inFile); getFile(outFile); getFile(cin); getStream(inFile); getStream(outFile); getStream(cin); Jeff West - Quiz Section 7
Stream States Streams offer us a whole bunch of different member functions to help us out while we are using them… some of the most important are eof(), clear(), and ! Perhaps the most important note to make about all of these state functions is that once a stream input operation has failed, any further operations will also fail until the stream state is cleared. This may seem like a subtle point but think about how it could “break” the following code: int k, j; cin >> k; // user inputs ‘z’ cin >> j; // user inputs 3 cout << k << ‘ ‘ << j; Jeff West - Quiz Section 7
A Midterm Question from Last Quarter… Here is a short program that reads three integer values and prints them. #include <iostream> using namespace std; int main() { int a = 1; int b = 2; int c = 3; cin >> a >> b >> c; cout << a << endl << b << endl << c << endl; return 0; } Now, suppose we execute this program and enter the following: 17 B52 143 10 What values will the program print? Jeff West - Quiz Section 7
Overloading Operators It is useful to provide clients using your class the ability to do things with it in the same manner that they would do things with other datatypes. Example: Suppose you are responsible for creating an ImaginaryNumber datatype. People using this imaginary number datatype might want to add, subtract, or print an imaginary number just as they would with an integer or a double. Design the class declaration (what would go in the header file) for an ImaginaryNumber class which will default to 0 + 0i, print its value using <<, add multiple numbers, and subtract multiple numbers. Jeff West - Quiz Section 7
imaginary.h // imaginary.h #include <iostream> using namespace std; class ImaginaryNumber { public: // construct a default number ImaginaryNumber(); // construct a new number with value a + bi ImaginaryNumber (double a, double b); private: // number elements double real, imaginary; Jeff West - Quiz Section 7
// allow output of an imaginary number datatype friend ostream& operator<< (ostream& s, const \ ImaginaryNumber num); // allow addition of two imaginary numbers friend ImaginaryNumber operator (const ImaginaryNumbe&r \ num1, const ImaginaryNumber& num2); // allow subtraction of two imaginary numbers friend ImaginaryNumber operator- (const ImaginaryNumber& num1, const ImaginaryNumber& num2); };
imaginary.cpp // imaginary.cpp #include “imaginary.h“ // construct a default imaginary number ImaginaryNumber::ImaginaryNumber() { real = 0.0; imaginary = 0.0; } // construct a new number with value a + bi ImaginaryNumber::ImaginaryNumber(double a, double b) { real = a; imaginary = b; } Jeff West - Quiz Section 7
// allow output of imaginary number datatype ostream& operator<< (ostream& s, const ImaginaryNumber num) { s << num.real << “ + “ << num.imaginary << ‘i’; return s; } // allow addition of two imaginary numbers point operator+ (const ImaginaryNumber& num1, const \ ImaginaryNumber& num2) { double real = num1.real + num2.real; double imaginary = num1.imaginary + num2.imaginary; ImaginaryNumber sum(real, imaginary); return sum; }
// allow subtraction of two imaginary numbers point operator- (const ImaginaryNumber& num1, const \ ImaginaryNumber& num2) { double real = num1.real – num2.real; double imaginary = num1.imaginary – num2.imaginary; ImaginaryNumber difference(real, imaginary); return difference; }
main.cpp // main.cpp #include “imaginary.h" int main() { ImaginaryNumber num(1,2); cout << num << '\n'; ImaginaryNumber num1(1, 3); ImaginaryNumber num2(2, 4); cout << num1 << '\n' << num2 << '\n' << (num1 + num2); return 0; } Jeff West - Quiz Section 7
Dynamic Memory int i; int* ip; double d; double* dp; Which of the following statements would be okay? Which would not? Remove the “bad” statements and draw a picture of memory if only the “good” statements were executed. i = 2; d = 3.14159; ip = i; ip = &i; d = 7.21; i = (*ip + 2); dp = &i; ip = 19; *ip = 13; *dp = 1.21; Jeff West - Quiz Section 7