180 likes | 263 Views
ECE 264 Object-Oriented Software Development. Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview. Lecture outline. Announcements/Reminders Lab 4 due next Monday, 10/08 Exam 1 on 10/10 (Wednesday 9:00 am- 9:50 am) General exam information
E N D
ECE 264Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview
Lecture outline • Announcements/Reminders • Lab 4 due next Monday, 10/08 • Exam 1 on 10/10 (Wednesday 9:00 am- 9:50 am) • General exam information • Exam review: what we’ve covered so far (not an exhaustive list!) • Software design cycle • I/O • Input: cin, output: cout • Output formatting • File I/O • Classes • Defining classes • Calling member functions • Implementing classes in separate files ECE 264: Lecture 13
General exam information • You may also use all paper-based materials such as lecture notes (printed), textbooks and other related materials. • One 8.5” x 11” double-sided sheet of notes allowed • All electronic devices (e.g., cellular phones, PDAs) and Computer are prohibited. ( reduced to 50 mins and the exam focuses on programming questions) • Start as close to 9:00 as possible and last 50 minutes • Exam will be held in Room S&E 212 • 3 questions, most of which have multiple parts • Short answer • Fill-in-the-blank • Understanding code (i.e., given some code, what’s output/what values do variables have?) • Writing short code sequences • Sample exam1 on web site (“Exam 1 sample”) under “Schedule and Materials” • Should at least give you idea of format • Note that topics were covered in a slightly different manner in previous years ECE 264: Lecture 13
Review: Basic I/O • Input (cin) streams • Use cin to read values into variables • E.g., cin >> x; • Skips whitespace characters • Input value must be compatible with type of x • Reading n characters: cin.get(buffer, n); • Reading 1 character: cin.get(ch); • Reading an entire line (at most m characters): cin.getline(buffer, m) • May need cin.ignore(x) to skip characters • Output (cout) streams • Can output multiple values in same statement • cout << “x=“ << x << “, y=“ << y << endl; • Namespaces • Introduced std namespace—includes cin, cout, etc. • Could include entire namespace: using namespace std; • Or, just include members being used: using std::cout; ECE 264: Lecture 13
//Example1: Determine the output #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> using std::string; int main() { int i, j; double x; string units = " cm"; cin >> i >> j; cin >> x; cout << "output \n"; cout << i << ',' << j << endl << x << units << endl; return 0; } //Input stream: 1 2 4.5 output 1,2 4.5 cm _ ECE 264: Lecture 13
//Example 2: Determine the output #include <iostream> using std::cout; using std::cin; using std::endl; int main() { int i, j; double x, y; cin >> i >> j >> x >> y; cout << "First output " << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; cin >> x >> y >> i >> j; cout << "Second output" << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; return 0; } //Input stream is: 1 2 3.4 5 2 3 3.4 7 First output 1,2,3.4,5 Second output 3,2,2,3 _ ECE 264: Lecture 13
Review: Formatted output, file I/O • Output formatting • Change base with dec/oct/hex or setbase() • Change precision (# places after decimal point) with precision() or setprecision() • Be sure to specify fixed format! • Force decimal point to be shown with showpoint • Specify field width with setw()or width() • Gives max number of input characters for cin (width()only) • Gives number of output characters for cout • Change justification with left/right/internal • showpos / showbase forces sign / base to be shown • Change fill characters with fill() or setfill() • File I/O • Specify ifstream or ofstream • Must explicitly open or close file ECE 264: Lecture 13
Example: bases int main() { int number; cout << "Enter a decimal number: "; cin >> number; // input number // use hex stream manipulator to show hexadecimal number cout << number << " in hexadecimal is: " << hex << number << endl; // use oct stream manipulator to show octal number cout << dec << number << " in octal is: " << oct << number << endl; // use setbase stream manipulator to show decimal number cout << setbase( 10 ) << number << " in decimal is: " << number << endl; return 0; } // end main ECE 264: Lecture 13
Example: showpoint, setprecision First output 1.000000,2.000000,3.400,5.000 _ #include <iostream> using std::cin; using std::cout; using std::endl; using std::fixed; using std::showpoint; #include <iomanip> using std::setprecision; int main() { double i, j, x, y; cin >> i >> j >> x >> y; cout << fixed << showpoint; cout <<"First output " << endl; cout << i << ',' << j << ',' << setprecision(3) << x << ',' << y << endl; return 0; } // Input stream is: 1 2 3.4 5 ECE 264: Lecture 13
Example: setfill, setw (cont.) int main() { int x = 10000; // display x cout << x << " printed as int right and left justified\n" << "and as hex with internal justification.\n" << "Using the default pad character (space):" << endl; // display x with base cout << showbase << setw( 10 ) << x << endl; // display x with left justification cout << left << setw( 10 ) << x << endl; // display x as hex with internal justification cout << internal << setw( 10 ) << hex << x << endl << endl; ECE 264: Lecture 13
Example: setfill, setw (cont.) // display x using padded characters (right justification) cout << right; cout.fill('*' ); cout << setw( 10 ) << dec << x << endl; // display x using padded characters (left justification) cout << left << setw( 10 ) << setfill( '%' ) << x << endl; // display x using padded characters (internal justification) cout << internal << setw( 10 ) << setfill( '^' ) << hex << x << endl; return 0; } // end main ECE 264: Lecture 13
Example: setfill, setw (output) ECE 264: Lecture 13
Review: Classes • Classes allow programmer to define their own types • Objects: instances of a class • Each class typically contains • Data members: attributes for each object • Each object has own copy of data members • Member functions: Tasks specific to class • Often includes “set” & “get” (mutator/accessor) functions • Changes made to member data in these functions remains persistent • Constructor(s): function called at object creation • Default constructor takes no arguments • Should always define—set default value(s) for data member(s) • Parameterized constructors initializes data members to specific values ECE 264: Lecture 13
Review: Classes (cont.) • Data/functions can be public or private • Private members only accessible within member functions • Access public members of class outside class definition using dot operator ( . ) • Example: GradeBook g1; g1.setCourseName(“ECE 264”); • Good programming practice: Split class into declaration (.h) and implementation (.cpp) • Declaration contains list of data, function prototypes • Implementation contains actual code for functions • Must specify class name for each function: <class_name>::<function_name>([param list]) { <function body> } ECE 264: Lecture 13
Example: GradeBook.h #include <string> using std::string; class GradeBook { public: GradeBook( ); GradeBook( string name ); void setCourseName(string name); string getCourseName(); void displayMessage(); private: string courseName; }; // end class GradeBook ECE 264: Lecture 13
Example: GradeBook.cpp // GradeBook.cpp #include “GradeBook.h” // Default constructor—initializes courseName // to empty string GradeBook::GradeBook( ) { setCourseName( "" ); } // end GradeBook constructor // Parameterized constructor GradeBook::GradeBook( string name ) { setCourseName( name ); } // end GradeBook constructor ECE 264: Lecture 13
Example: GradeBook.cpp (cont.) // function to set course name void GradeBook::setCourseName( string name ) { courseName = name; } // end function setCourseName // function to get the course name string GradeBook::getCourseName() { return courseName; } // end function getCourseName // display welcome message to user void GradeBook::displayMessage() { cout << "Welcome to the grade book for\n" << getCourseName() << endl; }// end function displayMessage ECE 264: Lecture 13
Good Luck! ECE 264: Lecture 13