Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall 2008 2-A. Using Class Type Objects.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Numeric Types, Expressions, and Output ROBERT REAVES.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.
計算機概論實習 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators.
MET CS 563 Software Development with C++ for Mathematical Finance Dr. Tanya Zlateva MET Computer Science Department 755 Commonwealth Ave., Room 103 Boston,
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 Chapter 3 Topics Constants of Type int and float l Evaluating Arithmetic Expressions l Implicit Type Coercion and Explicit Type Conversion l Calling.
Computer Science 1620 Formatting. Suppose you work for the HR dept. of a company you wish to write a program to show their earnings per month Details:
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems.
CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Exposure C++ Chapter VII Program Input and Output.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
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.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 05 (Part II) Control Statements: Part II.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
Library Functions. CSCE 1062 Outline  cmath class library functions {section 3.2}  iomanip class library functions {section 8.5}  string class library.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
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.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Programming Fundamentals
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
Output Stream Formatting
Formatting Screen Input/Output
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
2.1 Parts of a C++ Program.
Manipulators CSCE 121 J. Michael Moore
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Introduction to cout / cin
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Formatted Input, Output & File Input, Output
Let’s all Repeat Together
Presentation transcript:

Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall A. Using Class Type Objects 1 2-A. Using Objects of Class Type

Formal specifications for: 6/30/2015MET CS 563--Fall A. Using Class Type Objects 2 Procedural: Operations on Data Objects a 5 b 10 c 15 + (i) actions and type of data int -- indicates operation on integer (ii) variable with unique value assigned in memory (iii) applying actions to data c=a+b

6/30/2015MET CS 563--Fall A. Using Class Type Objects 3 Object Oriented: operations of/on objects and interactions between objects (ii) defining objects (iii) applying actions on objects Formal specifications for: (i) bundling actions and data today.advance() today.tellMo() today.tellDay() today.tellYear() Date attributes/data: behavior: advance() tellMo() tellDay() tellYear() month day year today attributes/data: behavior: advance() tellMo() tellDay() tellYear() month 9 day 8 year 2008

6/30/2015 MET CS 563--Fall A. Using Class Type Objects 4 Procedural  Object Oriented (ii) objects today.advance() today.tellMo() today.tellDay() today.tellYear() (iii) actions on objects (i) type Date attributes/data: behavior: advance() tellMo() tellDay() tellYear() a 5 b 10 c 15 int c=a+b month day year today attributes/data: behavior: advance() tellMo() tellDay() tellYear() month 9 day 8 year 2008

6/30/2015MET CS 563--Fall A. Using Class Type Objects 5 string Class (ii) defining objects(iii) applying actions on objects (i) bundling actions and data name1.length() name1.substr(0,1) name1 + name 2 cout << name1 cin >> name1 getline(cin, sentence) string data: behavior: length() substr(start,length) + >> << name1 data: behavior: length() substr() + >> << position01… char 0123 Mary dot operator on member functions global function getline(in, string)

#include contains the definitions of the string class (data and functions) string name; //declaration string name1 = "Mary"; //definition string name2 = "Emily Dickinson"; cin >> name; //input placed in name cout << name1; //outputs Mary 6/30/2015MET CS 563--Fall A. Using Class Type Objects 6 string Objects—declare, define, i/o

string s1 = "Henry"; string s1 = "David"; string s1 = "Thoreau"; cout << s1 + s2 + s3; //"HenryDavidThoreau" string name = s1 + " " + s2 + " " + s3; cout << name; //"Henry David Thoreau" 6/30/2015MET CS 563--Fall A. Using Class Type Objects 7 string Objects—operations

The input and output streams cin and cout are in fact predifined objects of iostream classes. In this they are similar to string objects name, sentence, etc. Output streams come with a set of special functions, called manipulators, that allow changing the appearance of the output, such as setw : sets the number of characters or width of the output field fixed : prints floating point numbers with trailing zeros scientific: prints floating point numbers in scientific notation setprecision : number of digits after decimal point when used with fixed or scientific left and right alignment (See o program fromattedIO.cpp for examples o specifications in documentation for more detail on manipulator use ) 6/30/2015MET CS 563--Fall A. Using Class Type Objects 8 Formatted Output--Manipulators

cout << manipulator << expression | identifier; Older notation cout << setiosflags(ios :: manipulator) << expression | identifier; Examples: fromatted_IO.cpp 6/30/2015MET CS 563--Fall A. Using Class Type Objects 9 IO Manipulators--Syntax

6/30/2015MET CS 563--Fall A. Using Class Type Objects 10 I/O Manipulators--Exmpales setprecision( ) sets the number of digits, or precision, to the right of the decimal point to integer given in the parentheses. Examples: setprecision(3) will print setprecision(4) will print in the form setiosflags( | |… ) Examples: setiosflags(ios::fixed) prints floating point numbers with decimal point and without exponent, in so called fixed-point formal (as opposed to scientific with exponent) setiosflags(ios::showpoint) prints decimal point and trailing zeros even if amount is an integer, e.g instead of 77

6/30/2015MET CS 563--Fall A. Using Class Type Objects 11 #include using namespace std; int main() { double x1= 1.34, x2 = 4.63; double y1 = , y2 = 72.02; double z1 = , z2 = ; //no formatting--default prints left justified w/o spaces cout << "No Formatting: " << endl; cout << x1 << x2 << endl; cout << y1 << y2 << endl; cout << z1 << z2 << endl <<endl; string: def and io No Formatting:

string: setw(n) 6/30/2015MET CS 563--Fall A. Using Class Type Objects 12 cout << "Using setw(20)" cout << "must be set before each output item" << endl; cout << setw(20) << x1 << setw(20) << x2 << endl; cout << setw(20) << y1 << setw(20) << y2 << endl; cout << setw(20) << z1 << setw(20) << z2 << endl <<endl; Using setw(20)--must be set before each output item

string: fixed 6/30/2015MET CS 563--Fall A. Using Class Type Objects 13 cout << "Using fixed: " cout << "default precision of " cout << "6 decimal digits after point printed " << endl; cout << "Once set remains in force till changed " << endl; cout << fixed; cout << setw(20) << x1 << setw(20) << x2 << endl; cout << setw(20) << y1 << setw(20) << y2 << endl; cout << setw(20) << z1 << setw(20) << z2 << endl <<endl; Using fixed: default precision of 6 decimal digits after point printed Once set remains in force till changed

string: fixed and setprecision 6/30/2015MET CS 563--Fall A. Using Class Type Objects 14 cout << fixed << setprecision(2); cout << setw(20) << x1 << setw(20) << x2 << endl; cout << setw(20) << y1 << setw(20) << y2 << endl; cout << setw(20) << z1 << setw(20) << z2 << endl <<endl;