i/o and data formatting introduction to class concepts infsy 307 spring 2003 lecture 3

Post on 05-Jan-2016

216 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

I/O and Data FormattingI/O and Data FormattingIntroduction to Class Introduction to Class

ConceptsConceptsINFSY 307 INFSY 307

Spring 2003Spring 2003

Lecture 3Lecture 3

I/O and Data FormattingI/O and Data Formatting

A stream is a flow of characters either “in” A stream is a flow of characters either “in” or “out” of your program.or “out” of your program.

C++ relies on libraries for input/output C++ relies on libraries for input/output related to the console, file, or other related to the console, file, or other devices.devices.

Input StreamInput Stream

Input streamInput stream is the input being fed into the is the input being fed into the programprogram– uses uses cincin

>>>> is the extraction operator is the extraction operator

– Program waits for input and a <Program waits for input and a <returnreturn> (after > (after all input) to designate that the input is completeall input) to designate that the input is complete

– Spaces or <Spaces or <returnreturn> on input will separate the > on input will separate the datadata

cin >> identifier1 >> identifier2;cin >> identifier1 >> identifier2;

Output StreamOutput Stream

Output streamOutput stream is the stream of output is the stream of output generated by the programgenerated by the program– uses uses coutcout

<<<< is the insertion operatoris the insertion operator

– Can include:Can include:arithmetic expressionsarithmetic expressions

““\n” or \n” or endlendl to indicate a new line to indicate a new line

quoted stringsquoted strings

cout << num1 << “number 1 \n”;cout << num1 << “number 1 \n”;

SampleSample cout cout statementsstatements

cout << num1;cout << num1; cout << num1 << “ number of pencils \n”;cout << num1 << “ number of pencils \n”; cout << num1 * pr << “ price of pencils \n”;cout << num1 * pr << “ price of pencils \n”; cout << num1 << “ pencils “ << pr << “ price “cout << num1 << “ pencils “ << pr << “ price “ << num1 * pr << “ price of pencils \n”;<< num1 * pr << “ price of pencils \n”; cout << “\n”;cout << “\n”; cout << endl;cout << endl;

FormattingFormatting

The same data can be displayed in many The same data can be displayed in many different ways:different ways:

320320

320.0320.0

320.00000320.00000

320320

3.2e+23.2e+2

+320.0+320.0

FormattingFormatting

Format FlagsFormat Flags setf(ios::<flagname>)setf(ios::<flagname>)

common flagnames:common flagnames: fixedfixed leftleft showpointshowpoint rightright

(Note: ios is a class from the stream library)(Note: ios is a class from the stream library)

Formatting FlagsFormatting Flags

fixed - ensures fixed format, i.e. no fixed - ensures fixed format, i.e. no scientific notationscientific notation

showpoint - ensures that decimal points showpoint - ensures that decimal points and trailing zeros are displayedand trailing zeros are displayed

left adjusts outputleft adjusts output

right adjusts outputright adjusts output

Examples:Examples:

cout.setf(ios::fixed);cout.setf(ios::fixed);

cout.setf(ios::showpoint);cout.setf(ios::showpoint);

cout.setf(ios::fixed | ios::showpoint);cout.setf(ios::fixed | ios::showpoint);

Formatting ManipulatorFormatting Manipulator

Manipulators are placed after the insertion Manipulators are placed after the insertion operator; the arrow notation operator; the arrow notation <<<< is called the is called the insertion operatorinsertion operator..

setprecision (2); - two decimal digits are outputsetprecision (2); - two decimal digits are output

setw (4); - describes width of next fieldsetw (4); - describes width of next field

cout.width (4); - describes width of next fieldcout.width (4); - describes width of next field

Note:Note: #include <iomanip> is required #include <iomanip> is required

ExamplesExamples

cout << setprecision (2) << “Value is: ” << 10.5;cout << setprecision (2) << “Value is: ” << 10.5;

cout << setw(7) << value1 << setw(5) << value2 << endl;cout << setw(7) << value1 << setw(5) << value2 << endl;

cout << setw(4) << “abc”; cout << setw(4) << “abc”; // Note: only for next variable// Note: only for next variable

cout.precision(4);cout.precision(4);

cout.width(10); cout.width(10); // Note: only for next variable// Note: only for next variable

Formatting RulesFormatting Rules

1 Flags and most manipulators remain set until reset or Flags and most manipulators remain set until reset or overridden with a new flag or manipulatoroverridden with a new flag or manipulator

2 When a field is too small, C++ will adjust it and print the When a field is too small, C++ will adjust it and print the true value of a numbertrue value of a number

3 When a string variable is output, the field width will be When a string variable is output, the field width will be filled with whatever is in memory unless a ‘\0’ is found.filled with whatever is in memory unless a ‘\0’ is found. ‘‘\0’ is referred to as the NULL character and is a special\0’ is referred to as the NULL character and is a special character similar to ‘\n’character similar to ‘\n’

4 Extra bytes are padded with spacesExtra bytes are padded with spaces

ObjectsObjects

Objects are all around us.Objects are all around us.

Computer programs attempt to mimic the Computer programs attempt to mimic the objects in the world.objects in the world.

An object is an An object is an instanceinstance of a class. of a class.

Example: person classExample: person class

ObjectsObjects

An object has structure:An object has structure:– Attributes (properties)Attributes (properties)– Behaviors or operations it can carry outBehaviors or operations it can carry out

ClassClass

A Class is a template for making objects A Class is a template for making objects (cookie cutter)(cookie cutter)– it becomes a data type definition like it becomes a data type definition like intint or or

charchar – a type whose variables are a type whose variables are objectsobjects

member functions (behaviors)member functions (behaviors)

holds data values (attributes)holds data values (attributes)

Class DefinitionClass Definition

class Student{

};

public:void display( );

private:string lastName;int age;

Keyword"class"

Class name

Bracesdelimitclass

Semicolonterminatesspecifier

Member functionprototype

Member variables

Abstract Data TypesAbstract Data TypesGuidelinesGuidelines

Make all member variables private Make all member variables private members of the class.members of the class.

Abstract Data TypesAbstract Data TypesGuidelinesGuidelines

Make each of the basic operations that Make each of the basic operations that the programmer needs a public member the programmer needs a public member function of the class, and fully specify function of the class, and fully specify how to use each public member function.how to use each public member function.– Interface:Interface:

Class definitionClass definition

Function prototypesFunction prototypes

CommentsComments

Abstract Data TypesAbstract Data TypesGuidelinesGuidelines

Implementation consists of function Implementation consists of function definitionsdefinitions

top related