Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Home Library Index Site Map  Links Search About

Formatted Character Input


 
 
 
 

Formatted Input using O.O. approach.
class PhoneNumber
#ifndef _PhoneNumber_h_
#define _PhoneNumber_h_

// INCLUDES
#include<iostream.h>

// NAME: PhoneNumber - TYPE: C++ CLASS
//
// SYNOPSIS:
// DESCRIPTION:
// parses phone numbers of type (0800) 555-1212
// into objects of type PhoneNumber
// EXAMPLES: see driver
// BUGS:
// SEE ALSO:
////////////////////////////////////////////


class PhoneNumber
{
public:
// LIFECYCLE

// PhoneNumber();
// constructor`
// PhoneNumber(const PhoneNumber&);
// copy constructor
// ~PhoneNumber();
// destructor

// OPERATORS
friend ostream& operator<<(ostream&, const PhoneNumber&);
friend istream& operator>>(istream&, PhoneNumber&);

// PhoneNumber& operator=(PhoneNumber&);
// assignment operator

// OPERATIONS
// ACCESS
// INQUIRY

protected:
private:
char areaCode[4]; // 3digit area code and NULL
char exchange[4]; // 3 digit exchange and NULL
char line[5]; // 4 digit line number and NULL
};

// overloaded stream insertion operator (cannot be a member function)
ostream& operator<<(ostream& output, const PhoneNumber& num)
{
output << "(" << num.areaCode << ")"
<< num.exchange << "-" << num.line;

return output; // enables cout << a << b << c;
}

// overloaded stream extraction operator
// (cannot be a member function)
istream& operator>>(istream& input, PhoneNumber& num)
{
input.ignore(); // skip(
input.getline(num.areaCode, 4); // input area code
input.ignore(2); // skip) and space
input.getline(num.exchange, 4); // input exchange code
input.ignore(); // skip dash (-)
input.getline(num.line, 5); // input line

return input; // enables cin >> a >> b >> c;
}

#endif // _PhoneNumber_h_

 
 
Class PhoneNumber has three overloaded operators:-
this provides the functionality for the class which
exists purely to input, process and output data.
Home Library Index Top

Driver for class PhoneNumber
 
#include "fonepars.h"
void main()
{

PhoneNumber phone; // creates object phone

cout << "Enter a phone number in the "
<< "form (123) 456-7890:\n";

cin >> phone;

cout << "The phone number entered was :";

cout << phone << endl;

}
 
Home Library Index Top

 

STL Database
Arrays
Binary File Input / Output
Character Input
Containers
Standard Template Library
Streams
Templates
Utility Functions
Win32 Programming
Miscellaneous
 

 
copyright notice

Copyright Robert Mitchell. Last Revised : 28 April, 2000

e-mail me
1