cse 143 section ad

17
7/10/2001 Jeff West - Quiz Section 7 1 CSE 143 Section AD Quiz Section 7

Upload: kara

Post on 04-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 1

CSE 143 Section AD

Quiz Section 7

Page 2: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 2

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.

Page 3: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 3

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?

Page 4: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 4

What is Printed To The Screenclass 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;}

Page 5: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 5

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;

}

Page 6: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 6

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];

}

Page 7: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 7

istream vs. ifstreamifstream 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);

Page 8: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 8

Stream StatesStreams 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 3cout << k << ‘ ‘ << j;

Page 9: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 9

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?

Page 10: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 10

Overloading OperatorsIt 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.

Page 11: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 11

imaginary.h// imaginary.h#include <iostream>using namespace std;

class ImaginaryNumber {public:

// construct a default numberImaginaryNumber();

// construct a new number with value a + bi ImaginaryNumber (double a, double b);private:

// number elements double real, imaginary;

Page 12: CSE 143 Section AD

// allow output of an imaginary number datatype friend ostream& operator<< (ostream& s, const \

ImaginaryNumber num);

// allow addition of two imaginary numbersfriend ImaginaryNumber operator (const ImaginaryNumbe&r \

num1, const ImaginaryNumber& num2);

// allow subtraction of two imaginary numbersfriend ImaginaryNumber operator- (const ImaginaryNumber&

num1, const ImaginaryNumber& num2);};

Page 13: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 13

imaginary.cpp// imaginary.cpp#include “imaginary.h“

// construct a default imaginary numberImaginaryNumber::ImaginaryNumber() {

real = 0.0;imaginary = 0.0;

}

// construct a new number with value a + biImaginaryNumber::ImaginaryNumber(double a, double b) { real = a; imaginary = b;}

Page 14: CSE 143 Section AD

// allow output of imaginary number datatypeostream& operator<< (ostream& s, const ImaginaryNumber

num) { s << num.real << “ + “ << num.imaginary << ‘i’;

return s;}

// allow addition of two imaginary numberspoint 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;}

Page 15: CSE 143 Section AD

// allow subtraction of two imaginary numberspoint 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;}

Page 16: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 16

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;}

Page 17: CSE 143 Section AD

7/10/2001 Jeff West - Quiz Section 7 17

Dynamic Memoryint 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;