ECE 264Object-Oriented SoftwareDevelopment
Instructor:  Dr. Honggang
Fall 2012
Lecture 8:  File I/O; Introduction to classes
Lecture outline
Announcements/reminders
Lab 2 due today
Will submit to M:\ECE-264\<username>
Make sure file name is right!
Lab 3 to be posted later tomorrow tonight
Exam 1: 2:00-3:30 pm, Wed. Oct/10 (No lab andClass)
Review: field widths, justification, and fill
Today
File I/O
Introduce classes
10/30/2015
ECE 264: Lecture 8
2
Review
Output formatting
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 withleft/right/internal
showpos / showbase forces sign / base to be shown
Change fill characters with fill() orsetfill()
10/30/2015
ECE 264: Lecture 8
3
File I/O
Very similar to standard I/O
Need file I/O streams: ifstreamofstream
Must #include <fstream>
Must specify using std::ifstream; or using std::ofstream;
Use open(“<filename>”) to open a file
Use close() to close it
Can then use file streams just like cin/cout
Example:
int numR;
char name[20];
ifstream in1;
in1.open("rooms.txt");
in1 >> numR;
in1.ignore(1);
in1.getline(name, 20);
in1.close();
10/30/2015
ECE 264: Lecture 8
4
Data Types
Data type: concrete implementation ofconcept
Built in types include:
int, double, char
Pre-defined class types include:
string, istream, ostream
Real world applications work with conceptsthat are not available as built-in or pre-defined types.
10/30/2015
ECE 264: Lecture 8
5
Classes, Objects, Member Functions and Data Members
Classes: user-defined types
Classes represent real concepts (e.g., car)
Functions describe mechanisms that perform tasks
Hide complex tasks from the user
Ex: driver can use gas pedal to accelerate withoutknowing how acceleration is performed
Must define classes before using them
Ex: a car must be designed and built before it can be driven
Many objects can be created from the same class
Object: instance of a particular type
In C++, every data type comes from an object
Ex: many cars can be built from same specifications
10/30/2015
ECE 264: Lecture 8
6
Classes, Objects, Member Functions and Data Members (Cont.)
Member function calls send messages to anobject to perform tasks
Ex: pressing pedal sends message to accelerate
Objects and cars both have attributes
Represented in classes as data members
Each object has its own copy of data (usually)
Ex: color, miles driven
10/30/2015
ECE 264: Lecture 8
7
Example 1: Basic class (GradeBook.h)
// Adapted from Fig. 3.3
// Define class GradeBook with a member function that takes a parameter
#include <string> // program uses C++ standard string class
using std::string;
// GradeBook class interface
class GradeBook
{
public:
   // function that displays a welcome message to the GradeBook user
   void displayMessage( string courseName );
}; // end class GradeBook
10/30/2015
ECE 264: Lecture 8
8
Example 1: Basic class (GradeBook.cpp)
// Adapted from Fig. 3.3
// Define class GradeBook with a member function that takes a parameter
#include “Gradebook.h”
#include <iostream>
using std::cout;
using std::endl;
// GradeBook class implementation
// function that displays a welcome message to the GradeBook user
void GradeBook::displayMessage( string courseName )
{
   cout << "Welcome to the grade book for\n" << courseName << "!"
      << endl;
// end function displayMessage
10/30/2015
ECE 264: Lecture 8
9
Example 1: Basic class (main program)
#include “Gradebook.h”
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
// function main begins program execution
int main()
{
   string nameOfCourse; // string of characters to store the course name
   GradeBook myGradeBook; // create a GradeBook object named myGradeBook
 
   // prompt for and input course name
   cout << "Please enter the course name:" << endl;
   getline( cin, nameOfCourse ); // read a course name with blanks
   cout << endl; // output a blank line
   // call myGradeBook's displayMessage function and pass nameOfCourse as an argument
   myGradeBook.displayMessage( nameOfCourse );
   return 0; // indicate successful termination
// end main
10/30/2015
ECE 264: Lecture 8
10
Please enter the course name: ECE 264
Welcome to the grade book for
ECE 264!
Input/Output:
Class definition
The class definition specifies:
What data members belong to a class
What member functions belong to a class
Ex: displayMessage()
Syntax:
Begins with the keyword class, followed by the classname
By convention, class names start with capital letters
Ex: class GradeBook
Body of the definition is enclosed in { }
public indicates function/data is accessible to
Other functions (e.g., main())
Member functions of other classes
10/30/2015
ECE 264: Lecture 8
11
Implementing classes in separate files
Main limitation of single file for class
Don’t want to copy and paste class into everyprogram in which you use it
Standard libraries don’t work this way
Solution: place class in separate files
Using one file allows user access toimplementation details
Two files:
Class interface: header (.h) file
 Shows all data members, but only function prototypes(function name, return type, and arguments)
Class implementation: source (.cpp) file
10/30/2015
ECE 264: Lecture 8
12
Class interface
File name should match class name
Example: GradeBook.h and GradeBook.cpp
Header file only contains interface
Any file using class interface includes header
User-defined header names in quotes:
#include “GradeBook.h”
Files that use interface include
Source file containing class implementation (e.g.,GradeBook.cpp)
Main program that uses class
Any other class that contains a GradeBook object
10/30/2015
ECE 264: Lecture 8
13
Class implementation
One key point: within .cpp file, don’t knowwhat namespace functions belong to
Function names must include class name as well
Format:
<class_name>::<function_name>([param list])
{ <function body> }
Example:
void GradeBook::setCourseName(string name)
{ courseName = name; }
10/30/2015
ECE 264: Lecture 8
14
Member function definition
Member function: a function associated with a class,which can be called:
An object of that class (if public)
Other member functions of that class
Member function definitions must contain:
A return type
A valid name, followed by parentheses ( )
The body of the function, enclosed by { }
Function parameters are optional
Default is pass by value
Syntax:
return-type method-name([optional parameter list])
{
[function body]
}
10/30/2015
ECE 264: Lecture 8
15
Calling member functions
Objects can access all public members (dataor functions) of that class
Use dot operator . )
Syntax: <object-name>.<member-name>
Example:
GradeBook myGradeBook;
...
  myGradeBook.displayMessage(courseName);
10/30/2015
ECE 264: Lecture 8
16
Final notes
Next time
Continue with classes
Data members
Constructors
Acknowledgements: this lecture borrowsheavily from lecture slides provided with thefollowing texts:
Deitel & Deitel, C++ How to Program, 8th ed.
Etter & Ingber, Engineering Problem Solving withC++, 2nd ed.
10/30/2015
ECE 264: Lecture 8
17