1-14

1-14 - Problem Solving with Computers-II CS 24 January 14,...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Problem Solving with Computers-II CS 24 January 14, 2010
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Announcements Note taker needed; Paid position; contact me for details. Lecture notes available online Username: ucsb-cs24 Password: w10-cs24 Feedback from Lab section Final Wednesday March 17, 8-11 am
Background image of page 2
Recap Dynamic binding Pointers Example code Abstract Data Types OOP Dynamic binding example
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Overloading and Polymorphism Person Employee Manager Each class has a method Print Person.Print just prints the name Employee.Print prints the name and job title Manager.Print prints name, job title, and department Print is overloaded Static binding is when the compiler can tell which Print to use; dynamic binding is when the determination cannot be made until run time
Background image of page 4
5 Dynamic Binding in C++ Only applies to “Virtual functions” Binding based on dynamic type of the object Benefits Apply the right method without elaborate case/ switch statements
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Warming up! struct ObjType { int value; ObjType* next; } x1, y1; int k; ObjType* p1 = new ObjType; ObjType* parray[5]; x1.value = 10; x1.next = &y1; y1.next = p1; y1.value = 20; p1->value = 30; p1-> next = &x1; for (k = 0; k < 5; k++) { parray[k] = new ObjType; parray[k]->value = 10*k; parray[k]->next = p1; } cout << x1.value; cout << (x1.next)->value; cout << (*p1).value;cout << p1->value; cout << (p1->next)->value; cout << parray[2]->value; cout << (parray[3]->next)->value;
Background image of page 6
Aside: Keyboard and Screen I/O #include <iostream> using namespace std; cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data output data
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
<iostream> is header file for a library that defines 3 objects an istream object named cin (keyboard) an ostream object named cout (screen) an ostream object named cerr (screen)
Background image of page 8
9 Insertion Operator ( << ) An output ( ostream ) operator that takes 2 operands The left operand is a stream expression, such as cout The right operand is an expression describing what to insert into the output stream E.g., cout << "The book costs" << cost;
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Extraction Operator ( >> ) An input ( istream ) operator that takes 2 operands The left operand is a stream expression, such as cin The right operand is a variable of simple type Operator >> attempts to extract the next item from the input stream and store its value in the right operand variable E.g., cin >> cost; The value being keyed in for cost must be the same type as that declared for variable cost
Background image of page 10
11 Whitespace characters Characters such as blanks, tabs, line feeds, form feed, carriage returns, and other characters that you cannot see on the screen Extraction operator >> “skips” leading whitespace characters before extracting the input value from the stream Use function get to read the next character in the input stream: cin.get(inputChar); get returns one character
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Example #include <iostream> int main( ) { using namespace std; // The standard scope
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/20/2010 for the course CS 24 taught by Professor Singh during the Winter '10 term at UCSB.

Page1 / 48

1-14 - Problem Solving with Computers-II CS 24 January 14,...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online