ECE 264 Object-Oriented Software Development

18
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview

description

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

Transcript of ECE 264 Object-Oriented Software Development

Page 1: ECE 264 Object-Oriented Software Development

ECE 264Object-Oriented

Software Development

Instructor: Dr. Honggang WangFall 2012

Lecture 13: Exam 1 Preview

Page 2: ECE 264 Object-Oriented Software Development

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

04/21/23 ECE 264: Lecture 13 2

Page 3: ECE 264 Object-Oriented Software Development

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

04/21/23 ECE 264: Lecture 13 3

Page 4: ECE 264 Object-Oriented Software Development

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;

04/21/23 ECE 264: Lecture 13 4

Page 5: ECE 264 Object-Oriented Software Development

04/21/23 ECE 264: Lecture 13 5

output1,24.5 cm_

//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

Page 6: ECE 264 Object-Oriented Software Development

04/21/23 ECE 264: Lecture 13 6

//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 23.4 52 3 3.4 7

First output1,2,3.4,5Second output3,2,2,3_

Page 7: ECE 264 Object-Oriented Software Development

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

04/21/23 ECE 264: Lecture 13 7

Page 8: ECE 264 Object-Oriented Software Development

Example: basesint 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

04/21/23 ECE 264: Lecture 13 8

Page 9: ECE 264 Object-Oriented Software Development

Example: showpoint, setprecision#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

04/21/23 ECE 264: Lecture 13 9

First output1.000000,2.000000,3.400,5.000_

Page 10: ECE 264 Object-Oriented Software Development

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;

04/21/23 ECE 264: Lecture 13 10

Page 11: ECE 264 Object-Oriented Software Development

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

04/21/23 ECE 264: Lecture 13 11

Page 12: ECE 264 Object-Oriented Software Development

Example: setfill, setw (output)

04/21/23 ECE 264: Lecture 13 12

Page 13: ECE 264 Object-Oriented Software Development

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

04/21/23 ECE 264: Lecture 13 13

Page 14: ECE 264 Object-Oriented Software Development

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

04/21/23 ECE 264: Lecture 13 14

Page 15: ECE 264 Object-Oriented Software Development

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

04/21/23 ECE 264: Lecture 13 15

Page 16: ECE 264 Object-Oriented Software Development

Example: GradeBook.cpp// GradeBook.cpp#include “GradeBook.h”

// Default constructor—initializes courseName// to empty stringGradeBook::GradeBook( ) { setCourseName( "" ); } // end GradeBook constructor

// Parameterized constructorGradeBook::GradeBook( string name ) { setCourseName( name ); } // end GradeBook constructor

04/21/23 ECE 264: Lecture 13 16

Page 17: ECE 264 Object-Oriented Software Development

Example: GradeBook.cpp (cont.)// function to set course name void GradeBook::setCourseName( string name ) { courseName = name; } // end function setCourseName

// function to get the course namestring GradeBook::getCourseName() { return courseName; } // end function getCourseName

// display welcome message to uservoid GradeBook::displayMessage() { cout << "Welcome to the grade book for\n"

<< getCourseName() << endl;} // end function displayMessage

04/21/23 ECE 264: Lecture 13 17

Page 18: ECE 264 Object-Oriented Software Development

Good Luck!

04/21/23 ECE 264: Lecture 13 18