Practice Building Classes Modeling Objects. Problem Write a program that computes the Dean’s List (full-time students whose GPA 3.0), using a student.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Program Input and the Software Design Process ROBERT REAVES.
Software Engineering 1 (Chap. 1) Object-Centered Design.
The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion.
1 Building Classes (the "++" in C++) (Chapter 14) Representing More Complex Objects.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
1 C++ Loop Statements Repetition Revisited. 2 Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
Problem Session Working in pairs of two, solve the following problem...
Matrices Introducing Inheritance. Consider A matrix is a grid in which numbers can be stored. Algorithms for problems in scientific computing frequently.
Vectors of Vectors Representing Objects with Two Dimensions.
Controlling Function Behavior Sequence, Selection and Repetition.
File I/O ifstreams and ofstreams Sections 11.1 &
File I/O ifstreams and ofstreams. Some istream Operations istream function Description cin >> ch; Extract next non-whitespace character from cin and store.
C++ Loop Statements Repetition Revisited. Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
1 CS161 Introduction to Computer Science Topic #13.
1 Simple Functions Writing Reuseable Formulas. In Math Suppose f (x) = 2 x 2 +5Suppose f (x) = 2 x 2 +5 f(5)=?f(5)=? f(5) = 2* =55f(5) = 2*
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given.
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
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.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Introduction to the Object-oriented Data Protocol © Dr. David Workman COP4331 School of EE and CS February 4, 2010.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
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.
CS212: Object Oriented Analysis and Design
ifstreams and ofstreams
Vectors of Vectors Representing Objects with Two Dimensions.
Introducing Inheritance
Functions Manipulating Files
Writing Reuseable Formulas
solve the following problem...
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
File I/O with Records Lesson xx
Basic File I/O and Stream Objects
Programming with Data Files
File I/O in C++ I.
Vectors of Vectors Representing Objects with Two Dimensions.
Programming with Data Files
ifstreams and ofstreams
File I/O in C++ I.
Presentation transcript:

Practice Building Classes Modeling Objects

Problem Write a program that computes the Dean’s List (full-time students whose GPA 3.0), using a student data file. Write a program that computes the Dean’s List (full-time students whose GPA  3.0), using a student data file. Each student-record in the file consists of four lines: Jane Doe representing student Jane Doe, with student id 12345, with GPA 3.35 and 14 credit hrs this semester.

Behavior Our program should prompt for and read the name of the data file from the user. It should then open an ifstream to that file. It should then prompt for and read the name of an output file and open an ofstream to that file. Using an input loop, our program should read in the sequence of students. It should then process this sequence by printing to the ofstream each full-time student whose GPA is 3.0 or greater.

Objects Description Type Kind Name in-file name string varying inFileName input fstream ifstream varying fin students vector varying students GPA double varying students[i].GPA() out-file name string varying outFileName output fstream ofstream varying fout

Operations Description Predefined? Library? Name display a string yes string << read a string yes string >> open/close fstreams yes fstream -- read students in an ?? -- for, if input loop break identify Deans List ?? -- for, if students [], ?? output a student ?? -- ??

Algorithm 0. Display purpose of program 1. Prompt for and read name of input file from cin into inFileName. 2. Open fin to inFileName, fout to outFileName, verify opens. 3. Read sequence of students from fin into students, using an input loop. 4. Close fin. 5. Prompt for and read name of output file from cin into outFileName. 6. Write students qualifying for the Dean’s List to fout. 7. Close fout. 8. Display a ‘processing completed’ message via cout.

OCD with Classes 0. Specify the behavior of the program. 1. Identify the objects in the behavior. 1a. If an object cannot be modeled with available types, declare a class by which such objects can be modeled. 2. Identify the operations in the behavior. 2a. If an operation is not predefined, build a function to perform that operation. 2b. If the left operand of an operation is a class object, make the operation a function member of the class. 3. Organize your objects and operations into an algorithm.

A Student Class Begin by defining variables to store the attributes of the object being represented (a student) in a class header file (e.g., Student.h): int myID; string myName; double myGPA; int myCredits; Clearly, it would be easy to add other data members (academic year, address,...) as necessary.

Building Classes We then wrap these variables in a class declaration: class Student { public: private: int myID; string myName; double myGPA; int myCredits; };

Prototypes class Student { public: private: //... data members omitted }; Student(); Student(int id, const string & Name, double gpa, int credits); int ID() const; string Name() const; double GPA() const; int Credits() const; friend istream & operator>>(istream & in, Student & stu); friend ostream & operator<<(ostream & in, const Student & stu);

Operation: Default Constructor The default constructor initializes the data members to default values: Student aStudent; Specification: Postcondition: myid == 0 && myName == “” && myGPA == 0.0 && myCredits == 0. myGPA == 0.0 && myCredits == 0.

Default Constructor inline Student::Student() { myID = 0; myName = “”; myGPA = 0.0; myCredits = 0; } This is sufficiently simple to define inline in Student.h:

Operation: Explicit-Value Constructor Student aStudent(12345, “Jane Doe”, 3.35, 14); This constructor lets you initialize the data members to specified values: Specification: Receive: id, an int; name, a string; gpa, a double, and credits, an int. credits, an int. Precondition: id is a valid id, gpa is a valid gpa, and credits is a valid number of credits. credits is a valid number of credits. Postcondition: myid == id && myName == name && myGPA == gpa && myCredits == credits. myGPA == gpa && myCredits == credits.

Explicit-Value Constructor //... #include “Student.h” Student::Student(int id, const string & name, double gpa, int credits) { assert(id > 0 && gpa >= 0.0 && gpa <= 4.0 && credits > 0 && credits < 21); myID = id; myName = name; myGPA = gpa; myCredits = credits; } This is sufficiently complicated to define in Student.cpp:

Operation: Extractors The extractors retrieve data member values: cout << aStudent.ID() << aStudent.Name() << aStudent.GPA() << aStudent.Credits(); Specifications: ID:Return myID. ID:Return myID. Name:Return myName. GPA:Return myGPA. Credits:Return myCredits.

Default Constructor inline int Student::ID() const { return myID; } These are sufficiently simple to define inline in Student.h: inline string Student::Name() const { return myName; } inline string Student::GPA() const { return myGPA; } inline string Student::Credits() const { return myCredits; }

Operation: Input cin >> aStudent; The input operator lets you read Student values: Specification: Receive: in, an istream; stu, a Student. Precondition: in contains a valid Student value. Input: the Student value from in. Passback: in, the Student value extracted from it; stu, containing the extracted value. stu, containing the extracted value. Return: in, for chaining.

Input Operator //... istream & operator>>(istream & in, Student & stu) { in >> stu.myID; // read id on one line char ch; in.get(ch); // eat the newline getline(in, stu.myName); // read name (2 words) in >> stu.myGPA // read GPA >> stu.myCredits; // read credits assert(in.good()); // weak precondition check return in; // allow chaining } This is sufficiently complicated to define in Student.cpp. Its form is dictated by the record-format in the data file:

Operation: Output cout << aStudent << endl; The output operator lets you write Student values: Specification: Receive: out, an ostream; stu, a Student. Output: stu’s Student value, via out. Passback: out, with the Student value inserted into it. Return: out, for chaining.

Output Operator //... ostream & operator<<(ostream & out, const Student & stu) { out << stu.myID << ‘\n’ // put id on one line << stu.myName << ‘\n’ // put name << stu.myGPA << ‘ ‘ // put GPA << stu.myCredits; // put credits return out; // allow chaining } This is sufficiently complicated to define in Student.cpp. We will use a format consistent with that in the data file:

Coding Our Algorithm We are now ready to implement our algorithm... // deansList.cpp //... documentation //... other #includes #include “Student.h” int main() { cout << “\nTo generate the Dean’s List,” << “\n enter the input file name: “; string inFileName; cin >> inFileName; ifstream fin(inFileName.data()); assert(fin.is_open());

Coding (Ct’d) //... deansList.cpp continued Student aStudent; vector students; for (;;) { fin >> aStudent; if (fin.eof()) break; students.push_back(aStudent); } fin.close(); //...

Coding (Ct’d) //... deansList.cpp continued cout << “\nEnter the output file name: “; string outFileName; cin >> outFileName; ofstream fout(outFileName.data()); assert(fout.is_open()); for (unsigned i = 0; i < students.size(); i++) if (students[i].GPA() >= 3.0 && students[i].Credits() >= 12) fout << students[i] << “\n\n”; fout.close(); cout << “\nProcessing complete. Results are in “ << outFileName << endl; }

Summary C++ classes allow “real world” objects to be modeled in software. Classes are objects, and can be stored in containers, such as the STL vector. Most classes require at least: –a default-value constructor –an explicit-value constructor –extractor functions –I/O functions