Download presentation
Presentation is loading. Please wait.
Published byEdmund Wiggins Modified over 8 years ago
1
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview
2
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 6/9/2016 ECE 264: Lecture 13 2
3
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”“Exam 1 sample Should at least give you idea of format Note that topics were covered in a slightly different manner in previous years 6/9/2016 ECE 264: Lecture 13 3
4
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; 6/9/2016 ECE 264: Lecture 13 4
5
6/9/2016 ECE 264: Lecture 13 5 output 1,2 4.5 cm _ //Example1: Determine the output #include using std::cout; using std::cin; using std::endl; #include 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
6
6/9/2016 ECE 264: Lecture 13 6 //Example 2: Determine the output #include 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 _
7
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 6/9/2016 ECE 264: Lecture 13 7
8
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 6/9/2016 ECE 264: Lecture 13 8
9
Example: showpoint, setprecision #include using std::cin; using std::cout; using std::endl; using std::fixed; using std::showpoint; #include 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 6/9/2016 ECE 264: Lecture 13 9 First output 1.000000,2.000000,3.400,5.000 _
10
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; 6/9/2016 ECE 264: Lecture 13 10
11
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 6/9/2016 ECE 264: Lecture 13 11
12
Example: setfill, setw (output) 6/9/2016 ECE 264: Lecture 13 12
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 6/9/2016 ECE 264: Lecture 13 13
14
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: :: ([param list]) { } 6/9/2016 ECE 264: Lecture 13 14
15
Example: GradeBook.h #include using std::string; class GradeBook { public: GradeBook( ); GradeBook( string name ); void setCourseName(string name); string getCourseName(); void displayMessage(); private: string courseName; }; // end class GradeBook 6/9/2016 ECE 264: Lecture 13 15
16
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 6/9/2016 ECE 264: Lecture 13 16
17
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 6/9/2016 ECE 264: Lecture 13 17
18
Good Luck! 6/9/2016 ECE 264: Lecture 13 18
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.