Download presentation
Presentation is loading. Please wait.
Published byCollin Horton Modified over 9 years ago
1
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes
2
Lecture outline Announcements/reminders Lab 2 due today Will submit to M:\ECE-264\ 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 and Class) Review: field widths, justification, and fill Today File I/O Introduce classes 10/30/2015 ECE 264: Lecture 8 2
3
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 with left/right/internal showpos / showbase forces sign / base to be shown Change fill characters with fill() or setfill() 10/30/2015 ECE 264: Lecture 8 3
4
File I/O Very similar to standard I/O Need file I/O streams: ifstream, ofstream Must #include Must specify using std::ifstream; or using std::ofstream; Use open(“ ”) 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
5
Data Types Data type: concrete implementation of concept Built in types include: int, double, char Pre-defined class types include: string, istream, ostream Real world applications work with concepts that are not available as built-in or pre- defined types. 10/30/2015 ECE 264: Lecture 8 5
6
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 without knowing 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
7
Classes, Objects, Member Functions and Data Members (Cont.) Member function calls send messages to an object 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
8
Example 1: Basic class (GradeBook.h) // Adapted from Fig. 3.3 // Define class GradeBook with a member function that takes a parameter #include // 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
9
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 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
10
Example 1: Basic class (main program) #include “Gradebook.h” #include 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:
11
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 class name 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
12
Implementing classes in separate files Main limitation of single file for class Don’t want to copy and paste class into every program in which you use it Standard libraries don’t work this way Solution: place class in separate files Using one file allows user access to implementation 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
13
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
14
Class implementation One key point: within.cpp file, don’t know what namespace functions belong to Function names must include class name as well Format: :: ([param list]) { } Example: void GradeBook::setCourseName(string name) { courseName = name; } 10/30/2015 ECE 264: Lecture 8 14
15
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
16
Calling member functions Objects can access all public members (data or functions) of that class Use dot operator (. ) Syntax:. Example: GradeBook myGradeBook;... myGradeBook.displayMessage(courseName); 10/30/2015 ECE 264: Lecture 8 16
17
Final notes Next time Continue with classes Data members Constructors Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 10/30/2015 ECE 264: Lecture 8 17
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.