ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.

Slides:



Advertisements
Similar presentations
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 01] Introduction to.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
Chapter 4 Introduction to Classes, Objects, Methods and strings
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
A DVANCED P ROGRAMMING C HAPTER 4: I NTRODUCTION TO I NTRODUCTION TO C LASSES, O BJECTS, M ETHODS AND STRINGS Dr Shahriar Bijani Winter 2016.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 16 is a copy of C++ How to Program Chapter.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 3 Introduction to Classes, Objects and Strings C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects
ECE 264 Object-Oriented Software Development
Chapter 3 Introduction to Classes, Objects and Strings
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3 Introduction to Classes and Objects
Dr Shahriar Bijani Winter 2017
Introduction to Classes and Objects
Chapter 3: Input/Output
Classes, Objects, Methods and Strings
Object Oriented Programming in java
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Introduction to Classes and Objects
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

ECE 264 Object-Oriented Software Development 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\ 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

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

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

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

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

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

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

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

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:

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

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

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

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 (data or functions) of that class Use dot operator (. )  Syntax:.  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 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